Skip to content

Commit cc11d73

Browse files
authored
Added tasks 3222-3229
1 parent b590a08 commit cc11d73

File tree

25 files changed

+729
-1
lines changed

25 files changed

+729
-1
lines changed

src/main/kotlin/g3201_3300/s3220_odd_and_even_transactions/script.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Write your MySQL query statement below
2-
# #Medium #2024_07_18_Time_272_ms_(100.00%)_Space_0B_(100.00%)
2+
# #Medium #Database #2024_07_23_Time_248_ms_(85.85%)_Space_0B_(100.00%)
33
select transaction_date,
44
sum(case when amount%2<>0 then amount else 0 end) as odd_sum,
55
sum(case when amount%2=0 then amount else 0 end) as even_sum from transactions
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3201_3300.s3222_find_the_winning_player_in_coin_game
2+
3+
// #Easy #Math #Simulation #Game_Theory #2024_07_23_Time_140_ms_(86.44%)_Space_34.3_MB_(77.97%)
4+
5+
class Solution {
6+
fun losingPlayer(x: Int, y: Int): String {
7+
var x = x
8+
var y = y
9+
var w = false
10+
while (x > 0 && y >= 4) {
11+
x--
12+
y -= 4
13+
w = !w
14+
}
15+
return if (w) "Alice" else "Bob"
16+
}
17+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
3222\. Find the Winning Player in Coin Game
2+
3+
Easy
4+
5+
You are given two **positive** integers `x` and `y`, denoting the number of coins with values 75 and 10 _respectively_.
6+
7+
Alice and Bob are playing a game. Each turn, starting with **Alice**, the player must pick up coins with a **total** value 115. If the player is unable to do so, they **lose** the game.
8+
9+
Return the _name_ of the player who wins the game if both players play **optimally**.
10+
11+
**Example 1:**
12+
13+
**Input:** x = 2, y = 7
14+
15+
**Output:** "Alice"
16+
17+
**Explanation:**
18+
19+
The game ends in a single turn:
20+
21+
* Alice picks 1 coin with a value of 75 and 4 coins with a value of 10.
22+
23+
**Example 2:**
24+
25+
**Input:** x = 4, y = 11
26+
27+
**Output:** "Bob"
28+
29+
**Explanation:**
30+
31+
The game ends in 2 turns:
32+
33+
* Alice picks 1 coin with a value of 75 and 4 coins with a value of 10.
34+
* Bob picks 1 coin with a value of 75 and 4 coins with a value of 10.
35+
36+
**Constraints:**
37+
38+
* `1 <= x, y <= 100`
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g3201_3300.s3223_minimum_length_of_string_after_operations
2+
3+
// #Medium #String #Hash_Table #Counting #2024_07_23_Time_316_ms_(60.00%)_Space_48.4_MB_(82.22%)
4+
5+
class Solution {
6+
fun minimumLength(s: String): Int {
7+
val freq = IntArray(26)
8+
for (i in 0..25) {
9+
freq[i] = 0
10+
}
11+
for (i in 0 until s.length) {
12+
freq[s[i].code - 'a'.code]++
13+
}
14+
var c = 0
15+
for (i in freq) {
16+
if (i != 0) {
17+
c += if (i % 2 == 0) {
18+
2
19+
} else {
20+
1
21+
}
22+
}
23+
}
24+
return c
25+
}
26+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
3223\. Minimum Length of String After Operations
2+
3+
Medium
4+
5+
You are given a string `s`.
6+
7+
You can perform the following process on `s` **any** number of times:
8+
9+
* Choose an index `i` in the string such that there is **at least** one character to the left of index `i` that is equal to `s[i]`, and **at least** one character to the right that is also equal to `s[i]`.
10+
* Delete the **closest** character to the **left** of index `i` that is equal to `s[i]`.
11+
* Delete the **closest** character to the **right** of index `i` that is equal to `s[i]`.
12+
13+
Return the **minimum** length of the final string `s` that you can achieve.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "abaacbcbb"
18+
19+
**Output:** 5
20+
21+
**Explanation:**
22+
We do the following operations:
23+
24+
* Choose index 2, then remove the characters at indices 0 and 3. The resulting string is `s = "bacbcbb"`.
25+
* Choose index 3, then remove the characters at indices 0 and 5. The resulting string is `s = "acbcb"`.
26+
27+
**Example 2:**
28+
29+
**Input:** s = "aa"
30+
31+
**Output:** 2
32+
33+
**Explanation:**
34+
We cannot perform any operations, so we return the length of the original string.
35+
36+
**Constraints:**
37+
38+
* <code>1 <= s.length <= 2 * 10<sup>5</sup></code>
39+
* `s` consists only of lowercase English letters.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g3201_3300.s3224_minimum_array_changes_to_make_differences_equal
2+
3+
// #Medium #Array #Hash_Table #Prefix_Sum #2024_07_23_Time_665_ms_(84.62%)_Space_69.3_MB_(53.85%)
4+
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun minChanges(nums: IntArray, k: Int): Int {
10+
val cm = IntArray(k + 2)
11+
for (i in 0 until nums.size / 2) {
12+
val a = min(nums[i], nums[nums.size - 1 - i])
13+
val b = max(nums[i], nums[nums.size - 1 - i])
14+
val d = b - a
15+
if (d > 0) {
16+
cm[0]++
17+
cm[d]--
18+
cm[d + 1]++
19+
val max = (max(a, (k - b)) + d)
20+
cm[max + 1]++
21+
} else {
22+
cm[1]++
23+
val max = max(a, (k - a))
24+
cm[max + 1]++
25+
}
26+
}
27+
var sum = cm[0]
28+
var res = cm[0]
29+
for (i in 1..k) {
30+
sum += cm[i]
31+
res = min(res, sum)
32+
}
33+
return res
34+
}
35+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3224\. Minimum Array Changes to Make Differences Equal
2+
3+
Medium
4+
5+
You are given an integer array `nums` of size `n` where `n` is **even**, and an integer `k`.
6+
7+
You can perform some changes on the array, where in one change you can replace **any** element in the array with **any** integer in the range from `0` to `k`.
8+
9+
You need to perform some changes (possibly none) such that the final array satisfies the following condition:
10+
11+
* There exists an integer `X` such that `abs(a[i] - a[n - i - 1]) = X` for all `(0 <= i < n)`.
12+
13+
Return the **minimum** number of changes required to satisfy the above condition.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [1,0,1,2,4,3], k = 4
18+
19+
**Output:** 2
20+
21+
**Explanation:**
22+
We can perform the following changes:
23+
24+
* Replace `nums[1]` by 2. The resulting array is <code>nums = [1,<ins>**2**</ins>,1,2,4,3]</code>.
25+
* Replace `nums[3]` by 3. The resulting array is <code>nums = [1,2,1,<ins>**3**</ins>,4,3]</code>.
26+
27+
The integer `X` will be 2.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [0,1,2,3,3,6,5,4], k = 6
32+
33+
**Output:** 2
34+
35+
**Explanation:**
36+
We can perform the following operations:
37+
38+
* Replace `nums[3]` by 0. The resulting array is <code>nums = [0,1,2,<ins>**0**</ins>,3,6,5,4]</code>.
39+
* Replace `nums[4]` by 4. The resulting array is <code>nums = [0,1,2,0,**<ins>4</ins>**,6,5,4]</code>.
40+
41+
The integer `X` will be 4.
42+
43+
**Constraints:**
44+
45+
* <code>2 <= n == nums.length <= 10<sup>5</sup></code>
46+
* `n` is even.
47+
* <code>0 <= nums[i] <= k <= 10<sup>5</sup></code>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package g3201_3300.s3225_maximum_score_from_grid_operations
2+
3+
// #Hard #Array #Dynamic_Programming #Matrix #Prefix_Sum
4+
// #2024_07_23_Time_371_ms_(100.00%)_Space_49.9_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun maximumScore(grid: Array<IntArray>): Long {
10+
val n = grid.size
11+
var dp1 = LongArray(n)
12+
var dp2 = LongArray(n + 1)
13+
var dp3 = LongArray(n + 1)
14+
var dp12 = LongArray(n)
15+
var dp22 = LongArray(n + 1)
16+
var dp32 = LongArray(n + 1)
17+
var res: Long = 0
18+
for (i in 0 until n) {
19+
var sum: Long = 0
20+
var pre: Long = 0
21+
for (ints in grid) {
22+
sum += ints[i].toLong()
23+
}
24+
for (j in n - 1 downTo 0) {
25+
var s2 = sum
26+
dp12[j] = s2 + dp3[n]
27+
for (k in 0..j) {
28+
s2 -= grid[k][i].toLong()
29+
var v = max((dp1[k] + s2), (dp3[j] + s2))
30+
v = max(v, (pre + s2))
31+
dp12[j] = max(dp12[j], v)
32+
if (k == j) {
33+
dp32[j] = v
34+
dp22[j] = dp32[j]
35+
res = max(res, v)
36+
}
37+
}
38+
if (i > 0) {
39+
pre = max((pre + grid[j][i]), (dp2[j] + grid[j][i]))
40+
}
41+
sum -= grid[j][i].toLong()
42+
}
43+
dp32[n] = pre
44+
dp22[n] = dp32[n]
45+
res = max(res, pre)
46+
for (j in 1..n) {
47+
dp32[j] = max(dp32[j], dp32[j - 1])
48+
}
49+
var tem = dp1
50+
dp1 = dp12
51+
dp12 = tem
52+
tem = dp2
53+
dp2 = dp22
54+
dp22 = tem
55+
tem = dp3
56+
dp3 = dp32
57+
dp32 = tem
58+
}
59+
return res
60+
}
61+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
3225\. Maximum Score From Grid Operations
2+
3+
Hard
4+
5+
You are given a 2D matrix `grid` of size `n x n`. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices `(i, j)`, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<sup>th</sup></code> row.
6+
7+
The grid score is the sum of all `grid[i][j]` such that cell `(i, j)` is white and it has a horizontally adjacent black cell.
8+
9+
Return the **maximum** score that can be achieved after some number of operations.
10+
11+
**Example 1:**
12+
13+
**Input:** grid = [[0,0,0,0,0],[0,0,3,0,0],[0,1,0,0,0],[5,0,0,3,0],[0,0,0,0,2]]
14+
15+
**Output:** 11
16+
17+
**Explanation:**
18+
19+
![](https://assets.leetcode.com/uploads/2024/05/11/one.png)
20+
21+
In the first operation, we color all cells in column 1 down to row 3, and in the second operation, we color all cells in column 4 down to the last row. The score of the resulting grid is `grid[3][0] + grid[1][2] + grid[3][3]` which is equal to 11.
22+
23+
**Example 2:**
24+
25+
**Input:** grid = [[10,9,0,0,15],[7,1,0,8,0],[5,20,0,11,0],[0,0,0,1,2],[8,12,1,10,3]]
26+
27+
**Output:** 94
28+
29+
**Explanation:**
30+
31+
![](https://assets.leetcode.com/uploads/2024/05/11/two-1.png)
32+
33+
We perform operations on 1, 2, and 3 down to rows 1, 4, and 0, respectively. The score of the resulting grid is `grid[0][0] + grid[1][0] + grid[2][1] + grid[4][1] + grid[1][3] + grid[2][3] + grid[3][3] + grid[4][3] + grid[0][4]` which is equal to 94.
34+
35+
**Constraints:**
36+
37+
* `1 <= n == grid.length <= 100`
38+
* `n == grid[i].length`
39+
* <code>0 <= grid[i][j] <= 10<sup>9</sup></code>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3201_3300.s3226_number_of_bit_changes_to_make_two_integers_equal
2+
3+
// #Easy #Bit_Manipulation #2024_07_23_Time_136_ms_(61.90%)_Space_33.2_MB_(90.48%)
4+
5+
class Solution {
6+
fun minChanges(n: Int, k: Int): Int {
7+
var n = n
8+
var k = k
9+
if ((n or k) != n) {
10+
return -1
11+
}
12+
var cnt = 0
13+
while (n > 0 || k > 0) {
14+
val bitN = n and 1
15+
val bitK = k and 1
16+
if (bitN == 1 && bitK == 0) {
17+
cnt++
18+
}
19+
n = n shr 1
20+
k = k shr 1
21+
}
22+
return cnt
23+
}
24+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
3226\. Number of Bit Changes to Make Two Integers Equal
2+
3+
Easy
4+
5+
You are given two positive integers `n` and `k`.
6+
7+
You can choose **any** bit in the **binary representation** of `n` that is equal to 1 and change it to 0.
8+
9+
Return the _number of changes_ needed to make `n` equal to `k`. If it is impossible, return -1.
10+
11+
**Example 1:**
12+
13+
**Input:** n = 13, k = 4
14+
15+
**Output:** 2
16+
17+
**Explanation:**
18+
Initially, the binary representations of `n` and `k` are <code>n = (1101)<sub>2</sub></code> and <code>k = (0100)<sub>2</sub></code>.
19+
We can change the first and fourth bits of `n`. The resulting integer is <code>n = (<ins>**0**</ins>10<ins>**0**</ins>)<sub>2</sub> = k</code>.
20+
21+
**Example 2:**
22+
23+
**Input:** n = 21, k = 21
24+
25+
**Output:** 0
26+
27+
**Explanation:**
28+
`n` and `k` are already equal, so no changes are needed.
29+
30+
**Example 3:**
31+
32+
**Input:** n = 14, k = 13
33+
34+
**Output:** \-1
35+
36+
**Explanation:**
37+
It is not possible to make `n` equal to `k`.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= n, k <= 10<sup>6</sup></code>

0 commit comments

Comments
 (0)