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

Added tasks 3663-3671 #877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
javadev merged 5 commits into javadev:main from jscrdev:tasks-3663-3671
Sep 7, 2025
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
1 change: 1 addition & 0 deletions gradle.properties
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
sonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml
org.gradle.jvmargs=-Xms512m -Xmx2048m
org.gradle.configuration-cache=true
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package g3601_3700.s3663_find_the_least_frequent_digit

// #Easy #Biweekly_Contest_164 #2025_09_07_Time_1_ms_(96.30%)_Space_40.60_MB_(100.00%)

class Solution {
fun getLeastFrequentDigit(n: Int): Int {
val freq = IntArray(10)
val numStr = n.toString()
for (c in numStr.toCharArray()) {
freq[c.code - '0'.code]++
}
var minFreq = Int.Companion.MAX_VALUE
var result = -1
for (d in 0..9) {
if (freq[d] == 0) {
continue
}
if (freq[d] < minFreq) {
minFreq = freq[d]
result = d
} else if (freq[d] == minFreq && d < result) {
result = d
}
}
return result
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
3663\. Find The Least Frequent Digit

Easy

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.

Return the chosen digit as an integer.

The **frequency** of a digit `x` is the number of times it appears in the decimal representation of `n`.

**Example 1:**

**Input:** n = 1553322

**Output:** 1

**Explanation:**

The least frequent digit in `n` is 1, which appears only once. All other digits appear twice.

**Example 2:**

**Input:** n = 723344511

**Output:** 2

**Explanation:**

The least frequent digits in `n` are 7, 2, and 5; each appears only once.

**Constraints:**

* <code>1 <= n <= 2<sup>31</sup> - 1</code>
57 changes: 57 additions & 0 deletions src/main/kotlin/g3601_3700/s3664_two_letter_card_game/Solution.kt
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package g3601_3700.s3664_two_letter_card_game

// #Medium #Biweekly_Contest_164 #2025_09_07_Time_11_ms_(100.00%)_Space_69.41_MB_(100.00%)

import kotlin.math.min

class Solution {
fun score(cards: Array<String>, x: Char): Int {
// store input midway as required
// counts for "x?" group by second char and "?x" group by first char
val left = IntArray(10)
val right = IntArray(10)
var xx = 0
for (c in cards) {
val a = c[0]
val b = c[1]
if (a == x && b == x) {
xx++
} else if (a == x) {
left[b.code - 'a'.code]++
} else if (b == x) {
right[a.code - 'a'.code]++
}
}
// max pairs inside a group where pairs must come from different buckets:
// pairs = min(total/2, total - maxBucket)
var l = 0
var maxL = 0
for (v in left) {
l += v
if (v > maxL) {
maxL = v
}
}
var r = 0
var maxR = 0
for (v in right) {
r += v
if (v > maxR) {
maxR = v
}
}
val pairsLeft = min(l / 2, l - maxL)
val pairsRight = min(r / 2, r - maxR)
// leftovers after internal pairing
val leftoverL = l - 2 * pairsLeft
val leftoverR = r - 2 * pairsRight
val leftovers = leftoverL + leftoverR
// First, use "xx" to pair with any leftovers
val useWithXX = min(xx, leftovers)
val xxLeft = xx - useWithXX
// If "xx" still remain, we can break existing internal pairs:
// breaking 1 internal pair frees 2 cards, which can pair with 2 "xx" to gain +1 net point
val extraByBreaking = min(xxLeft / 2, pairsLeft + pairsRight)
return pairsLeft + pairsRight + useWithXX + extraByBreaking
}
}
58 changes: 58 additions & 0 deletions src/main/kotlin/g3601_3700/s3664_two_letter_card_game/readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
3664\. Two-Letter Card Game

Medium

You are given a deck of cards represented by a string array `cards`, and each card displays two lowercase letters.

You are also given a letter `x`. You play a game with the following rules:

* Start with 0 points.
* On each turn, you must find two **compatible** cards from the deck that both contain the letter `x` in any position.
* Remove the pair of cards and earn **1 point**.
* The game ends when you can no longer find a pair of compatible cards.

Return the **maximum** number of points you can gain with optimal play.

Two cards are **compatible** if the strings differ in **exactly** 1 position.

**Example 1:**

**Input:** cards = ["aa","ab","ba","ac"], x = "a"

**Output:** 2

**Explanation:**

* On the first turn, select and remove cards `"ab"` and `"ac"`, which are compatible because they differ at only index 1.
* On the second turn, select and remove cards `"aa"` and `"ba"`, which are compatible because they differ at only index 0.

Because there are no more compatible pairs, the total score is 2.

**Example 2:**

**Input:** cards = ["aa","ab","ba"], x = "a"

**Output:** 1

**Explanation:**

* On the first turn, select and remove cards `"aa"` and `"ba"`.

Because there are no more compatible pairs, the total score is 1.

**Example 3:**

**Input:** cards = ["aa","ab","ba","ac"], x = "b"

**Output:** 0

**Explanation:**

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.

**Constraints:**

* <code>2 <= cards.length <= 10<sup>5</sup></code>
* `cards[i].length == 2`
* Each `cards[i]` is composed of only lowercase English letters between `'a'` and `'j'`.
* `x` is a lowercase English letter between `'a'` and `'j'`.
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package g3601_3700.s3665_twisted_mirror_path_count

// #Medium #Biweekly_Contest_164 #2025_09_07_Time_33_ms_(100.00%)_Space_113.52_MB_(72.73%)

class Solution {
fun uniquePaths(grid: Array<IntArray>): Int {
// 0 right, 1 down
val n = grid.size
val m = grid[0].size
val mod = 1000000007
var dp = IntArray(m)
dp[0] = 1
for (j in 1..<m) {
if (grid[0][j - 1] == 0) {
dp[j] = dp[j - 1]
}
}
for (i in 1..<n) {
val next = IntArray(m)
if (grid[i - 1][0] == 0 && grid[i][0] == 0) {
next[0] = dp[0]
}
for (j in 1..<m) {
if (grid[i][j] == 0) {
next[j] = (next[j] + dp[j]) % mod
}
if (grid[i][j - 1] == 0) {
next[j] = (next[j] + next[j - 1]) % mod
} else {
next[j] = (next[j] + dp[j - 1]) % mod
}
}
dp = next
}
return dp[m - 1]
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
3665\. Twisted Mirror Path Count

Medium

Given an `m x n` binary grid `grid` where:

* `grid[i][j] == 0` represents an empty cell, and
* `grid[i][j] == 1` represents a mirror.

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:

* If it tries to move **right** into a mirror, it is turned **down** and moved into the cell directly below the mirror.
* 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.

If this reflection would cause the robot to move outside the `grid` boundaries, the path is considered invalid and should not be counted.

Return the number of unique valid paths from `(0, 0)` to `(m - 1, n - 1)`.

Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.

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

**Example 1:**

**Input:** grid = [[0,1,0],[0,0,1],[1,0,0]]

**Output:** 5

**Explanation:**

| Number | Full Path |
|--------|---------------------------------------------------------------------|
| 1 | (0, 0) → (0, 1) [M] → (1, 1) → (1, 2) [M] → (2, 2) |
| 2 | (0, 0) → (0, 1) [M] → (1, 1) → (2, 1) → (2, 2) |
| 3 | (0, 0) → (1, 0) → (1, 1) → (1, 2) [M] → (2, 2) |
| 4 | (0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) |
| 5 | (0, 0) → (1, 0) → (2, 0) [M] → (2, 1) → (2, 2) |

* `[M]` indicates the robot attempted to enter a mirror cell and instead reflected.


**Example 2:**

**Input:** grid = [[0,0],[0,0]]

**Output:** 2

**Explanation:**

| Number | Full Path |
|--------|-----------------------------|
| 1 | (0, 0) → (0, 1) → (1, 1) |
| 2 | (0, 0) → (1, 0) → (1, 1) |

**Example 3:**

**Input:** grid = [[0,1,1],[1,1,0]]

**Output:** 1

**Explanation:**

| Number | Full Path |
|--------|-------------------------------------------|
| 1 | (0, 0) → (0, 1) [M] → (1, 1) [M] → (1, 2) |

`(0, 0) → (1, 0) [M] → (1, 1) [M] → (2, 1)` goes out of bounds, so it is invalid.

**Constraints:**

* `m == grid.length`
* `n == grid[i].length`
* `2 <= m, n <= 500`
* `grid[i][j]` is either `0` or `1`.
* `grid[0][0] == grid[m - 1][n - 1] == 0`
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package g3601_3700.s3666_minimum_operations_to_equalize_binary_string

// #Hard #Biweekly_Contest_164 #2025_09_07_Time_8_ms_(100.00%)_Space_46.70_MB_(100.00%)

import kotlin.math.max

class Solution {
fun minOperations(s: String, k: Int): Int {
val n = s.length
var cnt0 = 0
for (c in s.toCharArray()) {
if (c == '0') {
cnt0++
}
}
if (cnt0 == 0) {
return 0
}
if (k == n) {
return if (cnt0 == n) 1 else -1
}
val kP = k and 1
val needP = cnt0 and 1
var best = Long.Companion.MAX_VALUE
for (p in 0..1) {
if ((p * kP) % 2 != needP) {
continue
}
val mismatch = (if (p == 0) cnt0 else (n - cnt0)).toLong()
val b1 = (cnt0 + k - 1L) / k
val b2: Long
b2 = (mismatch + (n - k) - 1L) / (n - k)
var lb = max(b1, b2)
if (lb < 1) {
lb = 1
}
if ((lb and 1L) != p.toLong()) {
lb++
}
if (lb < best) {
best = lb
}
}
return if (best == Long.Companion.MAX_VALUE) -1 else best.toInt()
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
3666\. Minimum Operations to Equalize Binary String

Hard

You are given a binary string `s`, and an integer `k`.

In one operation, you must choose **exactly** `k` **different** indices and **flip** each `'0'` to `'1'` and each `'1'` to `'0'`.

Return the **minimum** number of operations required to make all characters in the string equal to `'1'`. If it is not possible, return -1.

**Example 1:**

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

**Output:** 1

**Explanation:**

* There is one `'0'` in `s`.
* Since `k = 1`, we can flip it directly in one operation.

**Example 2:**

**Input:** s = "0101", k = 3

**Output:** 2

**Explanation:**

One optimal set of operations choosing `k = 3` indices in each operation is:

* **Operation 1**: Flip indices `[0, 1, 3]`. `s` changes from `"0101"` to `"1000"`.
* **Operation 2**: Flip indices `[1, 2, 3]`. `s` changes from `"1000"` to `"1111"`.

Thus, the minimum number of operations is 2.

**Example 3:**

**Input:** s = "101", k = 2

**Output:** \-1

**Explanation:**

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.

**Constraints:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s[i]` is either `'0'` or `'1'`.
* `1 <= k <= s.length`
Loading
Loading

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