Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package g3601_3700.s3692_majority_frequency_characters;

// #Easy #Biweekly_Contest_166 #2025_09_28_Time_1_ms_(100.00%)_Space_43.06_MB_(100.00%)

public class Solution {
public String majorityFrequencyGroup(String s) {
int[] cntArray = new int[26];
for (int i = 0; i < s.length(); i++) {
cntArray[s.charAt(i) - 'a']++;
}
int[] freq = new int[s.length() + 1];
for (int i = 0; i < 26; i++) {
if (cntArray[i] > 0) {
freq[cntArray[i]]++;
}
}
int size = 0;
int bfreq = 0;
for (int i = 0; i <= s.length(); i++) {
int si = freq[i];
if (si > size || (si == size && i > bfreq)) {
size = si;
bfreq = i;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 26; i++) {
if (cntArray[i] == bfreq) {
sb.append((char) (i + 'a'));
}
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
3692\. Majority Frequency Characters

Easy

You are given a string `s` consisting of lowercase English letters.

The **frequency group** for a value `k` is the set of characters that appear exactly `k` times in s.

The **majority frequency group** is the frequency group that contains the largest number of **distinct** characters.

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**.

**Example 1:**

**Input:** s = "aaabbbccdddde"

**Output:** "ab"

**Explanation:**

| Frequency (k) | Distinct characters in group | Group size | Majority? |
|---------------|------------------------------|------------|-----------|
| 4 | {d} | 1 | No |
| 3 | {a, b} | 2 | **Yes** |
| 2 | {c} | 1 | No |
| 1 | {e} | 1 | No |

Both characters `'a'` and `'b'` share the same frequency 3, they are in the majority frequency group. `"ba"` is also a valid answer.

**Example 2:**

**Input:** s = "abcd"

**Output:** "abcd"

**Explanation:**

| Frequency (k) | Distinct characters in group | Group size | Majority? |
|---------------|------------------------------|------------|-----------|
| 1 | {a, b, c, d} | 4 | **Yes** |

All characters share the same frequency 1, they are all in the majority frequency group.

**Example 3:**

**Input:** s = "pfpfgi"

**Output:** "fp"

**Explanation:**

| Frequency (k) | Distinct characters in group | Group size | Majority? |
|---------------|------------------------------|------------|----------------------------------|
| 2 | {p, f} | 2 | **Yes** |
| 1 | {g, i} | 2 | No (tied size, lower frequency) |

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.

**Constraints:**

* `1 <= s.length <= 100`
* `s` consists only of lowercase English letters.
31 changes: 31 additions & 0 deletions src/main/java/g3601_3700/s3693_climbing_stairs_ii/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package g3601_3700.s3693_climbing_stairs_ii;

// #Medium #Biweekly_Contest_166 #2025_09_28_Time_2_ms_(100.00%)_Space_57.06_MB_(100.00%)

@SuppressWarnings({"unused", "java:S1172"})
public class Solution {
public int climbStairs(int n, int[] costs) {
if (costs.length == 1) {
return costs[0] + 1;
}
int one = costs[0] + 1;
int two = Math.min(one + costs[1] + 1, costs[1] + 4);
if (costs.length < 3) {
return two;
}
int three = Math.min(one + costs[2] + 4, Math.min(two + costs[2] + 1, costs[2] + 9));
if (costs.length < 4) {
return three;
}
for (int i = 3; i < costs.length; i++) {
int four =
(Math.min(
three + costs[i] + 1,
Math.min(two + costs[i] + 4, one + costs[i] + 9)));
one = two;
two = three;
three = four;
}
return three;
}
}
94 changes: 94 additions & 0 deletions src/main/java/g3601_3700/s3693_climbing_stairs_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
3693\. Climbing Stairs II

Medium

You are climbing a staircase with `n + 1` steps, numbered from 0 to `n`.

You are also given a **1-indexed** integer array `costs` of length `n`, where `costs[i]` is the cost of step `i`.

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>

You start from step 0 with `cost = 0`.

Return the **minimum** total cost to reach step `n`.

**Example 1:**

**Input:** n = 4, costs = [1,2,3,4]

**Output:** 13

**Explanation:**

One optimal path is `0 → 1 → 2 → 4`

Jump

Cost Calculation

Cost

0 → 1

<code>costs[1] + (1 - 0)<sup>2</sup> = 1 + 1</code>

2

1 → 2

<code>costs[2] + (2 - 1)<sup>2</sup> = 2 + 1</code>

3

2 → 4

<code>costs[4] + (4 - 2)<sup>2</sup> = 4 + 4</code>

8

Thus, the minimum total cost is `2 + 3 + 8 = 13`

**Example 2:**

**Input:** n = 4, costs = [5,1,6,2]

**Output:** 11

**Explanation:**

One optimal path is `0 → 2 → 4`

Jump

Cost Calculation

Cost

0 → 2

<code>costs[2] + (2 - 0)<sup>2</sup> = 1 + 4</code>

5

2 → 4

<code>costs[4] + (4 - 2)<sup>2</sup> = 2 + 4</code>

6

Thus, the minimum total cost is `5 + 6 = 11`

**Example 3:**

**Input:** n = 3, costs = [9,8,3]

**Output:** 12

**Explanation:**

The optimal path is `0 → 3` with total cost = <code>costs[3] + (3 - 0)<sup>2</sup> = 3 + 9 = 12</code>

**Constraints:**

* <code>1 <= n == costs.length <= 10<sup>5</sup></code>
* <code>1 <= costs[i] <= 10<sup>4</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package g3601_3700.s3694_distinct_points_reachable_after_substring_removal;

// #Medium #Biweekly_Contest_166 #2025_09_28_Time_37_ms_(100.00%)_Space_46.42_MB_(100.00%)

import java.util.HashSet;
import java.util.Set;

public class Solution {
public int distinctPoints(String s, int k) {
Set<Long> seen = new HashSet<>();
seen.add(0L);
int x = 0;
int y = 0;
for (int i = k; i < s.length(); i++) {
// add new step
switch (s.charAt(i)) {
case 'U':
y++;
break;
case 'D':
y--;
break;
case 'L':
x++;
break;
case 'R':
default:
x--;
break;
}
// remove old step
switch (s.charAt(i - k)) {
case 'U':
y--;
break;
case 'D':
y++;
break;
case 'L':
x--;
break;
case 'R':
default:
x++;
break;
}
seen.add(1000000L * x + y);
}
return seen.size();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
3694\. Distinct Points Reachable After Substring Removal

Medium

You are given a string `s` consisting of characters `'U'`, `'D'`, `'L'`, and `'R'`, representing moves on an infinite 2D Cartesian grid.

* `'U'`: Move from `(x, y)` to `(x, y + 1)`.
* `'D'`: Move from `(x, y)` to `(x, y - 1)`.
* `'L'`: Move from `(x, y)` to `(x - 1, y)`.
* `'R'`: Move from `(x, y)` to `(x + 1, y)`.

You are also given a positive integer `k`.

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.

Return an integer denoting the number of **distinct** final coordinates reachable.

**Example 1:**

**Input:** s = "LUL", k = 1

**Output:** 2

**Explanation:**

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.

**Example 2:**

**Input:** s = "UDLR", k = 4

**Output:** 1

**Explanation:**

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.

**Example 3:**

**Input:** s = "UU", k = 1

**Output:** 1

**Explanation:**

After removing a substring of length 1, `s` becomes `"U"`, which always ends at `(0, 1)`, so there is only one distinct final coordinate.

**Constraints:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s` consists of only `'U'`, `'D'`, `'L'`, and `'R'`.
* `1 <= k <= s.length`
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package g3601_3700.s3695_maximize_alternating_sum_using_swaps;

// #Hard #Biweekly_Contest_166 #2025_09_28_Time_27_ms_(99.82%)_Space_106.96_MB_(100.00%)

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("unchecked")
public class Solution {
private int[] root;

public long maxAlternatingSum(int[] nums, int[][] swaps) {
int n = nums.length;
root = new int[n];
List<Integer>[] list = new ArrayList[n];
int[] oddCount = new int[n];
for (int i = 0; i < n; i++) {
root[i] = i;
list[i] = new ArrayList<>();
}
for (int[] s : swaps) {
union(s[0], s[1]);
}
for (int i = 0; i < n; i++) {
int r = findRoot(i);
list[r].add(nums[i]);
if (i % 2 == 1) {
oddCount[r]++;
}
}
long result = 0;
for (int i = 0; i < n; i++) {
int r = root[i];
if (r != i) {
continue;
}
list[i].sort((a, b) -> a - b);
for (int j = 0; j < list[i].size(); j++) {
result += (long) list[i].get(j) * (j < oddCount[r] ? -1 : 1);
}
}
return result;
}

private void union(int a, int b) {
a = findRoot(a);
b = findRoot(b);
if (a == b) {
return;
}
if (a < b) {
root[b] = a;
} else {
root[a] = b;
}
}

private int findRoot(int a) {
if (a == root[a]) {
return a;
}
root[a] = findRoot(root[a]);
return root[a];
}
}
Loading