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 7c97dd3

Browse files
authored
Added tasks 3663-3671
1 parent b4f5451 commit 7c97dd3

File tree

25 files changed

+1107
-0
lines changed

25 files changed

+1107
-0
lines changed

‎gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
sonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml
22
org.gradle.jvmargs=-Xms512m -Xmx2048m
3+
org.gradle.configuration-cache=true
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3601_3700.s3663_find_the_least_frequent_digit
2+
3+
// #Easy #Biweekly_Contest_164 #2025_09_07_Time_1_ms_(96.30%)_Space_40.60_MB_(100.00%)
4+
5+
class Solution {
6+
fun getLeastFrequentDigit(n: Int): Int {
7+
val freq = IntArray(10)
8+
val numStr = n.toString()
9+
for (c in numStr.toCharArray()) {
10+
freq[c.code - '0'.code]++
11+
}
12+
var minFreq = Int.Companion.MAX_VALUE
13+
var result = -1
14+
for (d in 0..9) {
15+
if (freq[d] == 0) {
16+
continue
17+
}
18+
if (freq[d] < minFreq) {
19+
minFreq = freq[d]
20+
result = d
21+
} else if (freq[d] == minFreq && d < result) {
22+
result = d
23+
}
24+
}
25+
return result
26+
}
27+
}
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_09_07_Time_11_ms_(100.00%)_Space_69.41_MB_(100.00%)
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g3601_3700.s3665_twisted_mirror_path_count
2+
3+
// #Medium #Biweekly_Contest_164 #2025_09_07_Time_33_ms_(100.00%)_Space_113.52_MB_(72.73%)
4+
5+
class Solution {
6+
fun uniquePaths(grid: Array<IntArray>): Int {
7+
// 0 right, 1 down
8+
val n = grid.size
9+
val m = grid[0].size
10+
val mod = 1000000007
11+
var dp = IntArray(m)
12+
dp[0] = 1
13+
for (j in 1..<m) {
14+
if (grid[0][j - 1] == 0) {
15+
dp[j] = dp[j - 1]
16+
}
17+
}
18+
for (i in 1..<n) {
19+
val next = IntArray(m)
20+
if (grid[i - 1][0] == 0 && grid[i][0] == 0) {
21+
next[0] = dp[0]
22+
}
23+
for (j in 1..<m) {
24+
if (grid[i][j] == 0) {
25+
next[j] = (next[j] + dp[j]) % mod
26+
}
27+
if (grid[i][j - 1] == 0) {
28+
next[j] = (next[j] + next[j - 1]) % mod
29+
} else {
30+
next[j] = (next[j] + dp[j - 1]) % mod
31+
}
32+
}
33+
dp = next
34+
}
35+
return dp[m - 1]
36+
}
37+
}
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g3601_3700.s3666_minimum_operations_to_equalize_binary_string
2+
3+
// #Hard #Biweekly_Contest_164 #2025_09_07_Time_8_ms_(100.00%)_Space_46.70_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun minOperations(s: String, k: Int): Int {
9+
val n = s.length
10+
var cnt0 = 0
11+
for (c in s.toCharArray()) {
12+
if (c == '0') {
13+
cnt0++
14+
}
15+
}
16+
if (cnt0 == 0) {
17+
return 0
18+
}
19+
if (k == n) {
20+
return if (cnt0 == n) 1 else -1
21+
}
22+
val kP = k and 1
23+
val needP = cnt0 and 1
24+
var best = Long.Companion.MAX_VALUE
25+
for (p in 0..1) {
26+
if ((p * kP) % 2 != needP) {
27+
continue
28+
}
29+
val mismatch = (if (p == 0) cnt0 else (n - cnt0)).toLong()
30+
val b1 = (cnt0 + k - 1L) / k
31+
val b2: Long
32+
b2 = (mismatch + (n - k) - 1L) / (n - k)
33+
var lb = max(b1, b2)
34+
if (lb < 1) {
35+
lb = 1
36+
}
37+
if ((lb and 1L) != p.toLong()) {
38+
lb++
39+
}
40+
if (lb < best) {
41+
best = lb
42+
}
43+
}
44+
return if (best == Long.Companion.MAX_VALUE) -1 else best.toInt()
45+
}
46+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
3666\. Minimum Operations to Equalize Binary String
2+
3+
Hard
4+
5+
You are given a binary string `s`, and an integer `k`.
6+
7+
In one operation, you must choose **exactly** `k` **different** indices and **flip** each `'0'` to `'1'` and each `'1'` to `'0'`.
8+
9+
Return the **minimum** number of operations required to make all characters in the string equal to `'1'`. If it is not possible, return -1.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "110", k = 1
14+
15+
**Output:** 1
16+
17+
**Explanation:**
18+
19+
* There is one `'0'` in `s`.
20+
* Since `k = 1`, we can flip it directly in one operation.
21+
22+
**Example 2:**
23+
24+
**Input:** s = "0101", k = 3
25+
26+
**Output:** 2
27+
28+
**Explanation:**
29+
30+
One optimal set of operations choosing `k = 3` indices in each operation is:
31+
32+
* **Operation 1**: Flip indices `[0, 1, 3]`. `s` changes from `"0101"` to `"1000"`.
33+
* **Operation 2**: Flip indices `[1, 2, 3]`. `s` changes from `"1000"` to `"1111"`.
34+
35+
Thus, the minimum number of operations is 2.
36+
37+
**Example 3:**
38+
39+
**Input:** s = "101", k = 2
40+
41+
**Output:** \-1
42+
43+
**Explanation:**
44+
45+
Since `k = 2` and `s` has only one `'0'`, it is impossible to flip exactly `k` indices to make all `'1'`. Hence, the answer is -1.
46+
47+
**Constraints:**
48+
49+
* <code>1 <= s.length <= 10<sup>5</sup></code>
50+
* `s[i]` is either `'0'` or `'1'`.
51+
* `1 <= k <= s.length`

0 commit comments

Comments
(0)

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