Skip to content

Commit 46ba3a9

Browse files
authored
Added tasks 3216-3229
1 parent 5d4c922 commit 46ba3a9

File tree

14 files changed

+1015
-0
lines changed
  • src/main/kotlin/g3201_3300
    • s3216_lexicographically_smallest_string_after_a_swap
    • s3217_delete_nodes_from_linked_list_present_in_array
    • s3218_minimum_cost_for_cutting_cake_i
    • s3219_minimum_cost_for_cutting_cake_ii
    • s3220_odd_and_even_transactions
    • s3222_find_the_winning_player_in_coin_game
    • s3223_minimum_length_of_string_after_operations
    • s3224_minimum_array_changes_to_make_differences_equal
    • s3225_maximum_score_from_grid_operations
    • s3226_number_of_bit_changes_to_make_two_integers_equal
    • s3227_vowels_game_in_a_string
    • s3228_maximum_number_of_operations_to_move_ones_to_the_end
    • s3229_minimum_operations_to_make_array_equal_to_target

14 files changed

+1015
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,19 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3229 |[Minimum Operations to Make Array Equal to Target](src/main/kotlin/g3201_3300/s3229_minimum_operations_to_make_array_equal_to_target)| Hard | Array, Dynamic_Programming, Greedy, Stack, Monotonic_Stack | 636 | 57.14
1820+
| 3228 |[Maximum Number of Operations to Move Ones to the End](src/main/kotlin/g3201_3300/s3228_maximum_number_of_operations_to_move_ones_to_the_end)| Medium | String, Greedy, Counting | 232 | 44.00
1821+
| 3227 |[Vowels Game in a String](src/main/kotlin/g3201_3300/s3227_vowels_game_in_a_string)| Medium | String, Math, Game_Theory, Brainteaser | 234 | 90.32
1822+
| 3226 |[Number of Bit Changes to Make Two Integers Equal](src/main/kotlin/g3201_3300/s3226_number_of_bit_changes_to_make_two_integers_equal)| Easy | Bit_Manipulation | 136 | 61.90
1823+
| 3225 |[Maximum Score From Grid Operations](src/main/kotlin/g3201_3300/s3225_maximum_score_from_grid_operations)| Hard | Array, Dynamic_Programming, Matrix, Prefix_Sum | 371 | 100.00
1824+
| 3224 |[Minimum Array Changes to Make Differences Equal](src/main/kotlin/g3201_3300/s3224_minimum_array_changes_to_make_differences_equal)| Medium | Array, Hash_Table, Prefix_Sum | 665 | 84.62
1825+
| 3223 |[Minimum Length of String After Operations](src/main/kotlin/g3201_3300/s3223_minimum_length_of_string_after_operations)| Medium | String, Hash_Table, Counting | 316 | 60.00
1826+
| 3222 |[Find the Winning Player in Coin Game](src/main/kotlin/g3201_3300/s3222_find_the_winning_player_in_coin_game)| Easy | Math, Simulation, Game_Theory | 140 | 86.44
1827+
| 3220 |[Odd and Even Transactions](src/main/kotlin/g3201_3300/s3220_odd_and_even_transactions)| Medium | Database | 248 | 85.85
1828+
| 3219 |[Minimum Cost for Cutting Cake II](src/main/kotlin/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii)| Hard | Array, Sorting, Greedy | 776 | 100.00
1829+
| 3218 |[Minimum Cost for Cutting Cake I](src/main/kotlin/g3201_3300/s3218_minimum_cost_for_cutting_cake_i)| Medium | Array, Dynamic_Programming, Sorting, Greedy | 175 | 78.05
1830+
| 3217 |[Delete Nodes From Linked List Present in Array](src/main/kotlin/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array)| Medium | Array, Hash_Table, Linked_List | 872 | 98.31
1831+
| 3216 |[Lexicographically Smallest String After a Swap](src/main/kotlin/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap)| Easy | String, Greedy | 157 | 95.16
18191832
| 3213 |[Construct String with Minimum Cost](src/main/kotlin/g3201_3300/s3213_construct_string_with_minimum_cost)| Hard | Array, String, Dynamic_Programming, Suffix_Array | 1176 | 46.67
18201833
| 3212 |[Count Submatrices With Equal Frequency of X and Y](src/main/kotlin/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y)| Medium | Array, Matrix, Prefix_Sum | 1009 | 78.95
18211834
| 3211 |[Generate Binary Strings Without Adjacent Zeros](src/main/kotlin/g3201_3300/s3211_generate_binary_strings_without_adjacent_zeros)| Medium | String, Bit_Manipulation, Recursion | 237 | 38.18
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
## 3216\. Lexicographically Smallest String After a Swap
5+
6+
Easy
7+
8+
Given a string `s` containing only digits, return the lexicographically smallest string that can be obtained after swapping **adjacent** digits in `s` with the same **parity** at most **once**.
9+
10+
Digits have the same parity if both are odd or both are even. For example, 5 and 9, as well as 2 and 4, have the same parity, while 6 and 9 do not.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "45320"
15+
16+
**Output:** "43520"
17+
18+
**Explanation:**
19+
20+
`s[1] == '5'` and `s[2] == '3'` both have the same parity, and swapping them results in the lexicographically smallest string.
21+
22+
**Example 2:**
23+
24+
**Input:** s = "001"
25+
26+
**Output:** "001"
27+
28+
**Explanation:**
29+
30+
There is no need to perform a swap because `s` is already the lexicographically smallest.
31+
32+
**Constraints:**
33+
34+
* `2 <= s.length <= 100`
35+
* `s` consists only of digits.
36+
37+
## Solution
38+
39+
```kotlin
40+
class Solution {
41+
fun getSmallestString(s: String): String {
42+
val arr = s.toCharArray()
43+
for (i in 1 until arr.size) {
44+
if (arr[i - 1].code % 2 == arr[i].code % 2 && arr[i - 1] > arr[i]) {
45+
val temp = arr[i]
46+
arr[i] = arr[i - 1]
47+
arr[i - 1] = temp
48+
break
49+
}
50+
}
51+
return String(arr)
52+
}
53+
}
54+
```
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
## 3217\. Delete Nodes From Linked List Present in Array
5+
6+
Medium
7+
8+
You are given an array of integers `nums` and the `head` of a linked list. Return the `head` of the modified linked list after **removing** all nodes from the linked list that have a value that exists in `nums`.
9+
10+
**Example 1:**
11+
12+
**Input:** nums = [1,2,3], head = [1,2,3,4,5]
13+
14+
**Output:** [4,5]
15+
16+
**Explanation:**
17+
18+
**![](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample0.png)**
19+
20+
Remove the nodes with values 1, 2, and 3.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [1], head = [1,2,1,2,1,2]
25+
26+
**Output:** [2,2,2]
27+
28+
**Explanation:**
29+
30+
![](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample1.png)
31+
32+
Remove the nodes with value 1.
33+
34+
**Example 3:**
35+
36+
**Input:** nums = [5], head = [1,2,3,4]
37+
38+
**Output:** [1,2,3,4]
39+
40+
**Explanation:**
41+
42+
**![](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample2.png)**
43+
44+
No node has value 5.
45+
46+
**Constraints:**
47+
48+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
49+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
50+
* All elements in `nums` are unique.
51+
* The number of nodes in the given list is in the range <code>[1, 10<sup>5</sup>]</code>.
52+
* <code>1 <= Node.val <= 10<sup>5</sup></code>
53+
* The input is generated such that there is at least one node in the linked list that has a value not present in `nums`.
54+
55+
## Solution
56+
57+
```kotlin
58+
import com_github_leetcode.ListNode
59+
import kotlin.math.max
60+
61+
/*
62+
* Example:
63+
* var li = ListNode(5)
64+
* var v = li.`val`
65+
* Definition for singly-linked list.
66+
* class ListNode(var `val`: Int) {
67+
* var next: ListNode? = null
68+
* }
69+
*/
70+
class Solution {
71+
fun modifiedList(nums: IntArray, head: ListNode?): ListNode? {
72+
var maxv = 0
73+
for (v in nums) {
74+
maxv = max(maxv, v)
75+
}
76+
val rem = BooleanArray(maxv + 1)
77+
for (v in nums) {
78+
rem[v] = true
79+
}
80+
val h = ListNode(0)
81+
var t = h
82+
var p = head
83+
while (p != null) {
84+
if (p.`val` > maxv || !rem[p.`val`]) {
85+
t.next = p
86+
t = p
87+
}
88+
p = p.next
89+
}
90+
t.next = null
91+
return h.next
92+
}
93+
}
94+
```
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
## 3218\. Minimum Cost for Cutting Cake I
5+
6+
Medium
7+
8+
There is an `m x n` cake that needs to be cut into `1 x 1` pieces.
9+
10+
You are given integers `m`, `n`, and two arrays:
11+
12+
* `horizontalCut` of size `m - 1`, where `horizontalCut[i]` represents the cost to cut along the horizontal line `i`.
13+
* `verticalCut` of size `n - 1`, where `verticalCut[j]` represents the cost to cut along the vertical line `j`.
14+
15+
In one operation, you can choose any piece of cake that is not yet a `1 x 1` square and perform one of the following cuts:
16+
17+
1. Cut along a horizontal line `i` at a cost of `horizontalCut[i]`.
18+
2. Cut along a vertical line `j` at a cost of `verticalCut[j]`.
19+
20+
After the cut, the piece of cake is divided into two distinct pieces.
21+
22+
The cost of a cut depends only on the initial cost of the line and does not change.
23+
24+
Return the **minimum** total cost to cut the entire cake into `1 x 1` pieces.
25+
26+
**Example 1:**
27+
28+
**Input:** m = 3, n = 2, horizontalCut = [1,3], verticalCut = [5]
29+
30+
**Output:** 13
31+
32+
**Explanation:**
33+
34+
![](https://assets.leetcode.com/uploads/2024/06/04/ezgifcom-animated-gif-maker-1.gif)
35+
36+
* Perform a cut on the vertical line 0 with cost 5, current total cost is 5.
37+
* Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.
38+
* Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.
39+
* Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.
40+
* Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.
41+
42+
The total cost is `5 + 1 + 1 + 3 + 3 = 13`.
43+
44+
**Example 2:**
45+
46+
**Input:** m = 2, n = 2, horizontalCut = [7], verticalCut = [4]
47+
48+
**Output:** 15
49+
50+
**Explanation:**
51+
52+
* Perform a cut on the horizontal line 0 with cost 7.
53+
* Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.
54+
* Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.
55+
56+
The total cost is `7 + 4 + 4 = 15`.
57+
58+
**Constraints:**
59+
60+
* `1 <= m, n <= 20`
61+
* `horizontalCut.length == m - 1`
62+
* `verticalCut.length == n - 1`
63+
* <code>1 <= horizontalCut[i], verticalCut[i] <= 10<sup>3</sup></code>
64+
65+
## Solution
66+
67+
```kotlin
68+
import kotlin.math.min
69+
70+
class Solution {
71+
fun minimumCost(m: Int, n: Int, horizontalCut: IntArray, verticalCut: IntArray): Int {
72+
var sum = 0
73+
for (hc in horizontalCut) {
74+
sum += hc
75+
}
76+
for (vc in verticalCut) {
77+
sum += vc
78+
}
79+
for (hc in horizontalCut) {
80+
for (vc in verticalCut) {
81+
sum += min(hc, vc)
82+
}
83+
}
84+
return sum
85+
}
86+
}
87+
```
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
## 3219\. Minimum Cost for Cutting Cake II
5+
6+
Hard
7+
8+
There is an `m x n` cake that needs to be cut into `1 x 1` pieces.
9+
10+
You are given integers `m`, `n`, and two arrays:
11+
12+
* `horizontalCut` of size `m - 1`, where `horizontalCut[i]` represents the cost to cut along the horizontal line `i`.
13+
* `verticalCut` of size `n - 1`, where `verticalCut[j]` represents the cost to cut along the vertical line `j`.
14+
15+
In one operation, you can choose any piece of cake that is not yet a `1 x 1` square and perform one of the following cuts:
16+
17+
1. Cut along a horizontal line `i` at a cost of `horizontalCut[i]`.
18+
2. Cut along a vertical line `j` at a cost of `verticalCut[j]`.
19+
20+
After the cut, the piece of cake is divided into two distinct pieces.
21+
22+
The cost of a cut depends only on the initial cost of the line and does not change.
23+
24+
Return the **minimum** total cost to cut the entire cake into `1 x 1` pieces.
25+
26+
**Example 1:**
27+
28+
**Input:** m = 3, n = 2, horizontalCut = [1,3], verticalCut = [5]
29+
30+
**Output:** 13
31+
32+
**Explanation:**
33+
34+
![](https://assets.leetcode.com/uploads/2024/06/04/ezgifcom-animated-gif-maker-1.gif)
35+
36+
* Perform a cut on the vertical line 0 with cost 5, current total cost is 5.
37+
* Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.
38+
* Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.
39+
* Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.
40+
* Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.
41+
42+
The total cost is `5 + 1 + 1 + 3 + 3 = 13`.
43+
44+
**Example 2:**
45+
46+
**Input:** m = 2, n = 2, horizontalCut = [7], verticalCut = [4]
47+
48+
**Output:** 15
49+
50+
**Explanation:**
51+
52+
* Perform a cut on the horizontal line 0 with cost 7.
53+
* Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.
54+
* Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.
55+
56+
The total cost is `7 + 4 + 4 = 15`.
57+
58+
**Constraints:**
59+
60+
* <code>1 <= m, n <= 10<sup>5</sup></code>
61+
* `horizontalCut.length == m - 1`
62+
* `verticalCut.length == n - 1`
63+
* <code>1 <= horizontalCut[i], verticalCut[i] <= 10<sup>3</sup></code>
64+
65+
## Solution
66+
67+
```kotlin
68+
class Solution {
69+
fun minimumCost(m: Int, n: Int, horizontalCut: IntArray, verticalCut: IntArray): Long {
70+
val horizontalCounts = IntArray(N)
71+
val verticalCounts = IntArray(N)
72+
var max = 0
73+
for (x in horizontalCut) {
74+
if (x > max) {
75+
max = x
76+
}
77+
horizontalCounts[x]++
78+
}
79+
for (x in verticalCut) {
80+
if (x > max) {
81+
max = x
82+
}
83+
verticalCounts[x]++
84+
}
85+
var ans: Long = 0
86+
var horizontalCount = 1
87+
var verticalCount = 1
88+
for (x in max downTo 1) {
89+
ans += horizontalCounts[x].toLong() * x * horizontalCount
90+
verticalCount += horizontalCounts[x]
91+
horizontalCounts[x] = 0
92+
ans += verticalCounts[x].toLong() * x * verticalCount
93+
horizontalCount += verticalCounts[x]
94+
verticalCounts[x] = 0
95+
}
96+
return ans
97+
}
98+
99+
companion object {
100+
private const val N = 1001
101+
}
102+
}
103+
```

0 commit comments

Comments
 (0)