Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added tasks 1201-1606
  • Loading branch information
javadev committed Jun 14, 2023
commit d8ee0fe3b0d5c4fbc4a51d6f20fd290dcb4a0de7
230 changes: 230 additions & 0 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)

## 1174\. Immediate Food Delivery II

Medium

SQL Schema

Table: `Delivery`

+-----------------------------+---------+
| Column Name | Type |
+-----------------------------+---------+
| delivery_id | int |
| customer_id | int |
| order_date | date |
| customer_pref_delivery_date | date |
+-----------------------------+---------+

delivery_id is the primary key of this table. The table holds information about food delivery to customers that make orders at some date and specify a preferred delivery date (on the same order date or after it).

If the customer's preferred delivery date is the same as the order date, then the order is called **immediate;** otherwise, it is called **scheduled**.

The **first order** of a customer is the order with the earliest order date that the customer made. It is guaranteed that a customer has precisely one first order.

Write an SQL query to find the percentage of immediate orders in the first orders of all customers, **rounded to 2 decimal places**.

The query result format is in the following example.

**Example 1:**

**Input:** Delivery table:

+-------------+-------------+------------+-----------------------------+
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
+-------------+-------------+------------+-----------------------------+
| 1 | 1 | 2019-08-01 | 2019-08-02 |
| 2 | 2 | 2019-08-02 | 2019-08-02 |
| 3 | 1 | 2019-08-11 | 2019-08-12 |
| 4 | 3 | 2019-08-24 | 2019-08-24 |
| 5 | 3 | 2019-08-21 | 2019-08-22 |
| 6 | 2 | 2019-08-11 | 2019-08-13 |
| 7 | 4 | 2019-08-09 | 2019-08-09 |
+-------------+-------------+------------+-----------------------------+

**Output:**

+----------------------+
| immediate_percentage |
+----------------------+
| 50.00 |
+----------------------+

**Explanation:** The customer id 1 has a first order with delivery id 1 and it is scheduled. The customer id 2 has a first order with delivery id 2 and it is immediate. The customer id 3 has a first order with delivery id 5 and it is scheduled. The customer id 4 has a first order with delivery id 7 and it is immediate. Hence, half the customers have immediate first orders.

## Solution

```sql
# Write your MySQL query statement below
SELECT ROUND(AVG(CASE WHEN customer_pref_delivery_date = order_date THEN 1 ELSE 0 END) * 100, 2) AS immediate_percentage
FROM (
SELECT *,
DENSE_RANK() OVER(PARTITION BY customer_id ORDER BY order_date ASC) AS dnsrnk
FROM delivery
) subquery_alias
WHERE subquery_alias.dnsrnk = 1;
```
70 changes: 70 additions & 0 deletions src/main/kotlin/g1101_1200/s1193_monthly_transactions_i/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)

## 1193\. Monthly Transactions I

Medium

SQL Schema

Table: `Transactions`

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| country | varchar |
| state | enum |
| amount | int |
| trans_date | date |
+---------------+---------+

id is the primary key of this table.

The table has information about incoming transactions.

The state column is an enum of type ["approved", "declined"].

Write an SQL query to find for each month and country, the number of transactions and their total amount, the number of approved transactions and their total amount.

Return the result table in **any order**.

The query result format is in the following example.

**Example 1:**

**Input:** Transactions table:

+------+---------+----------+--------+------------+
| id | country | state | amount | trans_date |
+------+---------+----------+--------+------------+
| 121 | US | approved | 1000 | 2018-12-18 |
| 122 | US | declined | 2000 | 2018-12-19 |
| 123 | US | approved | 2000 | 2019-01-01 |
| 124 | DE | approved | 2000 | 2019-01-07 |
+------+---------+----------+--------+------------+

**Output:**

+----------+---------+-------------+----------------+--------------------+-----------------------+
| month | country | trans_count | approved_count | trans_total_amount | approved_total_amount |
+----------+---------+-------------+----------------+--------------------+-----------------------+
| 2018-12 | US | 2 | 1 | 3000 | 1000 |
| 2019-01 | US | 1 | 1 | 2000 | 2000 |
| 2019-01 | DE | 1 | 1 | 2000 | 2000 |
+----------+---------+-------------+----------------+--------------------+-----------------------+

## Solution

```sql
# Write your MySQL query statement below
SELECT
FORMATDATETIME(trans_date, 'yyyy-MM') AS trans_month,
country,
COUNT(*) AS trans_count,
SUM(CASE WHEN state = 'approved' THEN 1 ELSE 0 END) AS approved_count,
SUM(amount) AS trans_total_amount,
SUM(CASE WHEN state = 'approved' THEN amount ELSE 0 END) AS approved_total_amount
FROM Transactions
GROUP BY trans_month, country;
```
81 changes: 81 additions & 0 deletions src/main/kotlin/g1201_1300/s1201_ugly_number_iii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)

## 1201\. Ugly Number III

Medium

An **ugly number** is a positive integer that is divisible by `a`, `b`, or `c`.

Given four integers `n`, `a`, `b`, and `c`, return the <code>n<sup>th</sup></code> **ugly number**.

**Example 1:**

**Input:** n = 3, a = 2, b = 3, c = 5

**Output:** 4

**Explanation:** The ugly numbers are 2, 3, 4, 5, 6, 8, 9, 10... The 3<sup>rd</sup> is 4.

**Example 2:**

**Input:** n = 4, a = 2, b = 3, c = 4

**Output:** 6

**Explanation:** The ugly numbers are 2, 3, 4, 6, 8, 9, 10, 12... The 4<sup>th</sup> is 6.

**Example 3:**

**Input:** n = 5, a = 2, b = 11, c = 13

**Output:** 10

**Explanation:** The ugly numbers are 2, 4, 6, 8, 10, 11, 12, 13... The 5<sup>th</sup> is 10.

**Constraints:**

* <code>1 <= n, a, b, c <= 10<sup>9</sup></code>
* <code>1 <= a * b * c <= 10<sup>18</sup></code>
* It is guaranteed that the result will be in range <code>[1, 2 * 10<sup>9</sup>]</code>.

## Solution

```kotlin
class Solution {
private fun getLcm(a: Long, b: Long): Long {
var mx = a
var mn = b
if (a < b) {
mx = b
mn = a
}
while (mn != 0L) {
val tmp = mn
mn = mx % mn
mx = tmp
}
return a * b / mx
}

fun nthUglyNumber(n: Int, a: Int, b: Int, c: Int): Int {
val ab = getLcm(a.toLong(), b.toLong())
val ac = getLcm(a.toLong(), c.toLong())
val bc = getLcm(b.toLong(), c.toLong())
val abc = getLcm(a.toLong(), bc)
var left: Long = 1
var right: Long = 2000000001
if (a != 0 && b != 0 && c != 0 && bc != 0L) {
while (left < right) {
val mid = left + (right - left) / 2
if (mid / a + mid / b + mid / c - mid / ab - mid / ac - mid / bc + mid / abc >= n) {
right = mid
} else {
left = mid + 1
}
}
}
return left.toInt()
}
}
```
119 changes: 119 additions & 0 deletions src/main/kotlin/g1201_1300/s1202_smallest_string_with_swaps/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)

## 1202\. Smallest String With Swaps

Medium

You are given a string `s`, and an array of pairs of indices in the string `pairs` where `pairs[i] = [a, b]` indicates 2 indices(0-indexed) of the string.

You can swap the characters at any pair of indices in the given `pairs` **any number of times**.

Return the lexicographically smallest string that `s` can be changed to after using the swaps.

**Example 1:**

**Input:** s = "dcab", pairs = \[\[0,3],[1,2]]

**Output:** "bacd" **Explaination:**

Swap s[0] and s[3], s = "bcad"

Swap s[1] and s[2], s = "bacd"

**Example 2:**

**Input:** s = "dcab", pairs = \[\[0,3],[1,2],[0,2]]

**Output:** "abcd" **Explaination:**

Swap s[0] and s[3], s = "bcad"

Swap s[0] and s[2], s = "acbd"

Swap s[1] and s[2], s = "abcd"

**Example 3:**

**Input:** s = "cba", pairs = \[\[0,1],[1,2]]

**Output:** "abc" **Explaination:**

Swap s[0] and s[1], s = "bca"

Swap s[1] and s[2], s = "bac"

Swap s[0] and s[1], s = "abc"

**Constraints:**

* `1 <= s.length <= 10^5`
* `0 <= pairs.length <= 10^5`
* `0 <= pairs[i][0], pairs[i][1] < s.length`
* `s` only contains lower case English letters.

## Solution

```kotlin
class Solution {
fun smallestStringWithSwaps(s: String, pairs: List<List<Int>>): String {
val uf = UF(s.length)
for (p in pairs) {
uf.union(p[0], p[1])
}
val freqMapPerRoot: MutableMap<Int, IntArray> = HashMap()
for (i in s.indices) {
freqMapPerRoot.computeIfAbsent(uf.find(i)) { IntArray(26) }[s[i].code - 'a'.code]++
}
val ans = CharArray(s.length)
for (i in ans.indices) {
val css = freqMapPerRoot[uf.find(i)]
for (j in css!!.indices) {
if (css[j] > 0) {
ans[i] = (j + 'a'.code).toChar()
css[j]--
break
}
}
}
return String(ans)
}

internal class UF(n: Int) {
var root: IntArray
var rank: IntArray

init {
root = IntArray(n)
rank = IntArray(n)
for (i in 0 until n) {
root[i] = i
rank[i] = 1
}
}

fun find(u: Int): Int {
if (u == root[u]) {
return u
}
root[u] = find(root[u])
return root[u]
}

fun union(u: Int, v: Int) {
val ru = find(root[u])
val rv = find(root[v])
if (ru != rv) {
if (rank[ru] < rank[rv]) {
root[ru] = root[rv]
} else if (rank[ru] > rank[rv]) {
root[rv] = root[ru]
} else {
root[rv] = root[ru]
rank[ru]++
}
}
}
}
}
```
Loading