Skip to content

Added tasks 3643-3646 #2031

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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
@@ -1,6 +1,7 @@
package g3601_3700.s3627_maximum_median_sum_of_subsequences_of_size_3;

// #Medium #Weekly_Contest_460 #2025_07_27_Time_22_ms_(100.00%)_Space_129.50_MB_(86.95%)
// #Medium #Array #Math #Sorting #Greedy #Game_Theory #Weekly_Contest_460
// #2025_08_14_Time_23_ms_(98.36%)_Space_129.60_MB_(75.26%)

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package g3601_3700.s3628_maximum_number_of_subsequences_after_one_inserting;

// #Medium #Weekly_Contest_460 #2025_07_27_Time_12_ms_(100.00%)_Space_45.76_MB_(72.28%)
// #Medium #String #Dynamic_Programming #Greedy #Prefix_Sum #Weekly_Contest_460
// #2025_08_14_Time_12_ms_(100.00%)_Space_45.79_MB_(76.82%)

public class Solution {
public long numOfSubsequences(String s) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package g3601_3700.s3629_minimum_jumps_to_reach_end_via_prime_teleportation;

// #Medium #Weekly_Contest_460 #2025_07_27_Time_116_ms_(99.81%)_Space_76.00_MB_(67.96%)
// #Medium #Array #Hash_Table #Math #Breadth_First_Search #Number_Theory #Weekly_Contest_460
// #2025_08_14_Time_112_ms_(99.76%)_Space_76.04_MB_(74.71%)

import java.util.ArrayDeque;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package g3601_3700.s3633_earliest_finish_time_for_land_and_water_rides_i;

// #Easy #Biweekly_Contest_162 #2025_08_03_Time_3_ms_(100.00%)_Space_45.04_MB_(33.33%)
// #Easy #Array #Sorting #Greedy #Binary_Search #Two_Pointers #Biweekly_Contest_162
// #2025_08_14_Time_3_ms_(93.86%)_Space_45.39_MB_(29.02%)

public class Solution {
public int earliestFinishTime(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package g3601_3700.s3634_minimum_removals_to_balance_array;

// #Medium #Biweekly_Contest_162 #2025_08_03_Time_21_ms_(100.00%)_Space_60.28_MB_(100.00%)
// #Medium #Array #Sorting #Sliding_Window #Biweekly_Contest_162
// #2025_08_14_Time_20_ms_(99.40%)_Space_60.70_MB_(34.02%)

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package g3601_3700.s3635_earliest_finish_time_for_land_and_water_rides_ii;

// #Medium #Biweekly_Contest_162 #2025_08_03_Time_2_ms_(100.00%)_Space_55.88_MB_(50.00%)
// #Medium #Array #Sorting #Greedy #Binary_Search #Two_Pointers #Biweekly_Contest_162
// #2025_08_14_Time_2_ms_(100.00%)_Space_55.78_MB_(68.53%)

public class Solution {
public int earliestFinishTime(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package g3601_3700.s3636_threshold_majority_queries;

// #Hard #Biweekly_Contest_162 #2025_08_06_Time_82_ms_(98.38%)_Space_71.28_MB_(74.76%)
// #Hard #Array #Hash_Table #Binary_Search #Prefix_Sum #Counting #Divide_and_Conquer
// #Biweekly_Contest_162 #2025_08_14_Time_83_ms_(96.87%)_Space_71.30_MB_(75.24%)

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
37 changes: 17 additions & 20 deletions src/main/java/g3601_3700/s3637_trionic_array_i/Solution.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
package g3601_3700.s3637_trionic_array_i;

// #Easy #Weekly_Contest_461 #2025_08_03_Time_1_ms_(100.00%)_Space_42.62_MB_(100.00%)
// #Easy #Array #Weekly_Contest_461 #2025_08_14_Time_0_ms_(100.00%)_Space_43.15_MB_(44.56%)

public class Solution {
public boolean isTrionic(int[] nums) {
int p = 0;
int q = 0;
int i = 1;
int n = nums.length;
for (int i = 1; i < n - 1; ++i) {
if (nums[i - 1] == nums[i]) {
return false;
}
if (nums[i - 1] < nums[i] && nums[i] > nums[i + 1]) {
if (p != 0) {
return false;
}
p = i;
}
if (nums[i - 1] > nums[i] && nums[i] < nums[i + 1]) {
if (p == 0 || q != 0) {
return false;
}
q = i;
}
while (i < n && nums[i] > nums[i - 1]) {
i++;
}
return q > 0;
if (i == n || i == 1) {
return false;
}
while (i < n && nums[i] < nums[i - 1]) {
i++;
}
if (i == n) {
return false;
}
while (i < n && nums[i] > nums[i - 1]) {
i++;
}
return i == n;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package g3601_3700.s3638_maximum_balanced_shipments;

// #Medium #Weekly_Contest_461 #2025_08_03_Time_3_ms_(100.00%)_Space_62.17_MB_(100.00%)
// #Medium #Array #Dynamic_Programming #Greedy #Stack #Monotonic_Stack #Weekly_Contest_461
// #2025_08_14_Time_2_ms_(100.00%)_Space_62.34_MB_(68.23%)

public class Solution {
public int maxBalancedShipments(int[] weight) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package g3601_3700.s3639_minimum_time_to_activate_string;

// #Medium #Weekly_Contest_461 #2025_08_03_Time_122_ms_(100.00%)_Space_58.43_MB_(100.00%)

import java.util.TreeSet;
// #Medium #Array #Binary_Search #Weekly_Contest_461
// #2025_08_14_Time_6_ms_(99.91%)_Space_57.07_MB_(91.05%)

public class Solution {
public int minTime(String s, int[] order, int k) {
int n = s.length();
// Use a TreeSet to maintain a sorted list of indices
TreeSet<Integer> pos = new TreeSet<>();
pos.add(-1);
pos.add(n);
// Iterate through the order of removal
int localK = k;
for (int t = 0; t < order.length; ++t) {
long total = n * (n + 1L) / 2;
if (total < k) {
return -1;
}
int[] prev = new int[n + 1];
int[] next = new int[n + 1];
for (int i = 0; i < n; ++i) {
prev[i] = i - 1;
next[i] = i + 1;
}
for (int t = n - 1; t >= 0; t--) {
int i = order[t];
// Find the elements in the sorted set that bracket the current index 'i'
// 'r' is the smallest element >= i
Integer r = pos.ceiling(i);
// 'l' is the largest element <= i
Integer l = pos.floor(i);
// The 'cost' to remove an item is the product of the distances to its neighbors
localK -= (int) ((long) (i - l) * (r - i));
pos.add(i);
// If the total cost is exhausted, return the current time 't'
if (localK <= 0) {
int left = prev[i];
int right = next[i];
total -= (long) (i - left) * (right - i);
if (total < k) {
return t;
}
if (left >= 0) {
next[left] = right;
}
prev[right] = left;
}
// If all items are removed and k is not exhausted, return -1
return -1;
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package g3601_3700.s3640_trionic_array_ii;

// #Hard #Weekly_Contest_461 #2025_08_03_Time_4_ms_(100.00%)_Space_58.85_MB_(100.00%)
// #Hard #Array #Dynamic_Programming #Weekly_Contest_461
// #2025_08_14_Time_4_ms_(87.79%)_Space_61.72_MB_(36.78%)

public class Solution {
public long maxSumTrionic(int[] nums) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g3601_3700.s3643_flip_square_submatrix_vertically;

// #Easy #Array #Matrix #Two_Pointers #Weekly_Contest_462
// #2025_08_14_Time_0_ms_(100.00%)_Space_45.75_MB_(56.07%)

public class Solution {
public int[][] reverseSubmatrix(int[][] grid, int x, int y, int k) {
for (int i = 0; i < k / 2; i++) {
int top = x + i;
int bottom = x + k - 1 - i;
for (int col = 0; col < k; col++) {
int temp = grid[top][y + col];
grid[top][y + col] = grid[bottom][y + col];
grid[bottom][y + col] = temp;
}
}
return grid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
3643\. Flip Square Submatrix Vertically

Easy

You are given an `m x n` integer matrix `grid`, and three integers `x`, `y`, and `k`.

The integers `x` and `y` represent the row and column indices of the **top-left** corner of a **square** submatrix and the integer `k` represents the size (side length) of the square submatrix.

Your task is to flip the submatrix by reversing the order of its rows vertically.

Return the updated matrix.

**Example 1:**

![](https://assets.leetcode.com/uploads/2025/07/20/gridexmdrawio.png)

**Input:** grid = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], x = 1, y = 0, k = 3

**Output:** [[1,2,3,4],[13,14,15,8],[9,10,11,12],[5,6,7,16]]

**Explanation:**

The diagram above shows the grid before and after the transformation.

**Example 2:**

![](https://assets.leetcode.com/uploads/2025/07/20/gridexm2drawio.png)

**Input:** grid = [[3,4,2,3],[2,3,4,2]], x = 0, y = 2, k = 2

**Output:** [[3,4,4,2],[2,3,2,3]]

**Explanation:**

The diagram above shows the grid before and after the transformation.

**Constraints:**

* `m == grid.length`
* `n == grid[i].length`
* `1 <= m, n <= 50`
* `1 <= grid[i][j] <= 100`
* `0 <= x < m`
* `0 <= y < n`
* `1 <= k <= min(m - x, n - y)`
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package g3601_3700.s3644_maximum_k_to_sort_a_permutation;

// #Medium #Weekly_Contest_462 #2025_08_14_Time_1_ms_(100.00%)_Space_61.65_MB_(71.89%)

public class Solution {
public int sortPermutation(int[] nums) {
int n = nums.length;
int res = -1;
for (int i = 0; i < n; i++) {
if (nums[i] == i) {
continue;
}
if (res == -1) {
res = nums[i];
} else {
res &= nums[i];
}
}
if (res == -1) {
return 0;
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
3644\. Maximum K to Sort a Permutation

Medium

You are given an integer array `nums` of length `n`, where `nums` is a **permutation** of the numbers in the range `[0..n - 1]`.

You may swap elements at indices `i` and `j` **only if** `nums[i] AND nums[j] == k`, where `AND` denotes the bitwise AND operation and `k` is a **non-negative** integer.

Return the **maximum** value of `k` such that the array can be sorted in **non-decreasing** order using any number of such swaps. If `nums` is already sorted, return 0.

A **permutation** is a rearrangement of all the elements of an array.

**Example 1:**

**Input:** nums = [0,3,2,1]

**Output:** 1

**Explanation:**

Choose `k = 1`. Swapping `nums[1] = 3` and `nums[3] = 1` is allowed since `nums[1] AND nums[3] == 1`, resulting in a sorted permutation: `[0, 1, 2, 3]`.

**Example 2:**

**Input:** nums = [0,1,3,2]

**Output:** 2

**Explanation:**

Choose `k = 2`. Swapping `nums[2] = 3` and `nums[3] = 2` is allowed since `nums[2] AND nums[3] == 2`, resulting in a sorted permutation: `[0, 1, 2, 3]`.

**Example 3:**

**Input:** nums = [3,2,1,0]

**Output:** 0

**Explanation:**

Only `k = 0` allows sorting since no greater `k` allows the required swaps where `nums[i] AND nums[j] == k`.

**Constraints:**

* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
* `0 <= nums[i] <= n - 1`
* `nums` is a permutation of integers from `0` to `n - 1`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package g3601_3700.s3645_maximum_total_from_optimal_activation_order;

// #Medium #Array #Sorting #Greedy #Two_Pointers #Heap_Priority_Queue #Weekly_Contest_462
// #2025_08_14_Time_37_ms_(96.20%)_Space_63.84_MB_(42.59%)

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@SuppressWarnings("unchecked")
public class Solution {
public long maxTotal(int[] value, int[] limit) {
int n = value.length;
List<Integer>[] groups = new ArrayList[n + 1];
for (int i = 0; i < n; i++) {
int l = limit[i];
if (groups[l] == null) {
groups[l] = new ArrayList<>();
}
groups[l].add(value[i]);
}
long total = 0;
for (int l = 1; l <= n; l++) {
List<Integer> list = groups[l];
if (list == null) {
continue;
}
list.sort(Collections.reverseOrder());
int cap = Math.min(l, list.size());
for (int i = 0; i < cap; i++) {
total += list.get(i);
}
}
return total;
}
}
Loading
Loading