Skip to content

Added tasks 3643-3646 #872

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 8 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_46_ms_(91.67%)_Space_133.87_MB_(16.67%)
// #Medium #Array #Math #Sorting #Greedy #Game_Theory #Weekly_Contest_460
// #2025_07_27_Time_46_ms_(91.67%)_Space_133.87_MB_(16.67%)

class Solution {
fun maximumMedianSum(nums: IntArray): Long {
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_13_ms_(100.00%)_Space_48.00_MB_(75.00%)
// #Medium #String #Dynamic_Programming #Greedy #Prefix_Sum #Weekly_Contest_460
// #2025_07_27_Time_13_ms_(100.00%)_Space_48.00_MB_(75.00%)

class Solution {
fun numOfSubsequences(s: String): Long {
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_406_ms_(100.00%)_Space_153.64_MB_(100.00%)
// #Medium #Array #Hash_Table #Math #Breadth_First_Search #Number_Theory #Weekly_Contest_460
// #2025_07_27_Time_406_ms_(100.00%)_Space_153.64_MB_(100.00%)

import java.util.ArrayDeque
import kotlin.math.max
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_15_ms_(100.00%)_Space_48.53_MB_(100.00%)
// #Easy #Array #Sorting #Greedy #Binary_Search #Two_Pointers #Biweekly_Contest_162
// #2025_08_03_Time_15_ms_(100.00%)_Space_48.53_MB_(100.00%)

import kotlin.math.max
import kotlin.math.min
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_43_ms_(100.00%)_Space_66.87_MB_(100.00%)
// #Medium #Array #Sorting #Sliding_Window #Biweekly_Contest_162
// #2025_08_03_Time_43_ms_(100.00%)_Space_66.87_MB_(100.00%)

import kotlin.math.max

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_5_ms_(100.00%)_Space_73.02_MB_(100.00%)
// #Medium #Array #Sorting #Greedy #Binary_Search #Two_Pointers #Biweekly_Contest_162
// #2025_08_03_Time_5_ms_(100.00%)_Space_73.02_MB_(100.00%)

import kotlin.math.max
import kotlin.math.min
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_03_Time_848_ms_(100.00%)_Space_99.90_MB_(100.00%)
// #Hard #Array #Hash_Table #Binary_Search #Prefix_Sum #Counting #Divide_and_Conquer
// #Biweekly_Contest_162 #2025_08_03_Time_848_ms_(100.00%)_Space_99.90_MB_(100.00%)

import java.util.TreeSet
import kotlin.math.max
Expand Down
37 changes: 17 additions & 20 deletions src/main/kotlin/g3601_3700/s3637_trionic_array_i/Solution.kt
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_43.69_MB_(100.00%)
// #Easy #Array #Weekly_Contest_461 #2025_08_14_Time_1_ms_(100.00%)_Space_43.95_MB_(38.57%)

class Solution {
fun isTrionic(nums: IntArray): Boolean {
var p = 0
var q = 0
var i = 1
val n = nums.size
for (i in 1..<n - 1) {
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_5_ms_(100.00%)_Space_78.25_MB_(100.00%)
// #Medium #Array #Dynamic_Programming #Greedy #Stack #Monotonic_Stack #Weekly_Contest_461
// #2025_08_03_Time_5_ms_(100.00%)_Space_78.25_MB_(100.00%)

import kotlin.math.max

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_160_ms_(100.00%)_Space_85.36_MB_(100.00%)

import java.util.TreeSet
// #Medium #Array #Binary_Search #Weekly_Contest_461
// #2025_08_14_Time_7_ms_(100.00%)_Space_79.04_MB_(50.00%)

class Solution {
fun minTime(s: String, order: IntArray, k: Int): Int {
val n = s.length
// Use a TreeSet to maintain a sorted list of indices
val pos = TreeSet<Int?>()
pos.add(-1)
pos.add(n)
// Iterate through the order of removal
var localK = k
for (t in order.indices) {
var total = n * (n + 1L) / 2
if (total < k) {
return -1
}
val prev = IntArray(n + 1)
val next = IntArray(n + 1)
for (i in 0..<n) {
prev[i] = i - 1
next[i] = i + 1
}
for (t in n - 1 downTo 0) {
val i = order[t]
// Find the elements in the sorted set that bracket the current index 'i'
// 'r' is the smallest element >= i
val r = pos.ceiling(i)
// 'l' is the largest element <= i
val l = pos.floor(i)
// The 'cost' to remove an item is the product of the distances to its neighbors
localK -= ((i - l!!).toLong() * (r!! - i)).toInt()
pos.add(i)
// If the total cost is exhausted, return the current time 't'
if (localK <= 0) {
val left = prev[i]
val right = next[i]
total -= (i - left).toLong() * (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_7_ms_(100.00%)_Space_79.14_MB_(100.00%)
// #Hard #Array #Dynamic_Programming #Weekly_Contest_461
// #2025_08_03_Time_7_ms_(100.00%)_Space_79.14_MB_(100.00%)

import kotlin.math.max

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_11_Time_1_ms_(100.00%)_Space_49.92_MB_(100.00%)

class Solution {
fun reverseSubmatrix(grid: Array<IntArray>, x: Int, y: Int, k: Int): Array<IntArray> {
for (i in 0..<k / 2) {
val top = x + i
val bottom = x + k - 1 - i
for (col in 0..<k) {
val 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_11_Time_4_ms_(100.00%)_Space_83.96_MB_(100.00%)

class Solution {
fun sortPermutation(nums: IntArray): Int {
val n = nums.size
var res = -1
for (i in 0..<n) {
if (nums[i] == i) {
continue
}
if (res == -1) {
res = nums[i]
} else {
res = res and 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,34 @@
package g3601_3700.s3645_maximum_total_from_optimal_activation_order

// #Medium #Array #Sorting #Greedy #Two_Pointers #Heap_Priority_Queue #Weekly_Contest_462
// #2025_08_11_Time_77_ms_(100.00%)_Space_88.24_MB_(100.00%)

import java.util.Collections
import kotlin.math.min

class Solution {
fun maxTotal(value: IntArray, limit: IntArray): Long {
val n = value.size
val groups: Array<MutableList<Int>?> = arrayOfNulls<MutableList<Int>>(n + 1)
for (i in 0..<n) {
val l = limit[i]
if (groups[l] == null) {
groups[l] = ArrayList<Int>()
}
groups[l]!!.add(value[i])
}
var total: Long = 0
for (l in 1..n) {
val list = groups[l]
if (list == null) {
continue
}
list.sortWith(Collections.reverseOrder<Int>())
val cap = min(l, list.size)
for (i in 0..<cap) {
total += list[i].toLong()
}
}
return total
}
}
Loading
Loading