There was an error while loading. Please reload this page.
2 parents 17a1f23 + 335e079 commit 96b4abcCopy full SHA for 96b4abc
2101-2200/2161_partition_array_according_to_given_pivot.rb
@@ -0,0 +1,42 @@
1
+# @param {Integer[]} nums
2
+# @param {Integer} pivot
3
+# @return {Integer[]}
4
+def pivot_array(nums, pivot)
5
+ low = []
6
+ high = []
7
+ pivots = []
8
+ nums.each { |num|
9
+ if num == pivot
10
+ pivots << num
11
+ elsif num > pivot
12
+ high << num
13
+ else
14
+ low << num
15
+ end
16
+ }
17
+
18
+ low + pivots + high
19
+end
20
21
+# 2 pointers
22
23
+ re_arrange = Array.new(nums.size, pivot)
24
+ i = 0
25
+ j = nums.size - 1
26
+ (0...nums.size).each { |k|
27
+ if nums[-k-1] > pivot
28
+ re_arrange[j] = nums[-k-1]
29
+ j -= 1
30
31
32
+ if nums[k] < pivot
33
+ re_arrange[i] = nums[k]
34
+ i += 1
35
36
37
38
+ re_arrange
39
40
41
42
0 commit comments