Skip to content

Commit b590a08

Browse files
authored
Added tasks 3216-3220
1 parent a4fe502 commit b590a08

File tree

14 files changed

+498
-0
lines changed

14 files changed

+498
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3201_3300.s3216_lexicographically_smallest_string_after_a_swap
2+
3+
// #Easy #String #Greedy #2024_07_19_Time_157_ms_(95.16%)_Space_35.4_MB_(88.71%)
4+
5+
class Solution {
6+
fun getSmallestString(s: String): String {
7+
val arr = s.toCharArray()
8+
for (i in 1 until arr.size) {
9+
if (arr[i - 1].code % 2 == arr[i].code % 2 && arr[i - 1] > arr[i]) {
10+
val temp = arr[i]
11+
arr[i] = arr[i - 1]
12+
arr[i - 1] = temp
13+
break
14+
}
15+
}
16+
return String(arr)
17+
}
18+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
3216\. Lexicographically Smallest String After a Swap
2+
3+
Easy
4+
5+
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**.
6+
7+
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.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "45320"
12+
13+
**Output:** "43520"
14+
15+
**Explanation:**
16+
17+
`s[1] == '5'` and `s[2] == '3'` both have the same parity, and swapping them results in the lexicographically smallest string.
18+
19+
**Example 2:**
20+
21+
**Input:** s = "001"
22+
23+
**Output:** "001"
24+
25+
**Explanation:**
26+
27+
There is no need to perform a swap because `s` is already the lexicographically smallest.
28+
29+
**Constraints:**
30+
31+
* `2 <= s.length <= 100`
32+
* `s` consists only of digits.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g3201_3300.s3217_delete_nodes_from_linked_list_present_in_array
2+
3+
// #Medium #Array #Hash_Table #Linked_List #2024_07_19_Time_872_ms_(98.31%)_Space_71.9_MB_(93.22%)
4+
5+
import com_github_leetcode.ListNode
6+
import kotlin.math.max
7+
8+
/*
9+
* Example:
10+
* var li = ListNode(5)
11+
* var v = li.`val`
12+
* Definition for singly-linked list.
13+
* class ListNode(var `val`: Int) {
14+
* var next: ListNode? = null
15+
* }
16+
*/
17+
class Solution {
18+
fun modifiedList(nums: IntArray, head: ListNode?): ListNode? {
19+
var maxv = 0
20+
for (v in nums) {
21+
maxv = max(maxv, v)
22+
}
23+
val rem = BooleanArray(maxv + 1)
24+
for (v in nums) {
25+
rem[v] = true
26+
}
27+
val h = ListNode(0)
28+
var t = h
29+
var p = head
30+
while (p != null) {
31+
if (p.`val` > maxv || !rem[p.`val`]) {
32+
t.next = p
33+
t = p
34+
}
35+
p = p.next
36+
}
37+
t.next = null
38+
return h.next
39+
}
40+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
3217\. Delete Nodes From Linked List Present in Array
2+
3+
Medium
4+
5+
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`.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,2,3], head = [1,2,3,4,5]
10+
11+
**Output:** [4,5]
12+
13+
**Explanation:**
14+
15+
**![](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample0.png)**
16+
17+
Remove the nodes with values 1, 2, and 3.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [1], head = [1,2,1,2,1,2]
22+
23+
**Output:** [2,2,2]
24+
25+
**Explanation:**
26+
27+
![](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample1.png)
28+
29+
Remove the nodes with value 1.
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [5], head = [1,2,3,4]
34+
35+
**Output:** [1,2,3,4]
36+
37+
**Explanation:**
38+
39+
**![](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample2.png)**
40+
41+
No node has value 5.
42+
43+
**Constraints:**
44+
45+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
46+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
47+
* All elements in `nums` are unique.
48+
* The number of nodes in the given list is in the range <code>[1, 10<sup>5</sup>]</code>.
49+
* <code>1 <= Node.val <= 10<sup>5</sup></code>
50+
* The input is generated such that there is at least one node in the linked list that has a value not present in `nums`.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3201_3300.s3218_minimum_cost_for_cutting_cake_i
2+
3+
// #Medium #Array #Dynamic_Programming #Sorting #Greedy
4+
// #2024_07_19_Time_175_ms_(78.05%)_Space_35.1_MB_(100.00%)
5+
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun minimumCost(m: Int, n: Int, horizontalCut: IntArray, verticalCut: IntArray): Int {
10+
var sum = 0
11+
for (hc in horizontalCut) {
12+
sum += hc
13+
}
14+
for (vc in verticalCut) {
15+
sum += vc
16+
}
17+
for (hc in horizontalCut) {
18+
for (vc in verticalCut) {
19+
sum += min(hc, vc)
20+
}
21+
}
22+
return sum
23+
}
24+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
3218\. Minimum Cost for Cutting Cake I
2+
3+
Medium
4+
5+
There is an `m x n` cake that needs to be cut into `1 x 1` pieces.
6+
7+
You are given integers `m`, `n`, and two arrays:
8+
9+
* `horizontalCut` of size `m - 1`, where `horizontalCut[i]` represents the cost to cut along the horizontal line `i`.
10+
* `verticalCut` of size `n - 1`, where `verticalCut[j]` represents the cost to cut along the vertical line `j`.
11+
12+
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:
13+
14+
1. Cut along a horizontal line `i` at a cost of `horizontalCut[i]`.
15+
2. Cut along a vertical line `j` at a cost of `verticalCut[j]`.
16+
17+
After the cut, the piece of cake is divided into two distinct pieces.
18+
19+
The cost of a cut depends only on the initial cost of the line and does not change.
20+
21+
Return the **minimum** total cost to cut the entire cake into `1 x 1` pieces.
22+
23+
**Example 1:**
24+
25+
**Input:** m = 3, n = 2, horizontalCut = [1,3], verticalCut = [5]
26+
27+
**Output:** 13
28+
29+
**Explanation:**
30+
31+
![](https://assets.leetcode.com/uploads/2024/06/04/ezgifcom-animated-gif-maker-1.gif)
32+
33+
* Perform a cut on the vertical line 0 with cost 5, current total cost is 5.
34+
* Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.
35+
* Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.
36+
* Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.
37+
* Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.
38+
39+
The total cost is `5 + 1 + 1 + 3 + 3 = 13`.
40+
41+
**Example 2:**
42+
43+
**Input:** m = 2, n = 2, horizontalCut = [7], verticalCut = [4]
44+
45+
**Output:** 15
46+
47+
**Explanation:**
48+
49+
* Perform a cut on the horizontal line 0 with cost 7.
50+
* Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.
51+
* Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.
52+
53+
The total cost is `7 + 4 + 4 = 15`.
54+
55+
**Constraints:**
56+
57+
* `1 <= m, n <= 20`
58+
* `horizontalCut.length == m - 1`
59+
* `verticalCut.length == n - 1`
60+
* <code>1 <= horizontalCut[i], verticalCut[i] <= 10<sup>3</sup></code>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g3201_3300.s3219_minimum_cost_for_cutting_cake_ii
2+
3+
// #Hard #Array #Sorting #Greedy #2024_07_19_Time_776_ms_(100.00%)_Space_66.8_MB_(96.88%)
4+
5+
class Solution {
6+
fun minimumCost(m: Int, n: Int, horizontalCut: IntArray, verticalCut: IntArray): Long {
7+
val horizontalCounts = IntArray(N)
8+
val verticalCounts = IntArray(N)
9+
var max = 0
10+
for (x in horizontalCut) {
11+
if (x > max) {
12+
max = x
13+
}
14+
horizontalCounts[x]++
15+
}
16+
for (x in verticalCut) {
17+
if (x > max) {
18+
max = x
19+
}
20+
verticalCounts[x]++
21+
}
22+
var ans: Long = 0
23+
var horizontalCount = 1
24+
var verticalCount = 1
25+
for (x in max downTo 1) {
26+
ans += horizontalCounts[x].toLong() * x * horizontalCount
27+
verticalCount += horizontalCounts[x]
28+
horizontalCounts[x] = 0
29+
ans += verticalCounts[x].toLong() * x * verticalCount
30+
horizontalCount += verticalCounts[x]
31+
verticalCounts[x] = 0
32+
}
33+
return ans
34+
}
35+
36+
companion object {
37+
private const val N = 1001
38+
}
39+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
3219\. Minimum Cost for Cutting Cake II
2+
3+
Hard
4+
5+
There is an `m x n` cake that needs to be cut into `1 x 1` pieces.
6+
7+
You are given integers `m`, `n`, and two arrays:
8+
9+
* `horizontalCut` of size `m - 1`, where `horizontalCut[i]` represents the cost to cut along the horizontal line `i`.
10+
* `verticalCut` of size `n - 1`, where `verticalCut[j]` represents the cost to cut along the vertical line `j`.
11+
12+
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:
13+
14+
1. Cut along a horizontal line `i` at a cost of `horizontalCut[i]`.
15+
2. Cut along a vertical line `j` at a cost of `verticalCut[j]`.
16+
17+
After the cut, the piece of cake is divided into two distinct pieces.
18+
19+
The cost of a cut depends only on the initial cost of the line and does not change.
20+
21+
Return the **minimum** total cost to cut the entire cake into `1 x 1` pieces.
22+
23+
**Example 1:**
24+
25+
**Input:** m = 3, n = 2, horizontalCut = [1,3], verticalCut = [5]
26+
27+
**Output:** 13
28+
29+
**Explanation:**
30+
31+
![](https://assets.leetcode.com/uploads/2024/06/04/ezgifcom-animated-gif-maker-1.gif)
32+
33+
* Perform a cut on the vertical line 0 with cost 5, current total cost is 5.
34+
* Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.
35+
* Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.
36+
* Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.
37+
* Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.
38+
39+
The total cost is `5 + 1 + 1 + 3 + 3 = 13`.
40+
41+
**Example 2:**
42+
43+
**Input:** m = 2, n = 2, horizontalCut = [7], verticalCut = [4]
44+
45+
**Output:** 15
46+
47+
**Explanation:**
48+
49+
* Perform a cut on the horizontal line 0 with cost 7.
50+
* Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.
51+
* Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.
52+
53+
The total cost is `7 + 4 + 4 = 15`.
54+
55+
**Constraints:**
56+
57+
* <code>1 <= m, n <= 10<sup>5</sup></code>
58+
* `horizontalCut.length == m - 1`
59+
* `verticalCut.length == n - 1`
60+
* <code>1 <= horizontalCut[i], verticalCut[i] <= 10<sup>3</sup></code>

0 commit comments

Comments
 (0)