Skip to content

Commit 6f08d34

Browse files
authored
Added tasks 3461-3464
1 parent 0cd2a8a commit 6f08d34

File tree

12 files changed

+509
-0
lines changed

12 files changed

+509
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3401_3500.s3461_check_if_digits_are_equal_in_string_after_operations_i;
2+
3+
// #Easy #String #Math #Simulation #Number_Theory #Combinatorics
4+
// #2025_02_25_Time_2_ms_(96.71%)_Space_42.26_MB_(97.03%)
5+
6+
public class Solution {
7+
public boolean hasSameDigits(String s) {
8+
char[] ch = s.toCharArray();
9+
int k = ch.length - 1;
10+
while (k != 1) {
11+
for (int i = 0; i < k; i++) {
12+
int a = ch[i] - 48;
13+
int b = ch[i + 1] - 48;
14+
int d = (a + b) % 10;
15+
char c = (char) (d + '0');
16+
ch[i] = c;
17+
}
18+
k--;
19+
}
20+
return ch[0] == ch[1];
21+
}
22+
}
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,31 @@
1+
package g3401_3500.s3462_maximum_sum_with_at_most_k_elements;
2+
3+
// #Medium #Array #Sorting #Greedy #Matrix #Heap_(Priority_Queue)
4+
// #2025_02_25_Time_62_ms_(99.82%)_Space_78.09_MB_(20.19%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public long maxSum(int[][] grid, int[] limits, int k) {
10+
int l = 0;
11+
for (int i = 0; i < limits.length; i++) {
12+
l += limits[i];
13+
}
14+
int[] dp = new int[l];
15+
int a = 0;
16+
for (int i = 0; i < grid.length; i++) {
17+
int lim = limits[i];
18+
Arrays.sort(grid[i]);
19+
for (int j = grid[i].length - lim; j < grid[i].length; j++) {
20+
dp[a] = grid[i][j];
21+
a++;
22+
}
23+
}
24+
Arrays.sort(dp);
25+
long sum = 0L;
26+
for (int i = l - 1; i >= l - k; i--) {
27+
sum += dp[i];
28+
}
29+
return sum;
30+
}
31+
}
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,74 @@
1+
package g3401_3500.s3463_check_if_digits_are_equal_in_string_after_operations_ii;
2+
3+
// #Hard #String #Math #Number_Theory #Combinatorics
4+
// #2025_02_25_Time_43_ms_(99.64%)_Space_49.40_MB_(10.02%)
5+
6+
public class Solution {
7+
private int powMod10(int a, int n) {
8+
int x = 1;
9+
while (n >= 1) {
10+
if (n % 2 == 1) {
11+
x = (x * a) % 10;
12+
}
13+
a = (a * a) % 10;
14+
n /= 2;
15+
}
16+
return x;
17+
}
18+
19+
private int[] f(int n) {
20+
int[] ns = new int[n + 1];
21+
int[] n2 = new int[n + 1];
22+
int[] n5 = new int[n + 1];
23+
ns[0] = 1;
24+
for (int i = 1; i <= n; ++i) {
25+
int m = i;
26+
n2[i] = n2[i - 1];
27+
n5[i] = n5[i - 1];
28+
while (m % 2 == 0) {
29+
m /= 2;
30+
n2[i]++;
31+
}
32+
while (m % 5 == 0) {
33+
m /= 5;
34+
n5[i]++;
35+
}
36+
ns[i] = (ns[i - 1] * m) % 10;
37+
}
38+
int[] inv = new int[10];
39+
for (int i = 1; i < 10; ++i) {
40+
for (int j = 0; j < 10; ++j) {
41+
if (i * j % 10 == 1) {
42+
inv[i] = j;
43+
}
44+
}
45+
}
46+
int[] xs = new int[n + 1];
47+
for (int k = 0; k <= n; ++k) {
48+
int a = 0;
49+
int s2 = n2[n] - n2[n - k] - n2[k];
50+
int s5 = n5[n] - n5[n - k] - n5[k];
51+
if (s2 == 0 || s5 == 0) {
52+
a = (ns[n] * inv[ns[n - k]] * inv[ns[k]] * powMod10(2, s2) * powMod10(5, s5)) % 10;
53+
}
54+
xs[k] = a;
55+
}
56+
return xs;
57+
}
58+
59+
public boolean hasSameDigits(String s) {
60+
int n = s.length();
61+
int[] xs = f(n - 2);
62+
int[] arr = new int[n];
63+
for (int i = 0; i < n; i++) {
64+
arr[i] = s.charAt(i) - '0';
65+
}
66+
int num1 = 0;
67+
int num2 = 0;
68+
for (int i = 0; i < n - 1; i++) {
69+
num1 = (num1 + xs[i] * arr[i]) % 10;
70+
num2 = (num2 + xs[i] * arr[i + 1]) % 10;
71+
}
72+
return num1 == num2;
73+
}
74+
}
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,79 @@
1+
package g3401_3500.s3464_maximize_the_distance_between_points_on_a_square;
2+
3+
// #Hard #Array #Greedy #Binary_Search #2025_02_25_Time_18_ms_(98.51%)_Space_49.78_MB_(46.27%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int maxDistance(int side, int[][] pts, int k) {
9+
int n = pts.length;
10+
long[] p = new long[n];
11+
for (int i = 0; i < n; i++) {
12+
int x = pts[i][0];
13+
int y = pts[i][1];
14+
long c;
15+
if (y == 0) {
16+
c = x;
17+
} else if (x == side) {
18+
c = side + (long) y;
19+
} else if (y == side) {
20+
c = 2L * side + (side - x);
21+
} else {
22+
c = 3L * side + (side - y);
23+
}
24+
p[i] = c;
25+
}
26+
Arrays.sort(p);
27+
long c = 4L * side;
28+
int tot = 2 * n;
29+
long[] dArr = new long[tot];
30+
for (int i = 0; i < n; i++) {
31+
dArr[i] = p[i];
32+
dArr[i + n] = p[i] + c;
33+
}
34+
int lo = 0;
35+
int hi = 2 * side;
36+
int ans = 0;
37+
while (lo <= hi) {
38+
int mid = (lo + hi) >>> 1;
39+
if (check(mid, dArr, n, k, c)) {
40+
ans = mid;
41+
lo = mid + 1;
42+
} else {
43+
hi = mid - 1;
44+
}
45+
}
46+
return ans;
47+
}
48+
49+
private boolean check(int d, long[] dArr, int n, int k, long c) {
50+
int len = dArr.length;
51+
int[] nxt = new int[len];
52+
int j = 0;
53+
for (int i = 0; i < len; i++) {
54+
if (j < i + 1) {
55+
j = i + 1;
56+
}
57+
while (j < len && dArr[j] < dArr[i] + d) {
58+
j++;
59+
}
60+
nxt[i] = (j < len) ? j : -1;
61+
}
62+
for (int i = 0; i < n; i++) {
63+
int cnt = 1;
64+
int cur = i;
65+
while (cnt < k) {
66+
int nx = nxt[cur];
67+
if (nx == -1 || nx >= i + n) {
68+
break;
69+
}
70+
cur = nx;
71+
cnt++;
72+
}
73+
if (cnt == k && (dArr[i] + c - dArr[cur]) >= d) {
74+
return true;
75+
}
76+
}
77+
return false;
78+
}
79+
}

0 commit comments

Comments
 (0)