forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
481 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/main/java/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_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); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
.../java/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
35 changes: 35 additions & 0 deletions
35
src/main/java/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
.../java/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
22 changes: 22 additions & 0 deletions
22
src/main/java/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/g3201_3300/s3218_minimum_cost_for_cutting_cake_i/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
37 changes: 37 additions & 0 deletions
37
src/main/java/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
65 changes: 65 additions & 0 deletions
65
src/main/java/g3201_3300/s3220_odd_and_even_transactions/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
6 changes: 6 additions & 0 deletions
6
src/main/java/g3201_3300/s3220_odd_and_even_transactions/script.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
Oops, something went wrong.