Skip to content
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

Added tasks 3461-3464 #1915

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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,19 @@
package g3401_3500.s3461_check_if_digits_are_equal_in_string_after_operations_i;

// #Easy #2025_02_23_Time_23_ms_(100.00%)_Space_45.51_MB_(100.00%)

public class Solution {
public boolean hasSameDigits(String s) {
int n = s.length();
while (n > 2) {
StringBuilder nstr = new StringBuilder();
for (int i = 1; i < n; i++) {
int next = ((s.charAt(i) - '0') + (s.charAt(i - 1) - '0')) % 10;
nstr.append(next);
}
n--;
s = nstr.toString();
}
return s.charAt(0) == s.charAt(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,34 @@
package g3401_3500.s3462_maximum_sum_with_at_most_k_elements;

// #Medium #2025_02_23_Time_278_ms_(_%)_Space_73.54_MB_(_%)

import java.util.Collections;
import java.util.PriorityQueue;

public class Solution {
public long maxSum(int[][] grid, int[] limits, int k) {
if (grid.length == 0) {
return 0;
}
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue<Integer> temp;
for (int i = 0; i < grid.length; i++) {
temp = new PriorityQueue<>(Collections.reverseOrder());
for (int j = 0; j < grid[i].length; j++) {
temp.add(grid[i][j]);
}
int cnt = 0;
while (!temp.isEmpty() && cnt < limits[i]) {
pq.add(temp.poll());
cnt += 1;
}
}
long result = 0;
long count = 0;
while (!pq.isEmpty() && count < k) {
result += pq.poll();
count += 1;
}
return result;
}
}
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,49 @@
package g3401_3500.s3463_check_if_digits_are_equal_in_string_after_operations_ii;

// #Hard #2025_02_23_Time_103_ms_(100.00%)_Space_45.58_MB_(100.00%)

public class Solution {
public boolean hasSameDigits(String s) {
int n = s.length();
int nMunus2 = n - 2;
int f0 = 0;
int f1 = 0;
for (int j = 0; j <= nMunus2; j++) {
int c = binomMod10(nMunus2, j);
f0 = (f0 + c * (s.charAt(j) - '0')) % 10;
f1 = (f1 + c * (s.charAt(j + 1) - '0')) % 10;
}
return f0 == f1;
}

private int binomMod10(int n, int k) {
int r2 = binomMod2(n, k);
int r5 = binomMod5(n, k);
for (int x = 0; x < 10; x++) {
if (x % 2 == r2 && x % 5 == r5) {
return x;
}
}
return 0;
}

private int binomMod2(int n, int k) {
return ((n & k) == k) ? 1 : 0;
}

private int binomMod5(int n, int k) {
int[][] t = {{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 1, 4, 1}};
int res = 1;
while (n > 0 || k > 0) {
int nd = n % 5;
int kd = k % 5;
if (kd > nd) {
return 0;
}
res = (res * t[nd][kd]) % 5;
n /= 5;
k /= 5;
}
return res;
}
}
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,101 @@
package g3401_3500.s3464_maximize_the_distance_between_points_on_a_square;

// #Hard #2025_02_23_Time_222_ms_(100.00%)_Space_52.33_MB_(100.00%)

import java.util.Arrays;

public class Solution {
public int maxDistance(int sideLength, int[][] points, int requiredPoints) {
long perimeter = 4L * sideLength;
int numPoints = points.length;
long[] mappedPositions = new long[numPoints];
for (int i = 0; i < numPoints; i++) {
mappedPositions[i] = mapToPerimeter(sideLength, points[i][0], points[i][1]);
}
Arrays.sort(mappedPositions);
long low = 0;
long high = perimeter;
while (low < high) {
long mid = (low + high + 1) / 2;
if (isValidDistance(mid, requiredPoints, mappedPositions, perimeter)) {
low = mid;
} else {
high = mid - 1;
}
}
return (int) low;
}

private long mapToPerimeter(int sideLength, int x, int y) {
if (y == sideLength) {
return x;
}
if (x == sideLength) {
return sideLength + (long) (sideLength - y);
}
if (y == 0) {
return 2L * sideLength + (sideLength - x);
}
return 3L * sideLength + y;
}

private boolean isValidDistance(
long minDistance, int requiredPoints, long[] mappedPositions, long perimeter) {
int numPoints = mappedPositions.length;
long[] extendedPositions = new long[2 * numPoints];
for (int i = 0; i < numPoints; i++) {
extendedPositions[i] = mappedPositions[i];
extendedPositions[i + numPoints] = mappedPositions[i] + perimeter;
}
for (int i = 0; i < numPoints; i++) {
if (canSelectRequiredPoints(
i,
minDistance,
requiredPoints,
mappedPositions,
extendedPositions,
perimeter)) {
return true;
}
}
return false;
}

private boolean canSelectRequiredPoints(
int startIndex,
long minDistance,
int requiredPoints,
long[] mappedPositions,
long[] extendedPositions,
long perimeter) {
int selectedCount = 1;
long previousPosition = mappedPositions[startIndex];
int currentIndex = startIndex;
for (int i = 1; i < requiredPoints; i++) {
long targetPosition = previousPosition + minDistance;
int left = currentIndex + 1;
int right = startIndex + mappedPositions.length;
int nextIndex = lowerBound(extendedPositions, left, right, targetPosition);
if (nextIndex >= right) {
return false;
}
selectedCount++;
previousPosition = extendedPositions[nextIndex];
currentIndex = nextIndex;
}
return selectedCount == requiredPoints
&& (previousPosition - mappedPositions[startIndex] <= perimeter - minDistance);
}

private int lowerBound(long[] arr, int left, int right, long target) {
while (left < right) {
int mid = left + (right - left) / 2;
if (arr[mid] >= target) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
}
Loading