-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge2SortedArrays.java
58 lines (52 loc) · 1.41 KB
/
Merge2SortedArrays.java
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.*;
public class Solution {
public static List< Integer > sortedArray(int []a, int []b) {
// Write your code here
/**
* Brute force : TC : O(nlogn) + O(mlogm) + O(n + m)
* SC : O(n + m) + O(n + m)
*/
// Set<Integer> set = new TreeSet<>();
// for (int num : a) set.add(num);
// for (int num : b) set.add(num);
// List<Integer> list = new ArrayList<>(set);
// return list;
//Optimal approach
/**
* SC: O(n + m); TC : O(n + m)
*/
List<Integer> res = new ArrayList<>();
int i = 0, j = 0;
int n = a.length;
int m = b.length;
while (i < n && j < m) {
int ele = 0;
if (a[i] < b[j]) {
ele = a[i];
i++;
} else {
ele = b[j];
j++;
}
if (res.size() > 0 && res.get(res.size() - 1) == ele) continue;
res.add(ele);
}
while (i < n) {
if (res.get(res.size() - 1) == a[i]) {
i++;
continue;
}
res.add(a[i]);
i++;
}
while (j < m) {
if (res.get(res.size() - 1) == b[j]) {
j++;
continue;
}
res.add(b[j]);
j++;
}
return res;
}
}