Skip to content

Commit 00369a0

Browse files
committed
Added tasks 3521-3525
1 parent c7a853a commit 00369a0

File tree

22 files changed

+584
-25
lines changed
  • src/main/java
    • g0701_0800/s0706_design_hashmap
    • g1801_1900/s1837_sum_of_digits_in_base_k
    • g2301_2400
      • s2332_the_latest_time_to_catch_a_bus
      • s2380_time_needed_to_rearrange_a_binary_string
      • s2384_largest_palindromic_number
    • g2401_2500
      • s2410_maximum_matching_of_players_with_trainers
      • s2468_split_message_based_on_limit
    • g2701_2800/s2745_construct_the_longest_new_string
    • g2801_2900
      • s2812_find_the_safest_path_in_a_grid
      • s2816_double_a_number_represented_as_a_linked_list
    • g2901_3000
      • s2932_maximum_strong_pair_xor_i
      • s2972_count_the_number_of_incremovable_subarrays_ii
    • g3101_3200/s3108_minimum_cost_walk_in_weighted_graph
    • g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i
    • g3401_3500
      • s3407_substring_matching_pattern
      • s3424_minimum_cost_to_make_arrays_identical
    • g3501_3600
      • s3521_find_product_recommendation_pairs
      • s3522_calculate_score_after_performing_instructions
      • s3523_make_array_non_decreasing
      • s3524_find_x_value_of_array_i
      • s3525_find_x_value_of_array_ii

22 files changed

+584
-25
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,11 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3525 |[Find X Value of Array II](src/main/java/g3501_3600/s3525_find_x_value_of_array_ii)| Hard | Array, Math, Segment_Tree | 177 | 79.87
2092+
| 3524 |[Find X Value of Array I](src/main/java/g3501_3600/s3524_find_x_value_of_array_i)| Medium | Array, Dynamic_Programming, Math | 12 | 95.54
2093+
| 3523 |[Make Array Non-decreasing](src/main/java/g3501_3600/s3523_make_array_non_decreasing)| Medium | Array, Greedy, Stack, Monotonic_Stack | 3 | 63.29
2094+
| 3522 |[Calculate Score After Performing Instructions](src/main/java/g3501_3600/s3522_calculate_score_after_performing_instructions)| Medium | Array, String, Hash_Table, Simulation | 1 | 100.00
2095+
| 3521 |[Find Product Recommendation Pairs](src/main/java/g3501_3600/s3521_find_product_recommendation_pairs)| Medium | Database | 611 | 70.71
20912096
| 3519 |[Count Numbers with Non-Decreasing Digits](src/main/java/g3501_3600/s3519_count_numbers_with_non_decreasing_digits)| Hard | String, Dynamic_Programming, Math | 19 | 100.00
20922097
| 3518 |[Smallest Palindromic Rearrangement II](src/main/java/g3501_3600/s3518_smallest_palindromic_rearrangement_ii)| Hard | String, Hash_Table, Math, Counting, Combinatorics | 34 | 100.00
20932098
| 3517 |[Smallest Palindromic Rearrangement I](src/main/java/g3501_3600/s3517_smallest_palindromic_rearrangement_i)| Medium | String, Sorting, Counting_Sort | 33 | 100.00
@@ -4276,7 +4281,7 @@
42764281
| 0775 |[Global and Local Inversions](src/main/java/g0701_0800/s0775_global_and_local_inversions)| Medium | Array, Math | 1 | 99.74
42774282
| 0773 |[Sliding Puzzle](src/main/java/g0701_0800/s0773_sliding_puzzle)| Hard | Array, Breadth_First_Search, Matrix | 9 | 81.80
42784283
| 0771 |[Jewels and Stones](src/main/java/g0701_0800/s0771_jewels_and_stones)| Easy | String, Hash_Table | 1 | 91.74
4279-
| 0770 |[Basic Calculator IV](src/main/java/g0701_0800/s0770_basic_calculator_iv)| Hard | String, Hash_Table, Math, Stack, Recursion | 8 | 96.92
4284+
| 0770 |[Basic Calculator IV](src/main/java/g0701_0800/s0770_basic_calculator_iv)| Hard | String, Hash_Table, Math, Stack, Recursion | 8 | 95.70
42804285
| 0769 |[Max Chunks To Make Sorted](src/main/java/g0701_0800/s0769_max_chunks_to_make_sorted)| Medium | Array, Sorting, Greedy, Stack, Monotonic_Stack | 0 | 100.00
42814286
| 0768 |[Max Chunks To Make Sorted II](src/main/java/g0701_0800/s0768_max_chunks_to_make_sorted_ii)| Hard | Array, Sorting, Greedy, Stack, Monotonic_Stack | 1 | 87.58
42824287
| 0767 |[Reorganize String](src/main/java/g0701_0800/s0767_reorganize_string)| Medium | String, Hash_Table, Sorting, Greedy, Heap_Priority_Queue, Counting | 1 | 94.60

src/main/java/g0701_0800/s0706_design_hashmap/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import java.util.ArrayList;
4848

4949
@SuppressWarnings("unchecked")
5050
public class MyHashMap {
51-
private ArrayList[] arr = null;
51+
private final ArrayList[] arr;
5252

5353
public MyHashMap() {
5454
arr = new ArrayList[1000];

src/main/java/g1801_1900/s1837_sum_of_digits_in_base_k/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ After converting, each digit should be interpreted as a base `10` number, and th
3535
```java
3636
public class Solution {
3737
public int sumBase(int n, int k) {
38-
int a = 0;
38+
int a;
3939
int sum = 0;
40-
int b = 0;
40+
int b;
4141
while (n != 0) {
4242
a = n % k;
4343
b = n / k;

src/main/java/g2301_2400/s2332_the_latest_time_to_catch_a_bus/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public class Solution {
8484
b++;
8585
}
8686
}
87-
int start = 0;
87+
int start;
8888
if (c == capacity) {
8989
// capcity is full in last bus, find time last passenger might have boarded
9090
start = Math.min(passengers[p - 1], buses[blen - 1]);

src/main/java/g2301_2400/s2380_time_needed_to_rearrange_a_binary_string/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class Solution {
5151
public int secondsToRemoveOccurrences(String s) {
5252
int lastOne = -1;
5353
int result = 0;
54-
int prevResult = 0;
54+
int prevResult;
5555
int curResult = 0;
5656
int countOne = 0;
5757
int countZero = 0;

src/main/java/g2301_2400/s2384_largest_palindromic_number/readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,26 @@ public class Solution {
5555
for (char c : num.toCharArray()) {
5656
count[c - '0']++;
5757
}
58-
int c = 0;
58+
int c;
5959
for (int i = 9; i >= 0; i--) {
6060
c = 0;
6161
if (count[i] % 2 == 1 && center == -1) {
6262
center = i;
6363
}
64-
if (first.length() == 0 && i == 0) {
64+
if (first.isEmpty() && i == 0) {
6565
continue;
6666
}
6767
while (c < count[i] / 2) {
68-
first.append(String.valueOf(i));
68+
first.append(i);
6969
c++;
7070
}
7171
}
7272
second = new StringBuilder(first.toString());
7373
if (center != -1) {
7474
first.append(center);
7575
}
76-
first.append(second.reverse().toString());
77-
return first.length() == 0 ? "0" : first.toString();
76+
first.append(second.reverse());
77+
return first.isEmpty() ? "0" : first.toString();
7878
}
7979
}
8080
```

src/main/java/g2401_2500/s2410_maximum_matching_of_players_with_trainers/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class Solution {
5454
public int matchPlayersAndTrainers(int[] players, int[] trainers) {
5555
Arrays.sort(players);
5656
Arrays.sort(trainers);
57-
int i = 0;
57+
int i;
5858
int j = 0;
5959
int count = 0;
6060
i = 0;

src/main/java/g2401_2500/s2468_split_message_based_on_limit/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Under the given constraints, the string can be split into two parts:
5555
@SuppressWarnings("java:S3518")
5656
public class Solution {
5757
public String[] splitMessage(String message, int limit) {
58-
int total = 0;
58+
int total;
5959
int running = 0;
6060
int count;
6161
int totalReq;

src/main/java/g2701_2800/s2745_construct_the_longest_new_string/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ A **substring** is a contiguous **non-empty** sequence of characters within a st
3939
public class Solution {
4040
public int longestString(int x, int y, int z) {
4141
int min = Math.min(x, y);
42-
int res = 0;
42+
int res;
4343
if (x == y) {
4444
res = 2 * min + z;
4545
} else {

src/main/java/g2801_2900/s2812_find_the_safest_path_in_a_grid/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public class Solution {
9999
int[] tmpDeque;
100100
int[] queue = new int[yLen * xLen];
101101
int[] root = new int[yLen * xLen];
102-
int head = -1;
102+
int head;
103103
int tail = -1;
104104
int qIdx = -1;
105105
int end = yLen * xLen - 1;

src/main/java/g2801_2900/s2816_double_a_number_represented_as_a_linked_list/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public class Solution {
7676

7777
private ListNode revList(ListNode head) {
7878
ListNode prev = null;
79-
ListNode nxt = null;
79+
ListNode nxt;
8080
ListNode current = head;
8181
while (current != null) {
8282
nxt = current.next;

src/main/java/g2901_3000/s2932_maximum_strong_pair_xor_i/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Return _the **maximum**_ `XOR` _value out of all possible strong pairs in the ar
5050
public class Solution {
5151
public int maximumStrongPairXor(int[] nums) {
5252
int max = 0;
53-
int pair = 0;
53+
int pair;
5454
for (int i = 0; i < nums.length; i++) {
5555
for (int j = i; j < nums.length; j++) {
5656
if (Math.abs(nums[i] - nums[j]) <= Math.min(nums[i], nums[j])) {

src/main/java/g2901_3000/s2972_count_the_number_of_incremovable_subarrays_ii/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ A **subarray** is a contiguous non-empty sequence of elements within an array.
4949
```java
5050
public class Solution {
5151
public long incremovableSubarrayCount(int[] nums) {
52-
long ans = 0;
52+
long ans;
5353
int n = nums.length;
5454
int l = 0;
5555
int r = n - 1;

src/main/java/g3101_3200/s3108_minimum_cost_walk_in_weighted_graph/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public class Solution {
8080
if (parent1 == parent2) {
8181
bitwise[parent1] &= weight;
8282
} else {
83-
int bitwiseVal = 0;
83+
int bitwiseVal;
8484
boolean check1 = bitwise[parent1] == -1;
8585
boolean check2 = bitwise[parent2] == -1;
8686
if (check1 && check2) {

src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ import java.util.Map;
6060

6161
public class Solution {
6262
private static final int MOD = (int) 1e9 + 7;
63-
private long[] c2 = new long[1001];
63+
private final long[] c2 = new long[1001];
6464

6565
public int subsequencesWithMiddleMode(int[] nums) {
6666
if (c2[2] == 0) {
6767
c2[0] = c2[1] = 0;
6868
c2[2] = 1;
6969
for (int i = 3; i < c2.length; ++i) {
70-
c2[i] = i * (i - 1) / 2;
70+
c2[i] = (long) i * (i - 1) / 2;
7171
}
7272
}
7373
int n = nums.length;
@@ -111,8 +111,8 @@ public class Solution {
111111
ans -=
112112
leftY
113113
* rightY
114-
* (leftX * (right - rightX - rightY)
115-
+ rightX * (left - leftX - leftY));
114+
* ((long) leftX * (right - rightX - rightY)
115+
+ (long) rightX * (left - leftX - leftY));
116116
}
117117
}
118118
leftCount[x]++;

src/main/java/g3401_3500/s3407_substring_matching_pattern/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public class Solution {
7373
private int fun(String s, String k) {
7474
int n = s.length();
7575
int m = k.length();
76-
int j = 0;
76+
int j;
7777
for (int i = 0; i <= n - m; i++) {
7878
for (j = 0; j < m; j++) {
7979
char ch1 = s.charAt(j + i);

src/main/java/g3401_3500/s3424_minimum_cost_to_make_arrays_identical/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class Solution {
5454
public long minCost(int[] arr, int[] brr, long k) {
5555
int n = arr.length;
5656
long sum1 = 0;
57-
long sum2 = 0;
57+
long sum2;
5858
for (int i = 0; i < n; i++) {
5959
sum1 += Math.abs(arr[i] - brr[i]);
6060
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3521\. Find Product Recommendation Pairs
5+
6+
Medium
7+
8+
Table: `ProductPurchases`
9+
10+
+-------------+------+
11+
| Column Name | Type |
12+
+-------------+------+
13+
| user_id | int |
14+
| product_id | int |
15+
| quantity | int |
16+
+-------------+------+
17+
(user_id, product_id) is the unique key for this table.
18+
Each row represents a purchase of a product by a user in a specific quantity.
19+
20+
Table: `ProductInfo`
21+
22+
+-------------+---------+
23+
| Column Name | Type |
24+
+-------------+---------+
25+
| product_id | int |
26+
| category | varchar |
27+
| price | decimal |
28+
+-------------+---------+
29+
product_id is the primary key for this table. Each row assigns a category and price to a product.
30+
31+
Amazon wants to implement the **Customers who bought this also bought...** feature based on **co-purchase patterns**. Write a solution to :
32+
33+
1. Identify **distinct** product pairs frequently **purchased together by the same customers** (where `product1_id` < `product2_id`)
34+
2. For **each product pair**, determine how many customers purchased **both** products
35+
36+
**A product pair** is considered for recommendation **if** **at least** `3` **different** customers have purchased **both products**.
37+
38+
Return _the_ _result table ordered by **customer\_count** in **descending** order, and in case of a tie, by_ `product1_id` _in **ascending** order, and then by_ `product2_id` _in **ascending** order_.
39+
40+
The result format is in the following example.
41+
42+
**Example:**
43+
44+
**Input:**
45+
46+
ProductPurchases table:
47+
48+
+---------+------------+----------+
49+
| user_id | product_id | quantity |
50+
+---------+------------+----------+
51+
| 1 | 101 | 2 |
52+
| 1 | 102 | 1 |
53+
| 1 | 103 | 3 |
54+
| 2 | 101 | 1 |
55+
| 2 | 102 | 5 |
56+
| 2 | 104 | 1 |
57+
| 3 | 101 | 2 |
58+
| 3 | 103 | 1 |
59+
| 3 | 105 | 4 |
60+
| 4 | 101 | 1 |
61+
| 4 | 102 | 1 |
62+
| 4 | 103 | 2 |
63+
| 4 | 104 | 3 |
64+
| 5 | 102 | 2 |
65+
| 5 | 104 | 1 |
66+
+---------+------------+----------+
67+
68+
ProductInfo table:
69+
70+
+------------+-------------+-------+
71+
| product_id | category | price |
72+
+------------+-------------+-------+
73+
| 101 | Electronics | 100 |
74+
| 102 | Books | 20 |
75+
| 103 | Clothing | 35 |
76+
| 104 | Kitchen | 50 |
77+
| 105 | Sports | 75 |
78+
+------------+-------------+-------+
79+
80+
**Output:**
81+
82+
+-------------+-------------+-------------------+-------------------+----------------+
83+
| product1_id | product2_id | product1_category | product2_category | customer_count |
84+
+-------------+-------------+-------------------+-------------------+----------------+
85+
| 101 | 102 | Electronics | Books | 3 |
86+
| 101 | 103 | Electronics | Clothing | 3 |
87+
| 102 | 104 | Books | Kitchen | 3 |
88+
+-------------+-------------+-------------------+-------------------+----------------+
89+
90+
**Explanation:**
91+
92+
* **Product pair (101, 102):**
93+
* Purchased by users 1, 2, and 4 (3 customers)
94+
* Product 101 is in Electronics category
95+
* Product 102 is in Books category
96+
* **Product pair (101, 103):**
97+
* Purchased by users 1, 3, and 4 (3 customers)
98+
* Product 101 is in Electronics category
99+
* Product 103 is in Clothing category
100+
* **Product pair (102, 104):**
101+
* Purchased by users 2, 4, and 5 (3 customers)
102+
* Product 102 is in Books category
103+
* Product 104 is in Kitchen category
104+
105+
The result is ordered by customer\_count in descending order. For pairs with the same customer\_count, they are ordered by product1\_id and then product2\_id in ascending order.
106+
107+
## Solution
108+
109+
```sql
110+
# Write your MySQL query statement below
111+
SELECT
112+
P1.product_id AS product1_id,
113+
P2.product_id AS product2_id,
114+
PI1.category AS product1_category,
115+
PI2.category AS product2_category,
116+
COUNT(P1.user_id) AS customer_count
117+
FROM ProductPurchases P1
118+
INNER JOIN ProductPurchases P2 ON P1.user_id=P2.user_id AND P1.product_id<P2.product_id
119+
LEFT JOIN ProductInfo PI1 ON P1.product_id=PI1.product_id
120+
LEFT JOIN ProductInfo PI2 ON P2.product_id=PI2.product_id
121+
GROUP BY 1,2,3,4
122+
HAVING COUNT(P1.user_id)>=3
123+
ORDER BY customer_count DESC,product1_id,product2_id
124+
```

0 commit comments

Comments
 (0)