Skip to content

Commit 892e1f8

Browse files
committed
Added tasks 3461-3464
1 parent 709a68c commit 892e1f8

File tree

12 files changed

+496
-0
lines changed

12 files changed

+496
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g3401_3500.s3461_check_if_digits_are_equal_in_string_after_operations_i;
2+
3+
// #Easy #2025_02_23_Time_23_ms_(100.00%)_Space_45.51_MB_(100.00%)
4+
5+
public class Solution {
6+
public boolean hasSameDigits(String s) {
7+
int n = s.length();
8+
while (n > 2) {
9+
StringBuilder nstr = new StringBuilder();
10+
for (int i = 1; i < n; i++) {
11+
int next = ((s.charAt(i) - '0') + (s.charAt(i - 1) - '0')) % 10;
12+
nstr.append(next);
13+
}
14+
n--;
15+
s = nstr.toString();
16+
}
17+
return s.charAt(0) == s.charAt(1);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
3461\. Check If Digits Are Equal in String After Operations I
2+
3+
Easy
4+
5+
You are given a string `s` consisting of digits. Perform the following operation repeatedly until the string has **exactly** two digits:
6+
7+
* For each pair of consecutive digits in `s`, starting from the first digit, calculate a new digit as the sum of the two digits **modulo** 10.
8+
* Replace `s` with the sequence of newly calculated digits, _maintaining the order_ in which they are computed.
9+
10+
Return `true` if the final two digits in `s` are the **same**; otherwise, return `false`.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "3902"
15+
16+
**Output:** true
17+
18+
**Explanation:**
19+
20+
* Initially, `s = "3902"`
21+
* First operation:
22+
* `(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2`
23+
* `(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9`
24+
* `(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2`
25+
* `s` becomes `"292"`
26+
* Second operation:
27+
* `(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1`
28+
* `(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1`
29+
* `s` becomes `"11"`
30+
* Since the digits in `"11"` are the same, the output is `true`.
31+
32+
**Example 2:**
33+
34+
**Input:** s = "34789"
35+
36+
**Output:** false
37+
38+
**Explanation:**
39+
40+
* Initially, `s = "34789"`.
41+
* After the first operation, `s = "7157"`.
42+
* After the second operation, `s = "862"`.
43+
* After the third operation, `s = "48"`.
44+
* Since `'4' != '8'`, the output is `false`.
45+
46+
**Constraints:**
47+
48+
* `3 <= s.length <= 100`
49+
* `s` consists of only digits.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g3401_3500.s3462_maximum_sum_with_at_most_k_elements;
2+
3+
// #Medium #2025_02_23_Time_278_ms_(_%)_Space_73.54_MB_(_%)
4+
5+
import java.util.Collections;
6+
import java.util.PriorityQueue;
7+
8+
public class Solution {
9+
public long maxSum(int[][] grid, int[] limits, int k) {
10+
if (grid.length == 0) {
11+
return 0;
12+
}
13+
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
14+
PriorityQueue<Integer> temp;
15+
for (int i = 0; i < grid.length; i++) {
16+
temp = new PriorityQueue<>(Collections.reverseOrder());
17+
for (int j = 0; j < grid[i].length; j++) {
18+
temp.add(grid[i][j]);
19+
}
20+
int cnt = 0;
21+
while (!temp.isEmpty() && cnt < limits[i]) {
22+
pq.add(temp.poll());
23+
cnt += 1;
24+
}
25+
}
26+
long result = 0;
27+
long count = 0;
28+
while (!pq.isEmpty() && count < k) {
29+
result += pq.poll();
30+
count += 1;
31+
}
32+
return result;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3462\. Maximum Sum With at Most K Elements
2+
3+
Medium
4+
5+
You are given a 2D integer matrix `grid` of size `n x m`, an integer array `limits` of length `n`, and an integer `k`. The task is to find the **maximum sum** of **at most** `k` elements from the matrix `grid` such that:
6+
7+
* The number of elements taken from the <code>i<sup>th</sup></code> row of `grid` does not exceed `limits[i]`.
8+
9+
10+
Return the **maximum sum**.
11+
12+
**Example 1:**
13+
14+
**Input:** grid = [[1,2],[3,4]], limits = [1,2], k = 2
15+
16+
**Output:** 7
17+
18+
**Explanation:**
19+
20+
* From the second row, we can take at most 2 elements. The elements taken are 4 and 3.
21+
* The maximum possible sum of at most 2 selected elements is `4 + 3 = 7`.
22+
23+
**Example 2:**
24+
25+
**Input:** grid = [[5,3,7],[8,2,6]], limits = [2,2], k = 3
26+
27+
**Output:** 21
28+
29+
**Explanation:**
30+
31+
* From the first row, we can take at most 2 elements. The element taken is 7.
32+
* From the second row, we can take at most 2 elements. The elements taken are 8 and 6.
33+
* The maximum possible sum of at most 3 selected elements is `7 + 8 + 6 = 21`.
34+
35+
**Constraints:**
36+
37+
* `n == grid.length == limits.length`
38+
* `m == grid[i].length`
39+
* `1 <= n, m <= 500`
40+
* <code>0 <= grid[i][j] <= 10<sup>5</sup></code>
41+
* `0 <= limits[i] <= m`
42+
* `0 <= k <= min(n * m, sum(limits))`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g3401_3500.s3463_check_if_digits_are_equal_in_string_after_operations_ii;
2+
3+
// #Hard #2025_02_23_Time_103_ms_(100.00%)_Space_45.58_MB_(100.00%)
4+
5+
public class Solution {
6+
public boolean hasSameDigits(String s) {
7+
int n = s.length();
8+
int nMunus2 = n - 2;
9+
int f0 = 0;
10+
int f1 = 0;
11+
for (int j = 0; j <= nMunus2; j++) {
12+
int c = binomMod10(nMunus2, j);
13+
f0 = (f0 + c * (s.charAt(j) - '0')) % 10;
14+
f1 = (f1 + c * (s.charAt(j + 1) - '0')) % 10;
15+
}
16+
return f0 == f1;
17+
}
18+
19+
private int binomMod10(int n, int k) {
20+
int r2 = binomMod2(n, k);
21+
int r5 = binomMod5(n, k);
22+
for (int x = 0; x < 10; x++) {
23+
if (x % 2 == r2 && x % 5 == r5) {
24+
return x;
25+
}
26+
}
27+
return 0;
28+
}
29+
30+
private int binomMod2(int n, int k) {
31+
return ((n & k) == k) ? 1 : 0;
32+
}
33+
34+
private int binomMod5(int n, int k) {
35+
int[][] t = {{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 1, 4, 1}};
36+
int res = 1;
37+
while (n > 0 || k > 0) {
38+
int nd = n % 5;
39+
int kd = k % 5;
40+
if (kd > nd) {
41+
return 0;
42+
}
43+
res = (res * t[nd][kd]) % 5;
44+
n /= 5;
45+
k /= 5;
46+
}
47+
return res;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
3463\. Check If Digits Are Equal in String After Operations II
2+
3+
Hard
4+
5+
You are given a string `s` consisting of digits. Perform the following operation repeatedly until the string has **exactly** two digits:
6+
7+
* For each pair of consecutive digits in `s`, starting from the first digit, calculate a new digit as the sum of the two digits **modulo** 10.
8+
* Replace `s` with the sequence of newly calculated digits, _maintaining the order_ in which they are computed.
9+
10+
Return `true` if the final two digits in `s` are the **same**; otherwise, return `false`.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "3902"
15+
16+
**Output:** true
17+
18+
**Explanation:**
19+
20+
* Initially, `s = "3902"`
21+
* First operation:
22+
* `(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2`
23+
* `(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9`
24+
* `(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2`
25+
* `s` becomes `"292"`
26+
* Second operation:
27+
* `(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1`
28+
* `(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1`
29+
* `s` becomes `"11"`
30+
* Since the digits in `"11"` are the same, the output is `true`.
31+
32+
**Example 2:**
33+
34+
**Input:** s = "34789"
35+
36+
**Output:** false
37+
38+
**Explanation:**
39+
40+
* Initially, `s = "34789"`.
41+
* After the first operation, `s = "7157"`.
42+
* After the second operation, `s = "862"`.
43+
* After the third operation, `s = "48"`.
44+
* Since `'4' != '8'`, the output is `false`.
45+
46+
**Constraints:**
47+
48+
* <code>3 <= s.length <= 10<sup>5</sup></code>
49+
* `s` consists of only digits.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package g3401_3500.s3464_maximize_the_distance_between_points_on_a_square;
2+
3+
// #Hard #2025_02_23_Time_222_ms_(100.00%)_Space_52.33_MB_(100.00%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int maxDistance(int sideLength, int[][] points, int requiredPoints) {
9+
long perimeter = 4L * sideLength;
10+
int numPoints = points.length;
11+
long[] mappedPositions = new long[numPoints];
12+
for (int i = 0; i < numPoints; i++) {
13+
mappedPositions[i] = mapToPerimeter(sideLength, points[i][0], points[i][1]);
14+
}
15+
Arrays.sort(mappedPositions);
16+
long low = 0;
17+
long high = perimeter;
18+
while (low < high) {
19+
long mid = (low + high + 1) / 2;
20+
if (isValidDistance(mid, requiredPoints, mappedPositions, perimeter)) {
21+
low = mid;
22+
} else {
23+
high = mid - 1;
24+
}
25+
}
26+
return (int) low;
27+
}
28+
29+
private long mapToPerimeter(int sideLength, int x, int y) {
30+
if (y == sideLength) {
31+
return x;
32+
}
33+
if (x == sideLength) {
34+
return sideLength + (sideLength - y);
35+
}
36+
if (y == 0) {
37+
return 2L * sideLength + (sideLength - x);
38+
}
39+
return 3L * sideLength + y;
40+
}
41+
42+
private boolean isValidDistance(
43+
long minDistance, int requiredPoints, long[] mappedPositions, long perimeter) {
44+
int numPoints = mappedPositions.length;
45+
long[] extendedPositions = new long[2 * numPoints];
46+
for (int i = 0; i < numPoints; i++) {
47+
extendedPositions[i] = mappedPositions[i];
48+
extendedPositions[i + numPoints] = mappedPositions[i] + perimeter;
49+
}
50+
for (int i = 0; i < numPoints; i++) {
51+
if (canSelectRequiredPoints(
52+
i,
53+
minDistance,
54+
requiredPoints,
55+
mappedPositions,
56+
extendedPositions,
57+
perimeter)) {
58+
return true;
59+
}
60+
}
61+
return false;
62+
}
63+
64+
private boolean canSelectRequiredPoints(
65+
int startIndex,
66+
long minDistance,
67+
int requiredPoints,
68+
long[] mappedPositions,
69+
long[] extendedPositions,
70+
long perimeter) {
71+
int selectedCount = 1;
72+
long previousPosition = mappedPositions[startIndex];
73+
int currentIndex = startIndex;
74+
for (int i = 1; i < requiredPoints; i++) {
75+
long targetPosition = previousPosition + minDistance;
76+
int left = currentIndex + 1;
77+
int right = startIndex + mappedPositions.length;
78+
int nextIndex = lowerBound(extendedPositions, left, right, targetPosition);
79+
if (nextIndex >= right) {
80+
return false;
81+
}
82+
selectedCount++;
83+
previousPosition = extendedPositions[nextIndex];
84+
currentIndex = nextIndex;
85+
}
86+
return selectedCount == requiredPoints
87+
&& (previousPosition - mappedPositions[startIndex] <= perimeter - minDistance);
88+
}
89+
90+
private int lowerBound(long[] arr, int left, int right, long target) {
91+
while (left < right) {
92+
int mid = left + (right - left) / 2;
93+
if (arr[mid] >= target) {
94+
right = mid;
95+
} else {
96+
left = mid + 1;
97+
}
98+
}
99+
return left;
100+
}
101+
}

0 commit comments

Comments
 (0)