Skip to content

Added tasks 3461-3464 #1915

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
merged 9 commits into from
Feb 25, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g3401_3500.s3461_check_if_digits_are_equal_in_string_after_operations_i;

// #Easy #String #Math #Simulation #Number_Theory #Combinatorics
// #2025_02_25_Time_2_ms_(96.71%)_Space_42.26_MB_(97.03%)

public class Solution {
public boolean hasSameDigits(String s) {
char[] ch = s.toCharArray();
int k = ch.length - 1;
while (k != 1) {
for (int i = 0; i < k; i++) {
int a = ch[i] - 48;
int b = ch[i + 1] - 48;
int d = (a + b) % 10;
char c = (char) (d + '0');
ch[i] = c;
}
k--;
}
return ch[0] == ch[1];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
3461\. Check If Digits Are Equal in String After Operations I

Easy

You are given a string `s` consisting of digits. Perform the following operation repeatedly until the string has **exactly** two digits:

* 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.
* Replace `s` with the sequence of newly calculated digits, _maintaining the order_ in which they are computed.

Return `true` if the final two digits in `s` are the **same**; otherwise, return `false`.

**Example 1:**

**Input:** s = "3902"

**Output:** true

**Explanation:**

* Initially, `s = "3902"`
* First operation:
* `(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2`
* `(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9`
* `(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2`
* `s` becomes `"292"`
* Second operation:
* `(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1`
* `(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1`
* `s` becomes `"11"`
* Since the digits in `"11"` are the same, the output is `true`.

**Example 2:**

**Input:** s = "34789"

**Output:** false

**Explanation:**

* Initially, `s = "34789"`.
* After the first operation, `s = "7157"`.
* After the second operation, `s = "862"`.
* After the third operation, `s = "48"`.
* Since `'4' != '8'`, the output is `false`.

**Constraints:**

* `3 <= s.length <= 100`
* `s` consists of only digits.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package g3401_3500.s3462_maximum_sum_with_at_most_k_elements;

// #Medium #Array #Sorting #Greedy #Matrix #Heap_(Priority_Queue)
// #2025_02_25_Time_62_ms_(99.82%)_Space_78.09_MB_(20.19%)

import java.util.Arrays;

public class Solution {
public long maxSum(int[][] grid, int[] limits, int k) {
int l = 0;
for (int i = 0; i < limits.length; i++) {
l += limits[i];
}
int[] dp = new int[l];
int a = 0;
for (int i = 0; i < grid.length; i++) {
int lim = limits[i];
Arrays.sort(grid[i]);
for (int j = grid[i].length - lim; j < grid[i].length; j++) {
dp[a] = grid[i][j];
a++;
}
}
Arrays.sort(dp);
long sum = 0L;
for (int i = l - 1; i >= l - k; i--) {
sum += dp[i];
}
return sum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
3462\. Maximum Sum With at Most K Elements

Medium

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:

* The number of elements taken from the <code>i<sup>th</sup></code> row of `grid` does not exceed `limits[i]`.


Return the **maximum sum**.

**Example 1:**

**Input:** grid = [[1,2],[3,4]], limits = [1,2], k = 2

**Output:** 7

**Explanation:**

* From the second row, we can take at most 2 elements. The elements taken are 4 and 3.
* The maximum possible sum of at most 2 selected elements is `4 + 3 = 7`.

**Example 2:**

**Input:** grid = [[5,3,7],[8,2,6]], limits = [2,2], k = 3

**Output:** 21

**Explanation:**

* From the first row, we can take at most 2 elements. The element taken is 7.
* From the second row, we can take at most 2 elements. The elements taken are 8 and 6.
* The maximum possible sum of at most 3 selected elements is `7 + 8 + 6 = 21`.

**Constraints:**

* `n == grid.length == limits.length`
* `m == grid[i].length`
* `1 <= n, m <= 500`
* <code>0 <= grid[i][j] <= 10<sup>5</sup></code>
* `0 <= limits[i] <= m`
* `0 <= k <= min(n * m, sum(limits))`
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package g3401_3500.s3463_check_if_digits_are_equal_in_string_after_operations_ii;

// #Hard #String #Math #Number_Theory #Combinatorics
// #2025_02_25_Time_43_ms_(99.64%)_Space_49.40_MB_(10.02%)

public class Solution {
private int powMod10(int a, int n) {
int x = 1;
while (n >= 1) {
if (n % 2 == 1) {
x = (x * a) % 10;
}
a = (a * a) % 10;
n /= 2;
}
return x;
}

private int[] f(int n) {
int[] ns = new int[n + 1];
int[] n2 = new int[n + 1];
int[] n5 = new int[n + 1];
ns[0] = 1;
for (int i = 1; i <= n; ++i) {
int m = i;
n2[i] = n2[i - 1];
n5[i] = n5[i - 1];
while (m % 2 == 0) {
m /= 2;
n2[i]++;
}
while (m % 5 == 0) {
m /= 5;
n5[i]++;
}
ns[i] = (ns[i - 1] * m) % 10;
}
int[] inv = new int[10];
for (int i = 1; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
if (i * j % 10 == 1) {
inv[i] = j;
}
}
}
int[] xs = new int[n + 1];
for (int k = 0; k <= n; ++k) {
int a = 0;
int s2 = n2[n] - n2[n - k] - n2[k];
int s5 = n5[n] - n5[n - k] - n5[k];
if (s2 == 0 || s5 == 0) {
a = (ns[n] * inv[ns[n - k]] * inv[ns[k]] * powMod10(2, s2) * powMod10(5, s5)) % 10;
}
xs[k] = a;
}
return xs;
}

public boolean hasSameDigits(String s) {
int n = s.length();
int[] xs = f(n - 2);
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = s.charAt(i) - '0';
}
int num1 = 0;
int num2 = 0;
for (int i = 0; i < n - 1; i++) {
num1 = (num1 + xs[i] * arr[i]) % 10;
num2 = (num2 + xs[i] * arr[i + 1]) % 10;
}
return num1 == num2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
3463\. Check If Digits Are Equal in String After Operations II

Hard

You are given a string `s` consisting of digits. Perform the following operation repeatedly until the string has **exactly** two digits:

* 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.
* Replace `s` with the sequence of newly calculated digits, _maintaining the order_ in which they are computed.

Return `true` if the final two digits in `s` are the **same**; otherwise, return `false`.

**Example 1:**

**Input:** s = "3902"

**Output:** true

**Explanation:**

* Initially, `s = "3902"`
* First operation:
* `(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2`
* `(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9`
* `(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2`
* `s` becomes `"292"`
* Second operation:
* `(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1`
* `(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1`
* `s` becomes `"11"`
* Since the digits in `"11"` are the same, the output is `true`.

**Example 2:**

**Input:** s = "34789"

**Output:** false

**Explanation:**

* Initially, `s = "34789"`.
* After the first operation, `s = "7157"`.
* After the second operation, `s = "862"`.
* After the third operation, `s = "48"`.
* Since `'4' != '8'`, the output is `false`.

**Constraints:**

* <code>3 <= s.length <= 10<sup>5</sup></code>
* `s` consists of only digits.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package g3401_3500.s3464_maximize_the_distance_between_points_on_a_square;

// #Hard #Array #Greedy #Binary_Search #2025_02_25_Time_18_ms_(98.51%)_Space_49.78_MB_(46.27%)

import java.util.Arrays;

public class Solution {
public int maxDistance(int side, int[][] pts, int k) {
int n = pts.length;
long[] p = new long[n];
for (int i = 0; i < n; i++) {
int x = pts[i][0];
int y = pts[i][1];
long c;
if (y == 0) {
c = x;
} else if (x == side) {
c = side + (long) y;
} else if (y == side) {
c = 2L * side + (side - x);
} else {
c = 3L * side + (side - y);
}
p[i] = c;
}
Arrays.sort(p);
long c = 4L * side;
int tot = 2 * n;
long[] dArr = new long[tot];
for (int i = 0; i < n; i++) {
dArr[i] = p[i];
dArr[i + n] = p[i] + c;
}
int lo = 0;
int hi = 2 * side;
int ans = 0;
while (lo <= hi) {
int mid = (lo + hi) >>> 1;
if (check(mid, dArr, n, k, c)) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}

private boolean check(int d, long[] dArr, int n, int k, long c) {
int len = dArr.length;
int[] nxt = new int[len];
int j = 0;
for (int i = 0; i < len; i++) {
if (j < i + 1) {
j = i + 1;
}
while (j < len && dArr[j] < dArr[i] + d) {
j++;
}
nxt[i] = (j < len) ? j : -1;
}
for (int i = 0; i < n; i++) {
int cnt = 1;
int cur = i;
while (cnt < k) {
int nx = nxt[cur];
if (nx == -1 || nx >= i + n) {
break;
}
cur = nx;
cnt++;
}
if (cnt == k && (dArr[i] + c - dArr[cur]) >= d) {
return true;
}
}
return false;
}
}
Loading