Skip to content

Commit e417203

Browse files
committed
Added tasks 3663-3671
1 parent b4f5451 commit e417203

File tree

24 files changed

+1076
-0
lines changed

24 files changed

+1076
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3601_3700.s3663_find_the_least_frequent_digit
2+
3+
// #Easy #Biweekly_Contest_164 #2025_08_31_Time_3_ms_(100.00%)_Space_41.13_MB_(100.00%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun getLeastFrequentDigit(n: Int): Int {
9+
val s = n.toString()
10+
val k = s.length
11+
val freq: MutableMap<Int, Int> = HashMap<Int, Int>()
12+
for (i in 0..<k) {
13+
val digit = s.get(i).code - '0'.code
14+
freq.put(digit, freq.getOrDefault(digit, 0) + 1)
15+
}
16+
var minfreq = Int.Companion.MAX_VALUE
17+
for (it in freq.entries) {
18+
minfreq = min(minfreq, it.value)
19+
}
20+
var result = 10
21+
for (it in freq.entries) {
22+
if (it.value == minfreq) {
23+
result = min(result, it.key)
24+
}
25+
}
26+
return result
27+
}
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
3663\. Find The Least Frequent Digit
2+
3+
Easy
4+
5+
Given an integer `n`, find the digit that occurs **least** frequently in its decimal representation. If multiple digits have the same frequency, choose the **smallest** digit.
6+
7+
Return the chosen digit as an integer.
8+
9+
The **frequency** of a digit `x` is the number of times it appears in the decimal representation of `n`.
10+
11+
**Example 1:**
12+
13+
**Input:** n = 1553322
14+
15+
**Output:** 1
16+
17+
**Explanation:**
18+
19+
The least frequent digit in `n` is 1, which appears only once. All other digits appear twice.
20+
21+
**Example 2:**
22+
23+
**Input:** n = 723344511
24+
25+
**Output:** 2
26+
27+
**Explanation:**
28+
29+
The least frequent digits in `n` are 7, 2, and 5; each appears only once.
30+
31+
**Constraints:**
32+
33+
* <code>1 <= n <= 2<sup>31</sup> - 1</code>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package g3601_3700.s3664_two_letter_card_game
2+
3+
// #Medium #Biweekly_Contest_164 #2025_08_31_Time_11_ms_(100.00%)_Space_82.34_MB_(_%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun score(cards: Array<String>, x: Char): Int {
9+
// store input midway as required
10+
// counts for "x?" group by second char and "?x" group by first char
11+
val left = IntArray(10)
12+
val right = IntArray(10)
13+
var xx = 0
14+
for (c in cards) {
15+
val a = c[0]
16+
val b = c[1]
17+
if (a == x && b == x) {
18+
xx++
19+
} else if (a == x) {
20+
left[b.code - 'a'.code]++
21+
} else if (b == x) {
22+
right[a.code - 'a'.code]++
23+
}
24+
}
25+
// max pairs inside a group where pairs must come from different buckets:
26+
// pairs = min(total/2, total - maxBucket)
27+
var l = 0
28+
var maxL = 0
29+
for (v in left) {
30+
l += v
31+
if (v > maxL) {
32+
maxL = v
33+
}
34+
}
35+
var r = 0
36+
var maxR = 0
37+
for (v in right) {
38+
r += v
39+
if (v > maxR) {
40+
maxR = v
41+
}
42+
}
43+
val pairsLeft = min(l / 2, l - maxL)
44+
val pairsRight = min(r / 2, r - maxR)
45+
// leftovers after internal pairing
46+
val leftoverL = l - 2 * pairsLeft
47+
val leftoverR = r - 2 * pairsRight
48+
val leftovers = leftoverL + leftoverR
49+
// First, use "xx" to pair with any leftovers
50+
val useWithXX = min(xx, leftovers)
51+
val xxLeft = xx - useWithXX
52+
// If "xx" still remain, we can break existing internal pairs:
53+
// breaking 1 internal pair frees 2 cards, which can pair with 2 "xx" to gain +1 net point
54+
val extraByBreaking = min(xxLeft / 2, pairsLeft + pairsRight)
55+
return pairsLeft + pairsRight + useWithXX + extraByBreaking
56+
}
57+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
3664\. Two-Letter Card Game
2+
3+
Medium
4+
5+
You are given a deck of cards represented by a string array `cards`, and each card displays two lowercase letters.
6+
7+
You are also given a letter `x`. You play a game with the following rules:
8+
9+
* Start with 0 points.
10+
* On each turn, you must find two **compatible** cards from the deck that both contain the letter `x` in any position.
11+
* Remove the pair of cards and earn **1 point**.
12+
* The game ends when you can no longer find a pair of compatible cards.
13+
14+
Return the **maximum** number of points you can gain with optimal play.
15+
16+
Two cards are **compatible** if the strings differ in **exactly** 1 position.
17+
18+
**Example 1:**
19+
20+
**Input:** cards = ["aa","ab","ba","ac"], x = "a"
21+
22+
**Output:** 2
23+
24+
**Explanation:**
25+
26+
* On the first turn, select and remove cards `"ab"` and `"ac"`, which are compatible because they differ at only index 1.
27+
* On the second turn, select and remove cards `"aa"` and `"ba"`, which are compatible because they differ at only index 0.
28+
29+
Because there are no more compatible pairs, the total score is 2.
30+
31+
**Example 2:**
32+
33+
**Input:** cards = ["aa","ab","ba"], x = "a"
34+
35+
**Output:** 1
36+
37+
**Explanation:**
38+
39+
* On the first turn, select and remove cards `"aa"` and `"ba"`.
40+
41+
Because there are no more compatible pairs, the total score is 1.
42+
43+
**Example 3:**
44+
45+
**Input:** cards = ["aa","ab","ba","ac"], x = "b"
46+
47+
**Output:** 0
48+
49+
**Explanation:**
50+
51+
The only cards that contain the character `'b'` are `"ab"` and `"ba"`. However, they differ in both indices, so they are not compatible. Thus, the output is 0.
52+
53+
**Constraints:**
54+
55+
* <code>2 <= cards.length <= 10<sup>5</sup></code>
56+
* `cards[i].length == 2`
57+
* Each `cards[i]` is composed of only lowercase English letters between `'a'` and `'j'`.
58+
* `x` is a lowercase English letter between `'a'` and `'j'`.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g3601_3700.s3665_twisted_mirror_path_count
2+
3+
// #Medium #Biweekly_Contest_164 #2025_08_31_Time_114_ms_(100.00%)_Space_120.02_MB_(100.00%)
4+
5+
class Solution {
6+
fun uniquePaths(grid: Array<IntArray>): Int {
7+
val n = grid.size
8+
val m = grid[0].size
9+
val dp = Array<Array<IntArray>>(n) { Array<IntArray>(m) { IntArray(2) } }
10+
for (i in 0..<n) {
11+
for (j in 0..<m) {
12+
dp[i][j].fill(-1)
13+
}
14+
}
15+
return f(0, 0, 0, grid, n, m, dp)
16+
}
17+
18+
private fun f(i: Int, j: Int, dir: Int, grid: Array<IntArray>, n: Int, m: Int, dp: Array<Array<IntArray>>): Int {
19+
if (i == n - 1 && j == m - 1) {
20+
return 1
21+
}
22+
if (i >= n || j >= m) {
23+
return 0
24+
}
25+
if (dp[i][j][dir] != -1) {
26+
return dp[i][j][dir]
27+
}
28+
var ways: Long = 0
29+
if (grid[i][j] == 1) {
30+
ways = if (dir == 0) {
31+
f(i + 1, j, 1, grid, n, m, dp).toLong()
32+
} else {
33+
f(i, j + 1, 0, grid, n, m, dp).toLong()
34+
}
35+
} else {
36+
ways += f(i + 1, j, 1, grid, n, m, dp).toLong()
37+
ways += f(i, j + 1, 0, grid, n, m, dp).toLong()
38+
}
39+
dp[i][j][dir] = ways.toInt() % MOD
40+
return dp[i][j][dir]
41+
}
42+
43+
companion object {
44+
private const val MOD = 1000000007
45+
}
46+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
3665\. Twisted Mirror Path Count
2+
3+
Medium
4+
5+
Given an `m x n` binary grid `grid` where:
6+
7+
* `grid[i][j] == 0` represents an empty cell, and
8+
* `grid[i][j] == 1` represents a mirror.
9+
10+
A robot starts at the top-left corner of the grid `(0, 0)` and wants to reach the bottom-right corner `(m - 1, n - 1)`. It can move only **right** or **down**. If the robot attempts to move into a mirror cell, it is **reflected** before entering that cell:
11+
12+
* If it tries to move **right** into a mirror, it is turned **down** and moved into the cell directly below the mirror.
13+
* If it tries to move **down** into a mirror, it is turned **right** and moved into the cell directly to the right of the mirror.
14+
15+
If this reflection would cause the robot to move outside the `grid` boundaries, the path is considered invalid and should not be counted.
16+
17+
Return the number of unique valid paths from `(0, 0)` to `(m - 1, n - 1)`.
18+
19+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
20+
21+
**Note**: If a reflection moves the robot into a mirror cell, the robot is immediately reflected again based on the direction it used to enter that mirror: if it entered while moving right, it will be turned down; if it entered while moving down, it will be turned right. This process will continue until either the last cell is reached, the robot moves out of bounds or the robot moves to a non-mirror cell.
22+
23+
**Example 1:**
24+
25+
**Input:** grid = [[0,1,0],[0,0,1],[1,0,0]]
26+
27+
**Output:** 5
28+
29+
**Explanation:**
30+
31+
| Number | Full Path |
32+
|--------|---------------------------------------------------------------------|
33+
| 1 | (0, 0) → (0, 1) [M] → (1, 1) → (1, 2) [M] → (2, 2) |
34+
| 2 | (0, 0) → (0, 1) [M] → (1, 1) → (2, 1) → (2, 2) |
35+
| 3 | (0, 0) → (1, 0) → (1, 1) → (1, 2) [M] → (2, 2) |
36+
| 4 | (0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) |
37+
| 5 | (0, 0) → (1, 0) → (2, 0) [M] → (2, 1) → (2, 2) |
38+
39+
* `[M]` indicates the robot attempted to enter a mirror cell and instead reflected.
40+
41+
42+
**Example 2:**
43+
44+
**Input:** grid = [[0,0],[0,0]]
45+
46+
**Output:** 2
47+
48+
**Explanation:**
49+
50+
| Number | Full Path |
51+
|--------|-----------------------------|
52+
| 1 | (0, 0) → (0, 1) → (1, 1) |
53+
| 2 | (0, 0) → (1, 0) → (1, 1) |
54+
55+
**Example 3:**
56+
57+
**Input:** grid = [[0,1,1],[1,1,0]]
58+
59+
**Output:** 1
60+
61+
**Explanation:**
62+
63+
| Number | Full Path |
64+
|--------|-------------------------------------------|
65+
| 1 | (0, 0) → (0, 1) [M] → (1, 1) [M] → (1, 2) |
66+
67+
`(0, 0) → (1, 0) [M] → (1, 1) [M] → (2, 1)` goes out of bounds, so it is invalid.
68+
69+
**Constraints:**
70+
71+
* `m == grid.length`
72+
* `n == grid[i].length`
73+
* `2 <= m, n <= 500`
74+
* `grid[i][j]` is either `0` or `1`.
75+
* `grid[0][0] == grid[m - 1][n - 1] == 0`
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package g3601_3700.s3666_minimum_operations_to_equalize_binary_string
2+
3+
// #Hard #Biweekly_Contest_164 #2025_08_31_Time_29_ms_(100.00%)_Space_48.47_MB_(100.00%)
4+
5+
import java.util.ArrayDeque
6+
import java.util.Queue
7+
import kotlin.math.max
8+
import kotlin.math.min
9+
10+
class Solution {
11+
fun minOperations(s: String, k: Int): Int {
12+
val zeros = s.chars().map { x: Int -> if (x == '0'.code) 1 else 0 }.sum()
13+
if ((zeros % k) == 0) {
14+
return zeros / k
15+
}
16+
val n = s.length
17+
val q: Queue<Int> = ArrayDeque<Int>()
18+
q.add(zeros)
19+
var res = 1
20+
// use bounds for optimization
21+
val bounds = Array<IntArray>(2) { IntArray(2) }
22+
bounds[zeros and 1][1] = zeros
23+
bounds[zeros and 1][0] = bounds[zeros and 1][1]
24+
bounds[1 - (zeros and 1)][0] = Int.Companion.MAX_VALUE
25+
bounds[1 - (zeros and 1)][1] = Int.Companion.MIN_VALUE
26+
while (!q.isEmpty()) {
27+
// find min number of zeros and max number of zeros in this round
28+
var minv = Int.Companion.MAX_VALUE
29+
var maxv = Int.Companion.MIN_VALUE
30+
for (len in q.size downTo 1) {
31+
val h: Int = q.poll()
32+
val t = n - h
33+
var x = min(h, k)
34+
if (t >= k - x) {
35+
val fst = h - x + (k - x)
36+
minv = min(minv, fst)
37+
maxv = max(maxv, fst)
38+
}
39+
x = min(t, k)
40+
if (h >= k - x) {
41+
val snd = h - (k - x) + x
42+
minv = min(minv, snd)
43+
maxv = max(maxv, snd)
44+
}
45+
}
46+
// possible children are sequence of equal difference 2
47+
val ind = minv and 1
48+
var temp = minv
49+
while (temp <= maxv) {
50+
if ((temp % k) == 0) {
51+
return res + temp / k
52+
}
53+
if (temp < bounds[ind][0] || temp > bounds[ind][1]) {
54+
q.add(temp)
55+
temp += 2
56+
} else {
57+
temp = bounds[ind][1] + 2
58+
}
59+
}
60+
bounds[ind][0] = min(bounds[ind][0], minv)
61+
bounds[ind][1] = max(bounds[ind][1], maxv)
62+
res++
63+
}
64+
return -1
65+
}
66+
}

0 commit comments

Comments
 (0)