diff --git a/src/main/java/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/Solution.java b/src/main/java/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/Solution.java new file mode 100644 index 000000000..cdaef4764 --- /dev/null +++ b/src/main/java/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/Solution.java @@ -0,0 +1,18 @@ +package g3201_3300.s3216_lexicographically_smallest_string_after_a_swap; + +// #Easy #String #Greedy #2024_07_18_Time_1_ms_(100.00%)_Space_43.3_MB_(20.48%) + +public class Solution { + public String getSmallestString(String s) { + final char[] arr = s.toCharArray(); + for (int i = 1; i < arr.length; i++) { + if (arr[i - 1] % 2 == arr[i] % 2 && arr[i - 1] > arr[i]) { + final char temp = arr[i]; + arr[i] = arr[i - 1]; + arr[i - 1] = temp; + break; + } + } + return String.valueOf(arr); + } +} diff --git a/src/main/java/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/readme.md b/src/main/java/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/readme.md new file mode 100644 index 000000000..650b7d83c --- /dev/null +++ b/src/main/java/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/java/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/Solution.java b/src/main/java/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/Solution.java new file mode 100644 index 000000000..efa5c41f6 --- /dev/null +++ b/src/main/java/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/Solution.java @@ -0,0 +1,35 @@ +package g3201_3300.s3217_delete_nodes_from_linked_list_present_in_array; + +import com_github_leetcode.ListNode; + +// #Medium #Array #Hash_Table #Linked_List #2024_07_18_Time_3_ms_(100.00%)_Space_63.9_MB_(93.81%) + +/** + * Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode() {} + * ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; + * this.next = next; } } + */ +public class Solution { + public ListNode modifiedList(int[] nums, ListNode head) { + int maxv = 0; + for (int v : nums) { + maxv = Math.max(maxv, v); + } + boolean[] rem = new boolean[maxv + 1]; + for (int v : nums) { + rem[v] = true; + } + ListNode h = new ListNode(0); + ListNode t = h; + ListNode 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/java/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/readme.md b/src/main/java/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/java/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/java/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/Solution.java b/src/main/java/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/Solution.java new file mode 100644 index 000000000..edb4d4971 --- /dev/null +++ b/src/main/java/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/Solution.java @@ -0,0 +1,22 @@ +package g3201_3300.s3218_minimum_cost_for_cutting_cake_i; + +// #Medium #Array #Dynamic_Programming #Sorting #Greedy +// #2024_07_18_Time_0_ms_(100.00%)_Space_42.4_MB_(32.85%) + +public class Solution { + public int minimumCost(int ignoredM, int ignoredN, int[] horizontalCut, int[] verticalCut) { + int sum = 0; + for (int hc : horizontalCut) { + sum += hc; + } + for (int vc : verticalCut) { + sum += vc; + } + for (int hc : horizontalCut) { + for (int vc : verticalCut) { + sum += Math.min(hc, vc); + } + } + return sum; + } +} diff --git a/src/main/java/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/readme.md b/src/main/java/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/readme.md new file mode 100644 index 000000000..75eb03c6f --- /dev/null +++ b/src/main/java/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/java/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/Solution.java b/src/main/java/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/Solution.java new file mode 100644 index 000000000..1feb3e124 --- /dev/null +++ b/src/main/java/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/Solution.java @@ -0,0 +1,37 @@ +package g3201_3300.s3219_minimum_cost_for_cutting_cake_ii; + +// #Hard #Array #Sorting #Greedy #2024_07_18_Time_3_ms_(100.00%)_Space_62.6_MB_(25.82%) + +public class Solution { + private static final int N = 1001; + private static final int[] HORIZONTAL_COUNTS = new int[N]; + private static final int[] VERTICAL_COUNTS = new int[N]; + + public long minimumCost(int m, int n, int[] horizontalCut, int[] verticalCut) { + int max = 0; + for (int x : horizontalCut) { + if (x > max) { + max = x; + } + HORIZONTAL_COUNTS[x]++; + } + for (int x : verticalCut) { + if (x > max) { + max = x; + } + VERTICAL_COUNTS[x]++; + } + long ans = 0; + int horizontalCount = 1; + int verticalCount = 1; + for (int x = max; x > 0; x--) { + ans += (long) HORIZONTAL_COUNTS[x] * x * horizontalCount; + verticalCount += HORIZONTAL_COUNTS[x]; + HORIZONTAL_COUNTS[x] = 0; + ans += (long) VERTICAL_COUNTS[x] * x * verticalCount; + horizontalCount += VERTICAL_COUNTS[x]; + VERTICAL_COUNTS[x] = 0; + } + return ans; + } +} diff --git a/src/main/java/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/readme.md b/src/main/java/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/readme.md new file mode 100644 index 000000000..ee28b98af --- /dev/null +++ b/src/main/java/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/java/g3201_3300/s3220_odd_and_even_transactions/readme.md b/src/main/java/g3201_3300/s3220_odd_and_even_transactions/readme.md new file mode 100644 index 000000000..d8327c130 --- /dev/null +++ b/src/main/java/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/java/g3201_3300/s3220_odd_and_even_transactions/script.sql b/src/main/java/g3201_3300/s3220_odd_and_even_transactions/script.sql new file mode 100644 index 000000000..7d845c450 --- /dev/null +++ b/src/main/java/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; diff --git a/src/test/java/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/SolutionTest.java b/src/test/java/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/SolutionTest.java new file mode 100644 index 000000000..18f217669 --- /dev/null +++ b/src/test/java/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/SolutionTest.java @@ -0,0 +1,18 @@ +package g3201_3300.s3216_lexicographically_smallest_string_after_a_swap; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void getSmallestString() { + assertThat(new Solution().getSmallestString("45320"), equalTo("43520")); + } + + @Test + void getSmallestString2() { + assertThat(new Solution().getSmallestString("001"), equalTo("001")); + } +} diff --git a/src/test/java/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/SolutionTest.java b/src/test/java/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/SolutionTest.java new file mode 100644 index 000000000..34e93d47d --- /dev/null +++ b/src/test/java/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/SolutionTest.java @@ -0,0 +1,42 @@ +package g3201_3300.s3217_delete_nodes_from_linked_list_present_in_array; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import com_github_leetcode.LinkedListUtils; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void modifiedList() { + assertThat( + new Solution() + .modifiedList( + new int[] {1, 2, 3}, + LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4, 5})) + .toString(), + equalTo("4, 5")); + } + + @Test + void modifiedList2() { + assertThat( + new Solution() + .modifiedList( + new int[] {1}, + LinkedListUtils.contructLinkedList(new int[] {1, 2, 1, 2, 1, 2})) + .toString(), + equalTo("2, 2, 2")); + } + + @Test + void modifiedList3() { + assertThat( + new Solution() + .modifiedList( + new int[] {5}, + LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 4})) + .toString(), + equalTo("1, 2, 3, 4")); + } +} diff --git a/src/test/java/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/SolutionTest.java b/src/test/java/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/SolutionTest.java new file mode 100644 index 000000000..eb9fc5241 --- /dev/null +++ b/src/test/java/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/SolutionTest.java @@ -0,0 +1,18 @@ +package g3201_3300.s3218_minimum_cost_for_cutting_cake_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumCost() { + assertThat(new Solution().minimumCost(3, 2, new int[] {1, 3}, new int[] {5}), equalTo(13)); + } + + @Test + void minimumCost2() { + assertThat(new Solution().minimumCost(2, 2, new int[] {7}, new int[] {4}), equalTo(15)); + } +} diff --git a/src/test/java/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/SolutionTest.java b/src/test/java/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/SolutionTest.java new file mode 100644 index 000000000..1fa843b2c --- /dev/null +++ b/src/test/java/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/SolutionTest.java @@ -0,0 +1,18 @@ +package g3201_3300.s3219_minimum_cost_for_cutting_cake_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumCost() { + assertThat(new Solution().minimumCost(3, 2, new int[] {1, 3}, new int[] {5}), equalTo(13L)); + } + + @Test + void minimumCost2() { + assertThat(new Solution().minimumCost(2, 2, new int[] {7}, new int[] {4}), equalTo(15L)); + } +}