diff --git a/src/main/kotlin/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/Solution.kt b/src/main/kotlin/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/Solution.kt new file mode 100644 index 000000000..1816e0445 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/Solution.kt @@ -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) + } +} diff --git a/src/main/kotlin/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/readme.md b/src/main/kotlin/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/readme.md new file mode 100644 index 000000000..650b7d83c --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/readme.md @@ -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. \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/Solution.kt b/src/main/kotlin/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/Solution.kt new file mode 100644 index 000000000..9cd89ec75 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/Solution.kt @@ -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 + } +} diff --git a/src/main/kotlin/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/readme.md b/src/main/kotlin/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/readme.md new file mode 100644 index 000000000..77883ba4b --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/readme.md @@ -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:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 105 +* All elements in `nums` are unique. +* The number of nodes in the given list is in the range [1, 105]. +* 1 <= Node.val <= 105 +* The input is generated such that there is at least one node in the linked list that has a value not present in `nums`. \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/Solution.kt b/src/main/kotlin/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/Solution.kt new file mode 100644 index 000000000..c8ae953c4 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/Solution.kt @@ -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 + } +} diff --git a/src/main/kotlin/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/readme.md b/src/main/kotlin/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/readme.md new file mode 100644 index 000000000..75eb03c6f --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/readme.md @@ -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` +* 1 <= horizontalCut[i], verticalCut[i] <= 103 \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/Solution.kt b/src/main/kotlin/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/Solution.kt new file mode 100644 index 000000000..404bfe802 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/Solution.kt @@ -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 + } +} diff --git a/src/main/kotlin/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/readme.md b/src/main/kotlin/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/readme.md new file mode 100644 index 000000000..ee28b98af --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/readme.md @@ -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:** + +* 1 <= m, n <= 105 +* `horizontalCut.length == m - 1` +* `verticalCut.length == n - 1` +* 1 <= horizontalCut[i], verticalCut[i] <= 103 \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3220_odd_and_even_transactions/readme.md b/src/main/kotlin/g3201_3300/s3220_odd_and_even_transactions/readme.md new file mode 100644 index 000000000..d8327c130 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3220_odd_and_even_transactions/readme.md @@ -0,0 +1,65 @@ +3220\. Odd and Even Transactions + +Medium + +SQL Schema + +Table: `transactions` + + +------------------+------+ + | Column Name | Type | + +------------------+------+ + | transaction_id | int | + | amount | int | + | transaction_date | date | + +------------------+------+ + The transactions_id column uniquely identifies each row in this table. + Each row of this table contains the transaction id, amount and transaction date. + +Write a solution to find the **sum of amounts** for **odd** and **even** transactions for each day. If there are no odd or even transactions for a specific date, display as `0`. + +Return _the result table ordered by_ `transaction_date` _in **ascending** order_. + +The result format is in the following example. + +**Example:** + +**Input:** + +`transactions` table: + + +----------------+--------+------------------+ + | transaction_id | amount | transaction_date | + +----------------+--------+------------------+ + | 1 | 150 | 2024-07-01 | + | 2 | 200 | 2024-07-01 | + | 3 | 75 | 2024-07-01 | + | 4 | 300 | 2024-07-02 | + | 5 | 50 | 2024-07-02 | + | 6 | 120 | 2024-07-03 | + +----------------+--------+------------------+ + +**Output:** + + +------------------+---------+----------+ + | transaction_date | odd_sum | even_sum | + +------------------+---------+----------+ + | 2024-07-01 | 75 | 350 | + | 2024-07-02 | 0 | 350 | + | 2024-07-03 | 0 | 120 | + +------------------+---------+----------+ + +**Explanation:** + +* For transaction dates: + * 2024-07-01: + * Sum of amounts for odd transactions: 75 + * Sum of amounts for even transactions: 150 + 200 = 350 + * 2024-07-02: + * Sum of amounts for odd transactions: 0 + * Sum of amounts for even transactions: 300 + 50 = 350 + * 2024-07-03: + * Sum of amounts for odd transactions: 0 + * Sum of amounts for even transactions: 120 + +**Note:** The output table is ordered by `transaction_date` in ascending order. \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3220_odd_and_even_transactions/script.sql b/src/main/kotlin/g3201_3300/s3220_odd_and_even_transactions/script.sql new file mode 100644 index 000000000..33c0eda87 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3220_odd_and_even_transactions/script.sql @@ -0,0 +1,6 @@ +# Write your MySQL query statement below +# #Medium #2024_07_18_Time_272_ms_(100.00%)_Space_0B_(100.00%) +select transaction_date, +sum(case when amount%2<>0 then amount else 0 end) as odd_sum, +sum(case when amount%2=0 then amount else 0 end) as even_sum from transactions +group by transaction_date order by transaction_date asc; diff --git a/src/test/kotlin/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/SolutionTest.kt new file mode 100644 index 000000000..df5e2e2a5 --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3201_3300.s3216_lexicographically_smallest_string_after_a_swap + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun smallestString() { + assertThat(Solution().getSmallestString("45320"), equalTo("43520")) + } + + @Test + fun smallestString2() { + assertThat(Solution().getSmallestString("001"), equalTo("001")) + } +} diff --git a/src/test/kotlin/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/SolutionTest.kt new file mode 100644 index 000000000..c7d66f802 --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/SolutionTest.kt @@ -0,0 +1,47 @@ +package g3201_3300.s3217_delete_nodes_from_linked_list_present_in_array + +import com_github_leetcode.LinkedListUtils.contructLinkedList +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun modifiedList() { + assertThat( + Solution() + .modifiedList( + intArrayOf(1, 2, 3), + contructLinkedList(intArrayOf(1, 2, 3, 4, 5)) + ) + .toString(), + equalTo("4, 5") + ) + } + + @Test + fun modifiedList2() { + assertThat( + Solution() + .modifiedList( + intArrayOf(1), + contructLinkedList(intArrayOf(1, 2, 1, 2, 1, 2)) + ) + .toString(), + equalTo("2, 2, 2") + ) + } + + @Test + fun modifiedList3() { + assertThat( + Solution() + .modifiedList( + intArrayOf(5), + contructLinkedList(intArrayOf(1, 2, 3, 4)) + ) + .toString(), + equalTo("1, 2, 3, 4") + ) + } +} diff --git a/src/test/kotlin/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/SolutionTest.kt new file mode 100644 index 000000000..a341dbaa9 --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/SolutionTest.kt @@ -0,0 +1,20 @@ +package g3201_3300.s3218_minimum_cost_for_cutting_cake_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minimumCost() { + assertThat( + Solution().minimumCost(3, 2, intArrayOf(1, 3), intArrayOf(5)), + equalTo(13) + ) + } + + @Test + fun minimumCost2() { + assertThat(Solution().minimumCost(2, 2, intArrayOf(7), intArrayOf(4)), equalTo(15)) + } +} diff --git a/src/test/kotlin/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/SolutionTest.kt new file mode 100644 index 000000000..8d6a3854c --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/SolutionTest.kt @@ -0,0 +1,20 @@ +package g3201_3300.s3219_minimum_cost_for_cutting_cake_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minimumCost() { + assertThat( + Solution().minimumCost(3, 2, intArrayOf(1, 3), intArrayOf(5)), + equalTo(13L) + ) + } + + @Test + fun minimumCost2() { + assertThat(Solution().minimumCost(2, 2, intArrayOf(7), intArrayOf(4)), equalTo(15L)) + } +}