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
300 changes: 154 additions & 146 deletions README.md

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/main/kotlin/g0001_0100/s0089_gray_code/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ Given an integer `n`, return _any valid **n-bit gray code sequence**_.
```kotlin
@Suppress("NAME_SHADOWING")
class Solution {
fun grayCode(n: Int): List<Int?> {
fun grayCode(n: Int): List<Int> {
var n = n
var n1 = arrayOf<Int?>(0)
var n1 = arrayOf(0)
var shift = 1
while (n > 0) {
val temp = arrayOfNulls<Int>(n1.size * 2)
val temp = Array(n1.size * 2) { 0 }
var pos = 0
for (integer in n1) {
temp[pos++] = integer
}
for (i in n1.indices.reversed()) {
temp[pos++] = n1[i]!! or shift
temp[pos++] = n1[i] or shift
}
n1 = temp
shift = shift shl 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,20 @@ It is guaranteed that the given RPN expression is always valid. That means the e
## Solution

```kotlin
import java.util.function.BiFunction

class Solution {
val op = mapOf<String, BiFunction<Int, Int, Int>>(
"/" to BiFunction { a, b -> a / b },
"*" to BiFunction { a, b -> a * b },
"+" to BiFunction { a, b -> a + b },
"-" to BiFunction { a, b -> a - b }
val op = mapOf<String, (Int, Int) -> Int>(
"/" to { a, b -> a / b },
"*" to { a, b -> a * b },
"+" to { a, b -> a + b },
"-" to { a, b -> a - b }
)
fun evalRPN(tokens: Array<String>): Int {
val stack = ArrayDeque<String>()
for (t in tokens) {
if (op.contains(t)) {
val b = stack.removeFirst().toInt()
val a = stack.removeFirst().toInt()
val c = op.getValue(t).apply(a, b)
val c = op.getValue(t).invoke(a, b)
stack.addFirst(c.toString())
} else {
stack.addFirst(t)
Expand Down
57 changes: 32 additions & 25 deletions src/main/kotlin/g1001_1100/s1020_number_of_enclaves/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,44 @@ Return _the number of land cells in_ `grid` _for which we cannot walk off the bo

```kotlin
class Solution {
fun numEnclaves(grid: Array<IntArray>): Int {
val visited = Array(grid.size) {
BooleanArray(
grid[0].size
)
private fun walk(a: Array<IntArray>, visited: Array<BooleanArray>, x: Int, y: Int) {
if (x >= a.size || x < 0 || y >= a[0].size || y < 0) {
return
}
for (i in grid.indices) {
for (j in grid[0].indices) {
if (grid[i][j] == 1 && (i == 0 || j == 0 || i == grid.size - 1 || j == grid[0].size - 1)) {
move(grid, i, j, visited)
}
}
if (visited[x][y]) {
return
}
var count = 0
for (i in 1 until visited.size - 1) {
for (j in 1 until visited[0].size - 1) {
if (!visited[i][j] && grid[i][j] == 1) count++
}
if (a[x][y] == 0) {
return
}
return count
visited[x][y] = true
walk(a, visited, x - 1, y)
walk(a, visited, x, y - 1)
walk(a, visited, x, y + 1)
walk(a, visited, x + 1, y)
}

companion object {
fun move(g: Array<IntArray>, i: Int, j: Int, b: Array<BooleanArray>) {
if (i < 0 || j < 0 || i == g.size || j == g[0].size || g[i][j] == 0 || b[i][j]) return
b[i][j] = true
move(g, i + 1, j, b)
move(g, i - 1, j, b)
move(g, i, j - 1, b)
move(g, i, j + 1, b)
fun numEnclaves(a: Array<IntArray>): Int {
val n = a.size
val m = a[0].size
val visited = Array(n) { BooleanArray(m) }
for (i in 0 until n) {
walk(a, visited, i, 0)
walk(a, visited, i, m - 1)
}
for (j in 0 until m) {
walk(a, visited, 0, j)
walk(a, visited, n - 1, j)
}
var unreachables = 0
for (i in 0 until n) {
for (j in 0 until m) {
if (a[i][j] == 1 && !visited[i][j]) {
++unreachables
}
}
}
return unreachables
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,25 @@ You may assume all calls to the `checkIn` and `checkOut` methods are consistent.
## Solution

```kotlin
import java.util.LinkedList

class UndergroundSystem {
private class StationAndTime(var station: String, var time: Int)

private val averageTimeMap: MutableMap<String, DoubleArray>
private val travelerMap: MutableMap<Int, LinkedList<StationAndTime>>
private val travelerMap: MutableMap<Int, ArrayList<StationAndTime>>

init {
averageTimeMap = HashMap()
travelerMap = HashMap()
}

fun checkIn(id: Int, stationName: String, t: Int) {
travelerMap.putIfAbsent(id, LinkedList())
travelerMap.putIfAbsent(id, ArrayList())
travelerMap[id]!!.add(StationAndTime(stationName, t))
}

fun checkOut(id: Int, stationName: String, t: Int) {
val list = travelerMap[id]!!
val stationAndTime = list.last
val stationAndTime = list.last()
val startToEndStation: String = stationAndTime.station + "->" + stationName
val duration: Int = t - stationAndTime.time
if (averageTimeMap.containsKey(startToEndStation)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,21 @@ A _subsequence_ of an array is obtained by deleting some number of elements (can
## Solution

```kotlin
import java.util.LinkedList

class Solution {
fun constrainedSubsetSum(nums: IntArray, k: Int): Int {
val n = nums.size
var res = Int.MIN_VALUE
val mono = LinkedList<IntArray>()
val mono = ArrayList<IntArray>()
for (i in 0 until n) {
var take = nums[i]
while (mono.isNotEmpty() && i - mono.first[0] > k) {
while (mono.isNotEmpty() && i - mono.first()[0] > k) {
mono.removeFirst()
}
if (mono.isNotEmpty()) {
val mx = Math.max(0, mono.first[1])
val mx = Math.max(0, mono.first()[1])
take += mx
}
while (mono.isNotEmpty() && take > mono.last[1]) {
while (mono.isNotEmpty() && take > mono.last()[1]) {
mono.removeLast()
}
mono.add(intArrayOf(i, take))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,44 +56,39 @@ Notice that the modulo is performed after getting the maximum product.
class Solution {
private class Tuple(var max: Long, var min: Long)

fun maxProductPath(grid: Array<IntArray?>?): Int {
fun maxProductPath(grid: Array<IntArray>): Int {
// DP
if (grid == null || grid.size == 0 || grid[0] == null || grid[0]!!.size == 0) {
if (grid.isEmpty() || grid[0].isEmpty()) {
return 0
}
val rows = grid.size
val cols = grid[0]!!.size
val dp = Array(rows) { arrayOfNulls<Tuple>(cols) }
for (i in 0 until rows) {
for (j in 0 until cols) {
dp[i][j] = Tuple(1, 1)
}
}
val cols = grid[0].size
val dp = Array(rows) { Array(cols) { Tuple(1, 1) } }
// Init first row and column
dp[0][0]!!.max = grid[0]!![0].toLong()
dp[0][0]!!.min = grid[0]!![0].toLong()
dp[0][0].max = grid[0][0].toLong()
dp[0][0].min = grid[0][0].toLong()
for (i in 1 until rows) {
dp[i][0]!!.max = grid[i]!![0] * dp[i - 1][0]!!.max
dp[i][0]!!.min = grid[i]!![0] * dp[i - 1][0]!!.min
dp[i][0].max = grid[i][0] * dp[i - 1][0].max
dp[i][0].min = grid[i][0] * dp[i - 1][0].min
}
for (i in 1 until cols) {
dp[0][i]!!.max = grid[0]!![i] * dp[0][i - 1]!!.max
dp[0][i]!!.min = grid[0]!![i] * dp[0][i - 1]!!.min
dp[0][i].max = grid[0][i] * dp[0][i - 1].max
dp[0][i].min = grid[0][i] * dp[0][i - 1].min
}
// DP
for (i in 1 until rows) {
for (j in 1 until cols) {
val up1 = dp[i - 1][j]!!.max * grid[i]!![j]
val up2 = dp[i - 1][j]!!.min * grid[i]!![j]
val left1 = dp[i][j - 1]!!.max * grid[i]!![j]
val left2 = dp[i][j - 1]!!.min * grid[i]!![j]
dp[i][j]!!.max = Math.max(up1, Math.max(up2, Math.max(left1, left2)))
dp[i][j]!!.min = Math.min(up1, Math.min(up2, Math.min(left1, left2)))
val up1 = dp[i - 1][j].max * grid[i][j]
val up2 = dp[i - 1][j].min * grid[i][j]
val left1 = dp[i][j - 1].max * grid[i][j]
val left2 = dp[i][j - 1].min * grid[i][j]
dp[i][j].max = Math.max(up1, Math.max(up2, Math.max(left1, left2)))
dp[i][j].min = Math.min(up1, Math.min(up2, Math.min(left1, left2)))
}
}
return if (dp[rows - 1][cols - 1]!!.max < 0) {
return if (dp[rows - 1][cols - 1].max < 0) {
-1
} else (dp[rows - 1][cols - 1]!!.max % (1e9 + 7)).toInt()
} else (dp[rows - 1][cols - 1].max % (1e9 + 7)).toInt()
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@ Given two `n x n` binary matrices `mat` and `target`, return `true` _if it is po
## Solution

```kotlin
import java.util.Arrays

class Solution {
fun findRotation(mat: Array<IntArray>, target: Array<IntArray?>?): Boolean {
fun findRotation(mat: Array<IntArray>, target: Array<IntArray>): Boolean {
for (i in 0..3) {
if (Arrays.deepEquals(mat, target)) {
if (mat.contentDeepEquals(target)) {
return true
}
rotate(mat)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ Since friend 0 sat on chair 2, we return 2.
## Solution

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

class Solution {
Expand All @@ -81,9 +80,8 @@ class Solution {
all[2 * i] = Person(i, times[i][0], false, true)
all[2 * i + 1] = Person(i, times[i][1], true, false)
}
Arrays.sort(
all
) { a: Person?, b: Person? ->

all.sortWith { a: Person?, b: Person? ->
val i = if (a!!.leave) -1 else 1
val j = if (b!!.leave) -1 else 1
if (a.time == b.time) i - j else a.time - b.time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,17 @@ The 2<sup>nd</sup> largest integer in nums is "0".
## Solution

```kotlin
import java.util.Arrays

class Solution {
fun kthLargestNumber(nums: Array<String>, k: Int): String {
Arrays.sort(nums) { n1: String, n2: String -> compareStringInt(n2, n1) }
nums.sortWith { n1: String, n2: String -> compareStringInt(n2, n1) }
return nums[k - 1]
}

private fun compareStringInt(n1: String, n2: String): Int {
if (n1.length != n2.length) {
return if (n1.length < n2.length) -1 else 1
}
for (i in 0 until n1.length) {
for (i in n1.indices) {
val n1Digit = n1[i].code - '0'.code
val n2Digit = n2[i].code - '0'.code
if (n1Digit > n2Digit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ Return _the number of **weak** characters_.
## Solution

```kotlin
import java.util.Arrays

class Solution {
fun numberOfWeakCharacters(properties: Array<IntArray>): Int {
Arrays.sort(properties) { a: IntArray, b: IntArray -> if (a[0] == b[0]) b[1] - a[1] else a[0] - b[0] }
properties.sortWith { a: IntArray, b: IntArray -> if (a[0] == b[0]) b[1] - a[1] else a[0] - b[0] }
var max = properties[properties.size - 1][1]
var count = 0
for (i in properties.size - 2 downTo 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@ We earn 9 + 5 + 6 = 20 dollars in total.
## Solution

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

@Suppress("UNUSED_PARAMETER")
class Solution {
fun maxTaxiEarnings(n: Int, rides: Array<IntArray>): Long {
// Sort based on start time
Arrays.sort(rides) { a: IntArray, b: IntArray ->
rides.sortWith { a: IntArray, b: IntArray ->
a[0] - b[0]
}
var max: Long = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ Note that the start time and end time is **inclusive**: that is, you cannot atte
## Solution

```kotlin
import java.util.Arrays

class Solution {
fun maxTwoEvents(events: Array<IntArray>): Int {
Arrays.sort(events) { a: IntArray, b: IntArray -> a[0] - b[0] }
events.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] }
val max = IntArray(events.size)
for (i in events.indices.reversed()) {
if (i == events.size - 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,10 @@ Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.
## Solution

```kotlin
import java.util.Arrays

@Suppress("NAME_SHADOWING")
class Solution {
fun findAllPeople(n: Int, meetings: Array<IntArray>, firstPerson: Int): List<Int> {
Arrays.sort(meetings) { a: IntArray, b: IntArray -> a[2] - b[2] }
meetings.sortWith { a: IntArray, b: IntArray -> a[2] - b[2] }
val uf = UF(n)
// base
uf.union(0, firstPerson)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ Thus, on day 2, all the seeds are blooming.
## Solution

```kotlin
import java.util.Arrays
import java.util.Collections

class Solution {
Expand All @@ -88,7 +87,7 @@ class Solution {
for (i in 0 until n) {
arr[i] = Seed(plantTime[i], growTime[i])
}
Arrays.sort(arr, Collections.reverseOrder())
arr.sortWith(Collections.reverseOrder())
var ans = arr[0]!!.plantTime + arr[0]!!.growTime
var lastPlantDay = arr[0]!!.plantTime
for (i in 1 until n) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ Therefore, we return [1, 3].
## Solution

```kotlin
import java.util.Arrays

class Solution {
fun countRectangles(rectangles: Array<IntArray>, points: Array<IntArray>): IntArray {
val n = rectangles.size
Expand All @@ -80,7 +78,7 @@ class Solution {
for (i in 0 until q) {
es[n + i] = intArrayOf(points[i][0], points[i][1], i)
}
Arrays.sort(es) { x: IntArray?, y: IntArray? -> if (x!![0] != y!![0]) -(x[0] - y[0]) else x.size - y.size }
es.sortWith { x: IntArray?, y: IntArray? -> if (x!![0] != y!![0]) -(x[0] - y[0]) else x.size - y.size }
val ct = IntArray(101)
val ans = IntArray(q)
for (e in es) {
Expand Down
Loading