Skip to content

Commit fa155d8

Browse files
committed
3
1 parent 2774859 commit fa155d8

File tree

2 files changed

+197
-10
lines changed

2 files changed

+197
-10
lines changed

CF/algNotes/hf1.md

Lines changed: 196 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11

22
```java
33

4-
/***
5-
Valid Word Abbreviation
4+
/*************Valid Word Abbreviation******************************
5+
66
Input : s = "internationalization", abbr = "i12iz4n"
77
Output : true
8-
*****************************************************
9-
10-
Merge Intervals
8+
********************Merge Intervals*********************************
119
Example 1:
1210
Input: [(1,3)]
1311
Output: [(1,3)]
1412
Example 2:
1513
Input: [(1,3),(2,6),(8,10),(15,18)]
1614
Output: [(1,6),(8,10),(15,18)]
1715
18-
*****************************************************
19-
Log Sorting
16+
***************Log Sorting**************************************
2017
Input:
2118
logs = [
2219
"zo4 4 7",
@@ -34,14 +31,203 @@ Output:
3431
Explanation: "Act zoo" < "act car", so the output is as above.
3532
3633
37-
38-
*****************************************************
39-
Decode Ways
34+
******************Decode Ways***********************************
4035
Input: "12"
4136
Output: 2
4237
Explanation: It could be decoded as AB (1 2) or L (12).
38+
39+
40+
******************Course Schedule I/II***********************************
41+
42+
43+
44+
******************High Five***********************************
45+
There are two properties in the node student id and scores, to ensure that each student will have at least 5 points, find the average of 5 highest scores for each person.
46+
47+
Example
48+
Example 1:
49+
50+
Input:
51+
[[1,91],[1,92],[2,93],[2,99],[2,98],[2,97],[1,60],[1,58],[2,100],[1,61]]
52+
Output:
53+
1: 72.40
54+
2: 97.40
55+
56+
57+
58+
******************612. K Closest Points***********************************
59+
Given some points and an origin point in two-dimensional space, find k points which are nearest to the origin.
60+
Return these points sorted by distance, if they are same in distance, sorted by the x-axis, and if they are same in the x-axis, sorted by y-axis.
61+
62+
Example
63+
Example 1:
64+
65+
Input: points = [[4,6],[4,7],[4,4],[2,5],[1,1]], origin = [0, 0], k = 3
66+
Output: [[1,1],[2,5],[4,4]]
67+
4368
*/
4469

70+
public class Solution {
71+
/**
72+
* @param points a list of points
73+
* @param origin a point
74+
* @param k an integer
75+
* @return the k closest points
76+
*/
77+
private Point global_origin = null;
78+
public Point[] kClosest(Point[] points, Point origin, int k) {
79+
// Write your code here
80+
global_origin = origin;
81+
PriorityQueue<Point> pq = new PriorityQueue<Point> (k, new Comparator<Point> () {
82+
@Override
83+
public int compare(Point a, Point b) {
84+
int diff = getDistance(b, global_origin) - getDistance(a, global_origin);
85+
if (diff == 0)
86+
diff = b.x - a.x;
87+
if (diff == 0)
88+
diff = b.y - a.y;
89+
return diff;
90+
}
91+
});
92+
93+
for (int i = 0; i < points.length; i++) {
94+
pq.offer(points[i]);
95+
if (pq.size() > k)
96+
pq.poll();
97+
}
98+
99+
k = pq.size();
100+
Point[] ret = new Point[k];
101+
while (!pq.isEmpty())
102+
ret[--k] = pq.poll();
103+
return ret;
104+
}
105+
106+
private int getDistance(Point a, Point b) {
107+
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
108+
}
109+
}
110+
111+
//Course Schedule
112+
113+
public Map<Integer, Double> highFive(Record[] results) {
114+
// Write your code here
115+
116+
Map<Integer, PriorityQueue<Integer>> hash = new HashMap<Integer, PriorityQueue<Integer>>();
117+
Map<Integer,Double> answer = new HashMap<>();
118+
119+
for(Record record : results){
120+
if(!hash.containsKey(record.id)){
121+
PriorityQueue<Integer> pq = new PriorityQueue<>();
122+
123+
hash.put(record.id,pq);
124+
}
125+
126+
PriorityQueue<Integer> pq = hash.get(record.id);
127+
if(pq.size() < 5){
128+
pq.add(record.score);
129+
}else{
130+
if(pq.peek() < record.score){
131+
pq.poll();
132+
pq.add(record.score);
133+
}
134+
}
135+
}
136+
137+
138+
139+
for (Map.Entry<Integer, PriorityQueue<Integer>> entry : hash.entrySet()) {
140+
int id = entry.getKey();
141+
PriorityQueue<Integer> scores = entry.getValue();
142+
double average = 0;
143+
for (int i = 0; i < 5; ++i)
144+
average += scores.poll();
145+
average /= 5.0;
146+
answer.put(id, average);
147+
}
148+
return answer;
149+
}
150+
151+
152+
public class Solution {
153+
/*
154+
* @param numCourses: a total of n courses
155+
* @param prerequisites: a list of prerequisite pairs
156+
* @return: true if can finish all courses or false
157+
*/
158+
public int[] findOrder(int numCourses, int[][] prerequisites) {
159+
// write your code here
160+
161+
HashMap<Integer, List<Integer>> graph= mapping(numCourses, prerequisites);
162+
163+
Map<Integer, Integer> indegreeMap = inDegree(numCourses,graph);
164+
165+
Queue<Integer> myQ= new LinkedList<>();
166+
int re[] = new int[numCourses];
167+
int count = numCourses -1;
168+
169+
for(int i = 0; i < numCourses; i++){
170+
if(!indegreeMap.containsKey(i)){
171+
myQ.offer(i);
172+
re[count--] = i;
173+
}
174+
}
175+
176+
while(!myQ.isEmpty()){
177+
178+
Integer temp = myQ.poll();
179+
for(Integer item: graph.get(temp)){
180+
indegreeMap.put(item, indegreeMap.get(item) -1);
181+
182+
if( indegreeMap.get(item) ==0 ){
183+
myQ.offer(item);
184+
re[count--] = item;
185+
}
186+
}
187+
188+
}
189+
190+
if(count != -1){
191+
return new int[0];
192+
}
193+
194+
return re;
195+
196+
}
197+
198+
private Map<Integer,Integer> inDegree (int numCourses, HashMap<Integer, List<Integer>> courseMap){
199+
200+
HashMap<Integer, Integer> indegreeMap = new HashMap<>();
201+
202+
for (int i = 0; i < numCourses; i++) {
203+
for (Integer neighbor : courseMap.get(i)) {
204+
if (indegreeMap.containsKey(neighbor)) {
205+
indegreeMap.put(neighbor, indegreeMap.get(neighbor) + 1);
206+
} else {
207+
indegreeMap.put(neighbor, 1);
208+
}
209+
}
210+
}
211+
212+
return indegreeMap;
213+
}
214+
215+
216+
217+
private HashMap<Integer, List<Integer>> mapping(int numCourses, int[][] prerequisites){
218+
219+
HashMap<Integer, List<Integer>> map = new HashMap<>();
220+
for (int i = 0; i < numCourses; i++) {
221+
map.put(i, new ArrayList<>());
222+
}
223+
for (int i = 0; i < prerequisites.length; i++) {
224+
int cur = prerequisites[i][0];
225+
int pre = prerequisites[i][1];
226+
map.get(cur).add(pre);
227+
}
228+
return map;
229+
}
230+
}
45231

46232

47233
public class Solution {

CF/algNotes/tips.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ PriorityQueue<Integer> minheap = new PriorityQueue<Integer>(k, new Comparator<In
2626
});
2727

2828
Map<Integer, PriorityQueue<Integer>> hash = new HashMap<Integer, PriorityQueue<Integer>>();
29+
Map.Entry<Integer, PriorityQueue<Integer>> entry : hash.entrySet()
2930
PriorityQueue<Integer> pq = hash.get(r.id);
3031
hash.getKey()
3132
hash.getVlaue()

0 commit comments

Comments
 (0)