kt: add «2161. Partition Array According to Given Pivot»
URL: https://leetcode.com/problems/partition-array-according-to-given-pivot/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
667b9abb98
commit
9eff71a1a7
1 changed files with 28 additions and 0 deletions
28
kt/partition-array-according-to-given-pivot.kt
Normal file
28
kt/partition-array-according-to-given-pivot.kt
Normal file
|
@ -0,0 +1,28 @@
|
|||
class Solution {
|
||||
fun pivotArray(
|
||||
nums: IntArray,
|
||||
pivot: Int,
|
||||
): IntArray {
|
||||
val res = IntArray(nums.size)
|
||||
|
||||
var (smaller, greater) = 0 to nums.size - 1
|
||||
nums.indices.map { i -> i to nums.size - 1 - i }.forEach { (i, j) ->
|
||||
if (nums[i] < pivot) {
|
||||
res[smaller] = nums[i]
|
||||
smaller++
|
||||
}
|
||||
|
||||
if (nums[j] > pivot) {
|
||||
res[greater] = nums[j]
|
||||
greater--
|
||||
}
|
||||
}
|
||||
|
||||
while (smaller <= greater) {
|
||||
res[smaller] = pivot
|
||||
smaller++
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue