-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2.java
More file actions
36 lines (32 loc) · 894 Bytes
/
day2.java
File metadata and controls
36 lines (32 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// leetcode (que1)
// problem of the day:- partition array according to given pivot
import java.util.ArrayList;
class Solution {
public int[] pivotArray(int[] nums, int pivot) {
ArrayList<Integer> less = new ArrayList<>();
ArrayList<Integer> equal = new ArrayList<>();
ArrayList<Integer> greater = new ArrayList<>();
for (int num : nums) {
if (num < pivot) {
less.add(num);
} else if (num == pivot) {
equal.add(num);
} else {
greater.add(num);
}
}
int index = 0;
for (int num : less) {
nums[index++] = num;
}
for (int num : equal) {
nums[index++] = num;
}
for (int num : greater) {
nums[index++] = num;
}
return nums;
}
}
// T.C=O(n)
// S.C=O(n)