Skip to content

Commit bd13d7d

Browse files
committed
Added tasks 3657-3661
1 parent 4dd8e29 commit bd13d7d

File tree

6 files changed

+495
-0
lines changed

6 files changed

+495
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,11 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3661 |[Maximum Walls Destroyed by Robots](src/main/kotlin/g3601_3700/s3661_maximum_walls_destroyed_by_robots)| Hard | Weekly_Contest_464 | 146 | 100.00
2092+
| 3660 |[Jump Game IX](src/main/kotlin/g3601_3700/s3660_jump_game_ix)| Medium | Weekly_Contest_464 | 5 | 100.00
2093+
| 3659 |[Partition Array Into K-Distinct Groups](src/main/kotlin/g3601_3700/s3659_partition_array_into_k_distinct_groups)| Medium | Weekly_Contest_464 | 6 | 100.00
2094+
| 3658 |[GCD of Odd and Even Sums](src/main/kotlin/g3601_3700/s3658_gcd_of_odd_and_even_sums)| Easy | Weekly_Contest_464 | 0 | 100.00
2095+
| 3657 |[Find Loyal Customers](src/main/kotlin/g3601_3700/s3657_find_loyal_customers)| Medium | Database | 297 | 100.00
20912096
| 3655 |[XOR After Range Multiplication Queries II](src/main/kotlin/g3601_3700/s3655_xor_after_range_multiplication_queries_ii)| Hard | Array, Divide_and_Conquer, Weekly_Contest_463 | 26 | 100.00
20922097
| 3654 |[Minimum Sum After Divisible Sum Deletions](src/main/kotlin/g3601_3700/s3654_minimum_sum_after_divisible_sum_deletions)| Medium | Weekly_Contest_463 | 17 | 98.16
20932098
| 3653 |[XOR After Range Multiplication Queries I](src/main/kotlin/g3601_3700/s3653_xor_after_range_multiplication_queries_i)| Medium | Array, Simulation, Divide_and_Conquer, Weekly_Contest_463 | 26 | 100.00
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](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)
3+
4+
## 3657\. Find Loyal Customers
5+
6+
Medium
7+
8+
Table: `customer_transactions`
9+
10+
+------------------+---------+
11+
| Column Name | Type |
12+
+------------------+---------+
13+
| transaction_id | int |
14+
| customer_id | int |
15+
| transaction_date | date |
16+
| amount | decimal |
17+
| transaction_type | varchar |
18+
+------------------+---------+
19+
transaction_id is the unique identifier for this table. transaction_type can be either 'purchase' or 'refund'.
20+
21+
Write a solution to find **loyal customers**. A customer is considered **loyal** if they meet ALL the following criteria:
22+
23+
* Made **at least** `3` purchase transactions.
24+
* Have been active for **at least** `30` days.
25+
* Their **refund rate** is less than `20%` .
26+
27+
Return _the result table ordered by_ `customer_id` _in **ascending** order_.
28+
29+
The result format is in the following example.
30+
31+
**Example:**
32+
33+
**Input:**
34+
35+
customer\_transactions table:
36+
37+
+----------------+-------------+------------------+--------+------------------+
38+
| transaction_id | customer_id | transaction_date | amount | transaction_type |
39+
|----------------|-------------|------------------|--------|------------------|
40+
| 1 | 101 | 2024-01-05 | 150.00 | purchase |
41+
| 2 | 101 | 2024-01-15 | 200.00 | purchase |
42+
| 3 | 101 | 2024-02-10 | 180.00 | purchase |
43+
| 4 | 101 | 2024-02-20 | 250.00 | purchase |
44+
| 5 | 102 | 2024-01-10 | 100.00 | purchase |
45+
| 6 | 102 | 2024-01-12 | 120.00 | purchase |
46+
| 7 | 102 | 2024-01-15 | 80.00 | refund |
47+
| 8 | 102 | 2024-01-18 | 90.00 | refund |
48+
| 9 | 102 | 2024-02-15 | 130.00 | purchase |
49+
| 10 | 103 | 2024-01-01 | 500.00 | purchase |
50+
| 11 | 103 | 2024-01-02 | 450.00 | purchase |
51+
| 12 | 103 | 2024-01-03 | 400.00 | purchase |
52+
| 13 | 104 | 2024-01-01 | 200.00 | purchase |
53+
| 14 | 104 | 2024-02-01 | 250.00 | purchase |
54+
| 15 | 104 | 2024-02-15 | 300.00 | purchase |
55+
| 16 | 104 | 2024-03-01 | 350.00 | purchase |
56+
| 17 | 104 | 2024-03-10 | 280.00 | purchase |
57+
| 18 | 104 | 2024-03-15 | 100.00 | refund |
58+
+----------------+-------------+------------------+--------+------------------+
59+
60+
61+
**Output:**
62+
63+
+-------------+
64+
| customer_id |
65+
|-------------|
66+
| 101 |
67+
| 104 |
68+
+-------------+
69+
70+
**Explanation:**
71+
72+
* **Customer 101**:
73+
* Purchase transactions: 4 (IDs: 1, 2, 3, 4)
74+
* Refund transactions: 0
75+
* Refund rate: 0/4 = 0% (less than 20%)
76+
* Active period: Jan 5 to Feb 20 = 46 days (at least 30 days)
77+
* Qualifies as loyal
78+
* **Customer 102**:
79+
* Purchase transactions: 3 (IDs: 5, 6, 9)
80+
* Refund transactions: 2 (IDs: 7, 8)
81+
* Refund rate: 2/5 = 40% (exceeds 20%)
82+
* Not loyal
83+
* **Customer 103**:
84+
* Purchase transactions: 3 (IDs: 10, 11, 12)
85+
* Refund transactions: 0
86+
* Refund rate: 0/3 = 0% (less than 20%)
87+
* Active period: Jan 1 to Jan 3 = 2 days (less than 30 days)
88+
* Not loyal
89+
* **Customer 104**:
90+
* Purchase transactions: 5 (IDs: 13, 14, 15, 16, 17)
91+
* Refund transactions: 1 (ID: 18)
92+
* Refund rate: 1/6 = 16.67% (less than 20%)
93+
* Active period: Jan 1 to Mar 15 = 73 days (at least 30 days)
94+
* Qualifies as loyal
95+
96+
The result table is ordered by customer\_id in ascending order.
97+
98+
## Solution
99+
100+
```sql
101+
# Write your MySQL query statement below
102+
SELECT
103+
customer_id
104+
FROM
105+
customer_transactions
106+
GROUP BY
107+
customer_id
108+
HAVING
109+
COUNT(CASE WHEN transaction_type = 'purchase' THEN 1 END) > 2
110+
AND TIMESTAMPDIFF(DAY, MIN(transaction_date), MAX(transaction_date)) > 29
111+
AND (COUNT(CASE WHEN transaction_type = 'refund' THEN 1 END) * 1.0 / COUNT(*)) < 0.2
112+
ORDER BY
113+
customer_id ASC;
114+
```
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](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)
3+
4+
## 3658\. GCD of Odd and Even Sums
5+
6+
Easy
7+
8+
You are given an integer `n`. Your task is to compute the **GCD** (greatest common divisor) of two values:
9+
10+
* `sumOdd`: the sum of the first `n` odd numbers.
11+
12+
* `sumEven`: the sum of the first `n` even numbers.
13+
14+
15+
Return the GCD of `sumOdd` and `sumEven`.
16+
17+
**Example 1:**
18+
19+
**Input:** n = 4
20+
21+
**Output:** 4
22+
23+
**Explanation:**
24+
25+
* Sum of the first 4 odd numbers `sumOdd = 1 + 3 + 5 + 7 = 16`
26+
* Sum of the first 4 even numbers `sumEven = 2 + 4 + 6 + 8 = 20`
27+
28+
Hence, `GCD(sumOdd, sumEven) = GCD(16, 20) = 4`.
29+
30+
**Example 2:**
31+
32+
**Input:** n = 5
33+
34+
**Output:** 5
35+
36+
**Explanation:**
37+
38+
* Sum of the first 5 odd numbers `sumOdd = 1 + 3 + 5 + 7 + 9 = 25`
39+
* Sum of the first 5 even numbers `sumEven = 2 + 4 + 6 + 8 + 10 = 30`
40+
41+
Hence, `GCD(sumOdd, sumEven) = GCD(25, 30) = 5`.
42+
43+
**Constraints:**
44+
45+
* `1 <= n <= 1000`
46+
47+
## Solution
48+
49+
```kotlin
50+
class Solution {
51+
fun gcdOfOddEvenSums(n: Int): Int {
52+
return if (n < 0) -n else n
53+
}
54+
}
55+
```
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](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)
3+
4+
## 3659\. Partition Array Into K-Distinct Groups
5+
6+
Medium
7+
8+
You are given an integer array `nums` and an integer `k`.
9+
10+
Your task is to determine whether it is possible to partition all elements of `nums` into one or more groups such that:
11+
12+
* Each group contains **exactly** `k` **distinct** elements.
13+
* Each element in `nums` must be assigned to **exactly** one group.
14+
15+
Return `true` if such a partition is possible, otherwise return `false`.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [1,2,3,4], k = 2
20+
21+
**Output:** true
22+
23+
**Explanation:**
24+
25+
One possible partition is to have 2 groups:
26+
27+
* Group 1: `[1, 2]`
28+
* Group 2: `[3, 4]`
29+
30+
Each group contains `k = 2` distinct elements, and all elements are used exactly once.
31+
32+
**Example 2:**
33+
34+
**Input:** nums = [3,5,2,2], k = 2
35+
36+
**Output:** true
37+
38+
**Explanation:**
39+
40+
One possible partition is to have 2 groups:
41+
42+
* Group 1: `[2, 3]`
43+
* Group 2: `[2, 5]`
44+
45+
Each group contains `k = 2` distinct elements, and all elements are used exactly once.
46+
47+
**Example 3:**
48+
49+
**Input:** nums = [1,5,2,3], k = 3
50+
51+
**Output:** false
52+
53+
**Explanation:**
54+
55+
We cannot form groups of `k = 3` distinct elements using all values exactly once.
56+
57+
**Constraints:**
58+
59+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
60+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
61+
* `1 <= k <= nums.length`
62+
63+
## Solution
64+
65+
```kotlin
66+
import kotlin.math.max
67+
68+
class Solution {
69+
fun partitionArray(nums: IntArray, k: Int): Boolean {
70+
val n = nums.size
71+
if (n % k != 0) {
72+
return false
73+
}
74+
var max = 0
75+
for (x in nums) {
76+
max = max(max, x)
77+
}
78+
val count = IntArray(max + 1)
79+
val limit = n / k
80+
for (x in nums) {
81+
if (++count[x] > limit) {
82+
return false
83+
}
84+
}
85+
return true
86+
}
87+
}
88+
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](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)
3+
4+
## 3660\. Jump Game IX
5+
6+
Medium
7+
8+
You are given an integer array `nums`.
9+
10+
From any index `i`, you can jump to another index `j` under the following rules:
11+
12+
* Jump to index `j` where `j > i` is allowed only if `nums[j] < nums[i]`.
13+
* Jump to index `j` where `j < i` is allowed only if `nums[j] > nums[i]`.
14+
15+
For each index `i`, find the **maximum** **value** in `nums` that can be reached by following **any** sequence of valid jumps starting at `i`.
16+
17+
Return an array `ans` where `ans[i]` is the **maximum** **value** reachable starting from index `i`.
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [2,1,3]
22+
23+
**Output:** [2,2,3]
24+
25+
**Explanation:**
26+
27+
* For `i = 0`: No jump increases the value.
28+
* For `i = 1`: Jump to `j = 0` as `nums[j] = 2` is greater than `nums[i]`.
29+
* For `i = 2`: Since `nums[2] = 3` is the maximum value in `nums`, no jump increases the value.
30+
31+
Thus, `ans = [2, 2, 3]`.
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [2,3,1]
36+
37+
**Output:** [3,3,3]
38+
39+
**Explanation:**
40+
41+
* For `i = 0`: Jump forward to `j = 2` as `nums[j] = 1` is less than `nums[i] = 2`, then from `i = 2` jump to `j = 1` as `nums[j] = 3` is greater than `nums[2]`.
42+
* For `i = 1`: Since `nums[1] = 3` is the maximum value in `nums`, no jump increases the value.
43+
* For `i = 2`: Jump to `j = 1` as `nums[j] = 3` is greater than `nums[2] = 1`.
44+
45+
Thus, `ans = [3, 3, 3]`.
46+
47+
**Constraints:**
48+
49+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
50+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
51+
52+
## Solution
53+
54+
```kotlin
55+
import kotlin.math.max
56+
import kotlin.math.min
57+
58+
class Solution {
59+
fun maxValue(nums: IntArray): IntArray {
60+
val f = IntArray(nums.size)
61+
var cur = 0
62+
for (i in nums.indices) {
63+
cur = max(cur, nums[i])
64+
f[i] = cur
65+
}
66+
var min = nums[nums.size - 1]
67+
for (i in nums.size - 2 downTo 0) {
68+
if (f[i] > min) {
69+
f[i] = max(f[i], f[i + 1])
70+
}
71+
min = min(min, nums[i])
72+
}
73+
return f
74+
}
75+
}
76+
```

0 commit comments

Comments
 (0)