Skip to content

Commit e63949d

Browse files
authored
Added tasks 3360-3373
1 parent ca1159e commit e63949d

File tree

14 files changed

+1350
-161
lines changed
  • src/main/kotlin
    • g0201_0300/s0212_word_search_ii
    • g3301_3400
      • s3360_stone_removal_game
      • s3361_shift_distance_between_two_strings
      • s3362_zero_array_transformation_iii
      • s3363_find_the_maximum_number_of_fruits_collected
      • s3364_minimum_positive_sum_subarray
      • s3365_rearrange_k_substrings_to_form_target_string
      • s3366_minimum_array_sum
      • s3367_maximize_sum_of_weights_after_edge_removals
      • s3370_smallest_number_with_all_set_bits
      • s3371_identify_the_largest_outlier_in_an_array
      • s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i
      • s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii

14 files changed

+1350
-161
lines changed

README.md

Lines changed: 170 additions & 158 deletions
Large diffs are not rendered by default.

src/main/kotlin/g0201_0300/s0212_word_search_ii/readme.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ Each word must be constructed from letters of sequentially adjacent cells, where
4242
@Suppress("NAME_SHADOWING")
4343
class Solution {
4444
private var root: Tree? = null
45-
fun findWords(board: Array<CharArray>, words: Array<String?>): List<String> {
46-
if (board.size < 1 || board[0].size < 1) {
45+
46+
fun findWords(board: Array<CharArray>, words: Array<String>): List<String> {
47+
if (board.isEmpty() || board[0].isEmpty()) {
4748
return emptyList()
4849
}
4950
root = Tree()
5051
for (word in words) {
51-
Tree.addWord(root, word!!)
52+
Tree.addWord(root, word)
5253
}
5354
val collected: MutableList<String> = ArrayList()
5455
for (i in board.indices) {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
## 3360\. Stone Removal Game
5+
6+
Easy
7+
8+
Alice and Bob are playing a game where they take turns removing stones from a pile, with _Alice going first_.
9+
10+
* Alice starts by removing **exactly** 10 stones on her first turn.
11+
* For each subsequent turn, each player removes **exactly** 1 fewer stone than the previous opponent.
12+
13+
The player who cannot make a move loses the game.
14+
15+
Given a positive integer `n`, return `true` if Alice wins the game and `false` otherwise.
16+
17+
**Example 1:**
18+
19+
**Input:** n = 12
20+
21+
**Output:** true
22+
23+
**Explanation:**
24+
25+
* Alice removes 10 stones on her first turn, leaving 2 stones for Bob.
26+
* Bob cannot remove 9 stones, so Alice wins.
27+
28+
**Example 2:**
29+
30+
**Input:** n = 1
31+
32+
**Output:** false
33+
34+
**Explanation:**
35+
36+
* Alice cannot remove 10 stones, so Alice loses.
37+
38+
**Constraints:**
39+
40+
* `1 <= n <= 50`
41+
42+
## Solution
43+
44+
```kotlin
45+
class Solution {
46+
fun canAliceWin(n: Int): Boolean {
47+
if (n < 10) {
48+
return false
49+
}
50+
var stonesRemaining = n - 10
51+
var stonesToBeRemoved = 9
52+
var i = 1
53+
while (stonesRemaining >= stonesToBeRemoved) {
54+
stonesRemaining -= stonesToBeRemoved
55+
i++
56+
stonesToBeRemoved--
57+
}
58+
return i % 2 != 0
59+
}
60+
}
61+
```
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
## 3361\. Shift Distance Between Two Strings
5+
6+
Medium
7+
8+
You are given two strings `s` and `t` of the same length, and two integer arrays `nextCost` and `previousCost`.
9+
10+
In one operation, you can pick any index `i` of `s`, and perform **either one** of the following actions:
11+
12+
* 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.
13+
* 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.
14+
15+
The **shift distance** is the **minimum** total cost of operations required to transform `s` into `t`.
16+
17+
Return the **shift distance** from `s` to `t`.
18+
19+
**Example 1:**
20+
21+
**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]
22+
23+
**Output:** 2
24+
25+
**Explanation:**
26+
27+
* We choose index `i = 0` and shift `s[0]` 25 times to the previous character for a total cost of 1.
28+
* We choose index `i = 1` and shift `s[1]` 25 times to the next character for a total cost of 0.
29+
* We choose index `i = 2` and shift `s[2]` 25 times to the previous character for a total cost of 1.
30+
* We choose index `i = 3` and shift `s[3]` 25 times to the next character for a total cost of 0.
31+
32+
**Example 2:**
33+
34+
**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]
35+
36+
**Output:** 31
37+
38+
**Explanation:**
39+
40+
* We choose index `i = 0` and shift `s[0]` 9 times to the previous character for a total cost of 9.
41+
* We choose index `i = 1` and shift `s[1]` 10 times to the next character for a total cost of 10.
42+
* We choose index `i = 2` and shift `s[2]` 1 time to the previous character for a total cost of 1.
43+
* We choose index `i = 3` and shift `s[3]` 11 times to the next character for a total cost of 11.
44+
45+
**Constraints:**
46+
47+
* <code>1 <= s.length == t.length <= 10<sup>5</sup></code>
48+
* `s` and `t` consist only of lowercase English letters.
49+
* `nextCost.length == previousCost.length == 26`
50+
* <code>0 <= nextCost[i], previousCost[i] <= 10<sup>9</sup></code>
51+
52+
## Solution
53+
54+
```kotlin
55+
import kotlin.math.min
56+
57+
class Solution {
58+
fun shiftDistance(s: String, t: String, nextCost: IntArray, previousCost: IntArray): Long {
59+
val costs = Array<LongArray?>(26) { LongArray(26) }
60+
var cost: Long
61+
for (i in 0..25) {
62+
cost = nextCost[i].toLong()
63+
var j = if (i == 25) 0 else i + 1
64+
while (j != i) {
65+
costs[i]!![j] = cost
66+
cost += nextCost[j].toLong()
67+
if (j == 25) {
68+
j = -1
69+
}
70+
j++
71+
}
72+
}
73+
for (i in 0..25) {
74+
cost = previousCost[i].toLong()
75+
var j = if (i == 0) 25 else i - 1
76+
while (j != i) {
77+
costs[i]!![j] = min(costs[i]!![j].toDouble(), cost.toDouble()).toLong()
78+
cost += previousCost[j].toLong()
79+
if (j == 0) {
80+
j = 26
81+
}
82+
j--
83+
}
84+
}
85+
val n = s.length
86+
var ans: Long = 0
87+
for (i in 0..<n) {
88+
ans += costs[s[i].code - 'a'.code]!![t[i].code - 'a'.code]
89+
}
90+
return ans
91+
}
92+
}
93+
```
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
## 3362\. Zero Array Transformation III
5+
6+
Medium
7+
8+
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>.
9+
10+
Each `queries[i]` represents the following action on `nums`:
11+
12+
* 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.
13+
* The amount by which the value is decremented can be chosen **independently** for each index.
14+
15+
A **Zero Array** is an array with all its elements equal to 0.
16+
17+
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.
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [2,0,2], queries = \[\[0,2],[0,2],[1,1]]
22+
23+
**Output:** 1
24+
25+
**Explanation:**
26+
27+
After removing `queries[2]`, `nums` can still be converted to a zero array.
28+
29+
* Using `queries[0]`, decrement `nums[0]` and `nums[2]` by 1 and `nums[1]` by 0.
30+
* Using `queries[1]`, decrement `nums[0]` and `nums[2]` by 1 and `nums[1]` by 0.
31+
32+
**Example 2:**
33+
34+
**Input:** nums = [1,1,1,1], queries = \[\[1,3],[0,2],[1,3],[1,2]]
35+
36+
**Output:** 2
37+
38+
**Explanation:**
39+
40+
We can remove `queries[2]` and `queries[3]`.
41+
42+
**Example 3:**
43+
44+
**Input:** nums = [1,2,3,4], queries = \[\[0,3]]
45+
46+
**Output:** \-1
47+
48+
**Explanation:**
49+
50+
`nums` cannot be converted to a zero array even after using all the queries.
51+
52+
**Constraints:**
53+
54+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
55+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
56+
* <code>1 <= queries.length <= 10<sup>5</sup></code>
57+
* `queries[i].length == 2`
58+
* <code>0 <= l<sub>i</sub> <= r<sub>i</sub> < nums.length</code>
59+
60+
## Solution
61+
62+
```kotlin
63+
import java.util.PriorityQueue
64+
65+
class Solution {
66+
fun maxRemoval(nums: IntArray, queries: Array<IntArray>): Int {
67+
queries.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] }
68+
val last = PriorityQueue<Int?>(Comparator { a: Int?, b: Int? -> b!! - a!! })
69+
val diffs = IntArray(nums.size + 1)
70+
var idx = 0
71+
var cur = 0
72+
for (i in nums.indices) {
73+
while (idx < queries.size && queries[idx][0] == i) {
74+
last.add(queries[idx][1])
75+
idx++
76+
}
77+
cur += diffs[i]
78+
while (cur < nums[i] && last.isNotEmpty() && last.peek()!! >= i) {
79+
cur++
80+
diffs[last.poll()!! + 1]--
81+
}
82+
if (cur < nums[i]) {
83+
return -1
84+
}
85+
}
86+
return last.size
87+
}
88+
}
89+
```
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
## 3363\. Find the Maximum Number of Fruits Collected
5+
6+
Hard
7+
8+
There is a game dungeon comprised of `n x n` rooms arranged in a grid.
9+
10+
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)`.
11+
12+
The children will make **exactly** `n - 1` moves according to the following rules to reach the room `(n - 1, n - 1)`:
13+
14+
* 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.
15+
* 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.
16+
* 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.
17+
18+
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.
19+
20+
Return the **maximum** number of fruits the children can collect from the dungeon.
21+
22+
**Example 1:**
23+
24+
**Input:** fruits = \[\[1,2,3,4],[5,6,8,7],[9,10,11,12],[13,14,15,16]]
25+
26+
**Output:** 100
27+
28+
**Explanation:**
29+
30+
![](https://assets.leetcode.com/uploads/2024/10/15/example_1.gif)
31+
32+
In this example:
33+
34+
* The 1<sup>st</sup> child (green) moves on the path `(0,0) -> (1,1) -> (2,2) -> (3, 3)`.
35+
* The 2<sup>nd</sup> child (red) moves on the path `(0,3) -> (1,2) -> (2,3) -> (3, 3)`.
36+
* The 3<sup>rd</sup> child (blue) moves on the path `(3,0) -> (3,1) -> (3,2) -> (3, 3)`.
37+
38+
In total they collect `1 + 6 + 11 + 1 + 4 + 8 + 12 + 13 + 14 + 15 = 100` fruits.
39+
40+
**Example 2:**
41+
42+
**Input:** fruits = \[\[1,1],[1,1]]
43+
44+
**Output:** 4
45+
46+
**Explanation:**
47+
48+
In this example:
49+
50+
* The 1<sup>st</sup> child moves on the path `(0,0) -> (1,1)`.
51+
* The 2<sup>nd</sup> child moves on the path `(0,1) -> (1,1)`.
52+
* The 3<sup>rd</sup> child moves on the path `(1,0) -> (1,1)`.
53+
54+
In total they collect `1 + 1 + 1 + 1 = 4` fruits.
55+
56+
**Constraints:**
57+
58+
* `2 <= n == fruits.length == fruits[i].length <= 1000`
59+
* `0 <= fruits[i][j] <= 1000`
60+
61+
## Solution
62+
63+
```kotlin
64+
import kotlin.math.max
65+
66+
class Solution {
67+
fun maxCollectedFruits(fruits: Array<IntArray>): Int {
68+
val n = fruits.size
69+
// Set inaccessible cells to 0
70+
for (i in 0..<n) {
71+
for (j in 0..<n) {
72+
if (i < j && j < n - 1 - i) {
73+
fruits[i][j] = 0
74+
}
75+
if (j < i && i < n - 1 - j) {
76+
fruits[i][j] = 0
77+
}
78+
}
79+
}
80+
var res = 0
81+
for (i in 0..<n) {
82+
res += fruits[i][i]
83+
}
84+
for (i in 1..<n) {
85+
for (j in i + 1..<n) {
86+
fruits[i][j] = (
87+
fruits[i][j] + max(
88+
fruits[i - 1][j - 1],
89+
max(fruits[i - 1][j], if (j + 1 < n) fruits[i - 1][j + 1] else 0),
90+
)
91+
)
92+
}
93+
}
94+
for (j in 1..<n) {
95+
for (i in j + 1..<n) {
96+
fruits[i][j] = (
97+
fruits[i][j] + max(
98+
fruits[i - 1][j - 1],
99+
max(fruits[i][j - 1], if (i + 1 < n) fruits[i + 1][j - 1] else 0),
100+
)
101+
)
102+
}
103+
}
104+
return res + fruits[n - 1][n - 2] + fruits[n - 2][n - 1]
105+
}
106+
}
107+
```

0 commit comments

Comments
 (0)