Skip to content

Commit d884d3d

Browse files
authored
Added tasks 3127-3134
1 parent 49409ae commit d884d3d

File tree

37 files changed

+1014
-285
lines changed
  • src/main/kotlin
    • g0001_0100/s0089_gray_code
    • g0101_0200/s0150_evaluate_reverse_polish_notation
    • g1001_1100/s1020_number_of_enclaves
    • g1301_1400/s1396_design_underground_system
    • g1401_1500/s1425_constrained_subsequence_sum
    • g1501_1600/s1594_maximum_non_negative_product_in_a_matrix
    • g1801_1900/s1886_determine_whether_matrix_can_be_obtained_by_rotation
    • g1901_2000
      • s1942_the_number_of_the_smallest_unoccupied_chair
      • s1985_find_the_kth_largest_integer_in_the_array
      • s1996_the_number_of_weak_characters_in_the_game
    • g2001_2100
    • g2101_2200/s2136_earliest_possible_day_of_full_bloom
    • g2201_2300
      • s2250_count_number_of_rectangles_containing_each_point
      • s2251_number_of_flowers_in_full_bloom
      • s2271_maximum_white_tiles_covered_by_a_carpet
      • s2280_minimum_lines_to_represent_a_line_chart
    • g2401_2500
    • g2501_2600
      • s2503_maximum_number_of_points_from_grid_queries
      • s2542_maximum_subsequence_score
      • s2545_sort_the_students_by_their_kth_score
      • s2580_count_ways_to_group_overlapping_ranges
      • s2589_minimum_time_to_complete_all_tasks
    • g2701_2800
    • g3001_3100/s3068_find_the_maximum_sum_of_node_values
    • g3101_3200
      • s3127_make_a_square_with_the_same_color
      • s3128_right_triangles
      • s3129_find_all_possible_stable_binary_arrays_i
      • s3130_find_all_possible_stable_binary_arrays_ii
      • s3131_find_the_integer_added_to_array_i
      • s3132_find_the_integer_added_to_array_ii
      • s3133_minimum_array_end
      • s3134_find_the_median_of_the_uniqueness_array

37 files changed

+1014
-285
lines changed

README.md

Lines changed: 154 additions & 146 deletions
Large diffs are not rendered by default.

src/main/kotlin/g0001_0100/s0089_gray_code/readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,18 @@ Given an integer `n`, return _any valid **n-bit gray code sequence**_.
3838
```kotlin
3939
@Suppress("NAME_SHADOWING")
4040
class Solution {
41-
fun grayCode(n: Int): List<Int?> {
41+
fun grayCode(n: Int): List<Int> {
4242
var n = n
43-
var n1 = arrayOf<Int?>(0)
43+
var n1 = arrayOf(0)
4444
var shift = 1
4545
while (n > 0) {
46-
val temp = arrayOfNulls<Int>(n1.size * 2)
46+
val temp = Array(n1.size * 2) { 0 }
4747
var pos = 0
4848
for (integer in n1) {
4949
temp[pos++] = integer
5050
}
5151
for (i in n1.indices.reversed()) {
52-
temp[pos++] = n1[i]!! or shift
52+
temp[pos++] = n1[i] or shift
5353
}
5454
n1 = temp
5555
shift = shift shl 1

src/main/kotlin/g0101_0200/s0150_evaluate_reverse_polish_notation/readme.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,20 @@ It is guaranteed that the given RPN expression is always valid. That means the e
5757
## Solution
5858

5959
```kotlin
60-
import java.util.function.BiFunction
61-
6260
class Solution {
63-
val op = mapOf<String, BiFunction<Int, Int, Int>>(
64-
"/" to BiFunction { a, b -> a / b },
65-
"*" to BiFunction { a, b -> a * b },
66-
"+" to BiFunction { a, b -> a + b },
67-
"-" to BiFunction { a, b -> a - b }
61+
val op = mapOf<String, (Int, Int) -> Int>(
62+
"/" to { a, b -> a / b },
63+
"*" to { a, b -> a * b },
64+
"+" to { a, b -> a + b },
65+
"-" to { a, b -> a - b }
6866
)
6967
fun evalRPN(tokens: Array<String>): Int {
7068
val stack = ArrayDeque<String>()
7169
for (t in tokens) {
7270
if (op.contains(t)) {
7371
val b = stack.removeFirst().toInt()
7472
val a = stack.removeFirst().toInt()
75-
val c = op.getValue(t).apply(a, b)
73+
val c = op.getValue(t).invoke(a, b)
7674
stack.addFirst(c.toString())
7775
} else {
7876
stack.addFirst(t)

src/main/kotlin/g1001_1100/s1020_number_of_enclaves/readme.md

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,37 +42,44 @@ Return _the number of land cells in_ `grid` _for which we cannot walk off the bo
4242

4343
```kotlin
4444
class Solution {
45-
fun numEnclaves(grid: Array<IntArray>): Int {
46-
val visited = Array(grid.size) {
47-
BooleanArray(
48-
grid[0].size
49-
)
45+
private fun walk(a: Array<IntArray>, visited: Array<BooleanArray>, x: Int, y: Int) {
46+
if (x >= a.size || x < 0 || y >= a[0].size || y < 0) {
47+
return
5048
}
51-
for (i in grid.indices) {
52-
for (j in grid[0].indices) {
53-
if (grid[i][j] == 1 && (i == 0 || j == 0 || i == grid.size - 1 || j == grid[0].size - 1)) {
54-
move(grid, i, j, visited)
55-
}
56-
}
49+
if (visited[x][y]) {
50+
return
5751
}
58-
var count = 0
59-
for (i in 1 until visited.size - 1) {
60-
for (j in 1 until visited[0].size - 1) {
61-
if (!visited[i][j] && grid[i][j] == 1) count++
62-
}
52+
if (a[x][y] == 0) {
53+
return
6354
}
64-
return count
55+
visited[x][y] = true
56+
walk(a, visited, x - 1, y)
57+
walk(a, visited, x, y - 1)
58+
walk(a, visited, x, y + 1)
59+
walk(a, visited, x + 1, y)
6560
}
6661

67-
companion object {
68-
fun move(g: Array<IntArray>, i: Int, j: Int, b: Array<BooleanArray>) {
69-
if (i < 0 || j < 0 || i == g.size || j == g[0].size || g[i][j] == 0 || b[i][j]) return
70-
b[i][j] = true
71-
move(g, i + 1, j, b)
72-
move(g, i - 1, j, b)
73-
move(g, i, j - 1, b)
74-
move(g, i, j + 1, b)
62+
fun numEnclaves(a: Array<IntArray>): Int {
63+
val n = a.size
64+
val m = a[0].size
65+
val visited = Array(n) { BooleanArray(m) }
66+
for (i in 0 until n) {
67+
walk(a, visited, i, 0)
68+
walk(a, visited, i, m - 1)
69+
}
70+
for (j in 0 until m) {
71+
walk(a, visited, 0, j)
72+
walk(a, visited, n - 1, j)
73+
}
74+
var unreachables = 0
75+
for (i in 0 until n) {
76+
for (j in 0 until m) {
77+
if (a[i][j] == 1 && !visited[i][j]) {
78+
++unreachables
79+
}
80+
}
7581
}
82+
return unreachables
7683
}
7784
}
7885
```

src/main/kotlin/g1301_1400/s1396_design_underground_system/readme.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,25 @@ You may assume all calls to the `checkIn` and `checkOut` methods are consistent.
7474
## Solution
7575

7676
```kotlin
77-
import java.util.LinkedList
78-
7977
class UndergroundSystem {
8078
private class StationAndTime(var station: String, var time: Int)
8179

8280
private val averageTimeMap: MutableMap<String, DoubleArray>
83-
private val travelerMap: MutableMap<Int, LinkedList<StationAndTime>>
81+
private val travelerMap: MutableMap<Int, ArrayList<StationAndTime>>
8482

8583
init {
8684
averageTimeMap = HashMap()
8785
travelerMap = HashMap()
8886
}
8987

9088
fun checkIn(id: Int, stationName: String, t: Int) {
91-
travelerMap.putIfAbsent(id, LinkedList())
89+
travelerMap.putIfAbsent(id, ArrayList())
9290
travelerMap[id]!!.add(StationAndTime(stationName, t))
9391
}
9492

9593
fun checkOut(id: Int, stationName: String, t: Int) {
9694
val list = travelerMap[id]!!
97-
val stationAndTime = list.last
95+
val stationAndTime = list.last()
9896
val startToEndStation: String = stationAndTime.station + "->" + stationName
9997
val duration: Int = t - stationAndTime.time
10098
if (averageTimeMap.containsKey(startToEndStation)) {

src/main/kotlin/g1401_1500/s1425_constrained_subsequence_sum/readme.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,21 @@ A _subsequence_ of an array is obtained by deleting some number of elements (can
4141
## Solution
4242

4343
```kotlin
44-
import java.util.LinkedList
45-
4644
class Solution {
4745
fun constrainedSubsetSum(nums: IntArray, k: Int): Int {
4846
val n = nums.size
4947
var res = Int.MIN_VALUE
50-
val mono = LinkedList<IntArray>()
48+
val mono = ArrayList<IntArray>()
5149
for (i in 0 until n) {
5250
var take = nums[i]
53-
while (mono.isNotEmpty() && i - mono.first[0] > k) {
51+
while (mono.isNotEmpty() && i - mono.first()[0] > k) {
5452
mono.removeFirst()
5553
}
5654
if (mono.isNotEmpty()) {
57-
val mx = Math.max(0, mono.first[1])
55+
val mx = Math.max(0, mono.first()[1])
5856
take += mx
5957
}
60-
while (mono.isNotEmpty() && take > mono.last[1]) {
58+
while (mono.isNotEmpty() && take > mono.last()[1]) {
6159
mono.removeLast()
6260
}
6361
mono.add(intArrayOf(i, take))

src/main/kotlin/g1501_1600/s1594_maximum_non_negative_product_in_a_matrix/readme.md

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,44 +56,39 @@ Notice that the modulo is performed after getting the maximum product.
5656
class Solution {
5757
private class Tuple(var max: Long, var min: Long)
5858

59-
fun maxProductPath(grid: Array<IntArray?>?): Int {
59+
fun maxProductPath(grid: Array<IntArray>): Int {
6060
// DP
61-
if (grid == null || grid.size == 0 || grid[0] == null || grid[0]!!.size == 0) {
61+
if (grid.isEmpty() || grid[0].isEmpty()) {
6262
return 0
6363
}
6464
val rows = grid.size
65-
val cols = grid[0]!!.size
66-
val dp = Array(rows) { arrayOfNulls<Tuple>(cols) }
67-
for (i in 0 until rows) {
68-
for (j in 0 until cols) {
69-
dp[i][j] = Tuple(1, 1)
70-
}
71-
}
65+
val cols = grid[0].size
66+
val dp = Array(rows) { Array(cols) { Tuple(1, 1) } }
7267
// Init first row and column
73-
dp[0][0]!!.max = grid[0]!![0].toLong()
74-
dp[0][0]!!.min = grid[0]!![0].toLong()
68+
dp[0][0].max = grid[0][0].toLong()
69+
dp[0][0].min = grid[0][0].toLong()
7570
for (i in 1 until rows) {
76-
dp[i][0]!!.max = grid[i]!![0] * dp[i - 1][0]!!.max
77-
dp[i][0]!!.min = grid[i]!![0] * dp[i - 1][0]!!.min
71+
dp[i][0].max = grid[i][0] * dp[i - 1][0].max
72+
dp[i][0].min = grid[i][0] * dp[i - 1][0].min
7873
}
7974
for (i in 1 until cols) {
80-
dp[0][i]!!.max = grid[0]!![i] * dp[0][i - 1]!!.max
81-
dp[0][i]!!.min = grid[0]!![i] * dp[0][i - 1]!!.min
75+
dp[0][i].max = grid[0][i] * dp[0][i - 1].max
76+
dp[0][i].min = grid[0][i] * dp[0][i - 1].min
8277
}
8378
// DP
8479
for (i in 1 until rows) {
8580
for (j in 1 until cols) {
86-
val up1 = dp[i - 1][j]!!.max * grid[i]!![j]
87-
val up2 = dp[i - 1][j]!!.min * grid[i]!![j]
88-
val left1 = dp[i][j - 1]!!.max * grid[i]!![j]
89-
val left2 = dp[i][j - 1]!!.min * grid[i]!![j]
90-
dp[i][j]!!.max = Math.max(up1, Math.max(up2, Math.max(left1, left2)))
91-
dp[i][j]!!.min = Math.min(up1, Math.min(up2, Math.min(left1, left2)))
81+
val up1 = dp[i - 1][j].max * grid[i][j]
82+
val up2 = dp[i - 1][j].min * grid[i][j]
83+
val left1 = dp[i][j - 1].max * grid[i][j]
84+
val left2 = dp[i][j - 1].min * grid[i][j]
85+
dp[i][j].max = Math.max(up1, Math.max(up2, Math.max(left1, left2)))
86+
dp[i][j].min = Math.min(up1, Math.min(up2, Math.min(left1, left2)))
9287
}
9388
}
94-
return if (dp[rows - 1][cols - 1]!!.max < 0) {
89+
return if (dp[rows - 1][cols - 1].max < 0) {
9590
-1
96-
} else (dp[rows - 1][cols - 1]!!.max % (1e9 + 7)).toInt()
91+
} else (dp[rows - 1][cols - 1].max % (1e9 + 7)).toInt()
9792
}
9893
}
9994
```

src/main/kotlin/g1801_1900/s1886_determine_whether_matrix_can_be_obtained_by_rotation/readme.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ Given two `n x n` binary matrices `mat` and `target`, return `true` _if it is po
4747
## Solution
4848

4949
```kotlin
50-
import java.util.Arrays
51-
5250
class Solution {
53-
fun findRotation(mat: Array<IntArray>, target: Array<IntArray?>?): Boolean {
51+
fun findRotation(mat: Array<IntArray>, target: Array<IntArray>): Boolean {
5452
for (i in 0..3) {
55-
if (Arrays.deepEquals(mat, target)) {
53+
if (mat.contentDeepEquals(target)) {
5654
return true
5755
}
5856
rotate(mat)

src/main/kotlin/g1901_2000/s1942_the_number_of_the_smallest_unoccupied_chair/readme.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ Since friend 0 sat on chair 2, we return 2.
6969
## Solution
7070

7171
```kotlin
72-
import java.util.Arrays
7372
import java.util.PriorityQueue
7473

7574
class Solution {
@@ -81,9 +80,8 @@ class Solution {
8180
all[2 * i] = Person(i, times[i][0], false, true)
8281
all[2 * i + 1] = Person(i, times[i][1], true, false)
8382
}
84-
Arrays.sort(
85-
all
86-
) { a: Person?, b: Person? ->
83+
84+
all.sortWith { a: Person?, b: Person? ->
8785
val i = if (a!!.leave) -1 else 1
8886
val j = if (b!!.leave) -1 else 1
8987
if (a.time == b.time) i - j else a.time - b.time

src/main/kotlin/g1901_2000/s1985_find_the_kth_largest_integer_in_the_array/readme.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,17 @@ The 2<sup>nd</sup> largest integer in nums is "0".
5757
## Solution
5858

5959
```kotlin
60-
import java.util.Arrays
61-
6260
class Solution {
6361
fun kthLargestNumber(nums: Array<String>, k: Int): String {
64-
Arrays.sort(nums) { n1: String, n2: String -> compareStringInt(n2, n1) }
62+
nums.sortWith { n1: String, n2: String -> compareStringInt(n2, n1) }
6563
return nums[k - 1]
6664
}
6765

6866
private fun compareStringInt(n1: String, n2: String): Int {
6967
if (n1.length != n2.length) {
7068
return if (n1.length < n2.length) -1 else 1
7169
}
72-
for (i in 0 until n1.length) {
70+
for (i in n1.indices) {
7371
val n1Digit = n1[i].code - '0'.code
7472
val n2Digit = n2[i].code - '0'.code
7573
if (n1Digit > n2Digit) {

src/main/kotlin/g1901_2000/s1996_the_number_of_weak_characters_in_the_game/readme.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,9 @@ Return _the number of **weak** characters_.
4444
## Solution
4545

4646
```kotlin
47-
import java.util.Arrays
48-
4947
class Solution {
5048
fun numberOfWeakCharacters(properties: Array<IntArray>): Int {
51-
Arrays.sort(properties) { a: IntArray, b: IntArray -> if (a[0] == b[0]) b[1] - a[1] else a[0] - b[0] }
49+
properties.sortWith { a: IntArray, b: IntArray -> if (a[0] == b[0]) b[1] - a[1] else a[0] - b[0] }
5250
var max = properties[properties.size - 1][1]
5351
var count = 0
5452
for (i in properties.size - 2 downTo 0) {

src/main/kotlin/g2001_2100/s2008_maximum_earnings_from_taxi/readme.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,13 @@ We earn 9 + 5 + 6 = 20 dollars in total.
5050
## Solution
5151

5252
```kotlin
53-
import java.util.Arrays
5453
import java.util.PriorityQueue
5554

5655
@Suppress("UNUSED_PARAMETER")
5756
class Solution {
5857
fun maxTaxiEarnings(n: Int, rides: Array<IntArray>): Long {
5958
// Sort based on start time
60-
Arrays.sort(rides) { a: IntArray, b: IntArray ->
59+
rides.sortWith { a: IntArray, b: IntArray ->
6160
a[0] - b[0]
6261
}
6362
var max: Long = 0

src/main/kotlin/g2001_2100/s2054_two_best_non_overlapping_events/readme.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,9 @@ Note that the start time and end time is **inclusive**: that is, you cannot atte
5151
## Solution
5252

5353
```kotlin
54-
import java.util.Arrays
55-
5654
class Solution {
5755
fun maxTwoEvents(events: Array<IntArray>): Int {
58-
Arrays.sort(events) { a: IntArray, b: IntArray -> a[0] - b[0] }
56+
events.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] }
5957
val max = IntArray(events.size)
6058
for (i in events.indices.reversed()) {
6159
if (i == events.size - 1) {

src/main/kotlin/g2001_2100/s2092_find_all_people_with_secret/readme.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,10 @@ Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.
7878
## Solution
7979

8080
```kotlin
81-
import java.util.Arrays
82-
8381
@Suppress("NAME_SHADOWING")
8482
class Solution {
8583
fun findAllPeople(n: Int, meetings: Array<IntArray>, firstPerson: Int): List<Int> {
86-
Arrays.sort(meetings) { a: IntArray, b: IntArray -> a[2] - b[2] }
84+
meetings.sortWith { a: IntArray, b: IntArray -> a[2] - b[2] }
8785
val uf = UF(n)
8886
// base
8987
uf.union(0, firstPerson)

src/main/kotlin/g2101_2200/s2136_earliest_possible_day_of_full_bloom/readme.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ Thus, on day 2, all the seeds are blooming.
7575
## Solution
7676

7777
```kotlin
78-
import java.util.Arrays
7978
import java.util.Collections
8079

8180
class Solution {
@@ -88,7 +87,7 @@ class Solution {
8887
for (i in 0 until n) {
8988
arr[i] = Seed(plantTime[i], growTime[i])
9089
}
91-
Arrays.sort(arr, Collections.reverseOrder())
90+
arr.sortWith(Collections.reverseOrder())
9291
var ans = arr[0]!!.plantTime + arr[0]!!.growTime
9392
var lastPlantDay = arr[0]!!.plantTime
9493
for (i in 1 until n) {

src/main/kotlin/g2201_2300/s2250_count_number_of_rectangles_containing_each_point/readme.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ Therefore, we return [1, 3].
6969
## Solution
7070

7171
```kotlin
72-
import java.util.Arrays
73-
7472
class Solution {
7573
fun countRectangles(rectangles: Array<IntArray>, points: Array<IntArray>): IntArray {
7674
val n = rectangles.size
@@ -80,7 +78,7 @@ class Solution {
8078
for (i in 0 until q) {
8179
es[n + i] = intArrayOf(points[i][0], points[i][1], i)
8280
}
83-
Arrays.sort(es) { x: IntArray?, y: IntArray? -> if (x!![0] != y!![0]) -(x[0] - y[0]) else x.size - y.size }
81+
es.sortWith { x: IntArray?, y: IntArray? -> if (x!![0] != y!![0]) -(x[0] - y[0]) else x.size - y.size }
8482
val ct = IntArray(101)
8583
val ans = IntArray(q)
8684
for (e in es) {

0 commit comments

Comments
 (0)