Skip to content

Commit e4ec307

Browse files
authored
Added tasks 3216-3229
1 parent 8fc39e1 commit e4ec307

File tree

14 files changed

+998
-0
lines changed
  • src/main/java/g3201_3300
    • s3216_lexicographically_smallest_string_after_a_swap
    • s3217_delete_nodes_from_linked_list_present_in_array
    • s3218_minimum_cost_for_cutting_cake_i
    • s3219_minimum_cost_for_cutting_cake_ii
    • s3220_odd_and_even_transactions
    • s3222_find_the_winning_player_in_coin_game
    • s3223_minimum_length_of_string_after_operations
    • s3224_minimum_array_changes_to_make_differences_equal
    • s3225_maximum_score_from_grid_operations
    • s3226_number_of_bit_changes_to_make_two_integers_equal
    • s3227_vowels_game_in_a_string
    • s3228_maximum_number_of_operations_to_move_ones_to_the_end
    • s3229_minimum_operations_to_make_array_equal_to_target

14 files changed

+998
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,19 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3229 |[Minimum Operations to Make Array Equal to Target](src/main/java/g3201_3300/s3229_minimum_operations_to_make_array_equal_to_target)| Hard | Array, Dynamic_Programming, Greedy, Stack, Monotonic_Stack | 2 | 100.00
1820+
| 3228 |[Maximum Number of Operations to Move Ones to the End](src/main/java/g3201_3300/s3228_maximum_number_of_operations_to_move_ones_to_the_end)| Medium | String, Greedy, Counting | 5 | 100.00
1821+
| 3227 |[Vowels Game in a String](src/main/java/g3201_3300/s3227_vowels_game_in_a_string)| Medium | String, Math, Game_Theory, Brainteaser | 4 | 96.15
1822+
| 3226 |[Number of Bit Changes to Make Two Integers Equal](src/main/java/g3201_3300/s3226_number_of_bit_changes_to_make_two_integers_equal)| Easy | Bit_Manipulation | 0 | 100.00
1823+
| 3225 |[Maximum Score From Grid Operations](src/main/java/g3201_3300/s3225_maximum_score_from_grid_operations)| Hard | Array, Dynamic_Programming, Matrix, Prefix_Sum | 21 | 100.00
1824+
| 3224 |[Minimum Array Changes to Make Differences Equal](src/main/java/g3201_3300/s3224_minimum_array_changes_to_make_differences_equal)| Medium | Array, Hash_Table, Prefix_Sum | 4 | 100.00
1825+
| 3223 |[Minimum Length of String After Operations](src/main/java/g3201_3300/s3223_minimum_length_of_string_after_operations)| Medium | String, Hash_Table, Counting | 9 | 94.23
1826+
| 3222 |[Find the Winning Player in Coin Game](src/main/java/g3201_3300/s3222_find_the_winning_player_in_coin_game)| Easy | Math, Simulation, Game_Theory | 0 | 100.00
1827+
| 3220 |[Odd and Even Transactions](src/main/java/g3201_3300/s3220_odd_and_even_transactions)| Medium | Database | 248 | 85.85
1828+
| 3219 |[Minimum Cost for Cutting Cake II](src/main/java/g3201_3300/s3219_minimum_cost_for_cutting_cake_ii)| Hard | Array, Sorting, Greedy | 3 | 100.00
1829+
| 3218 |[Minimum Cost for Cutting Cake I](src/main/java/g3201_3300/s3218_minimum_cost_for_cutting_cake_i)| Medium | Array, Dynamic_Programming, Sorting, Greedy | 0 | 100.00
1830+
| 3217 |[Delete Nodes From Linked List Present in Array](src/main/java/g3201_3300/s3217_delete_nodes_from_linked_list_present_in_array)| Medium | Array, Hash_Table, Linked_List | 3 | 100.00
1831+
| 3216 |[Lexicographically Smallest String After a Swap](src/main/java/g3201_3300/s3216_lexicographically_smallest_string_after_a_swap)| Easy | String, Greedy | 1 | 100.00
18191832
| 3213 |[Construct String with Minimum Cost](src/main/java/g3201_3300/s3213_construct_string_with_minimum_cost)| Hard | Array, String, Dynamic_Programming, Suffix_Array | 261 | 88.55
18201833
| 3212 |[Count Submatrices With Equal Frequency of X and Y](src/main/java/g3201_3300/s3212_count_submatrices_with_equal_frequency_of_x_and_y)| Medium | Array, Matrix, Prefix_Sum | 15 | 100.00
18211834
| 3211 |[Generate Binary Strings Without Adjacent Zeros](src/main/java/g3201_3300/s3211_generate_binary_strings_without_adjacent_zeros)| Medium | String, Bit_Manipulation, Recursion | 1 | 100.00
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3216\. Lexicographically Smallest String After a Swap
5+
6+
Easy
7+
8+
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**.
9+
10+
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.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "45320"
15+
16+
**Output:** "43520"
17+
18+
**Explanation:**
19+
20+
`s[1] == '5'` and `s[2] == '3'` both have the same parity, and swapping them results in the lexicographically smallest string.
21+
22+
**Example 2:**
23+
24+
**Input:** s = "001"
25+
26+
**Output:** "001"
27+
28+
**Explanation:**
29+
30+
There is no need to perform a swap because `s` is already the lexicographically smallest.
31+
32+
**Constraints:**
33+
34+
* `2 <= s.length <= 100`
35+
* `s` consists only of digits.
36+
37+
## Solution
38+
39+
```java
40+
public class Solution {
41+
public String getSmallestString(String s) {
42+
final char[] arr = s.toCharArray();
43+
for (int i = 1; i < arr.length; i++) {
44+
if (arr[i - 1] % 2 == arr[i] % 2 && arr[i - 1] > arr[i]) {
45+
final char temp = arr[i];
46+
arr[i] = arr[i - 1];
47+
arr[i - 1] = temp;
48+
break;
49+
}
50+
}
51+
return String.valueOf(arr);
52+
}
53+
}
54+
```
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3217\. Delete Nodes From Linked List Present in Array
5+
6+
Medium
7+
8+
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`.
9+
10+
**Example 1:**
11+
12+
**Input:** nums = [1,2,3], head = [1,2,3,4,5]
13+
14+
**Output:** [4,5]
15+
16+
**Explanation:**
17+
18+
**![](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample0.png)**
19+
20+
Remove the nodes with values 1, 2, and 3.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [1], head = [1,2,1,2,1,2]
25+
26+
**Output:** [2,2,2]
27+
28+
**Explanation:**
29+
30+
![](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample1.png)
31+
32+
Remove the nodes with value 1.
33+
34+
**Example 3:**
35+
36+
**Input:** nums = [5], head = [1,2,3,4]
37+
38+
**Output:** [1,2,3,4]
39+
40+
**Explanation:**
41+
42+
**![](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample2.png)**
43+
44+
No node has value 5.
45+
46+
**Constraints:**
47+
48+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
49+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
50+
* All elements in `nums` are unique.
51+
* The number of nodes in the given list is in the range <code>[1, 10<sup>5</sup>]</code>.
52+
* <code>1 <= Node.val <= 10<sup>5</sup></code>
53+
* The input is generated such that there is at least one node in the linked list that has a value not present in `nums`.
54+
55+
## Solution
56+
57+
```java
58+
import com_github_leetcode.ListNode;
59+
60+
61+
/**
62+
* Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode() {}
63+
* ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val;
64+
* this.next = next; } }
65+
*/
66+
public class Solution {
67+
public ListNode modifiedList(int[] nums, ListNode head) {
68+
int maxv = 0;
69+
for (int v : nums) {
70+
maxv = Math.max(maxv, v);
71+
}
72+
boolean[] rem = new boolean[maxv + 1];
73+
for (int v : nums) {
74+
rem[v] = true;
75+
}
76+
ListNode h = new ListNode(0);
77+
ListNode t = h;
78+
ListNode p = head;
79+
while (p != null) {
80+
if (p.val > maxv || !rem[p.val]) {
81+
t.next = p;
82+
t = p;
83+
}
84+
p = p.next;
85+
}
86+
t.next = null;
87+
return h.next;
88+
}
89+
}
90+
```
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3218\. Minimum Cost for Cutting Cake I
5+
6+
Medium
7+
8+
There is an `m x n` cake that needs to be cut into `1 x 1` pieces.
9+
10+
You are given integers `m`, `n`, and two arrays:
11+
12+
* `horizontalCut` of size `m - 1`, where `horizontalCut[i]` represents the cost to cut along the horizontal line `i`.
13+
* `verticalCut` of size `n - 1`, where `verticalCut[j]` represents the cost to cut along the vertical line `j`.
14+
15+
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:
16+
17+
1. Cut along a horizontal line `i` at a cost of `horizontalCut[i]`.
18+
2. Cut along a vertical line `j` at a cost of `verticalCut[j]`.
19+
20+
After the cut, the piece of cake is divided into two distinct pieces.
21+
22+
The cost of a cut depends only on the initial cost of the line and does not change.
23+
24+
Return the **minimum** total cost to cut the entire cake into `1 x 1` pieces.
25+
26+
**Example 1:**
27+
28+
**Input:** m = 3, n = 2, horizontalCut = [1,3], verticalCut = [5]
29+
30+
**Output:** 13
31+
32+
**Explanation:**
33+
34+
![](https://assets.leetcode.com/uploads/2024/06/04/ezgifcom-animated-gif-maker-1.gif)
35+
36+
* Perform a cut on the vertical line 0 with cost 5, current total cost is 5.
37+
* Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.
38+
* Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.
39+
* Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.
40+
* Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.
41+
42+
The total cost is `5 + 1 + 1 + 3 + 3 = 13`.
43+
44+
**Example 2:**
45+
46+
**Input:** m = 2, n = 2, horizontalCut = [7], verticalCut = [4]
47+
48+
**Output:** 15
49+
50+
**Explanation:**
51+
52+
* Perform a cut on the horizontal line 0 with cost 7.
53+
* Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.
54+
* Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.
55+
56+
The total cost is `7 + 4 + 4 = 15`.
57+
58+
**Constraints:**
59+
60+
* `1 <= m, n <= 20`
61+
* `horizontalCut.length == m - 1`
62+
* `verticalCut.length == n - 1`
63+
* <code>1 <= horizontalCut[i], verticalCut[i] <= 10<sup>3</sup></code>
64+
65+
## Solution
66+
67+
```java
68+
@SuppressWarnings("java:S1172")
69+
public class Solution {
70+
public int minimumCost(int m, int n, int[] horizontalCut, int[] verticalCut) {
71+
int sum = 0;
72+
for (int hc : horizontalCut) {
73+
sum += hc;
74+
}
75+
for (int vc : verticalCut) {
76+
sum += vc;
77+
}
78+
for (int hc : horizontalCut) {
79+
for (int vc : verticalCut) {
80+
sum += Math.min(hc, vc);
81+
}
82+
}
83+
return sum;
84+
}
85+
}
86+
```
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3219\. Minimum Cost for Cutting Cake II
5+
6+
Hard
7+
8+
There is an `m x n` cake that needs to be cut into `1 x 1` pieces.
9+
10+
You are given integers `m`, `n`, and two arrays:
11+
12+
* `horizontalCut` of size `m - 1`, where `horizontalCut[i]` represents the cost to cut along the horizontal line `i`.
13+
* `verticalCut` of size `n - 1`, where `verticalCut[j]` represents the cost to cut along the vertical line `j`.
14+
15+
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:
16+
17+
1. Cut along a horizontal line `i` at a cost of `horizontalCut[i]`.
18+
2. Cut along a vertical line `j` at a cost of `verticalCut[j]`.
19+
20+
After the cut, the piece of cake is divided into two distinct pieces.
21+
22+
The cost of a cut depends only on the initial cost of the line and does not change.
23+
24+
Return the **minimum** total cost to cut the entire cake into `1 x 1` pieces.
25+
26+
**Example 1:**
27+
28+
**Input:** m = 3, n = 2, horizontalCut = [1,3], verticalCut = [5]
29+
30+
**Output:** 13
31+
32+
**Explanation:**
33+
34+
![](https://assets.leetcode.com/uploads/2024/06/04/ezgifcom-animated-gif-maker-1.gif)
35+
36+
* Perform a cut on the vertical line 0 with cost 5, current total cost is 5.
37+
* Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.
38+
* Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1.
39+
* Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.
40+
* Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3.
41+
42+
The total cost is `5 + 1 + 1 + 3 + 3 = 13`.
43+
44+
**Example 2:**
45+
46+
**Input:** m = 2, n = 2, horizontalCut = [7], verticalCut = [4]
47+
48+
**Output:** 15
49+
50+
**Explanation:**
51+
52+
* Perform a cut on the horizontal line 0 with cost 7.
53+
* Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.
54+
* Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4.
55+
56+
The total cost is `7 + 4 + 4 = 15`.
57+
58+
**Constraints:**
59+
60+
* <code>1 <= m, n <= 10<sup>5</sup></code>
61+
* `horizontalCut.length == m - 1`
62+
* `verticalCut.length == n - 1`
63+
* <code>1 <= horizontalCut[i], verticalCut[i] <= 10<sup>3</sup></code>
64+
65+
## Solution
66+
67+
```java
68+
@SuppressWarnings("java:S1172")
69+
public class Solution {
70+
private static final int N = 1001;
71+
72+
public long minimumCost(int m, int n, int[] horizontalCut, int[] verticalCut) {
73+
int[] horizontalCounts = new int[N];
74+
int[] verticalCounts = new int[N];
75+
int max = 0;
76+
for (int x : horizontalCut) {
77+
if (x > max) {
78+
max = x;
79+
}
80+
horizontalCounts[x]++;
81+
}
82+
for (int x : verticalCut) {
83+
if (x > max) {
84+
max = x;
85+
}
86+
verticalCounts[x]++;
87+
}
88+
long ans = 0;
89+
int horizontalCount = 1;
90+
int verticalCount = 1;
91+
for (int x = max; x > 0; x--) {
92+
ans += (long) horizontalCounts[x] * x * horizontalCount;
93+
verticalCount += horizontalCounts[x];
94+
horizontalCounts[x] = 0;
95+
ans += (long) verticalCounts[x] * x * verticalCount;
96+
horizontalCount += verticalCounts[x];
97+
verticalCounts[x] = 0;
98+
}
99+
return ans;
100+
}
101+
}
102+
```

0 commit comments

Comments
 (0)