Skip to content

Commit f6e0243

Browse files
committed
minor update, sort
1 parent af33955 commit f6e0243

19 files changed

+2961
-2536
lines changed

Java/2 Sum.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,19 @@ Always check (target - current) from the HashMap.
4242
*/
4343
class Solution {
4444
public int[] twoSum(int[] nums, int target) {
45-
if (nums == null || nums.length == 0) {
46-
return null;
47-
}
48-
int[] result = new int[2];
49-
Map<Integer, Integer> recordMap = new HashMap<>();
45+
int[] rst = new int[2];
46+
if (nums == null || nums.length <= 1) return rst;
47+
48+
Map<Integer, Integer> map = new HashMap<>();
5049
for (int i = 0; i < nums.length; i++) {
51-
if (recordMap.containsKey(target - nums[i])) {
52-
result[0] = recordMap.get(target - nums[i]);
53-
result[1] = i;
54-
return result;
55-
} else {
56-
recordMap.put(nums[i], i);
57-
}
50+
if (map.containsKey(target - nums[i])) {
51+
rst[0] = map.get(target - nums[i]);
52+
rst[1] = i;
53+
break;
54+
}
55+
map.put(nums[i], i);
5856
}
59-
return result;
57+
return rst;
6058
}
6159
}
6260

Java/H-Index.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#### Bucket count / Bucket Sort
2121
- O(n)
22-
- Bucket sort的思想: 过一遍 input, 把dictation value 作为 index, 分布在bucket[index]++
22+
- Bucket sort的思想(更像是counting sort?): 过一遍 input, 把dictation value 作为 index, 分布在bucket[index]++
2323
- bucket[x] count when # of citation == x.
2424
- 如果 x 大于 n的时候, 就超出了index范围, 但是刚好这个问题可以包容, 把这样的情况记位在bucket[n]就可以
2525
- 巧妙: `sum += bucket[h]` where `h = [n ~ 0]` 利用了h-index的definition:

Java/Insert Interval.java

Lines changed: 40 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
#### Basic Implementation
13-
- 这里已经给了sorted intervals by start point.
13+
- 这里已经给了 **sorted** intervals by start point.
1414
- 直接找到可以insert newInterval的位子. Insert
1515
- 然后loop to merge entire interval array
1616
- 因为给的是个list, 所以方便`intervals.remove(i)`
@@ -58,80 +58,68 @@
5858
2. sort point in min-heap
5959
3. when count increase and decreases to 0, that means we can close an interval
6060
*/
61-
/**
62-
* Definition for an interval.
63-
* public class Interval {
64-
* int start;
65-
* int end;
66-
* Interval() { start = 0; end = 0; }
67-
* Interval(int s, int e) { start = s; end = e; }
68-
* }
69-
*/
70-
public class Solution {
71-
61+
/*
62+
Option1:
63+
- convert all intervals into points with open/close flag, sort, and merge. When count == 0, close a interval.
64+
- priority queue, O(n) space, O(nlogn) time
65+
*/
66+
class Solution {
7267
class Point {
73-
int x;
74-
int flag;
75-
public Point(int x, int flag) {
76-
this.x = x;
68+
int val, flag;
69+
public Point(int val, int flag) {
70+
this.val = val;
7771
this.flag = flag;
7872
}
7973
}
8074
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
8175
List<Interval> rst = new ArrayList<>();
82-
if (intervals == null && newInterval == null) {
83-
return rst;
84-
} else if (intervals == null) {
85-
rst.add(newInterval);
76+
if (intervals == null || intervals.size() == 0 || newInterval == null) {
77+
if (newInterval != null) rst.add(newInterval);
8678
return rst;
87-
} else if (newInterval == null) {
88-
return intervals;
8979
}
90-
91-
PriorityQueue<Point> queue = new PriorityQueue<>(1, new Comparator<Point>(){
92-
public int compare(Point a, Point b){
93-
return a.x - b.x;
94-
}
95-
});
96-
97-
for (Interval range: intervals) {
98-
queue.add(new Point(range.start, 1));
99-
queue.add(new Point(range.end, -1));
100-
}
101-
102-
queue.add(new Point(newInterval.start, 1));
103-
queue.add(new Point(newInterval.end, -1));
104-
80+
81+
// build priority queue
82+
PriorityQueue<Point> queue = buildQueue(intervals, newInterval);
83+
// iterate over queue, count and build interval
10584
int count = 0;
106-
int startNew = 0;
107-
int endNew = 0;
85+
Interval interval = new Interval();
10886
while (!queue.isEmpty()) {
10987
Point p = queue.poll();
110-
if (count == 0) {
111-
startNew = p.x;
112-
}
88+
if (count == 0) {//detect start
89+
interval.start = p.val;
90+
}
91+
11392
count += p.flag;
114-
115-
while (!queue.isEmpty() && p.x == queue.peek().x) {
93+
while (!queue.isEmpty() && p.val == queue.peek().val) {
11694
p = queue.poll();
11795
count += p.flag;
11896
}
119-
97+
12098
if (count == 0) {
121-
endNew = p.x;
122-
Interval addInterval = new Interval(startNew, endNew);
123-
rst.add(addInterval);
99+
interval.end = p.val;
100+
rst.add(interval);
101+
interval = new Interval();
124102
}
125-
126-
}//end while
127-
103+
}
128104
return rst;
129-
130105
}
106+
107+
private PriorityQueue<Point> buildQueue(List<Interval> intervals, Interval newInterval) {
108+
PriorityQueue<Point> queue = new PriorityQueue<>(Comparator.comparing(p -> p.val));
109+
queue.offer(new Point(newInterval.start, 1));
110+
queue.offer(new Point(newInterval.end, -1));
111+
for (Interval interval : intervals) {
112+
queue.offer(new Point(interval.start, 1));
113+
queue.offer(new Point(interval.end, -1));
114+
}
115+
return queue;
116+
}
117+
131118
}
132119

133120

134121

122+
135123
/*
136124
O(n) time, O(1) space
137125
Thoughts:

0 commit comments

Comments
 (0)