Skip to content
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
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,37 @@

| # | Title | Difficulty | Tag | Time, ms | Time, %
|------|----------------|-------------|-------------|----------|--------
| 3108 |[Minimum Cost Walk in Weighted Graph](src/main/kotlin/g3101_3200/s3108_minimum_cost_walk_in_weighted_graph)| Hard | Array, Bit_Manipulation, Graph, Union_Find | 791 | 100.00
| 3107 |[Minimum Operations to Make Median of Array Equal to K](src/main/kotlin/g3101_3200/s3107_minimum_operations_to_make_median_of_array_equal_to_k)| Medium | Array, Sorting, Greedy | 554 | 100.00
| 3106 |[Lexicographically Smallest String After Operations With Constraint](src/main/kotlin/g3101_3200/s3106_lexicographically_smallest_string_after_operations_with_constraint)| Medium | String, Greedy | 162 | 74.19
| 3105 |[Longest Strictly Increasing or Strictly Decreasing Subarray](src/main/kotlin/g3101_3200/s3105_longest_strictly_increasing_or_strictly_decreasing_subarray)| Easy | Array | 159 | 94.00
| 3102 |[Minimize Manhattan Distances](src/main/kotlin/g3101_3200/s3102_minimize_manhattan_distances)| Hard | Array, Math | 701 | 95.83
| 3101 |[Count Alternating Subarrays](src/main/kotlin/g3101_3200/s3101_count_alternating_subarrays)| Medium | Array, Math | 499 | 97.78
| 3100 |[Water Bottles II](src/main/kotlin/g3001_3100/s3100_water_bottles_ii)| Medium | Math, Simulation | 137 | 70.49
| 3099 |[Harshad Number](src/main/kotlin/g3001_3100/s3099_harshad_number)| Easy | Math | 119 | 82.67
| 3098 |[Find the Sum of Subsequence Powers](src/main/kotlin/g3001_3100/s3098_find_the_sum_of_subsequence_powers)| Hard | Array, Dynamic_Programming, Sorting | 294 | 77.78
| 3097 |[Shortest Subarray With OR at Least K II](src/main/kotlin/g3001_3100/s3097_shortest_subarray_with_or_at_least_k_ii)| Medium | Array, Bit_Manipulation, Sliding_Window | 489 | 93.33
| 3096 |[Minimum Levels to Gain More Points](src/main/kotlin/g3001_3100/s3096_minimum_levels_to_gain_more_points)| Medium | Array, Prefix_Sum | 850 | 100.00
| 3095 |[Shortest Subarray With OR at Least K I](src/main/kotlin/g3001_3100/s3095_shortest_subarray_with_or_at_least_k_i)| Easy | Array, Bit_Manipulation, Sliding_Window | 161 | 95.65
| 3093 |[Longest Common Suffix Queries](src/main/kotlin/g3001_3100/s3093_longest_common_suffix_queries)| Hard | Array, String, Trie | 860 | 89.29
| 3092 |[Most Frequent IDs](src/main/kotlin/g3001_3100/s3092_most_frequent_ids)| Medium | Array, Hash_Table, Heap_Priority_Queue, Ordered_Set | 758 | 100.00
| 3091 |[Apply Operations to Make Sum of Array Greater Than or Equal to k](src/main/kotlin/g3001_3100/s3091_apply_operations_to_make_sum_of_array_greater_than_or_equal_to_k)| Medium | Math, Greedy, Enumeration | 134 | 73.21
| 3090 |[Maximum Length Substring With Two Occurrences](src/main/kotlin/g3001_3100/s3090_maximum_length_substring_with_two_occurrences)| Easy | String, Hash_Table, Sliding_Window | 157 | 90.24
| 3086 |[Minimum Moves to Pick K Ones](src/main/kotlin/g3001_3100/s3086_minimum_moves_to_pick_k_ones)| Hard | Array, Greedy, Prefix_Sum, Sliding_Window | 368 | 100.00
| 3085 |[Minimum Deletions to Make String K-Special](src/main/kotlin/g3001_3100/s3085_minimum_deletions_to_make_string_k_special)| Medium | String, Hash_Table, Sorting, Greedy, Counting | 221 | 93.33
| 3084 |[Count Substrings Starting and Ending with Given Character](src/main/kotlin/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character)| Medium | String, Math, Counting | 177 | 98.55
| 3083 |[Existence of a Substring in a String and Its Reverse](src/main/kotlin/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse)| Easy | String, Hash_Table | 168 | 79.49
| 3082 |[Find the Sum of the Power of All Subsequences](src/main/kotlin/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences)| Hard | Array, Dynamic_Programming | 176 | 90.00
| 3081 |[Replace Question Marks in String to Minimize Its Value](src/main/kotlin/g3001_3100/s3081_replace_question_marks_in_string_to_minimize_its_value)| Medium | String, Hash_Table, Sorting, Greedy, Heap_Priority_Queue, Counting | 249 | 100.00
| 3080 |[Mark Elements on Array by Performing Queries](src/main/kotlin/g3001_3100/s3080_mark_elements_on_array_by_performing_queries)| Medium | Array, Hash_Table, Sorting, Heap_Priority_Queue, Simulation | 937 | 97.78
| 3079 |[Find the Sum of Encrypted Integers](src/main/kotlin/g3001_3100/s3079_find_the_sum_of_encrypted_integers)| Easy | Array, Math | 172 | 80.60
| 3077 |[Maximum Strength of K Disjoint Subarrays](src/main/kotlin/g3001_3100/s3077_maximum_strength_of_k_disjoint_subarrays)| Hard | Array, Dynamic_Programming, Prefix_Sum | 351 | 75.00
| 3076 |[Shortest Uncommon Substring in an Array](src/main/kotlin/g3001_3100/s3076_shortest_uncommon_substring_in_an_array)| Medium | Array, String, Hash_Table, Trie | 256 | 100.00
| 3075 |[Maximize Happiness of Selected Children](src/main/kotlin/g3001_3100/s3075_maximize_happiness_of_selected_children)| Medium | Array, Sorting, Greedy | 608 | 93.24
| 3074 |[Apple Redistribution into Boxes](src/main/kotlin/g3001_3100/s3074_apple_redistribution_into_boxes)| Easy | Array, Sorting, Greedy | 168 | 97.37
| 3072 |[Distribute Elements Into Two Arrays II](src/main/kotlin/g3001_3100/s3072_distribute_elements_into_two_arrays_ii)| Hard | Array, Simulation, Segment_Tree, Binary_Indexed_Tree | 890 | 100.00
| 3071 |[Minimum Operations to Write the Letter Y on a Grid](src/main/kotlin/g3001_3100/s3071_minimum_operations_to_write_the_letter_y_on_a_grid)| Medium | Array, Hash_Table, Matrix, Counting | 268 | 91.11
| 3070 |[Count Submatrices with Top-Left Element and Sum Less Than k](src/main/kotlin/g3001_3100/s3070_count_submatrices_with_top_left_element_and_sum_less_than_k)| Medium | Array, Matrix, Prefix_Sum | 773 | 85.71
| 3069 |[Distribute Elements Into Two Arrays I](src/main/kotlin/g3001_3100/s3069_distribute_elements_into_two_arrays_i)| Easy | Array, Simulation | 207 | 96.92
| 3068 |[Find the Maximum Sum of Node Values](src/main/kotlin/g3001_3100/s3068_find_the_maximum_sum_of_node_values)| Hard | Array, Dynamic_Programming, Sorting, Greedy, Tree, Bit_Manipulation | 531 | 66.67
| 3067 |[Count Pairs of Connectable Servers in a Weighted Tree Network](src/main/kotlin/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network)| Medium | Array, Depth_First_Search, Tree | 578 | 83.33
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ class Solution {
}
}
}
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,9 @@ Return _the maximum number of edges you can remove, or return_ `-1` _if it's imp
## Solution

```kotlin
import java.util.Arrays

class Solution {
fun maxNumEdgesToRemove(n: Int, edges: Array<IntArray>): Int {
Arrays.sort(edges) { a: IntArray, b: IntArray -> b[0] - a[0] }
edges.sortWith { a: IntArray, b: IntArray -> b[0] - a[0] }
val alice = IntArray(n + 1)
val rankAlice = IntArray(n + 1)
val bob = IntArray(n + 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ The rank of matrix[1][1] is 3 because matrix[1][1] > matrix[0][1], matrix[1][1]
## Solution

```kotlin
import java.util.Arrays

class Solution {
fun matrixRankTransform(matrix: Array<IntArray>): Array<IntArray> {
val rowCount = matrix.size
Expand Down Expand Up @@ -108,7 +106,7 @@ class Solution {
} else {
val rowCount = matrix.size
val ufind = IntArray(rowCount + matrix[0].size)
Arrays.fill(ufind, -1)
ufind.fill(-1)
for (nIdx in startIdx until endIdx) {
val r = nums[nIdx].toInt() shr 16 and 0xFFFF
val c = nums[nIdx].toInt() and 0xFFFF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,9 @@ Starting with 27 energy, we finish the tasks in the following order:
## Solution

```kotlin
import java.util.Arrays

class Solution {
fun minimumEffort(tasks: Array<IntArray>): Int {
Arrays.sort(tasks) { a: IntArray, b: IntArray -> a[1] - a[0] - b[1] + b[0] }
tasks.sortWith { a: IntArray, b: IntArray -> a[1] - a[0] - b[1] + b[0] }
var prev = 0
for (item in tasks) {
prev = Math.max(prev + item[0], item[1])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,13 @@ The maximum height of stacked cuboids is 6 \* 17 = 102.
## Solution

```kotlin
import java.util.Arrays

class Solution {
fun maxHeight(cuboids: Array<IntArray>): Int {
for (a in cuboids) {
a.sort()
}
Arrays.sort(
cuboids
) { a: IntArray, b: IntArray ->

cuboids.sortWith sort@{ a: IntArray, b: IntArray ->
if (a[0] != b[0]) {
return@sort a[0] - b[0]
} else if (a[1] != b[1]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ For the second query, there is a path (0 -> 1 -> 2) of two edges with distances
## Solution

```kotlin
import java.util.Arrays

class Solution {
private class Dsu(n: Int) {
private val parent: IntArray
Expand Down Expand Up @@ -77,12 +75,12 @@ class Solution {
}

fun distanceLimitedPathsExist(n: Int, edgeList: Array<IntArray>, queries: Array<IntArray>): BooleanArray {
Arrays.sort(edgeList) { o1: IntArray, o2: IntArray -> Integer.compare(o1[2], o2[2]) }
edgeList.sortWith { o1: IntArray, o2: IntArray -> Integer.compare(o1[2], o2[2]) }
val data = Array(queries.size) { IntArray(4) }
for (i in queries.indices) {
data[i] = intArrayOf(queries[i][0], queries[i][1], queries[i][2], i)
}
Arrays.sort(data) { o1: IntArray, o2: IntArray -> Integer.compare(o1[2], o2[2]) }
data.sortWith { o1: IntArray, o2: IntArray -> Integer.compare(o1[2], o2[2]) }
val d = Dsu(n)
var j = 0
val ans = BooleanArray(queries.size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ Return _the **maximum** total number of **units** that can be put on the truck._
## Solution

```kotlin
import java.util.Arrays

@Suppress("NAME_SHADOWING")
class Solution {
fun maximumUnits(boxTypes: Array<IntArray>, truckSize: Int): Int {
var truckSize = truckSize
Arrays.sort(boxTypes) { b1: IntArray, b2: IntArray -> Integer.compare(b2[1], b1[1]) }
boxTypes.sortWith { b1: IntArray, b2: IntArray -> Integer.compare(b2[1], b1[1]) }
var maxUnits = 0
var i = 0
while (truckSize > 0 && i < boxTypes.size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Solution {
}
}
val n = events.size
Arrays.sort(events, { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) })
events.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) }
val memo = Array(n) { IntArray(k + 1) }
return dfs(events, 0, k, memo)
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/g1801_1900/s1847_closest_room/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ Query = [2,5]: Room number 3 is the only room with a size of at least 5. The ans
## Solution

```kotlin
import java.util.Arrays
import java.util.TreeSet

class Solution {
Expand All @@ -66,8 +65,8 @@ class Solution {
for (i in 0 until numQuery) {
queries[i] = intArrayOf(queries[i][0], queries[i][1], i)
}
Arrays.sort(rooms) { a: IntArray, b: IntArray -> if (a[1] != b[1]) a[1] - b[1] else a[0] - b[0] }
Arrays.sort(queries) { a: IntArray, b: IntArray -> if (a[1] != b[1]) a[1] - b[1] else a[0] - b[0] }
rooms.sortWith { a: IntArray, b: IntArray -> if (a[1] != b[1]) a[1] - b[1] else a[0] - b[0] }
queries.sortWith { a: IntArray, b: IntArray -> if (a[1] != b[1]) a[1] - b[1] else a[0] - b[0] }
val roomIds = TreeSet<Int>()
val result = IntArray(numQuery)
var j = numRoom - 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ Return _an array containing the answers to the queries_.
## Solution

```kotlin
import java.util.Arrays
import java.util.PriorityQueue

class Solution {
Expand All @@ -64,8 +63,8 @@ class Solution {
for (i in 0 until numQuery) {
queriesWithIndex[i] = intArrayOf(queries[i], i)
}
Arrays.sort(intervals, { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) })
Arrays.sort(queriesWithIndex, { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) })
intervals.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) }
queriesWithIndex.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) }
val minHeap = PriorityQueue({ a: IntArray, b: IntArray -> (a[1] - a[0]).compareTo(b[1] - b[0]) })
val result = IntArray(numQuery)
var j = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)

## 3070\. Count Submatrices with Top-Left Element and Sum Less Than k

Medium

You are given a **0-indexed** integer matrix `grid` and an integer `k`.

Return _the **number** of submatrices that contain the top-left element of the_ `grid`, _and have a sum less than or equal to_ `k`.

**Example 1:**

![](https://assets.leetcode.com/uploads/2024/01/01/example1.png)

**Input:** grid = \[\[7,6,3],[6,6,1]], k = 18

**Output:** 4

**Explanation:** There are only 4 submatrices, shown in the image above, that contain the top-left element of grid, and have a sum less than or equal to 18.

**Example 2:**

![](https://assets.leetcode.com/uploads/2024/01/01/example21.png)

**Input:** grid = \[\[7,2,9],[1,5,0],[2,6,6]], k = 20

**Output:** 6

**Explanation:** There are only 6 submatrices, shown in the image above, that contain the top-left element of grid, and have a sum less than or equal to 20.

**Constraints:**

* `m == grid.length`
* `n == grid[i].length`
* `1 <= n, m <= 1000`
* `0 <= grid[i][j] <= 1000`
* <code>1 <= k <= 10<sup>9</sup></code>

## Solution

```kotlin
class Solution {
fun countSubmatrices(grid: Array<IntArray>, k: Int): Int {
val n = grid[0].size
val sums = IntArray(n)
var ans = 0
for (ints in grid) {
var sum = 0
for (col in 0 until n) {
sum += ints[col]
sums[col] += sum
if (sums[col] <= k) {
ans++
} else {
break
}
}
}
return ans
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)

## 3071\. Minimum Operations to Write the Letter Y on a Grid

Medium

You are given a **0-indexed** `n x n` grid where `n` is odd, and `grid[r][c]` is `0`, `1`, or `2`.

We say that a cell belongs to the Letter **Y** if it belongs to one of the following:

* The diagonal starting at the top-left cell and ending at the center cell of the grid.
* The diagonal starting at the top-right cell and ending at the center cell of the grid.
* The vertical line starting at the center cell and ending at the bottom border of the grid.

The Letter **Y** is written on the grid if and only if:

* All values at cells belonging to the Y are equal.
* All values at cells not belonging to the Y are equal.
* The values at cells belonging to the Y are different from the values at cells not belonging to the Y.

Return _the **minimum** number of operations needed to write the letter Y on the grid given that in one operation you can change the value at any cell to_ `0`_,_ `1`_,_ _or_ `2`_._

**Example 1:**

![](https://assets.leetcode.com/uploads/2024/01/22/y2.png)

**Input:** grid = \[\[1,2,2],[1,1,0],[0,1,0]]

**Output:** 3

**Explanation:** We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 1 while those that do not belong to Y are equal to 0. It can be shown that 3 is the minimum number of operations needed to write Y on the grid.

**Example 2:**

![](https://assets.leetcode.com/uploads/2024/01/22/y3.png)

**Input:** grid = \[\[0,1,0,1,0],[2,1,0,1,2],[2,2,2,0,1],[2,2,2,2,2],[2,1,2,2,2]]

**Output:** 12

**Explanation:** We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 0 while those that do not belong to Y are equal to 2. It can be shown that 12 is the minimum number of operations needed to write Y on the grid.

**Constraints:**

* `3 <= n <= 49`
* `n == grid.length == grid[i].length`
* `0 <= grid[i][j] <= 2`
* `n` is odd.

## Solution

```kotlin
import kotlin.math.min

class Solution {
fun minimumOperationsToWriteY(arr: Array<IntArray>): Int {
val n = arr.size
val cnt1 = IntArray(3)
val cnt2 = IntArray(3)
val x = n / 2
val y = n / 2
for (j in x until n) {
cnt1[arr[j][y]]++
arr[j][y] = 3
}
for (j in x downTo 0) {
if (arr[j][j] != 3) {
cnt1[arr[j][j]]++
}
arr[j][j] = 3
}
for (j in x downTo 0) {
if (arr[j][n - 1 - j] != 3) {
cnt1[arr[j][n - 1 - j]]++
}
arr[j][n - 1 - j] = 3
}
for (ints in arr) {
for (j in 0 until n) {
if (ints[j] != 3) {
cnt2[ints[j]]++
}
}
}
val s1 = cnt1[0] + cnt1[1] + cnt1[2]
val s2 = cnt2[0] + cnt2[1] + cnt2[2]
var min = Int.MAX_VALUE
for (i in 0..2) {
for (j in 0..2) {
if (i != j) {
min = min((s1 - cnt1[i] + s2 - cnt2[j]), min)
}
}
}
return min
}
}
```
Loading