diff --git a/README.md b/README.md index 4748751fa..4d067a121 100644 --- a/README.md +++ b/README.md @@ -379,7 +379,7 @@ If you would like to have collaborator permissions on the repo to merge your own [0045 - Jump Game II](https://leetcode.com/problems/jump-game-ii/) |
[✔️](c%2F0045-jump-game-ii.c)
|
[✔️](cpp%2F0045-jump-game-ii.cpp)
|
[✔️](csharp%2F0045-jump-game-ii.cs)
|
|
[✔️](go%2F0045-jump-game-ii.go)
|
[✔️](java%2F0045-jump-game-ii.java)
|
[✔️](javascript%2F0045-jump-game-ii.js)
|
[✔️](kotlin%2F0045-jump-game-ii.kt)
|
[✔️](python%2F0045-jump-game-ii.py)
|
[✔️](ruby%2F0045-jump-game-ii.rb)
|
|
|
[✔️](swift%2F0045-jump-game-ii.swift)
|
[✔️](typescript%2F0045-jump-game-ii.ts)
[1871 - Jump Game VII](https://leetcode.com/problems/jump-game-vii/) |
|
|
|
|
|
|
|
|
|
|
|
|
|
[0134 - Gas Station](https://leetcode.com/problems/gas-station/) |
[✔️](c%2F0134-gas-station.c)
|
[✔️](cpp%2F0134-gas-station.cpp)
|
[✔️](csharp%2F0134-gas-station.cs)
|
|
[✔️](go%2F0134-gas-station.go)
|
[✔️](java%2F0134-gas-station.java)
|
[✔️](javascript%2F0134-gas-station.js)
|
[✔️](kotlin%2F0134-gas-station.kt)
|
[✔️](python%2F0134-gas-station.py)
|
[✔️](ruby%2F0134-gas-station.rb)
|
|
|
[✔️](swift%2F0134-gas-station.swift)
|
[✔️](typescript%2F0134-gas-station.ts)
-[0846 - Hand of Straights](https://leetcode.com/problems/hand-of-straights/) |
|
[✔️](cpp%2F0846-hand-of-straights.cpp)
|
[✔️](csharp%2F0846-hand-of-straights.cs)
|
|
[✔️](go%2F0846-hand-of-straights.go)
|
[✔️](java%2F0846-hand-of-straights.java)
|
[✔️](javascript%2F0846-hand-of-straights.js)
|
|
[✔️](python%2F0846-hand-of-straights.py)
|
[✔️](ruby%2F0846-hand-of-straights.rb)
|
|
|
|
[✔️](typescript%2F0846-hand-of-straights.ts)
+[0846 - Hand of Straights](https://leetcode.com/problems/hand-of-straights/) |
|
[✔️](cpp%2F0846-hand-of-straights.cpp)
|
[✔️](csharp%2F0846-hand-of-straights.cs)
|
|
[✔️](go%2F0846-hand-of-straights.go)
|
[✔️](java%2F0846-hand-of-straights.java)
|
[✔️](javascript%2F0846-hand-of-straights.js)
|
[✔️](kotlin%2F0846-hand-of-straights.kt)
|
[✔️](python%2F0846-hand-of-straights.py)
|
[✔️](ruby%2F0846-hand-of-straights.rb)
|
|
|
|
[✔️](typescript%2F0846-hand-of-straights.ts)
[1423 - Maximum Points You Can Obtain From Cards](https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/) |
|
|
[✔️](csharp%2F1423-Maximum-Points-You-Can-Obtain-from-Cards.cs)
|
|
|
|
|
|
|
|
|
|
|
[1899 - Merge Triplets to Form Target Triplet](https://leetcode.com/problems/merge-triplets-to-form-target-triplet/) |
|
[✔️](cpp%2F1899-merge-triplets-to-form-target-triplet.cpp)
|
[✔️](csharp%2F1899-Merge-Triplets-to-Form-Target-Triplet.cs)
|
|
|
[✔️](java%2F1899-merge-triplets-to-form-target-triplet.java)
|
[✔️](javascript%2F1899-merge-triplets-to-form-target-triplet.js)
|
|
[✔️](python%2F1899-merge-triplets-to-form-target-triplet.py)
|
[✔️](ruby%2F1899-merge-triplets-to-form-target-triplet.rb)
|
|
|
[✔️](swift%2F1899-Merge-Triplets-To-Form-Target-Triplet.swift)
|
[✔️](typescript%2F1899-Merge-Triplets-to-Form-Target-Triplet.ts)
[0763 - Partition Labels](https://leetcode.com/problems/partition-labels/) |
|
[✔️](cpp%2F0763-partition-labels.cpp)
|
[✔️](csharp%2F0763-partition-labels.cs)
|
|
[✔️](go%2F0763-partition-labels.go)
|
[✔️](java%2F0763-partition-labels.java)
|
[✔️](javascript%2F0763-partition-labels.js)
|
|
[✔️](python%2F0763-partition-labels.py)
|
[✔️](ruby%2F0763-partition-labels.rb)
|
|
|
|
diff --git a/kotlin/0846-hand-of-straights.kt b/kotlin/0846-hand-of-straights.kt new file mode 100644 index 000000000..57aefc240 --- /dev/null +++ b/kotlin/0846-hand-of-straights.kt @@ -0,0 +1,28 @@ +class Solution { + fun isNStraightHand(hand: IntArray, groupSize: Int): Boolean { + if (hand.size % groupSize != 0) return false + val countMap = mutableMapOf() + hand.forEach { countMap[it] = countMap.getOrDefault(it, 0) + 1 } + val minHeap = PriorityQueue(countMap.keys) + + while (minHeap.isNotEmpty()) { + val minValue = minHeap.peek() + if (countMap.getValue(minValue) == 0) { + minHeap.remove() + continue + } + // loop through consecutive numbers starting from the "minValue" number + for (consecutiveNumber in minValue until (minValue + groupSize)) { + if ( + consecutiveNumber !in countMap.keys || + countMap.getValue(consecutiveNumber) == 0 + ) return false + countMap[consecutiveNumber] = countMap.getValue(consecutiveNumber) - 1 + } + // if the loop successfully executes without returning, it indicates that + // it was possible to create a group of size [groupSize] with minValue + // as the first element in the group. + } + return true + } +} \ No newline at end of file diff --git a/kotlin/0978-longest-turbulent-subarray.kt b/kotlin/0978-longest-turbulent-subarray.kt new file mode 100644 index 000000000..9d9c407d8 --- /dev/null +++ b/kotlin/0978-longest-turbulent-subarray.kt @@ -0,0 +1,30 @@ +class Solution { + fun maxTurbulenceSize(arr: IntArray): Int { + if (arr.size == 1) return 1 + var previousEqualitySymbol = ' ' + var currentSubArraySize = 1 + var maxSubArraySize = 1 + for (i in 1..arr.lastIndex) { + if (arr[i - 1] > arr[i] && previousEqualitySymbol != '>') { + currentSubArraySize++ + maxSubArraySize = maxOf(maxSubArraySize, currentSubArraySize) + previousEqualitySymbol = '>' + continue + } + if (arr[i - 1] < arr[i] && previousEqualitySymbol != '<') { + currentSubArraySize++ + maxSubArraySize = maxOf(maxSubArraySize, currentSubArraySize) + previousEqualitySymbol = '<' + continue + } + currentSubArraySize = if (arr[i - 1] == arr[i]) 1 else 2 + maxSubArraySize = maxOf(maxSubArraySize, currentSubArraySize) + previousEqualitySymbol = when { + arr[i - 1] < arr[i] -> '<' + arr[i - 1] > arr[i] -> '>' + else -> ' ' + } + } + return maxSubArraySize + } +} \ No newline at end of file