Skip to content
Draft
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
93 changes: 93 additions & 0 deletions src/main/java/g3601_3700/s3657_find_loyal_customers/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
3657\. Find Loyal Customers

Medium

Table: `customer_transactions`

+------------------+---------+
| Column Name | Type |
+------------------+---------+
| transaction_id | int |
| customer_id | int |
| transaction_date | date |
| amount | decimal |
| transaction_type | varchar |
+------------------+---------+
transaction_id is the unique identifier for this table. transaction_type can be either 'purchase' or 'refund'.

Write a solution to find **loyal customers**. A customer is considered **loyal** if they meet ALL the following criteria:

* Made **at least** `3` purchase transactions.
* Have been active for **at least** `30` days.
* Their **refund rate** is less than `20%` .

Return _the result table ordered by_ `customer_id` _in **ascending** order_.

The result format is in the following example.

**Example:**

**Input:**

customer\_transactions table:

+----------------+-------------+------------------+--------+------------------+
| transaction_id | customer_id | transaction_date | amount | transaction_type |
|----------------|-------------|------------------|--------|------------------|
| 1 | 101 | 2024-01-05 | 150.00 | purchase |
| 2 | 101 | 2024-01-15 | 200.00 | purchase |
| 3 | 101 | 2024-02-10 | 180.00 | purchase |
| 4 | 101 | 2024-02-20 | 250.00 | purchase |
| 5 | 102 | 2024-01-10 | 100.00 | purchase |
| 6 | 102 | 2024-01-12 | 120.00 | purchase |
| 7 | 102 | 2024-01-15 | 80.00 | refund |
| 8 | 102 | 2024-01-18 | 90.00 | refund |
| 9 | 102 | 2024-02-15 | 130.00 | purchase |
| 10 | 103 | 2024-01-01 | 500.00 | purchase |
| 11 | 103 | 2024-01-02 | 450.00 | purchase |
| 12 | 103 | 2024-01-03 | 400.00 | purchase |
| 13 | 104 | 2024-01-01 | 200.00 | purchase |
| 14 | 104 | 2024-02-01 | 250.00 | purchase |
| 15 | 104 | 2024-02-15 | 300.00 | purchase |
| 16 | 104 | 2024-03-01 | 350.00 | purchase |
| 17 | 104 | 2024-03-10 | 280.00 | purchase |
| 18 | 104 | 2024-03-15 | 100.00 | refund |
+----------------+-------------+------------------+--------+------------------+


**Output:**

+-------------+
| customer_id |
|-------------|
| 101 |
| 104 |
+-------------+

**Explanation:**

* **Customer 101**:
* Purchase transactions: 4 (IDs: 1, 2, 3, 4)
* Refund transactions: 0
* Refund rate: 0/4 = 0% (less than 20%)
* Active period: Jan 5 to Feb 20 = 46 days (at least 30 days)
* Qualifies as loyal
* **Customer 102**:
* Purchase transactions: 3 (IDs: 5, 6, 9)
* Refund transactions: 2 (IDs: 7, 8)
* Refund rate: 2/5 = 40% (exceeds 20%)
* Not loyal
* **Customer 103**:
* Purchase transactions: 3 (IDs: 10, 11, 12)
* Refund transactions: 0
* Refund rate: 0/3 = 0% (less than 20%)
* Active period: Jan 1 to Jan 3 = 2 days (less than 30 days)
* Not loyal
* **Customer 104**:
* Purchase transactions: 5 (IDs: 13, 14, 15, 16, 17)
* Refund transactions: 1 (ID: 18)
* Refund rate: 1/6 = 16.67% (less than 20%)
* Active period: Jan 1 to Mar 15 = 73 days (at least 30 days)
* Qualifies as loyal

The result table is ordered by customer\_id in ascending order.
14 changes: 14 additions & 0 deletions src/main/java/g3601_3700/s3657_find_loyal_customers/script.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Write your MySQL query statement below
# #Medium #Database #2025_08_25_Time_297_ms_(100.00%)_Space_0.0_MB_(100.00%)
SELECT
customer_id
FROM
customer_transactions
GROUP BY
customer_id
HAVING
COUNT(CASE WHEN transaction_type = 'purchase' THEN 1 END) > 2
AND TIMESTAMPDIFF(DAY, MIN(transaction_date), MAX(transaction_date)) > 29
AND (COUNT(CASE WHEN transaction_type = 'refund' THEN 1 END) * 1.0 / COUNT(*)) < 0.2
ORDER BY
customer_id ASC;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package g3601_3700.s3658_gcd_of_odd_and_even_sums;

// #Easy #Weekly_Contest_464 #2025_08_24_Time_1_ms_(100.00%)_Space_41.16_MB_(100.00%)

public class Solution {
public int gcdOfOddEvenSums(int n) {
return (n < 0) ? -n : n;
}
}
42 changes: 42 additions & 0 deletions src/main/java/g3601_3700/s3658_gcd_of_odd_and_even_sums/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
3658\. GCD of Odd and Even Sums

Easy

You are given an integer `n`. Your task is to compute the **GCD** (greatest common divisor) of two values:

* `sumOdd`: the sum of the first `n` odd numbers.

* `sumEven`: the sum of the first `n` even numbers.


Return the GCD of `sumOdd` and `sumEven`.

**Example 1:**

**Input:** n = 4

**Output:** 4

**Explanation:**

* Sum of the first 4 odd numbers `sumOdd = 1 + 3 + 5 + 7 = 16`
* Sum of the first 4 even numbers `sumEven = 2 + 4 + 6 + 8 = 20`

Hence, `GCD(sumOdd, sumEven) = GCD(16, 20) = 4`.

**Example 2:**

**Input:** n = 5

**Output:** 5

**Explanation:**

* Sum of the first 5 odd numbers `sumOdd = 1 + 3 + 5 + 7 + 9 = 25`
* Sum of the first 5 even numbers `sumEven = 2 + 4 + 6 + 8 + 10 = 30`

Hence, `GCD(sumOdd, sumEven) = GCD(25, 30) = 5`.

**Constraints:**

* `1 <= n <= 1000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g3601_3700.s3659_partition_array_into_k_distinct_groups;

// #Medium #Weekly_Contest_464 #2025_08_24_Time_81_ms_(100.00%)_Space_62.78_MB_(100.00%)

import java.util.HashMap;

public class Solution {
public boolean partitionArray(int[] nums, int k) {
HashMap<Integer, Integer> mpp = new HashMap<>();
for (int x : nums) {
mpp.put(x, mpp.getOrDefault(x, 0) + 1);
}
for (int count : mpp.values()) {
if (count > nums.length / k) {
return false;
}
}
return nums.length % k == 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
3659\. Partition Array Into K-Distinct Groups

Medium

You are given an integer array `nums` and an integer `k`.

Your task is to determine whether it is possible to partition all elements of `nums` into one or more groups such that:

* Each group contains **exactly** `k` **distinct** elements.
* Each element in `nums` must be assigned to **exactly** one group.

Return `true` if such a partition is possible, otherwise return `false`.

**Example 1:**

**Input:** nums = [1,2,3,4], k = 2

**Output:** true

**Explanation:**

One possible partition is to have 2 groups:

* Group 1: `[1, 2]`
* Group 2: `[3, 4]`

Each group contains `k = 2` distinct elements, and all elements are used exactly once.

**Example 2:**

**Input:** nums = [3,5,2,2], k = 2

**Output:** true

**Explanation:**

One possible partition is to have 2 groups:

* Group 1: `[2, 3]`
* Group 2: `[2, 5]`

Each group contains `k = 2` distinct elements, and all elements are used exactly once.

**Example 3:**

**Input:** nums = [1,5,2,3], k = 3

**Output:** false

**Explanation:**

We cannot form groups of `k = 3` distinct elements using all values exactly once.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
* `1 <= k <= nums.length`
89 changes: 89 additions & 0 deletions src/main/java/g3601_3700/s3660_jump_game_ix/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package g3601_3700.s3660_jump_game_ix;

// #Medium #Weekly_Contest_464 #2025_08_24_Time_248_ms_(100.00%)_Space_72.98_MB_(100.00%)

import java.util.ArrayDeque;
import java.util.HashMap;

public class Solution {
public int[] maxValue(int[] nums) {
int n = nums.length;
ArrayDeque<Integer> st = new ArrayDeque<>();
UnionFind uf = new UnionFind(n);
for (int i = 0; i < n; i++) {
int prev = i;
if (!st.isEmpty()) {
prev = st.peek();
}
while (!st.isEmpty() && nums[i] < nums[st.peek()]) {
uf.union(st.pop(), i);
}
if (nums[i] > nums[prev]) {
st.push(i);
} else {
st.push(prev);
}
}
st.clear();
for (int i = n - 1; i >= 0; i--) {
int prev = i;
if (!st.isEmpty()) {
prev = st.peek();
}
while (!st.isEmpty() && nums[i] > nums[st.peek()]) {
uf.union(st.pop(), i);
}
if (nums[i] < nums[prev]) {
st.push(i);
} else {
st.push(prev);
}
}
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
int root = uf.find(i);
map.put(root, Math.max(map.getOrDefault(root, Integer.MIN_VALUE), nums[i]));
}
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
ans[i] = map.get(uf.find(i));
}
return ans;
}

private static class UnionFind {
int[] par;
int[] rank;

UnionFind(int n) {
par = new int[n];
rank = new int[n];
for (int i = 0; i < n; i++) {
par[i] = i;
}
}

int find(int x) {
if (par[x] != x) {
par[x] = find(par[x]);
}
return par[x];
}

void union(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return;
}
if (rank[x] < rank[y]) {
par[x] = y;
} else if (rank[x] > rank[y]) {
par[y] = x;
} else {
par[y] = x;
rank[x]++;
}
}
}
}
47 changes: 47 additions & 0 deletions src/main/java/g3601_3700/s3660_jump_game_ix/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
3660\. Jump Game IX

Medium

You are given an integer array `nums`.

From any index `i`, you can jump to another index `j` under the following rules:

* Jump to index `j` where `j > i` is allowed only if `nums[j] < nums[i]`.
* Jump to index `j` where `j < i` is allowed only if `nums[j] > nums[i]`.

For each index `i`, find the **maximum** **value** in `nums` that can be reached by following **any** sequence of valid jumps starting at `i`.

Return an array `ans` where `ans[i]` is the **maximum** **value** reachable starting from index `i`.

**Example 1:**

**Input:** nums = [2,1,3]

**Output:** [2,2,3]

**Explanation:**

* For `i = 0`: No jump increases the value.
* For `i = 1`: Jump to `j = 0` as `nums[j] = 2` is greater than `nums[i]`.
* For `i = 2`: Since `nums[2] = 3` is the maximum value in `nums`, no jump increases the value.

Thus, `ans = [2, 2, 3]`.

**Example 2:**

**Input:** nums = [2,3,1]

**Output:** [3,3,3]

**Explanation:**

* 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]`.
* For `i = 1`: Since `nums[1] = 3` is the maximum value in `nums`, no jump increases the value.
* For `i = 2`: Jump to `j = 1` as `nums[j] = 3` is greater than `nums[2] = 1`.

Thus, `ans = [3, 3, 3]`.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Loading
Loading