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:
Matej Focko 2025-03-03 20:37:02 +01:00
parent 667b9abb98
commit 9eff71a1a7
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View 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
}
}