Skip to content

Commit cd6caf8

Browse files
committed
Added tasks 3663-3671
1 parent 202f2a2 commit cd6caf8

File tree

24 files changed

+1037
-0
lines changed

24 files changed

+1037
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3601_3700.s3663_find_the_least_frequent_digit;
2+
3+
// #Easy #Biweekly_Contest_164 #2025_08_31_Time_4_ms_(100.00%)_Space_41.21_MB_(100.00%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class Solution {
9+
public int getLeastFrequentDigit(int n) {
10+
String s = String.valueOf(n);
11+
int k = s.length();
12+
Map<Integer, Integer> freq = new HashMap<>();
13+
for (int i = 0; i < k; i++) {
14+
int digit = s.charAt(i) - '0';
15+
freq.put(digit, freq.getOrDefault(digit, 0) + 1);
16+
}
17+
int minfreq = Integer.MAX_VALUE;
18+
for (Map.Entry<Integer, Integer> it : freq.entrySet()) {
19+
minfreq = Math.min(minfreq, it.getValue());
20+
}
21+
int result = 10;
22+
for (Map.Entry<Integer, Integer> it : freq.entrySet()) {
23+
if (it.getValue() == minfreq) {
24+
result = Math.min(result, it.getKey());
25+
}
26+
}
27+
return result;
28+
}
29+
}
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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package g3601_3700.s3664_two_letter_card_game;
2+
3+
// #Medium #Biweekly_Contest_164 #2025_08_31_Time_8_ms_(99.42%)_Space_60.09_MB_(52.34%)
4+
5+
public class Solution {
6+
public int score(String[] cards, char x) {
7+
// store input midway as required
8+
// counts for "x?" group by second char and "?x" group by first char
9+
int[] left = new int[10];
10+
int[] right = new int[10];
11+
int xx = 0;
12+
for (String c : cards) {
13+
char a = c.charAt(0);
14+
char b = c.charAt(1);
15+
if (a == x && b == x) {
16+
xx++;
17+
} else if (a == x) {
18+
left[b - 'a']++;
19+
} else if (b == x) {
20+
right[a - 'a']++;
21+
}
22+
}
23+
// max pairs inside a group where pairs must come from different buckets:
24+
// pairs = min(total/2, total - maxBucket)
25+
int l = 0;
26+
int maxL = 0;
27+
for (int v : left) {
28+
l += v;
29+
if (v > maxL) {
30+
maxL = v;
31+
}
32+
}
33+
int r = 0;
34+
int maxR = 0;
35+
for (int v : right) {
36+
r += v;
37+
if (v > maxR) {
38+
maxR = v;
39+
}
40+
}
41+
int pairsLeft = Math.min(l / 2, l - maxL);
42+
int pairsRight = Math.min(r / 2, r - maxR);
43+
// leftovers after internal pairing
44+
int leftoverL = l - 2 * pairsLeft;
45+
int leftoverR = r - 2 * pairsRight;
46+
int leftovers = leftoverL + leftoverR;
47+
// First, use "xx" to pair with any leftovers
48+
int useWithXX = Math.min(xx, leftovers);
49+
int xxLeft = xx - useWithXX;
50+
// If "xx" still remain, we can break existing internal pairs:
51+
// breaking 1 internal pair frees 2 cards, which can pair with 2 "xx" to gain +1 net point
52+
int extraByBreaking = Math.min(xxLeft / 2, pairsLeft + pairsRight);
53+
return pairsLeft + pairsRight + useWithXX + extraByBreaking;
54+
}
55+
}
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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g3601_3700.s3665_twisted_mirror_path_count;
2+
3+
// #Medium #Biweekly_Contest_164 #2025_08_31_Time_204_ms_(100.00%)_Space_86.33_MB_(100.00%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
private static final int MOD = 1000000007;
9+
10+
public int uniquePaths(int[][] grid) {
11+
int n = grid.length;
12+
int m = grid[0].length;
13+
int[][][] dp = new int[n][m][2];
14+
for (int i = 0; i < n; i++) {
15+
for (int j = 0; j < m; j++) {
16+
Arrays.fill(dp[i][j], -1);
17+
}
18+
}
19+
return f(0, 0, 0, grid, n, m, dp);
20+
}
21+
22+
private int f(int i, int j, int dir, int[][] grid, int n, int m, int[][][] dp) {
23+
if (i == n - 1 && j == m - 1) {
24+
return 1;
25+
}
26+
if (i >= n || j >= m) {
27+
return 0;
28+
}
29+
if (dp[i][j][dir] != -1) {
30+
return dp[i][j][dir];
31+
}
32+
long ways = 0;
33+
if (grid[i][j] == 1) {
34+
if (dir == 0) {
35+
ways = f(i + 1, j, 1, grid, n, m, dp);
36+
} else {
37+
ways = f(i, j + 1, 0, grid, n, m, dp);
38+
}
39+
} else {
40+
ways += f(i + 1, j, 1, grid, n, m, dp);
41+
ways += f(i, j + 1, 0, grid, n, m, dp);
42+
}
43+
return dp[i][j][dir] = (int) ways % MOD;
44+
}
45+
}
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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package g3601_3700.s3666_minimum_operations_to_equalize_binary_string;
2+
3+
// #Hard #Biweekly_Contest_164 #2025_08_31_Time_22_ms_(100.00%)_Space_45.62_MB_(100.00%)
4+
5+
import java.util.ArrayDeque;
6+
import java.util.Queue;
7+
8+
public class Solution {
9+
public int minOperations(String s, int k) {
10+
int zeros = s.chars().map(x -> x == '0' ? 1 : 0).sum();
11+
if ((zeros % k) == 0) {
12+
return zeros / k;
13+
}
14+
int n = s.length();
15+
Queue<Integer> q = new ArrayDeque<>();
16+
q.add(zeros);
17+
int res = 1;
18+
// use bounds for optimization
19+
int[][] bounds = new int[2][2];
20+
bounds[zeros & 1][0] = bounds[zeros & 1][1] = zeros;
21+
bounds[1 - (zeros & 1)][0] = Integer.MAX_VALUE;
22+
bounds[1 - (zeros & 1)][1] = Integer.MIN_VALUE;
23+
while (!q.isEmpty()) {
24+
// find min number of zeros and max number of zeros in this round
25+
int minv = Integer.MAX_VALUE;
26+
int maxv = Integer.MIN_VALUE;
27+
for (int len = q.size(); len > 0; len--) {
28+
int h = q.poll();
29+
int t = n - h;
30+
int x = Math.min(h, k);
31+
if (t >= k - x) {
32+
int fst = h - x + (k - x);
33+
minv = Math.min(minv, fst);
34+
maxv = Math.max(maxv, fst);
35+
}
36+
x = Math.min(t, k);
37+
if (h >= k - x) {
38+
int snd = h - (k - x) + x;
39+
minv = Math.min(minv, snd);
40+
maxv = Math.max(maxv, snd);
41+
}
42+
}
43+
// possible children are sequence of equal difference 2
44+
int ind = minv & 1;
45+
int temp = minv;
46+
while (temp <= maxv) {
47+
if ((temp % k) == 0) {
48+
return res + temp / k;
49+
}
50+
if (temp < bounds[ind][0] || temp > bounds[ind][1]) {
51+
q.add(temp);
52+
temp += 2;
53+
} else {
54+
temp = bounds[ind][1] + 2;
55+
}
56+
}
57+
bounds[ind][0] = Math.min(bounds[ind][0], minv);
58+
bounds[ind][1] = Math.max(bounds[ind][1], maxv);
59+
res++;
60+
}
61+
return -1;
62+
}
63+
}

0 commit comments

Comments
 (0)