Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 53149f4

Browse files
authored
Added tasks 3612-3615
1 parent 4ebde8b commit 53149f4

File tree

13 files changed

+604
-1
lines changed

13 files changed

+604
-1
lines changed

‎src/main/java/g3601_3700/s3609_minimum_moves_to_reach_target_in_grid/Solution.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ public int minMoves(int sx, int sy, int tx, int ty) {
77
if (sx == 0 && sy == 0) {
88
return tx == 0 && ty == 0 ? 0 : -1;
99
}
10-
1110
int res = 0;
1211
while (sx != tx || sy != ty) {
1312
if (sx > tx || sy > ty) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3601_3700.s3612_process_string_with_special_operations_i;
2+
3+
// #Medium #String #Simulation #2025_07_14_Time_3_ms_(100.00%)_Space_54.53_MB_(100.00%)
4+
5+
public class Solution {
6+
public String processStr(String s) {
7+
StringBuilder res = new StringBuilder();
8+
for (char c : s.toCharArray()) {
9+
if (c != '*' && c != '#' && c != '%') {
10+
res.append(c);
11+
} else if (c == '#') {
12+
res.append(res);
13+
} else if (c == '%') {
14+
res.reverse();
15+
} else {
16+
if (!res.isEmpty()) {
17+
res.deleteCharAt(res.length() - 1);
18+
}
19+
}
20+
}
21+
return res.toString();
22+
}
23+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
3612\. Process String with Special Operations I
2+
3+
Medium
4+
5+
You are given a string `s` consisting of lowercase English letters and the special characters: `*`, `#`, and `%`.
6+
7+
Build a new string `result` by processing `s` according to the following rules from left to right:
8+
9+
* If the letter is a **lowercase** English letter append it to `result`.
10+
* A `'*'` **removes** the last character from `result`, if it exists.
11+
* A `'#'` **duplicates** the current `result` and **appends** it to itself.
12+
* A `'%'` **reverses** the current `result`.
13+
14+
Return the final string `result` after processing all characters in `s`.
15+
16+
**Example 1:**
17+
18+
**Input:** s = "a#b%\*"
19+
20+
**Output:** "ba"
21+
22+
**Explanation:**
23+
24+
`i`
25+
26+
`s[i]`
27+
28+
Operation
29+
30+
Current `result`
31+
32+
0
33+
34+
`'a'`
35+
36+
Append `'a'`
37+
38+
`"a"`
39+
40+
1
41+
42+
`'#'`
43+
44+
Duplicate `result`
45+
46+
`"aa"`
47+
48+
2
49+
50+
`'b'`
51+
52+
Append `'b'`
53+
54+
`"aab"`
55+
56+
3
57+
58+
`'%'`
59+
60+
Reverse `result`
61+
62+
`"baa"`
63+
64+
4
65+
66+
`'*'`
67+
68+
Remove the last character
69+
70+
`"ba"`
71+
72+
Thus, the final `result` is `"ba"`.
73+
74+
**Example 2:**
75+
76+
**Input:** s = "z\*#"
77+
78+
**Output:** ""
79+
80+
**Explanation:**
81+
82+
`i`
83+
84+
`s[i]`
85+
86+
Operation
87+
88+
Current `result`
89+
90+
0
91+
92+
`'z'`
93+
94+
Append `'z'`
95+
96+
`"z"`
97+
98+
1
99+
100+
`'*'`
101+
102+
Remove the last character
103+
104+
`""`
105+
106+
2
107+
108+
`'#'`
109+
110+
Duplicate the string
111+
112+
`""`
113+
114+
Thus, the final `result` is `""`.
115+
116+
**Constraints:**
117+
118+
* `1 <= s.length <= 20`
119+
* `s` consists of only lowercase English letters and special characters `*`, `#`, and `%`.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g3601_3700.s3613_minimize_maximum_component_cost;
2+
3+
// #Medium #Binary_Search #Graph #Union_Find #Sort
4+
// #2025_07_14_Time_37_ms_(100.00%)_Space_88.50_MB_(98.52%)
5+
6+
public class Solution {
7+
public int minCost(int ui, int[][] pl, int zx) {
8+
int rt = 0;
9+
int gh = 0;
10+
int i = 0;
11+
while (i < pl.length) {
12+
gh = Math.max(gh, pl[i][2]);
13+
i++;
14+
}
15+
while (rt < gh) {
16+
int ty = rt + (gh - rt) / 2;
17+
if (dfgh(ui, pl, ty, zx)) {
18+
gh = ty;
19+
} else {
20+
rt = ty + 1;
21+
}
22+
}
23+
return rt;
24+
}
25+
26+
private boolean dfgh(int ui, int[][] pl, int jk, int zx) {
27+
int[] wt = new int[ui];
28+
int i = 0;
29+
while (i < ui) {
30+
wt[i] = i;
31+
i++;
32+
}
33+
int er = ui;
34+
i = 0;
35+
while (i < pl.length) {
36+
int[] df = pl[i];
37+
if (df[2] > jk) {
38+
i++;
39+
continue;
40+
}
41+
int u = cvb(wt, df[0]);
42+
int v = cvb(wt, df[1]);
43+
if (u != v) {
44+
wt[u] = v;
45+
er--;
46+
}
47+
i++;
48+
}
49+
return er <= zx;
50+
}
51+
52+
private int cvb(int[] wt, int i) {
53+
for (; wt[i] != i; i = wt[i]) {
54+
wt[i] = wt[wt[i]];
55+
}
56+
return i;
57+
}
58+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3613\. Minimize Maximum Component Cost
2+
3+
Medium
4+
5+
You are given an undirected connected graph with `n` nodes labeled from 0 to `n - 1` and a 2D integer array `edges` where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> denotes an undirected edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with weight <code>w<sub>i</sub></code>, and an integer `k`.
6+
7+
You are allowed to remove any number of edges from the graph such that the resulting graph has **at most** `k` connected components.
8+
9+
The **cost** of a component is defined as the **maximum** edge weight in that component. If a component has no edges, its cost is 0.
10+
11+
Return the **minimum** possible value of the **maximum** cost among all components **after such removals**.
12+
13+
**Example 1:**
14+
15+
**Input:** n = 5, edges = [[0,1,4],[1,2,3],[1,3,2],[3,4,6]], k = 2
16+
17+
**Output:** 4
18+
19+
**Explanation:**
20+
21+
![](https://assets.leetcode.com/uploads/2025/04/19/minimizemaximumm.jpg)
22+
23+
* Remove the edge between nodes 3 and 4 (weight 6).
24+
* The resulting components have costs of 0 and 4, so the overall maximum cost is 4.
25+
26+
**Example 2:**
27+
28+
**Input:** n = 4, edges = [[0,1,5],[1,2,5],[2,3,5]], k = 1
29+
30+
**Output:** 5
31+
32+
**Explanation:**
33+
34+
![](https://assets.leetcode.com/uploads/2025/04/19/minmax2.jpg)
35+
36+
* No edge can be removed, since allowing only one component (`k = 1`) requires the graph to stay fully connected.
37+
* That single component’s cost equals its largest edge weight, which is 5.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
42+
* <code>0 <= edges.length <= 10<sup>5</sup></code>
43+
* `edges[i].length == 3`
44+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> < n</code>
45+
* <code>1 <= w<sub>i</sub> <= 10<sup>6</sup></code>
46+
* `1 <= k <= n`
47+
* The input graph is connected.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g3601_3700.s3614_process_string_with_special_operations_ii;
2+
3+
// #Hard #String #Simulation #2025_07_14_Time_33_ms_(100.00%)_Space_50.49_MB_(100.00%)
4+
5+
public class Solution {
6+
public char processStr(String s, long k) {
7+
long len = 0;
8+
for (char c : s.toCharArray()) {
9+
if (Character.isLowerCase(c)) {
10+
len++;
11+
} else if (c == '*' && len > 0) {
12+
len--;
13+
} else if (c == '#') {
14+
len *= 2;
15+
}
16+
}
17+
if (k >= len) {
18+
return '.';
19+
}
20+
for (int i = s.length() - 1; i >= 0; i--) {
21+
char c = s.charAt(i);
22+
if (Character.isLowerCase(c)) {
23+
if (k == len - 1) {
24+
return c;
25+
}
26+
len--;
27+
} else if (c == '*') {
28+
len++;
29+
} else if (c == '#') {
30+
len /= 2;
31+
if (k >= len) {
32+
k -= len;
33+
}
34+
} else if (c == '%') {
35+
k = len - 1 - k;
36+
}
37+
}
38+
return '.';
39+
}
40+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
3614\. Process String with Special Operations II
2+
3+
Hard
4+
5+
You are given a string `s` consisting of lowercase English letters and the special characters: `'*'`, `'#'`, and `'%'`.
6+
7+
You are also given an integer `k`.
8+
9+
Build a new string `result` by processing `s` according to the following rules from left to right:
10+
11+
* If the letter is a **lowercase** English letter append it to `result`.
12+
* A `'*'` **removes** the last character from `result`, if it exists.
13+
* A `'#'` **duplicates** the current `result` and **appends** it to itself.
14+
* A `'%'` **reverses** the current `result`.
15+
16+
Return the <code>k<sup>th</sup></code> character of the final string `result`. If `k` is out of the bounds of `result`, return `'.'`.
17+
18+
**Example 1:**
19+
20+
**Input:** s = "a#b%\*", k = 1
21+
22+
**Output:** "a"
23+
24+
**Explanation:**
25+
26+
| i | s[i] | Operation | Current `result` |
27+
|---|-------|----------------------------|------------------|
28+
| 0 | `'a'` | Append `'a'` | `"a"` |
29+
| 1 | `'#'` | Duplicate `result` | `"aa"` |
30+
| 2 | `'b'` | Append `'b'` | `"aab"` |
31+
| 3 | `'%'` | Reverse `result` | `"baa"` |
32+
| 4 | `'*'` | Remove the last character | `"ba"` |
33+
34+
The final `result` is `"ba"`. The character at index `k = 1` is `'a'`.
35+
36+
**Example 2:**
37+
38+
**Input:** s = "cd%#\*#", k = 3
39+
40+
**Output:** "d"
41+
42+
**Explanation:**
43+
44+
| i | s[i] | Operation | Current `result` |
45+
|---|-------|----------------------------|------------------|
46+
| 0 | `'c'` | Append `'c'` | `"c"` |
47+
| 1 | `'d'` | Append `'d'` | `"cd"` |
48+
| 2 | `'%'` | Reverse `result` | `"dc"` |
49+
| 3 | `'#'` | Duplicate `result` | `"dcdc"` |
50+
| 4 | `'*'` | Remove the last character | `"dcd"` |
51+
| 5 | `'#'` | Duplicate `result` | `"dcddcd"` |
52+
53+
The final `result` is `"dcddcd"`. The character at index `k = 3` is `'d'`.
54+
55+
**Example 3:**
56+
57+
**Input:** s = "z\*#", k = 0
58+
59+
**Output:** "."
60+
61+
**Explanation:**
62+
63+
| i | s[i] | Operation | Current `result` |
64+
|---|-------|---------------------------|------------------|
65+
| 0 | `'z'` | Append `'z'` | `"z"` |
66+
| 1 | `'*'` | Remove the last character | `""` |
67+
| 2 | `'#'` | Duplicate the string | `""` |
68+
69+
The final `result` is `""`. Since index `k = 0` is out of bounds, the output is `'.'`.
70+
71+
**Constraints:**
72+
73+
* <code>1 <= s.length <= 10<sup>5</sup></code>
74+
* `s` consists of only lowercase English letters and special characters `'*'`, `'#'`, and `'%'`.
75+
* <code>0 <= k <= 10<sup>15</sup></code>
76+
* The length of `result` after processing `s` will not exceed <code>10<sup>15</sup></code>.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /