Skip to content

Commit 8eec1ea

Browse files
authored
Added tasks 3360-3373
1 parent bf2b4dc commit 8eec1ea

File tree

36 files changed

+1581
-0
lines changed

36 files changed

+1581
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g3301_3400.s3360_stone_removal_game
2+
3+
// #Easy #Math #Simulation #2024_12_03_Time_0_ms_(100.00%)_Space_34.3_MB_(6.00%)
4+
5+
class Solution {
6+
fun canAliceWin(n: Int): Boolean {
7+
if (n < 10) {
8+
return false
9+
}
10+
var stonesRemaining = n - 10
11+
var stonesToBeRemoved = 9
12+
var i = 1
13+
while (stonesRemaining >= stonesToBeRemoved) {
14+
stonesRemaining -= stonesToBeRemoved
15+
i++
16+
stonesToBeRemoved--
17+
}
18+
return i % 2 != 0
19+
}
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
3360\. Stone Removal Game
2+
3+
Easy
4+
5+
Alice and Bob are playing a game where they take turns removing stones from a pile, with _Alice going first_.
6+
7+
* Alice starts by removing **exactly** 10 stones on her first turn.
8+
* For each subsequent turn, each player removes **exactly** 1 fewer stone than the previous opponent.
9+
10+
The player who cannot make a move loses the game.
11+
12+
Given a positive integer `n`, return `true` if Alice wins the game and `false` otherwise.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 12
17+
18+
**Output:** true
19+
20+
**Explanation:**
21+
22+
* Alice removes 10 stones on her first turn, leaving 2 stones for Bob.
23+
* Bob cannot remove 9 stones, so Alice wins.
24+
25+
**Example 2:**
26+
27+
**Input:** n = 1
28+
29+
**Output:** false
30+
31+
**Explanation:**
32+
33+
* Alice cannot remove 10 stones, so Alice loses.
34+
35+
**Constraints:**
36+
37+
* `1 <= n <= 50`
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g3301_3400.s3361_shift_distance_between_two_strings
2+
3+
// #Medium #Array #String #Prefix_Sum #2024_12_03_Time_350_ms_(82.50%)_Space_41.7_MB_(57.50%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun shiftDistance(s: String, t: String, nextCost: IntArray, previousCost: IntArray): Long {
9+
val costs = Array<LongArray?>(26) { LongArray(26) }
10+
var cost: Long
11+
for (i in 0..25) {
12+
cost = nextCost[i].toLong()
13+
var j = if (i == 25) 0 else i + 1
14+
while (j != i) {
15+
costs[i]!![j] = cost
16+
cost += nextCost[j].toLong()
17+
if (j == 25) {
18+
j = -1
19+
}
20+
j++
21+
}
22+
}
23+
for (i in 0..25) {
24+
cost = previousCost[i].toLong()
25+
var j = if (i == 0) 25 else i - 1
26+
while (j != i) {
27+
costs[i]!![j] = min(costs[i]!![j].toDouble(), cost.toDouble()).toLong()
28+
cost += previousCost[j].toLong()
29+
if (j == 0) {
30+
j = 26
31+
}
32+
j--
33+
}
34+
}
35+
val n = s.length
36+
var ans: Long = 0
37+
for (i in 0..<n) {
38+
ans += costs[s[i].code - 'a'.code]!![t[i].code - 'a'.code]
39+
}
40+
return ans
41+
}
42+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3361\. Shift Distance Between Two Strings
2+
3+
Medium
4+
5+
You are given two strings `s` and `t` of the same length, and two integer arrays `nextCost` and `previousCost`.
6+
7+
In one operation, you can pick any index `i` of `s`, and perform **either one** of the following actions:
8+
9+
* Shift `s[i]` to the next letter in the alphabet. If `s[i] == 'z'`, you should replace it with `'a'`. This operation costs `nextCost[j]` where `j` is the index of `s[i]` in the alphabet.
10+
* Shift `s[i]` to the previous letter in the alphabet. If `s[i] == 'a'`, you should replace it with `'z'`. This operation costs `previousCost[j]` where `j` is the index of `s[i]` in the alphabet.
11+
12+
The **shift distance** is the **minimum** total cost of operations required to transform `s` into `t`.
13+
14+
Return the **shift distance** from `s` to `t`.
15+
16+
**Example 1:**
17+
18+
**Input:** s = "abab", t = "baba", nextCost = [100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], previousCost = [1,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
19+
20+
**Output:** 2
21+
22+
**Explanation:**
23+
24+
* We choose index `i = 0` and shift `s[0]` 25 times to the previous character for a total cost of 1.
25+
* We choose index `i = 1` and shift `s[1]` 25 times to the next character for a total cost of 0.
26+
* We choose index `i = 2` and shift `s[2]` 25 times to the previous character for a total cost of 1.
27+
* We choose index `i = 3` and shift `s[3]` 25 times to the next character for a total cost of 0.
28+
29+
**Example 2:**
30+
31+
**Input:** s = "leet", t = "code", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
32+
33+
**Output:** 31
34+
35+
**Explanation:**
36+
37+
* We choose index `i = 0` and shift `s[0]` 9 times to the previous character for a total cost of 9.
38+
* We choose index `i = 1` and shift `s[1]` 10 times to the next character for a total cost of 10.
39+
* We choose index `i = 2` and shift `s[2]` 1 time to the previous character for a total cost of 1.
40+
* We choose index `i = 3` and shift `s[3]` 11 times to the next character for a total cost of 11.
41+
42+
**Constraints:**
43+
44+
* <code>1 <= s.length == t.length <= 10<sup>5</sup></code>
45+
* `s` and `t` consist only of lowercase English letters.
46+
* `nextCost.length == previousCost.length == 26`
47+
* <code>0 <= nextCost[i], previousCost[i] <= 10<sup>9</sup></code>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g3301_3400.s3362_zero_array_transformation_iii
2+
3+
// #Medium #Array #Sorting #Greedy #Heap_Priority_Queue #Prefix_Sum
4+
// #2024_12_03_Time_195_ms_(81.82%)_Space_106.3_MB_(72.73%)
5+
6+
import java.util.PriorityQueue
7+
8+
class Solution {
9+
fun maxRemoval(nums: IntArray, queries: Array<IntArray>): Int {
10+
queries.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] }
11+
val last = PriorityQueue<Int?>(Comparator { a: Int?, b: Int? -> b!! - a!! })
12+
val diffs = IntArray(nums.size + 1)
13+
var idx = 0
14+
var cur = 0
15+
for (i in nums.indices) {
16+
while (idx < queries.size && queries[idx][0] == i) {
17+
last.add(queries[idx][1])
18+
idx++
19+
}
20+
cur += diffs[i]
21+
while (cur < nums[i] && last.isNotEmpty() && last.peek()!! >= i) {
22+
cur++
23+
diffs[last.poll()!! + 1]--
24+
}
25+
if (cur < nums[i]) {
26+
return -1
27+
}
28+
}
29+
return last.size
30+
}
31+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
3362\. Zero Array Transformation III
2+
3+
Medium
4+
5+
You are given an integer array `nums` of length `n` and a 2D array `queries` where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.
6+
7+
Each `queries[i]` represents the following action on `nums`:
8+
9+
* Decrement the value at each index in the range <code>[l<sub>i</sub>, r<sub>i</sub>]</code> in `nums` by **at most** 1.
10+
* The amount by which the value is decremented can be chosen **independently** for each index.
11+
12+
A **Zero Array** is an array with all its elements equal to 0.
13+
14+
Return the **maximum** number of elements that can be removed from `queries`, such that `nums` can still be converted to a **zero array** using the _remaining_ queries. If it is not possible to convert `nums` to a **zero array**, return -1.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [2,0,2], queries = [[0,2],[0,2],[1,1]]
19+
20+
**Output:** 1
21+
22+
**Explanation:**
23+
24+
After removing `queries[2]`, `nums` can still be converted to a zero array.
25+
26+
* Using `queries[0]`, decrement `nums[0]` and `nums[2]` by 1 and `nums[1]` by 0.
27+
* Using `queries[1]`, decrement `nums[0]` and `nums[2]` by 1 and `nums[1]` by 0.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [1,1,1,1], queries = [[1,3],[0,2],[1,3],[1,2]]
32+
33+
**Output:** 2
34+
35+
**Explanation:**
36+
37+
We can remove `queries[2]` and `queries[3]`.
38+
39+
**Example 3:**
40+
41+
**Input:** nums = [1,2,3,4], queries = [[0,3]]
42+
43+
**Output:** \-1
44+
45+
**Explanation:**
46+
47+
`nums` cannot be converted to a zero array even after using all the queries.
48+
49+
**Constraints:**
50+
51+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
52+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
53+
* <code>1 <= queries.length <= 10<sup>5</sup></code>
54+
* `queries[i].length == 2`
55+
* <code>0 <= l<sub>i</sub> <= r<sub>i</sub> < nums.length</code>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g3301_3400.s3363_find_the_maximum_number_of_fruits_collected
2+
3+
// #Hard #Array #Dynamic_Programming #Matrix
4+
// #2024_12_03_Time_39_ms_(88.89%)_Space_161.2_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun maxCollectedFruits(fruits: Array<IntArray>): Int {
10+
val n = fruits.size
11+
// Set inaccessible cells to 0
12+
for (i in 0..<n) {
13+
for (j in 0..<n) {
14+
if (i < j && j < n - 1 - i) {
15+
fruits[i][j] = 0
16+
}
17+
if (j < i && i < n - 1 - j) {
18+
fruits[i][j] = 0
19+
}
20+
}
21+
}
22+
var res = 0
23+
for (i in 0..<n) {
24+
res += fruits[i][i]
25+
}
26+
for (i in 1..<n) {
27+
for (j in i + 1..<n) {
28+
fruits[i][j] = (
29+
fruits[i][j] + max(
30+
fruits[i - 1][j - 1],
31+
max(fruits[i - 1][j], if (j + 1 < n) fruits[i - 1][j + 1] else 0),
32+
)
33+
)
34+
}
35+
}
36+
for (j in 1..<n) {
37+
for (i in j + 1..<n) {
38+
fruits[i][j] = (
39+
fruits[i][j] + max(
40+
fruits[i - 1][j - 1],
41+
max(fruits[i][j - 1], if (i + 1 < n) fruits[i + 1][j - 1] else 0),
42+
)
43+
)
44+
}
45+
}
46+
return res + fruits[n - 1][n - 2] + fruits[n - 2][n - 1]
47+
}
48+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
3363\. Find the Maximum Number of Fruits Collected
2+
3+
Hard
4+
5+
There is a game dungeon comprised of `n x n` rooms arranged in a grid.
6+
7+
You are given a 2D array `fruits` of size `n x n`, where `fruits[i][j]` represents the number of fruits in the room `(i, j)`. Three children will play in the game dungeon, with **initial** positions at the corner rooms `(0, 0)`, `(0, n - 1)`, and `(n - 1, 0)`.
8+
9+
The children will make **exactly** `n - 1` moves according to the following rules to reach the room `(n - 1, n - 1)`:
10+
11+
* The child starting from `(0, 0)` must move from their current room `(i, j)` to one of the rooms `(i + 1, j + 1)`, `(i + 1, j)`, and `(i, j + 1)` if the target room exists.
12+
* The child starting from `(0, n - 1)` must move from their current room `(i, j)` to one of the rooms `(i + 1, j - 1)`, `(i + 1, j)`, and `(i + 1, j + 1)` if the target room exists.
13+
* The child starting from `(n - 1, 0)` must move from their current room `(i, j)` to one of the rooms `(i - 1, j + 1)`, `(i, j + 1)`, and `(i + 1, j + 1)` if the target room exists.
14+
15+
When a child enters a room, they will collect all the fruits there. If two or more children enter the same room, only one child will collect the fruits, and the room will be emptied after they leave.
16+
17+
Return the **maximum** number of fruits the children can collect from the dungeon.
18+
19+
**Example 1:**
20+
21+
**Input:** fruits = [[1,2,3,4],[5,6,8,7],[9,10,11,12],[13,14,15,16]]
22+
23+
**Output:** 100
24+
25+
**Explanation:**
26+
27+
![](https://assets.leetcode.com/uploads/2024/10/15/example_1.gif)
28+
29+
In this example:
30+
31+
* The 1<sup>st</sup> child (green) moves on the path `(0,0) -> (1,1) -> (2,2) -> (3, 3)`.
32+
* The 2<sup>nd</sup> child (red) moves on the path `(0,3) -> (1,2) -> (2,3) -> (3, 3)`.
33+
* The 3<sup>rd</sup> child (blue) moves on the path `(3,0) -> (3,1) -> (3,2) -> (3, 3)`.
34+
35+
In total they collect `1 + 6 + 11 + 1 + 4 + 8 + 12 + 13 + 14 + 15 = 100` fruits.
36+
37+
**Example 2:**
38+
39+
**Input:** fruits = [[1,1],[1,1]]
40+
41+
**Output:** 4
42+
43+
**Explanation:**
44+
45+
In this example:
46+
47+
* The 1<sup>st</sup> child moves on the path `(0,0) -> (1,1)`.
48+
* The 2<sup>nd</sup> child moves on the path `(0,1) -> (1,1)`.
49+
* The 3<sup>rd</sup> child moves on the path `(1,0) -> (1,1)`.
50+
51+
In total they collect `1 + 1 + 1 + 1 = 4` fruits.
52+
53+
**Constraints:**
54+
55+
* `2 <= n == fruits.length == fruits[i].length <= 1000`
56+
* `0 <= fruits[i][j] <= 1000`
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3301_3400.s3364_minimum_positive_sum_subarray
2+
3+
// #Easy #Array #Prefix_Sum #Sliding_Window #2024_12_03_Time_3_ms_(98.15%)_Space_38.1_MB_(33.33%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun minimumSumSubarray(li: List<Int>, l: Int, r: Int): Int {
9+
val n = li.size
10+
var min = Int.Companion.MAX_VALUE
11+
val a = IntArray(n + 1)
12+
for (i in 1..n) {
13+
a[i] = a[i - 1] + li[i - 1]
14+
}
15+
for (size in l..r) {
16+
for (i in size - 1..<n) {
17+
val sum = a[i + 1] - a[i + 1 - size]
18+
if (sum > 0) {
19+
min = min(min, sum)
20+
}
21+
}
22+
}
23+
return if (min == Int.Companion.MAX_VALUE) {
24+
-1
25+
} else {
26+
min
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)