Skip to content

Added tasks 3216-3220 #670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g3201_3300.s3216_lexicographically_smallest_string_after_a_swap

// #Easy #String #Greedy #2024_07_19_Time_157_ms_(95.16%)_Space_35.4_MB_(88.71%)

class Solution {
fun getSmallestString(s: String): String {
val arr = s.toCharArray()
for (i in 1 until arr.size) {
if (arr[i - 1].code % 2 == arr[i].code % 2 && arr[i - 1] > arr[i]) {
val temp = arr[i]
arr[i] = arr[i - 1]
arr[i - 1] = temp
break
}
}
return String(arr)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
3216\. Lexicographically Smallest String After a Swap

Easy

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**.

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.

**Example 1:**

**Input:** s = "45320"

**Output:** "43520"

**Explanation:**

`s[1] == '5'` and `s[2] == '3'` both have the same parity, and swapping them results in the lexicographically smallest string.

**Example 2:**

**Input:** s = "001"

**Output:** "001"

**Explanation:**

There is no need to perform a swap because `s` is already the lexicographically smallest.

**Constraints:**

* `2 <= s.length <= 100`
* `s` consists only of digits.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package g3201_3300.s3217_delete_nodes_from_linked_list_present_in_array

// #Medium #Array #Hash_Table #Linked_List #2024_07_19_Time_872_ms_(98.31%)_Space_71.9_MB_(93.22%)

import com_github_leetcode.ListNode
import kotlin.math.max

/*
* Example:
* var li = ListNode(5)
* var v = li.`val`
* Definition for singly-linked list.
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/
class Solution {
fun modifiedList(nums: IntArray, head: ListNode?): ListNode? {
var maxv = 0
for (v in nums) {
maxv = max(maxv, v)
}
val rem = BooleanArray(maxv + 1)
for (v in nums) {
rem[v] = true
}
val h = ListNode(0)
var t = h
var p = head
while (p != null) {
if (p.`val` > maxv || !rem[p.`val`]) {
t.next = p
t = p
}
p = p.next
}
t.next = null
return h.next
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
3217\. Delete Nodes From Linked List Present in Array

Medium

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`.

**Example 1:**

**Input:** nums = [1,2,3], head = [1,2,3,4,5]

**Output:** [4,5]

**Explanation:**

**![](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample0.png)**

Remove the nodes with values 1, 2, and 3.

**Example 2:**

**Input:** nums = [1], head = [1,2,1,2,1,2]

**Output:** [2,2,2]

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample1.png)

Remove the nodes with value 1.

**Example 3:**

**Input:** nums = [5], head = [1,2,3,4]

**Output:** [1,2,3,4]

**Explanation:**

**![](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample2.png)**

No node has value 5.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
* All elements in `nums` are unique.
* The number of nodes in the given list is in the range <code>[1, 10<sup>5</sup>]</code>.
* <code>1 <= Node.val <= 10<sup>5</sup></code>
* The input is generated such that there is at least one node in the linked list that has a value not present in `nums`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package g3201_3300.s3218_minimum_cost_for_cutting_cake_i

// #Medium #Array #Dynamic_Programming #Sorting #Greedy
// #2024_07_19_Time_175_ms_(78.05%)_Space_35.1_MB_(100.00%)

import kotlin.math.min

class Solution {
fun minimumCost(m: Int, n: Int, horizontalCut: IntArray, verticalCut: IntArray): Int {
var sum = 0
for (hc in horizontalCut) {
sum += hc
}
for (vc in verticalCut) {
sum += vc
}
for (hc in horizontalCut) {
for (vc in verticalCut) {
sum += min(hc, vc)
}
}
return sum
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
3218\. Minimum Cost for Cutting Cake I

Medium

There is an `m x n` cake that needs to be cut into `1 x 1` pieces.

You are given integers `m`, `n`, and two arrays:

* `horizontalCut` of size `m - 1`, where `horizontalCut[i]` represents the cost to cut along the horizontal line `i`.
* `verticalCut` of size `n - 1`, where `verticalCut[j]` represents the cost to cut along the vertical line `j`.

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:

1. Cut along a horizontal line `i` at a cost of `horizontalCut[i]`.
2. Cut along a vertical line `j` at a cost of `verticalCut[j]`.

After the cut, the piece of cake is divided into two distinct pieces.

The cost of a cut depends only on the initial cost of the line and does not change.

Return the **minimum** total cost to cut the entire cake into `1 x 1` pieces.

**Example 1:**

**Input:** m = 3, n = 2, horizontalCut = [1,3], verticalCut = [5]

**Output:** 13

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/06/04/ezgifcom-animated-gif-maker-1.gif)

* Perform a cut on the vertical line 0 with cost 5, current total cost is 5.
* Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.
* Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.
* Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.
* Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.

The total cost is `5 + 1 + 1 + 3 + 3 = 13`.

**Example 2:**

**Input:** m = 2, n = 2, horizontalCut = [7], verticalCut = [4]

**Output:** 15

**Explanation:**

* Perform a cut on the horizontal line 0 with cost 7.
* Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.
* Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.

The total cost is `7 + 4 + 4 = 15`.

**Constraints:**

* `1 <= m, n <= 20`
* `horizontalCut.length == m - 1`
* `verticalCut.length == n - 1`
* <code>1 <= horizontalCut[i], verticalCut[i] <= 10<sup>3</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package g3201_3300.s3219_minimum_cost_for_cutting_cake_ii

// #Hard #Array #Sorting #Greedy #2024_07_19_Time_776_ms_(100.00%)_Space_66.8_MB_(96.88%)

class Solution {
fun minimumCost(m: Int, n: Int, horizontalCut: IntArray, verticalCut: IntArray): Long {
val horizontalCounts = IntArray(N)
val verticalCounts = IntArray(N)
var max = 0
for (x in horizontalCut) {
if (x > max) {
max = x
}
horizontalCounts[x]++
}
for (x in verticalCut) {
if (x > max) {
max = x
}
verticalCounts[x]++
}
var ans: Long = 0
var horizontalCount = 1
var verticalCount = 1
for (x in max downTo 1) {
ans += horizontalCounts[x].toLong() * x * horizontalCount
verticalCount += horizontalCounts[x]
horizontalCounts[x] = 0
ans += verticalCounts[x].toLong() * x * verticalCount
horizontalCount += verticalCounts[x]
verticalCounts[x] = 0
}
return ans
}

companion object {
private const val N = 1001
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
3219\. Minimum Cost for Cutting Cake II

Hard

There is an `m x n` cake that needs to be cut into `1 x 1` pieces.

You are given integers `m`, `n`, and two arrays:

* `horizontalCut` of size `m - 1`, where `horizontalCut[i]` represents the cost to cut along the horizontal line `i`.
* `verticalCut` of size `n - 1`, where `verticalCut[j]` represents the cost to cut along the vertical line `j`.

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:

1. Cut along a horizontal line `i` at a cost of `horizontalCut[i]`.
2. Cut along a vertical line `j` at a cost of `verticalCut[j]`.

After the cut, the piece of cake is divided into two distinct pieces.

The cost of a cut depends only on the initial cost of the line and does not change.

Return the **minimum** total cost to cut the entire cake into `1 x 1` pieces.

**Example 1:**

**Input:** m = 3, n = 2, horizontalCut = [1,3], verticalCut = [5]

**Output:** 13

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/06/04/ezgifcom-animated-gif-maker-1.gif)

* Perform a cut on the vertical line 0 with cost 5, current total cost is 5.
* Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.
* Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.
* Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.
* Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.

The total cost is `5 + 1 + 1 + 3 + 3 = 13`.

**Example 2:**

**Input:** m = 2, n = 2, horizontalCut = [7], verticalCut = [4]

**Output:** 15

**Explanation:**

* Perform a cut on the horizontal line 0 with cost 7.
* Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.
* Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.

The total cost is `7 + 4 + 4 = 15`.

**Constraints:**

* <code>1 <= m, n <= 10<sup>5</sup></code>
* `horizontalCut.length == m - 1`
* `verticalCut.length == n - 1`
* <code>1 <= horizontalCut[i], verticalCut[i] <= 10<sup>3</sup></code>
Loading