Skip to content

Commit e413ac3

Browse files
committed
Added tasks 3663-3671
1 parent d20f5be commit e413ac3

File tree

9 files changed

+1033
-144
lines changed
  • src/main/kotlin/g3601_3700
    • s3663_find_the_least_frequent_digit
    • s3664_two_letter_card_game
    • s3665_twisted_mirror_path_count
    • s3666_minimum_operations_to_equalize_binary_string
    • s3668_restore_finishing_order
    • s3669_balanced_k_factor_decomposition
    • s3670_maximum_product_of_two_integers_with_no_common_bits
    • s3671_sum_of_beautiful_subsequences

9 files changed

+1033
-144
lines changed

README.md

Lines changed: 152 additions & 144 deletions
Large diffs are not rendered by default.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3663\. Find The Least Frequent Digit
5+
6+
Easy
7+
8+
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.
9+
10+
Return the chosen digit as an integer.
11+
12+
The **frequency** of a digit `x` is the number of times it appears in the decimal representation of `n`.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 1553322
17+
18+
**Output:** 1
19+
20+
**Explanation:**
21+
22+
The least frequent digit in `n` is 1, which appears only once. All other digits appear twice.
23+
24+
**Example 2:**
25+
26+
**Input:** n = 723344511
27+
28+
**Output:** 2
29+
30+
**Explanation:**
31+
32+
The least frequent digits in `n` are 7, 2, and 5; each appears only once.
33+
34+
**Constraints:**
35+
36+
* <code>1 <= n <= 2<sup>31</sup> - 1</code>
37+
38+
## Solution
39+
40+
```kotlin
41+
class Solution {
42+
fun getLeastFrequentDigit(n: Int): Int {
43+
val freq = IntArray(10)
44+
val numStr = n.toString()
45+
for (c in numStr.toCharArray()) {
46+
freq[c.code - '0'.code]++
47+
}
48+
var minFreq = Int.Companion.MAX_VALUE
49+
var result = -1
50+
for (d in 0..9) {
51+
if (freq[d] == 0) {
52+
continue
53+
}
54+
if (freq[d] < minFreq) {
55+
minFreq = freq[d]
56+
result = d
57+
} else if (freq[d] == minFreq && d < result) {
58+
result = d
59+
}
60+
}
61+
return result
62+
}
63+
}
64+
```
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3664\. Two-Letter Card Game
5+
6+
Medium
7+
8+
You are given a deck of cards represented by a string array `cards`, and each card displays two lowercase letters.
9+
10+
You are also given a letter `x`. You play a game with the following rules:
11+
12+
* Start with 0 points.
13+
* On each turn, you must find two **compatible** cards from the deck that both contain the letter `x` in any position.
14+
* Remove the pair of cards and earn **1 point**.
15+
* The game ends when you can no longer find a pair of compatible cards.
16+
17+
Return the **maximum** number of points you can gain with optimal play.
18+
19+
Two cards are **compatible** if the strings differ in **exactly** 1 position.
20+
21+
**Example 1:**
22+
23+
**Input:** cards = ["aa","ab","ba","ac"], x = "a"
24+
25+
**Output:** 2
26+
27+
**Explanation:**
28+
29+
* On the first turn, select and remove cards `"ab"` and `"ac"`, which are compatible because they differ at only index 1.
30+
* On the second turn, select and remove cards `"aa"` and `"ba"`, which are compatible because they differ at only index 0.
31+
32+
Because there are no more compatible pairs, the total score is 2.
33+
34+
**Example 2:**
35+
36+
**Input:** cards = ["aa","ab","ba"], x = "a"
37+
38+
**Output:** 1
39+
40+
**Explanation:**
41+
42+
* On the first turn, select and remove cards `"aa"` and `"ba"`.
43+
44+
Because there are no more compatible pairs, the total score is 1.
45+
46+
**Example 3:**
47+
48+
**Input:** cards = ["aa","ab","ba","ac"], x = "b"
49+
50+
**Output:** 0
51+
52+
**Explanation:**
53+
54+
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.
55+
56+
**Constraints:**
57+
58+
* <code>2 <= cards.length <= 10<sup>5</sup></code>
59+
* `cards[i].length == 2`
60+
* Each `cards[i]` is composed of only lowercase English letters between `'a'` and `'j'`.
61+
* `x` is a lowercase English letter between `'a'` and `'j'`.
62+
63+
## Solution
64+
65+
```kotlin
66+
import kotlin.math.min
67+
68+
class Solution {
69+
fun score(cards: Array<String>, x: Char): Int {
70+
// store input midway as required
71+
// counts for "x?" group by second char and "?x" group by first char
72+
val left = IntArray(10)
73+
val right = IntArray(10)
74+
var xx = 0
75+
for (c in cards) {
76+
val a = c[0]
77+
val b = c[1]
78+
if (a == x && b == x) {
79+
xx++
80+
} else if (a == x) {
81+
left[b.code - 'a'.code]++
82+
} else if (b == x) {
83+
right[a.code - 'a'.code]++
84+
}
85+
}
86+
// max pairs inside a group where pairs must come from different buckets:
87+
// pairs = min(total/2, total - maxBucket)
88+
var l = 0
89+
var maxL = 0
90+
for (v in left) {
91+
l += v
92+
if (v > maxL) {
93+
maxL = v
94+
}
95+
}
96+
var r = 0
97+
var maxR = 0
98+
for (v in right) {
99+
r += v
100+
if (v > maxR) {
101+
maxR = v
102+
}
103+
}
104+
val pairsLeft = min(l / 2, l - maxL)
105+
val pairsRight = min(r / 2, r - maxR)
106+
// leftovers after internal pairing
107+
val leftoverL = l - 2 * pairsLeft
108+
val leftoverR = r - 2 * pairsRight
109+
val leftovers = leftoverL + leftoverR
110+
// First, use "xx" to pair with any leftovers
111+
val useWithXX = min(xx, leftovers)
112+
val xxLeft = xx - useWithXX
113+
// If "xx" still remain, we can break existing internal pairs:
114+
// breaking 1 internal pair frees 2 cards, which can pair with 2 "xx" to gain +1 net point
115+
val extraByBreaking = min(xxLeft / 2, pairsLeft + pairsRight)
116+
return pairsLeft + pairsRight + useWithXX + extraByBreaking
117+
}
118+
}
119+
```
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3665\. Twisted Mirror Path Count
5+
6+
Medium
7+
8+
Given an `m x n` binary grid `grid` where:
9+
10+
* `grid[i][j] == 0` represents an empty cell, and
11+
* `grid[i][j] == 1` represents a mirror.
12+
13+
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:
14+
15+
* If it tries to move **right** into a mirror, it is turned **down** and moved into the cell directly below the mirror.
16+
* 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.
17+
18+
If this reflection would cause the robot to move outside the `grid` boundaries, the path is considered invalid and should not be counted.
19+
20+
Return the number of unique valid paths from `(0, 0)` to `(m - 1, n - 1)`.
21+
22+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
23+
24+
**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.
25+
26+
**Example 1:**
27+
28+
**Input:** grid = \[\[0,1,0],[0,0,1],[1,0,0]]
29+
30+
**Output:** 5
31+
32+
**Explanation:**
33+
34+
| Number | Full Path |
35+
|--------|---------------------------------------------------------------------|
36+
| 1 | (0, 0) → (0, 1) [M] → (1, 1) → (1, 2) [M] → (2, 2) |
37+
| 2 | (0, 0) → (0, 1) [M] → (1, 1) → (2, 1) → (2, 2) |
38+
| 3 | (0, 0) → (1, 0) → (1, 1) → (1, 2) [M] → (2, 2) |
39+
| 4 | (0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) |
40+
| 5 | (0, 0) → (1, 0) → (2, 0) [M] → (2, 1) → (2, 2) |
41+
42+
* `[M]` indicates the robot attempted to enter a mirror cell and instead reflected.
43+
44+
45+
**Example 2:**
46+
47+
**Input:** grid = \[\[0,0],[0,0]]
48+
49+
**Output:** 2
50+
51+
**Explanation:**
52+
53+
| Number | Full Path |
54+
|--------|-----------------------------|
55+
| 1 | (0, 0) → (0, 1) → (1, 1) |
56+
| 2 | (0, 0) → (1, 0) → (1, 1) |
57+
58+
**Example 3:**
59+
60+
**Input:** grid = \[\[0,1,1],[1,1,0]]
61+
62+
**Output:** 1
63+
64+
**Explanation:**
65+
66+
| Number | Full Path |
67+
|--------|-------------------------------------------|
68+
| 1 | (0, 0) → (0, 1) [M] → (1, 1) [M] → (1, 2) |
69+
70+
`(0, 0) → (1, 0) [M] → (1, 1) [M] → (2, 1)` goes out of bounds, so it is invalid.
71+
72+
**Constraints:**
73+
74+
* `m == grid.length`
75+
* `n == grid[i].length`
76+
* `2 <= m, n <= 500`
77+
* `grid[i][j]` is either `0` or `1`.
78+
* `grid[0][0] == grid[m - 1][n - 1] == 0`
79+
80+
## Solution
81+
82+
```kotlin
83+
class Solution {
84+
fun uniquePaths(grid: Array<IntArray>): Int {
85+
// 0 right, 1 down
86+
val n = grid.size
87+
val m = grid[0].size
88+
val mod = 1000000007
89+
var dp = IntArray(m)
90+
dp[0] = 1
91+
for (j in 1..<m) {
92+
if (grid[0][j - 1] == 0) {
93+
dp[j] = dp[j - 1]
94+
}
95+
}
96+
for (i in 1..<n) {
97+
val next = IntArray(m)
98+
if (grid[i - 1][0] == 0 && grid[i][0] == 0) {
99+
next[0] = dp[0]
100+
}
101+
for (j in 1..<m) {
102+
if (grid[i][j] == 0) {
103+
next[j] = (next[j] + dp[j]) % mod
104+
}
105+
if (grid[i][j - 1] == 0) {
106+
next[j] = (next[j] + next[j - 1]) % mod
107+
} else {
108+
next[j] = (next[j] + dp[j - 1]) % mod
109+
}
110+
}
111+
dp = next
112+
}
113+
return dp[m - 1]
114+
}
115+
}
116+
```

0 commit comments

Comments
 (0)