Skip to content

Commit ed03e05

Browse files
authored
Added tasks 3692-3700
1 parent 95a295b commit ed03e05

File tree

24 files changed

+1220
-0
lines changed

24 files changed

+1220
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g3601_3700.s3692_majority_frequency_characters;
2+
3+
// #Easy #Biweekly_Contest_166 #2025_09_28_Time_1_ms_(100.00%)_Space_43.06_MB_(100.00%)
4+
5+
public class Solution {
6+
public String majorityFrequencyGroup(String s) {
7+
int[] cntArray = new int[26];
8+
for (int i = 0; i < s.length(); i++) {
9+
cntArray[s.charAt(i) - 'a']++;
10+
}
11+
int[] freq = new int[s.length() + 1];
12+
for (int i = 0; i < 26; i++) {
13+
if (cntArray[i] > 0) {
14+
freq[cntArray[i]]++;
15+
}
16+
}
17+
int size = 0;
18+
int bfreq = 0;
19+
for (int i = 0; i <= s.length(); i++) {
20+
int si = freq[i];
21+
if (si > size || (si == size && i > bfreq)) {
22+
size = si;
23+
bfreq = i;
24+
}
25+
}
26+
StringBuilder sb = new StringBuilder();
27+
for (int i = 0; i < 26; i++) {
28+
if (cntArray[i] == bfreq) {
29+
sb.append((char) (i + 'a'));
30+
}
31+
}
32+
return sb.toString();
33+
}
34+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
3692\. Majority Frequency Characters
2+
3+
Easy
4+
5+
You are given a string `s` consisting of lowercase English letters.
6+
7+
The **frequency group** for a value `k` is the set of characters that appear exactly `k` times in s.
8+
9+
The **majority frequency group** is the frequency group that contains the largest number of **distinct** characters.
10+
11+
Return a string containing all characters in the majority frequency group, in **any** order. If two or more frequency groups tie for that largest size, pick the group whose frequency `k` is **larger**.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "aaabbbccdddde"
16+
17+
**Output:** "ab"
18+
19+
**Explanation:**
20+
21+
| Frequency (k) | Distinct characters in group | Group size | Majority? |
22+
|---------------|------------------------------|------------|-----------|
23+
| 4 | {d} | 1 | No |
24+
| 3 | {a, b} | 2 | **Yes** |
25+
| 2 | {c} | 1 | No |
26+
| 1 | {e} | 1 | No |
27+
28+
Both characters `'a'` and `'b'` share the same frequency 3, they are in the majority frequency group. `"ba"` is also a valid answer.
29+
30+
**Example 2:**
31+
32+
**Input:** s = "abcd"
33+
34+
**Output:** "abcd"
35+
36+
**Explanation:**
37+
38+
| Frequency (k) | Distinct characters in group | Group size | Majority? |
39+
|---------------|------------------------------|------------|-----------|
40+
| 1 | {a, b, c, d} | 4 | **Yes** |
41+
42+
All characters share the same frequency 1, they are all in the majority frequency group.
43+
44+
**Example 3:**
45+
46+
**Input:** s = "pfpfgi"
47+
48+
**Output:** "fp"
49+
50+
**Explanation:**
51+
52+
| Frequency (k) | Distinct characters in group | Group size | Majority? |
53+
|---------------|------------------------------|------------|----------------------------------|
54+
| 2 | {p, f} | 2 | **Yes** |
55+
| 1 | {g, i} | 2 | No (tied size, lower frequency) |
56+
57+
Both characters `'p'` and `'f'` share the same frequency 2, they are in the majority frequency group. There is a tie in group size with frequency 1, but we pick the higher frequency: 2.
58+
59+
**Constraints:**
60+
61+
* `1 <= s.length <= 100`
62+
* `s` consists only of lowercase English letters.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g3601_3700.s3693_climbing_stairs_ii;
2+
3+
// #Medium #Biweekly_Contest_166 #2025_09_28_Time_2_ms_(100.00%)_Space_57.06_MB_(100.00%)
4+
5+
@SuppressWarnings({"unused", "java:S1172"})
6+
public class Solution {
7+
public int climbStairs(int n, int[] costs) {
8+
if (costs.length == 1) {
9+
return costs[0] + 1;
10+
}
11+
int one = costs[0] + 1;
12+
int two = Math.min(one + costs[1] + 1, costs[1] + 4);
13+
if (costs.length < 3) {
14+
return two;
15+
}
16+
int three = Math.min(one + costs[2] + 4, Math.min(two + costs[2] + 1, costs[2] + 9));
17+
if (costs.length < 4) {
18+
return three;
19+
}
20+
for (int i = 3; i < costs.length; i++) {
21+
int four =
22+
(Math.min(
23+
three + costs[i] + 1,
24+
Math.min(two + costs[i] + 4, one + costs[i] + 9)));
25+
one = two;
26+
two = three;
27+
three = four;
28+
}
29+
return three;
30+
}
31+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
3693\. Climbing Stairs II
2+
3+
Medium
4+
5+
You are climbing a staircase with `n + 1` steps, numbered from 0 to `n`.
6+
7+
You are also given a **1-indexed** integer array `costs` of length `n`, where `costs[i]` is the cost of step `i`.
8+
9+
From step `i`, you can jump **only** to step `i + 1`, `i + 2`, or `i + 3`. The cost of jumping from step `i` to step `j` is defined as: <code>costs[j] + (j - i)<sup>2</sup></code>
10+
11+
You start from step 0 with `cost = 0`.
12+
13+
Return the **minimum** total cost to reach step `n`.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 4, costs = [1,2,3,4]
18+
19+
**Output:** 13
20+
21+
**Explanation:**
22+
23+
One optimal path is `0 → 1 → 2 → 4`
24+
25+
Jump
26+
27+
Cost Calculation
28+
29+
Cost
30+
31+
0 → 1
32+
33+
<code>costs[1] + (1 - 0)<sup>2</sup> = 1 + 1</code>
34+
35+
2
36+
37+
1 → 2
38+
39+
<code>costs[2] + (2 - 1)<sup>2</sup> = 2 + 1</code>
40+
41+
3
42+
43+
2 → 4
44+
45+
<code>costs[4] + (4 - 2)<sup>2</sup> = 4 + 4</code>
46+
47+
8
48+
49+
Thus, the minimum total cost is `2 + 3 + 8 = 13`
50+
51+
**Example 2:**
52+
53+
**Input:** n = 4, costs = [5,1,6,2]
54+
55+
**Output:** 11
56+
57+
**Explanation:**
58+
59+
One optimal path is `0 → 2 → 4`
60+
61+
Jump
62+
63+
Cost Calculation
64+
65+
Cost
66+
67+
0 → 2
68+
69+
<code>costs[2] + (2 - 0)<sup>2</sup> = 1 + 4</code>
70+
71+
5
72+
73+
2 → 4
74+
75+
<code>costs[4] + (4 - 2)<sup>2</sup> = 2 + 4</code>
76+
77+
6
78+
79+
Thus, the minimum total cost is `5 + 6 = 11`
80+
81+
**Example 3:**
82+
83+
**Input:** n = 3, costs = [9,8,3]
84+
85+
**Output:** 12
86+
87+
**Explanation:**
88+
89+
The optimal path is `0 → 3` with total cost = <code>costs[3] + (3 - 0)<sup>2</sup> = 3 + 9 = 12</code>
90+
91+
**Constraints:**
92+
93+
* <code>1 <= n == costs.length <= 10<sup>5</sup></code>
94+
* <code>1 <= costs[i] <= 10<sup>4</sup></code>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g3601_3700.s3694_distinct_points_reachable_after_substring_removal;
2+
3+
// #Medium #Biweekly_Contest_166 #2025_09_28_Time_37_ms_(100.00%)_Space_46.42_MB_(100.00%)
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class Solution {
9+
public int distinctPoints(String s, int k) {
10+
Set<Long> seen = new HashSet<>();
11+
seen.add(0L);
12+
int x = 0;
13+
int y = 0;
14+
for (int i = k; i < s.length(); i++) {
15+
// add new step
16+
switch (s.charAt(i)) {
17+
case 'U':
18+
y++;
19+
break;
20+
case 'D':
21+
y--;
22+
break;
23+
case 'L':
24+
x++;
25+
break;
26+
case 'R':
27+
default:
28+
x--;
29+
break;
30+
}
31+
// remove old step
32+
switch (s.charAt(i - k)) {
33+
case 'U':
34+
y--;
35+
break;
36+
case 'D':
37+
y++;
38+
break;
39+
case 'L':
40+
x--;
41+
break;
42+
case 'R':
43+
default:
44+
x++;
45+
break;
46+
}
47+
seen.add(1000000L * x + y);
48+
}
49+
return seen.size();
50+
}
51+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
3694\. Distinct Points Reachable After Substring Removal
2+
3+
Medium
4+
5+
You are given a string `s` consisting of characters `'U'`, `'D'`, `'L'`, and `'R'`, representing moves on an infinite 2D Cartesian grid.
6+
7+
* `'U'`: Move from `(x, y)` to `(x, y + 1)`.
8+
* `'D'`: Move from `(x, y)` to `(x, y - 1)`.
9+
* `'L'`: Move from `(x, y)` to `(x - 1, y)`.
10+
* `'R'`: Move from `(x, y)` to `(x + 1, y)`.
11+
12+
You are also given a positive integer `k`.
13+
14+
You **must** choose and remove **exactly one** contiguous substring of length `k` from `s`. Then, start from coordinate `(0, 0)` and perform the remaining moves in order.
15+
16+
Return an integer denoting the number of **distinct** final coordinates reachable.
17+
18+
**Example 1:**
19+
20+
**Input:** s = "LUL", k = 1
21+
22+
**Output:** 2
23+
24+
**Explanation:**
25+
26+
After removing a substring of length 1, `s` can be `"UL"`, `"LL"` or `"LU"`. Following these moves, the final coordinates will be `(-1, 1)`, `(-2, 0)` and `(-1, 1)` respectively. There are two distinct points `(-1, 1)` and `(-2, 0)` so the answer is 2.
27+
28+
**Example 2:**
29+
30+
**Input:** s = "UDLR", k = 4
31+
32+
**Output:** 1
33+
34+
**Explanation:**
35+
36+
After removing a substring of length 4, `s` can only be the empty string. The final coordinates will be `(0, 0)`. There is only one distinct point `(0, 0)` so the answer is 1.
37+
38+
**Example 3:**
39+
40+
**Input:** s = "UU", k = 1
41+
42+
**Output:** 1
43+
44+
**Explanation:**
45+
46+
After removing a substring of length 1, `s` becomes `"U"`, which always ends at `(0, 1)`, so there is only one distinct final coordinate.
47+
48+
**Constraints:**
49+
50+
* <code>1 <= s.length <= 10<sup>5</sup></code>
51+
* `s` consists of only `'U'`, `'D'`, `'L'`, and `'R'`.
52+
* `1 <= k <= s.length`
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package g3601_3700.s3695_maximize_alternating_sum_using_swaps;
2+
3+
// #Hard #Biweekly_Contest_166 #2025_09_28_Time_27_ms_(99.82%)_Space_106.96_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
@SuppressWarnings("unchecked")
9+
public class Solution {
10+
private int[] root;
11+
12+
public long maxAlternatingSum(int[] nums, int[][] swaps) {
13+
int n = nums.length;
14+
root = new int[n];
15+
List<Integer>[] list = new ArrayList[n];
16+
int[] oddCount = new int[n];
17+
for (int i = 0; i < n; i++) {
18+
root[i] = i;
19+
list[i] = new ArrayList<>();
20+
}
21+
for (int[] s : swaps) {
22+
union(s[0], s[1]);
23+
}
24+
for (int i = 0; i < n; i++) {
25+
int r = findRoot(i);
26+
list[r].add(nums[i]);
27+
if (i % 2 == 1) {
28+
oddCount[r]++;
29+
}
30+
}
31+
long result = 0;
32+
for (int i = 0; i < n; i++) {
33+
int r = root[i];
34+
if (r != i) {
35+
continue;
36+
}
37+
list[i].sort((a, b) -> a - b);
38+
for (int j = 0; j < list[i].size(); j++) {
39+
result += (long) list[i].get(j) * (j < oddCount[r] ? -1 : 1);
40+
}
41+
}
42+
return result;
43+
}
44+
45+
private void union(int a, int b) {
46+
a = findRoot(a);
47+
b = findRoot(b);
48+
if (a == b) {
49+
return;
50+
}
51+
if (a < b) {
52+
root[b] = a;
53+
} else {
54+
root[a] = b;
55+
}
56+
}
57+
58+
private int findRoot(int a) {
59+
if (a == root[a]) {
60+
return a;
61+
}
62+
root[a] = findRoot(root[a]);
63+
return root[a];
64+
}
65+
}

0 commit comments

Comments
 (0)