Skip to content
Merged
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_10_03_Time_2_ms_(100.00%)_Space_43.05_MB_(100.00%)

class Solution {
fun majorityFrequencyGroup(s: String): String {
val cntArray = IntArray(26)
for (i in 0..<s.length) {
cntArray[s[i].code - 'a'.code]++
}
val freq = IntArray(s.length + 1)
for (i in 0..25) {
if (cntArray[i] > 0) {
freq[cntArray[i]]++
}
}
var size = 0
var bfreq = 0
for (i in 0..s.length) {
val si = freq[i]
if (si > size || (si == size && i > bfreq)) {
size = si
bfreq = i
}
}
val sb = StringBuilder()
for (i in 0..25) {
if (cntArray[i] == bfreq) {
sb.append((i + 'a'.code).toChar())
}
}
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.
36 changes: 36 additions & 0 deletions src/main/kotlin/g3601_3700/s3693_climbing_stairs_ii/Solution.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package g3601_3700.s3693_climbing_stairs_ii

// #Medium #Biweekly_Contest_166 #2025_10_03_Time_8_ms_(100.00%)_Space_80.61_MB_(12.90%)

import kotlin.math.min

@Suppress("unused")
class Solution {
fun climbStairs(n: Int, costs: IntArray): Int {
if (costs.size == 1) {
return costs[0] + 1
}
var one = costs[0] + 1
var two = min(one + costs[1] + 1, costs[1] + 4)
if (costs.size < 3) {
return two
}
var three = min(one + costs[2] + 4, min(two + costs[2] + 1, costs[2] + 9))
if (costs.size < 4) {
return three
}
for (i in 3..<costs.size) {
val four =
(
min(
three + costs[i] + 1,
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/kotlin/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,32 @@
package g3601_3700.s3694_distinct_points_reachable_after_substring_removal

// #Medium #Biweekly_Contest_166 #2025_10_03_Time_46_ms_(100.00%)_Space_48.62_MB_(100.00%)

class Solution {
fun distinctPoints(s: String, k: Int): Int {
val seen: MutableSet<Long> = HashSet()
seen.add(0L)
var x = 0
var y = 0
for (i in k..<s.length) {
// add new step
when (s[i]) {
'U' -> y++
'D' -> y--
'L' -> x++
'R' -> x--
else -> x--
}
// remove old step
when (s[i - k]) {
'U' -> y--
'D' -> y++
'L' -> x--
'R' -> x++
else -> x++
}
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,59 @@
package g3601_3700.s3695_maximize_alternating_sum_using_swaps

// #Hard #Biweekly_Contest_166 #2025_10_03_Time_61_ms_(100.00%)_Space_105.29_MB_(100.00%)

class Solution {
private lateinit var root: IntArray

fun maxAlternatingSum(nums: IntArray, swaps: Array<IntArray>): Long {
val n = nums.size
root = IntArray(n) { it }
val list = Array(n) { ArrayList<Int>() }
val oddCount = IntArray(n)
for (s in swaps) {
union(s[0], s[1])
}
for (i in nums.indices) {
val r = findRoot(i)
list[r].add(nums[i])
if (i % 2 == 1) {
oddCount[r]++
}
}

var result = 0L
for (i in 0 until n) {
if (root[i] != i) {
continue
}
val currentList = list[i]
val currentOddCount = oddCount[i]
currentList.sort()
for (j in currentList.indices) {
val value = currentList[j].toLong()
val multiplier = if (j < currentOddCount) -1 else 1
result += value * multiplier
}
}
return result
}

private fun union(a: Int, b: Int) {
val rootA = findRoot(a)
val rootB = findRoot(b)
if (rootA != rootB) {
if (rootA < rootB) {
root[rootB] = rootA
} else {
root[rootA] = rootB
}
}
}

private fun findRoot(a: Int): Int {
if (a == root[a]) {
return a
}
return findRoot(root[a]).also { root[a] = it }
}
}
Loading