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
1,509 changes: 827 additions & 682 deletions README.md

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions src/main/kotlin/g1001_1100/s1094_car_pooling/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[![](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)

## 1094\. Car Pooling

Medium

There is a car with `capacity` empty seats. The vehicle only drives east (i.e., it cannot turn around and drive west).

You are given the integer `capacity` and an array `trips` where <code>trips[i] = [numPassengers<sub>i</sub>, from<sub>i</sub>, to<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> trip has <code>numPassengers<sub>i</sub></code> passengers and the locations to pick them up and drop them off are <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> respectively. The locations are given as the number of kilometers due east from the car's initial location.

Return `true` _if it is possible to pick up and drop off all passengers for all the given trips, or_ `false` _otherwise_.

**Example 1:**

**Input:** trips = \[\[2,1,5],[3,3,7]], capacity = 4

**Output:** false

**Example 2:**

**Input:** trips = \[\[2,1,5],[3,3,7]], capacity = 5

**Output:** true

**Constraints:**

* `1 <= trips.length <= 1000`
* `trips[i].length == 3`
* <code>1 <= numPassengers<sub>i</sub> <= 100</code>
* <code>0 <= from<sub>i</sub> < to<sub>i</sub> <= 1000</code>
* <code>1 <= capacity <= 10<sup>5</sup></code>

## Solution

```kotlin
@Suppress("NAME_SHADOWING")
class Solution {
fun carPooling(trips: Array<IntArray>, capacity: Int): Boolean {
var capacity = capacity
val stops = IntArray(1001)
for (t in trips) {
stops[t[1]] += t[0]
stops[t[2]] -= t[0]
}
var i = 0
while (capacity >= 0 && i < 1001) {
capacity -= stops[i]
++i
}
return capacity >= 0
}
}
```
115 changes: 115 additions & 0 deletions src/main/kotlin/g1001_1100/s1095_find_in_mountain_array/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
[![](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)

## 1095\. Find in Mountain Array

Hard

_(This problem is an **interactive problem**.)_

You may recall that an array `arr` is a **mountain array** if and only if:

* `arr.length >= 3`
* There exists some `i` with `0 < i < arr.length - 1` such that:
* `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`
* `arr[i] > arr[i + 1] > ... > arr[arr.length - 1]`

Given a mountain array `mountainArr`, return the **minimum** `index` such that `mountainArr.get(index) == target`. If such an `index` does not exist, return `-1`.

**You cannot access the mountain array directly.** You may only access the array using a `MountainArray` interface:

* `MountainArray.get(k)` returns the element of the array at index `k` (0-indexed).
* `MountainArray.length()` returns the length of the array.

Submissions making more than `100` calls to `MountainArray.get` will be judged _Wrong Answer_. Also, any solutions that attempt to circumvent the judge will result in disqualification.

**Example 1:**

**Input:** array = [1,2,3,4,5,3,1], target = 3

**Output:** 2

**Explanation:** 3 exists in the array, at index=2 and index=5. Return the minimum index, which is 2.

**Example 2:**

**Input:** array = [0,1,2,4,2,1], target = 3

**Output:** -1

**Explanation:** 3 does not exist in `the array,` so we return -1.

**Constraints:**

* <code>3 <= mountain_arr.length() <= 10<sup>4</sup></code>
* <code>0 <= target <= 10<sup>9</sup></code>
* <code>0 <= mountain_arr.get(index) <= 10<sup>9</sup></code>

## Solution

```kotlin
/*
* // This is MountainArray's API interface.
* // You should not implement it, or speculate about its implementation
* class MountainArray {
* fun get(index: Int): Int {}
* fun length(): Int {}
* }
*/

class Solution {
fun findInMountainArray(target: Int, mountainArr: MountainArray): Int {
val peakIndex = findPeak(mountainArr)
if (target == mountainArr.get(peakIndex)) {
return peakIndex
}
val leftResult = findInPeakLeft(target, peakIndex, mountainArr)
return if (leftResult != -1) {
leftResult
} else findInPeakRight(target, peakIndex, mountainArr)
}

private fun findPeak(mountainArray: MountainArray): Int {
val len: Int = mountainArray.length()
var left = 0
var right = len - 1
while (left < right) {
val mid = left + (right - left) / 2
if (mountainArray.get(mid) < mountainArray.get(mid + 1)) {
left = mid + 1
} else {
right = mid
}
}
return left
}

private fun findInPeakLeft(target: Int, peakIndex: Int, mountainArray: MountainArray): Int {
var leftIndex = 0
var rightIndex = peakIndex - 1
while (leftIndex < rightIndex) {
val midIndex = leftIndex + (rightIndex - leftIndex) / 2
if (target > mountainArray.get(midIndex)) {
leftIndex = midIndex + 1
} else {
rightIndex = midIndex
}
}
return if (target == mountainArray.get(leftIndex)) leftIndex else -1
}

private fun findInPeakRight(target: Int, peakIndex: Int, mountainArray: MountainArray): Int {
var leftIndex = peakIndex + 1
var rightIndex: Int = mountainArray.length() - 1
while (leftIndex < rightIndex) {
val midIndex = leftIndex + (rightIndex - leftIndex) / 2
if (target < mountainArray.get(midIndex)) {
leftIndex = midIndex + 1
} else {
rightIndex = midIndex
}
}
return if (target == mountainArray.get(leftIndex)) leftIndex else -1
}
}
```
123 changes: 123 additions & 0 deletions src/main/kotlin/g1001_1100/s1096_brace_expansion_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
[![](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)

## 1096\. Brace Expansion II

Hard

Under the grammar given below, strings can represent a set of lowercase words. Let `R(expr)` denote the set of words the expression represents.

The grammar can best be understood through simple examples:

* Single letters represent a singleton set containing that word.
* `R("a") = {"a"}`
* `R("w") = {"w"}`
* When we take a comma-delimited list of two or more expressions, we take the union of possibilities.
* `R("{a,b,c}") = {"a","b","c"}`
* `R("{ {a,b},{b,c}}") = {"a","b","c"}` (notice the final set only contains each word at most once)
* When we concatenate two expressions, we take the set of possible concatenations between two words where the first word comes from the first expression and the second word comes from the second expression.
* `R("{a,b}{c,d}") = {"ac","ad","bc","bd"}`
* `R("a{b,c}{d,e}f{g,h}") = {"abdfg", "abdfh", "abefg", "abefh", "acdfg", "acdfh", "acefg", "acefh"}`

Formally, the three rules for our grammar:

* For every lowercase letter `x`, we have `R(x) = {x}`.
* For expressions <code>e<sub>1</sub>, e<sub>2</sub>, ... , e<sub>k</sub></code> with `k >= 2`, we have <code>R({e<sub>1</sub>, e<sub>2</sub>, ...}) = R(e<sub>1</sub>) ∪ R(e<sub>2</sub>) ∪ ...</code>
* For expressions <code>e<sub>1</sub></code> and <code>e<sub>2</sub></code>, we have <code>R(e<sub>1</sub> + e<sub>2</sub>) = {a + b for (a, b) in R(e<sub>1</sub>) × R(e<sub>2</sub>)}</code>, where `+` denotes concatenation, and `×` denotes the cartesian product.

Given an expression representing a set of words under the given grammar, return _the sorted list of words that the expression represents_.

**Example 1:**

**Input:** expression = "{a,b}{c,{d,e}}"

**Output:** ["ac","ad","ae","bc","bd","be"]

**Example 2:**

**Input:** expression = "{ {a,z},a{b,c},{ab,z}}"

**Output:** ["a","ab","ac","z"]

**Explanation:** Each distinct word is written only once in the final answer.

**Constraints:**

* `1 <= expression.length <= 60`
* `expression[i]` consists of `'{'`, `'}'`, `','`or lowercase English letters.
* The given `expression` represents a set of words based on the grammar given in the description.

## Solution

```kotlin
class Solution {
fun braceExpansionII(expression: String): List<String> {
val res = flatten(expression)
val sorted: MutableList<String> = ArrayList(res)
sorted.sort()
return sorted
}

private fun flatten(expression: String): Set<String> {
val res: MutableSet<String> = HashSet()
// A temp set to store cartesian product results.
var curSet: MutableSet<String> = HashSet()
var idx = 0
while (idx < expression.length) {
if (expression[idx] == '{') {
// end will be the index of matching "}"
val end = findClosingBrace(expression, idx)
val set = flatten(expression.substring(idx + 1, end))
curSet = concatenateSet(curSet, set)
idx = end + 1
} else if (Character.isLowerCase(expression[idx])) {
// Create set with single element
val set: Set<String> = HashSet(
listOf(
expression[idx].toString()
)
)
curSet = concatenateSet(curSet, set)
idx++
} else if (expression[idx] == ',') {
res.addAll(curSet)
curSet.clear()
idx++
}
}
// Don't forget!
res.addAll(curSet)
return res
}

private fun concatenateSet(set1: Set<String>, set2: Set<String>): MutableSet<String> {
if (set1.isEmpty() || set2.isEmpty()) {
return if (set2.isNotEmpty()) HashSet(set2) else HashSet(set1)
}
val res: MutableSet<String> = HashSet()
for (s1 in set1) {
for (s2 in set2) {
res.add(s1 + s2)
}
}
return res
}

private fun findClosingBrace(expression: String, start: Int): Int {
var count = 0
var idx = start
while (idx < expression.length) {
if (expression[idx] == '{') {
count++
} else if (expression[idx] == '}') {
count--
}
if (count == 0) {
break
}
idx++
}
return idx
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Solution {
return res
}

private operator fun not(): Boolean {
private fun not(): Boolean {
consume('!')
return !group()[0]
}
Expand Down Expand Up @@ -109,7 +109,7 @@ class Solution {
}

private val isAtEnd: Boolean
private get() = index >= source!!.length
get() = index >= source!!.length

private fun advance() {
if (isAtEnd) {
Expand Down
Loading