Skip to content

Commit 202f2a2

Browse files
authored
Added tasks 3658-3661
1 parent 4b8bf7e commit 202f2a2

File tree

16 files changed

+759
-1
lines changed

16 files changed

+759
-1
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
3657\. Find Loyal Customers
2+
3+
Medium
4+
5+
Table: `customer_transactions`
6+
7+
+------------------+---------+
8+
| Column Name | Type |
9+
+------------------+---------+
10+
| transaction_id | int |
11+
| customer_id | int |
12+
| transaction_date | date |
13+
| amount | decimal |
14+
| transaction_type | varchar |
15+
+------------------+---------+
16+
transaction_id is the unique identifier for this table. transaction_type can be either 'purchase' or 'refund'.
17+
18+
Write a solution to find **loyal customers**. A customer is considered **loyal** if they meet ALL the following criteria:
19+
20+
* Made **at least** `3` purchase transactions.
21+
* Have been active for **at least** `30` days.
22+
* Their **refund rate** is less than `20%` .
23+
24+
Return _the result table ordered by_ `customer_id` _in **ascending** order_.
25+
26+
The result format is in the following example.
27+
28+
**Example:**
29+
30+
**Input:**
31+
32+
customer\_transactions table:
33+
34+
+----------------+-------------+------------------+--------+------------------+
35+
| transaction_id | customer_id | transaction_date | amount | transaction_type |
36+
|----------------|-------------|------------------|--------|------------------|
37+
| 1 | 101 | 2024-01-05 | 150.00 | purchase |
38+
| 2 | 101 | 2024-01-15 | 200.00 | purchase |
39+
| 3 | 101 | 2024-02-10 | 180.00 | purchase |
40+
| 4 | 101 | 2024-02-20 | 250.00 | purchase |
41+
| 5 | 102 | 2024-01-10 | 100.00 | purchase |
42+
| 6 | 102 | 2024-01-12 | 120.00 | purchase |
43+
| 7 | 102 | 2024-01-15 | 80.00 | refund |
44+
| 8 | 102 | 2024-01-18 | 90.00 | refund |
45+
| 9 | 102 | 2024-02-15 | 130.00 | purchase |
46+
| 10 | 103 | 2024-01-01 | 500.00 | purchase |
47+
| 11 | 103 | 2024-01-02 | 450.00 | purchase |
48+
| 12 | 103 | 2024-01-03 | 400.00 | purchase |
49+
| 13 | 104 | 2024-01-01 | 200.00 | purchase |
50+
| 14 | 104 | 2024-02-01 | 250.00 | purchase |
51+
| 15 | 104 | 2024-02-15 | 300.00 | purchase |
52+
| 16 | 104 | 2024-03-01 | 350.00 | purchase |
53+
| 17 | 104 | 2024-03-10 | 280.00 | purchase |
54+
| 18 | 104 | 2024-03-15 | 100.00 | refund |
55+
+----------------+-------------+------------------+--------+------------------+
56+
57+
58+
**Output:**
59+
60+
+-------------+
61+
| customer_id |
62+
|-------------|
63+
| 101 |
64+
| 104 |
65+
+-------------+
66+
67+
**Explanation:**
68+
69+
* **Customer 101**:
70+
* Purchase transactions: 4 (IDs: 1, 2, 3, 4)
71+
* Refund transactions: 0
72+
* Refund rate: 0/4 = 0% (less than 20%)
73+
* Active period: Jan 5 to Feb 20 = 46 days (at least 30 days)
74+
* Qualifies as loyal
75+
* **Customer 102**:
76+
* Purchase transactions: 3 (IDs: 5, 6, 9)
77+
* Refund transactions: 2 (IDs: 7, 8)
78+
* Refund rate: 2/5 = 40% (exceeds 20%)
79+
* Not loyal
80+
* **Customer 103**:
81+
* Purchase transactions: 3 (IDs: 10, 11, 12)
82+
* Refund transactions: 0
83+
* Refund rate: 0/3 = 0% (less than 20%)
84+
* Active period: Jan 1 to Jan 3 = 2 days (less than 30 days)
85+
* Not loyal
86+
* **Customer 104**:
87+
* Purchase transactions: 5 (IDs: 13, 14, 15, 16, 17)
88+
* Refund transactions: 1 (ID: 18)
89+
* Refund rate: 1/6 = 16.67% (less than 20%)
90+
* Active period: Jan 1 to Mar 15 = 73 days (at least 30 days)
91+
* Qualifies as loyal
92+
93+
The result table is ordered by customer\_id in ascending order.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Write your MySQL query statement below
2+
# #Medium #Database #2025_08_25_Time_297_ms_(100.00%)_Space_0.0_MB_(100.00%)
3+
SELECT
4+
customer_id
5+
FROM
6+
customer_transactions
7+
GROUP BY
8+
customer_id
9+
HAVING
10+
COUNT(CASE WHEN transaction_type = 'purchase' THEN 1 END) > 2
11+
AND TIMESTAMPDIFF(DAY, MIN(transaction_date), MAX(transaction_date)) > 29
12+
AND (COUNT(CASE WHEN transaction_type = 'refund' THEN 1 END) * 1.0 / COUNT(*)) < 0.2
13+
ORDER BY
14+
customer_id ASC;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package g3601_3700.s3658_gcd_of_odd_and_even_sums;
2+
3+
// #Easy #Weekly_Contest_464 #2025_08_24_Time_1_ms_(100.00%)_Space_41.16_MB_(100.00%)
4+
5+
public class Solution {
6+
public int gcdOfOddEvenSums(int n) {
7+
return (n < 0) ? -n : n;
8+
}
9+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3658\. GCD of Odd and Even Sums
2+
3+
Easy
4+
5+
You are given an integer `n`. Your task is to compute the **GCD** (greatest common divisor) of two values:
6+
7+
* `sumOdd`: the sum of the first `n` odd numbers.
8+
9+
* `sumEven`: the sum of the first `n` even numbers.
10+
11+
12+
Return the GCD of `sumOdd` and `sumEven`.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 4
17+
18+
**Output:** 4
19+
20+
**Explanation:**
21+
22+
* Sum of the first 4 odd numbers `sumOdd = 1 + 3 + 5 + 7 = 16`
23+
* Sum of the first 4 even numbers `sumEven = 2 + 4 + 6 + 8 = 20`
24+
25+
Hence, `GCD(sumOdd, sumEven) = GCD(16, 20) = 4`.
26+
27+
**Example 2:**
28+
29+
**Input:** n = 5
30+
31+
**Output:** 5
32+
33+
**Explanation:**
34+
35+
* Sum of the first 5 odd numbers `sumOdd = 1 + 3 + 5 + 7 + 9 = 25`
36+
* Sum of the first 5 even numbers `sumEven = 2 + 4 + 6 + 8 + 10 = 30`
37+
38+
Hence, `GCD(sumOdd, sumEven) = GCD(25, 30) = 5`.
39+
40+
**Constraints:**
41+
42+
* `1 <= n <= 1000`
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3601_3700.s3659_partition_array_into_k_distinct_groups;
2+
3+
// #Medium #Weekly_Contest_464 #2025_08_29_Time_2_ms_(100.00%)_Space_55.86_MB_(99.96%)
4+
5+
public class Solution {
6+
public boolean partitionArray(int[] nums, int k) {
7+
int n = nums.length;
8+
if (n % k != 0) {
9+
return false;
10+
}
11+
int max = 0;
12+
for (int x : nums) {
13+
max = Math.max(max, x);
14+
}
15+
int[] count = new int[max + 1];
16+
int limit = n / k;
17+
for (int x : nums) {
18+
if (++count[x] > limit) {
19+
return false;
20+
}
21+
}
22+
return true;
23+
}
24+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
3659\. Partition Array Into K-Distinct Groups
2+
3+
Medium
4+
5+
You are given an integer array `nums` and an integer `k`.
6+
7+
Your task is to determine whether it is possible to partition all elements of `nums` into one or more groups such that:
8+
9+
* Each group contains **exactly** `k` **distinct** elements.
10+
* Each element in `nums` must be assigned to **exactly** one group.
11+
12+
Return `true` if such a partition is possible, otherwise return `false`.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,2,3,4], k = 2
17+
18+
**Output:** true
19+
20+
**Explanation:**
21+
22+
One possible partition is to have 2 groups:
23+
24+
* Group 1: `[1, 2]`
25+
* Group 2: `[3, 4]`
26+
27+
Each group contains `k = 2` distinct elements, and all elements are used exactly once.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [3,5,2,2], k = 2
32+
33+
**Output:** true
34+
35+
**Explanation:**
36+
37+
One possible partition is to have 2 groups:
38+
39+
* Group 1: `[2, 3]`
40+
* Group 2: `[2, 5]`
41+
42+
Each group contains `k = 2` distinct elements, and all elements are used exactly once.
43+
44+
**Example 3:**
45+
46+
**Input:** nums = [1,5,2,3], k = 3
47+
48+
**Output:** false
49+
50+
**Explanation:**
51+
52+
We cannot form groups of `k = 3` distinct elements using all values exactly once.
53+
54+
**Constraints:**
55+
56+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
57+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
58+
* `1 <= k <= nums.length`
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3601_3700.s3660_jump_game_ix;
2+
3+
// #Medium #Weekly_Contest_464 #2025_08_29_Time_3_ms_(100.00%)_Space_66.47_MB_(70.03%)
4+
5+
public class Solution {
6+
public int[] maxValue(int[] nums) {
7+
int[] f = new int[nums.length];
8+
int cur = 0;
9+
for (int i = 0; i < nums.length; i++) {
10+
cur = Math.max(cur, nums[i]);
11+
f[i] = cur;
12+
}
13+
int min = nums[nums.length - 1];
14+
for (int i = nums.length - 2; i >= 0; i--) {
15+
if (f[i] > min) {
16+
f[i] = Math.max(f[i], f[i + 1]);
17+
}
18+
min = Math.min(min, nums[i]);
19+
}
20+
return f;
21+
}
22+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3660\. Jump Game IX
2+
3+
Medium
4+
5+
You are given an integer array `nums`.
6+
7+
From any index `i`, you can jump to another index `j` under the following rules:
8+
9+
* Jump to index `j` where `j > i` is allowed only if `nums[j] < nums[i]`.
10+
* Jump to index `j` where `j < i` is allowed only if `nums[j] > nums[i]`.
11+
12+
For each index `i`, find the **maximum** **value** in `nums` that can be reached by following **any** sequence of valid jumps starting at `i`.
13+
14+
Return an array `ans` where `ans[i]` is the **maximum** **value** reachable starting from index `i`.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [2,1,3]
19+
20+
**Output:** [2,2,3]
21+
22+
**Explanation:**
23+
24+
* For `i = 0`: No jump increases the value.
25+
* For `i = 1`: Jump to `j = 0` as `nums[j] = 2` is greater than `nums[i]`.
26+
* For `i = 2`: Since `nums[2] = 3` is the maximum value in `nums`, no jump increases the value.
27+
28+
Thus, `ans = [2, 2, 3]`.
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [2,3,1]
33+
34+
**Output:** [3,3,3]
35+
36+
**Explanation:**
37+
38+
* 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]`.
39+
* For `i = 1`: Since `nums[1] = 3` is the maximum value in `nums`, no jump increases the value.
40+
* For `i = 2`: Jump to `j = 1` as `nums[j] = 3` is greater than `nums[2] = 1`.
41+
42+
Thus, `ans = [3, 3, 3]`.
43+
44+
**Constraints:**
45+
46+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
47+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>

0 commit comments

Comments
 (0)