Skip to content

Commit 5362614

Browse files
committed
Added tasks 3521-3525
1 parent 3bd424e commit 5362614

File tree

13 files changed

+567
-15
lines changed
  • src/main/kotlin
    • g0701_0800/s0770_basic_calculator_iv
    • g1801_1900/s1825_finding_mk_average
    • g2901_3000/s2973_find_number_of_coins_to_place_in_tree_nodes
    • g3101_3200/s3130_find_all_possible_stable_binary_arrays_ii
    • g3401_3500
      • s3429_paint_house_iv
      • s3468_find_the_number_of_copy_arrays
      • s3480_maximize_subarrays_after_removing_one_conflicting_pair
    • 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

13 files changed

+567
-15
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+
| 3525 |[Find X Value of Array II](src/main/kotlin/g3501_3600/s3525_find_x_value_of_array_ii)| Hard | Array, Math, Segment_Tree | 237 | 50.00
2092+
| 3524 |[Find X Value of Array I](src/main/kotlin/g3501_3600/s3524_find_x_value_of_array_i)| Medium | Array, Dynamic_Programming, Math | 12 | 100.00
2093+
| 3523 |[Make Array Non-decreasing](src/main/kotlin/g3501_3600/s3523_make_array_non_decreasing)| Medium | Array, Greedy, Stack, Monotonic_Stack | 4 | 75.00
2094+
| 3522 |[Calculate Score After Performing Instructions](src/main/kotlin/g3501_3600/s3522_calculate_score_after_performing_instructions)| Medium | Array, String, Hash_Table, Simulation | 3 | 100.00
2095+
| 3521 |[Find Product Recommendation Pairs](src/main/kotlin/g3501_3600/s3521_find_product_recommendation_pairs)| Medium | Database | 611 | 70.71
20912096
| 3519 |[Count Numbers with Non-Decreasing Digits](src/main/kotlin/g3501_3600/s3519_count_numbers_with_non_decreasing_digits)| Hard | String, Dynamic_Programming, Math | 31 | 100.00
20922097
| 3518 |[Smallest Palindromic Rearrangement II](src/main/kotlin/g3501_3600/s3518_smallest_palindromic_rearrangement_ii)| Hard | String, Hash_Table, Math, Counting, Combinatorics | 27 | 100.00
20932098
| 3517 |[Smallest Palindromic Rearrangement I](src/main/kotlin/g3501_3600/s3517_smallest_palindromic_rearrangement_i)| Medium | String, Sorting, Counting_Sort | 49 | 100.00

src/main/kotlin/g0701_0800/s0770_basic_calculator_iv/readme.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class Solution {
106106
return ans
107107
}
108108

109-
fun evaluate(vars: Map<String?, Int>): Node {
109+
fun evaluate(vars: Map<String, Int>): Node {
110110
val ans = Node()
111111
for (cur in mem.keys) {
112112
var cnt = mem[cur]!!
@@ -195,14 +195,14 @@ class Solution {
195195
return a.sub(b)
196196
}
197197

198-
fun basicCalculatorIV(expression: String?, evalvarS: Array<String?>?, evalintS: IntArray?): List<String> {
198+
fun basicCalculatorIV(expression: String, evalvars: Array<String>, evalints: IntArray): List<String> {
199199
val ans: List<String> = ArrayList()
200-
if (expression.isNullOrEmpty() || evalvarS == null || evalintS == null) {
200+
if (expression.isEmpty()) {
201201
return ans
202202
}
203-
val vars: MutableMap<String?, Int> = HashMap()
204-
for (i in evalvarS.indices) {
205-
vars[evalvarS[i]] = evalintS[i]
203+
val vars: MutableMap<String, Int> = HashMap()
204+
for (i in evalvars.indices) {
205+
vars[evalvars[i]] = evalints[i]
206206
}
207207
val n = expression.length
208208
val numS = ArrayDeque<Node>()

src/main/kotlin/g1801_1900/s1825_finding_mk_average/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ class MKAverage(private val capacity: Int, private val boundary: Int) {
7070
val count = nums[num]
7171
if (skipCount < 0) {
7272
sum += num * min(count, numsCount)
73-
numsCount = (numsCount - min(count, numsCount)).toInt()
73+
numsCount = numsCount - min(count, numsCount)
7474
} else {
7575
skipCount -= count
7676
if (skipCount < 0) {
7777
sum += num * min(abs(skipCount), numsCount)
78-
numsCount = (numsCount - min(abs(skipCount), numsCount)).toInt()
78+
numsCount = numsCount - min(abs(skipCount), numsCount)
7979
}
8080
}
8181
if (numsCount == 0) {

src/main/kotlin/g2901_3000/s2973_find_number_of_coins_to_place_in_tree_nodes/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ class Solution {
123123
val bb = if (pq2.isNotEmpty()) pq2.poll() else 0
124124
result[i] = max(0, (a.toLong() * b * c))
125125
result[i] = max(result[i], max(0, (a.toLong() * aa * bb)))
126-
.toLong()
127126
pq = PriorityQueue { x: Int, y: Int -> y - x }
128127
pq.add(a)
129128
pq.add(b)

src/main/kotlin/g3101_3200/s3130_find_all_possible_stable_binary_arrays_ii/readme.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ class Solution {
7676
}
7777
var ans: Long = 0
7878
val s = LongArray(one + 1)
79-
val n = (min(zero, one) + 1).toInt()
79+
val n = min(zero, one) + 1
8080
for (
8181
groups0 in (zero + limit - 1) / limit..min(zero, n)
82-
.toInt()
8382
) {
8483
val s0 = calc(groups0, zero, limit)
8584
for (

src/main/kotlin/g3401_3500/s3429_paint_house_iv/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class Solution {
9090
}
9191
var ans = Long.Companion.MAX_VALUE
9292
for (x in longArrayOf(dp0, dp1, dp2, dp3, dp4, dp5)) {
93-
ans = min(ans, x).toLong()
93+
ans = min(ans, x)
9494
}
9595
return ans
9696
}

src/main/kotlin/g3401_3500/s3468_find_the_number_of_copy_arrays/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Solution {
7575
val diff = original[i] - original[i - 1]
7676
low = max((low + diff), bounds[i][0])
7777
high = min((high + diff), bounds[i][1])
78-
ans = min(ans, (high - low + 1)).toInt()
78+
ans = min(ans, high - low + 1)
7979
}
8080
return max(ans, 0)
8181
}

src/main/kotlin/g3401_3500/s3480_maximize_subarrays_after_removing_one_conflicting_pair/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Solution {
7575
f[n + 1] = n + 1
7676
f[n] = h[n]
7777
for (i in n - 1 downTo 1) {
78-
f[i] = min(h[i], f[i + 1]).toInt()
78+
f[i] = min(h[i], f[i + 1])
7979
}
8080
// forbiddenCount(x) returns (n - x + 1) if x <= n, else 0.
8181
// This is the number of forbidden subarrays starting at some i when f[i] = x.
@@ -96,7 +96,7 @@ class Solution {
9696
continue
9797
}
9898
// Simulate removal: new candidate at j becomes d2[j]
99-
val newCandidate = if (j < n) min(d2[j], f[j + 1]).toInt() else d2[j]
99+
val newCandidate = if (j < n) min(d2[j], f[j + 1]) else d2[j]
100100
// We'll recompute the new f values for indices 1..j.
101101
// Let newF[i] denote the updated value.
102102
// For i > j, newF[i] remains as original f[i].
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-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+
## 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+
```
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
## 3522\. Calculate Score After Performing Instructions
5+
6+
Medium
7+
8+
You are given two arrays, `instructions` and `values`, both of size `n`.
9+
10+
You need to simulate a process based on the following rules:
11+
12+
* You start at the first instruction at index `i = 0` with an initial score of 0.
13+
* If `instructions[i]` is `"add"`:
14+
* Add `values[i]` to your score.
15+
* Move to the next instruction `(i + 1)`.
16+
* If `instructions[i]` is `"jump"`:
17+
* Move to the instruction at index `(i + values[i])` without modifying your score.
18+
19+
The process ends when you either:
20+
21+
* Go out of bounds (i.e., `i < 0 or i >= n`), or
22+
* Attempt to revisit an instruction that has been previously executed. The revisited instruction is not executed.
23+
24+
Return your score at the end of the process.
25+
26+
**Example 1:**
27+
28+
**Input:** instructions = ["jump","add","add","jump","add","jump"], values = [2,1,3,1,-2,-3]
29+
30+
**Output:** 1
31+
32+
**Explanation:**
33+
34+
Simulate the process starting at instruction 0:
35+
36+
* At index 0: Instruction is `"jump"`, move to index `0 + 2 = 2`.
37+
* At index 2: Instruction is `"add"`, add `values[2] = 3` to your score and move to index 3. Your score becomes 3.
38+
* At index 3: Instruction is `"jump"`, move to index `3 + 1 = 4`.
39+
* At index 4: Instruction is `"add"`, add `values[4] = -2` to your score and move to index 5. Your score becomes 1.
40+
* At index 5: Instruction is `"jump"`, move to index `5 + (-3) = 2`.
41+
* At index 2: Already visited. The process ends.
42+
43+
**Example 2:**
44+
45+
**Input:** instructions = ["jump","add","add"], values = [3,1,1]
46+
47+
**Output:** 0
48+
49+
**Explanation:**
50+
51+
Simulate the process starting at instruction 0:
52+
53+
* At index 0: Instruction is `"jump"`, move to index `0 + 3 = 3`.
54+
* At index 3: Out of bounds. The process ends.
55+
56+
**Example 3:**
57+
58+
**Input:** instructions = ["jump"], values = [0]
59+
60+
**Output:** 0
61+
62+
**Explanation:**
63+
64+
Simulate the process starting at instruction 0:
65+
66+
* At index 0: Instruction is `"jump"`, move to index `0 + 0 = 0`.
67+
* At index 0: Already visited. The process ends.
68+
69+
**Constraints:**
70+
71+
* `n == instructions.length == values.length`
72+
* <code>1 <= n <= 10<sup>5</sup></code>
73+
* `instructions[i]` is either `"add"` or `"jump"`.
74+
* <code>-10<sup>5</sup> <= values[i] <= 10<sup>5</sup></code>
75+
76+
## Solution
77+
78+
```kotlin
79+
class Solution {
80+
fun calculateScore(instructions: Array<String>, values: IntArray): Long {
81+
var ans: Long = 0
82+
val seen = BooleanArray(instructions.size)
83+
var pos = 0
84+
while (pos >= 0 && pos < instructions.size && !seen[pos]) {
85+
seen[pos] = true
86+
if (instructions[pos][0] == 'a') {
87+
ans += values[pos].toLong()
88+
pos++
89+
} else {
90+
pos += values[pos]
91+
}
92+
}
93+
return ans
94+
}
95+
}
96+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
## 3523\. Make Array Non-decreasing
5+
6+
Medium
7+
8+
You are given an integer array `nums`. In one operation, you can select a subarray and replace it with a single element equal to its **maximum** value.
9+
10+
Return the **maximum possible size** of the array after performing zero or more operations such that the resulting array is **non-decreasing**.
11+
12+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [4,2,5,3,5]
17+
18+
**Output:** 3
19+
20+
**Explanation:**
21+
22+
One way to achieve the maximum size is:
23+
24+
1. Replace subarray `nums[1..2] = [2, 5]` with `5``[4, 5, 3, 5]`.
25+
2. Replace subarray `nums[2..3] = [3, 5]` with `5``[4, 5, 5]`.
26+
27+
The final array `[4, 5, 5]` is non-decreasing with size 3.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [1,2,3]
32+
33+
**Output:** 3
34+
35+
**Explanation:**
36+
37+
No operation is needed as the array `[1,2,3]` is already non-decreasing.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= nums.length <= 2 * 10<sup>5</sup></code>
42+
* <code>1 <= nums[i] <= 2 * 10<sup>5</sup></code>
43+
44+
## Solution
45+
46+
```kotlin
47+
class Solution {
48+
fun maximumPossibleSize(nums: IntArray): Int {
49+
var res = 0
50+
var prev = Int.Companion.MIN_VALUE
51+
for (x in nums) {
52+
if (x >= prev) {
53+
res++
54+
prev = x
55+
}
56+
}
57+
return res
58+
}
59+
}
60+
```

0 commit comments

Comments
 (0)