Skip to content

Commit 6e8527d

Browse files
author
piggy1991
committed
2 parents 930129b + 2a54ee1 commit 6e8527d

File tree

1 file changed

+166
-1
lines changed

1 file changed

+166
-1
lines changed
Lines changed: 166 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,170 @@
11
package Algorithms.algorithm.interviews.uber;
22

3+
import java.util.*;
34
public class Example {
4-
5+
/* sqrt(100) => 10,1 , sqrt(300) => 10,3 300 = 10^2 * 3 */
6+
// Running time: O(sqrt(n))
7+
public int[] sqrt(int num){
8+
int[] rst = {-1,-1};
9+
10+
if (num < 0){// edge case
11+
return rst;
12+
}
13+
14+
int n = (int) Math.sqrt(num);
15+
while (n >= 1){
16+
if (num % (n*n) == 0){
17+
rst[0] = n;
18+
rst[1] = num/(n*n);
19+
return rst;
20+
}
21+
n--;
22+
}
23+
return rst;
24+
}
25+
26+
/* given a number, find the next prime which is bigger than it */
27+
// Running time: O(nlogm) => m is an average recursion depth for each number, how to optimize it?
28+
public int getNextPrime(int value){
29+
if (value <= 1){
30+
return 2;
31+
}
32+
int target = value+1;
33+
while (!isPrime(target)){
34+
target++;
35+
}
36+
37+
return target;
38+
}
39+
public boolean isPrime(int target){
40+
int n = 2;
41+
while (n*n <= target){// may overflow here!
42+
if (target % n == 0){
43+
return false;
44+
}
45+
n++;
46+
}
47+
return true;
48+
}
49+
/* given many logs <username,log_time,logout_time>, output <time,number_of_users> */
50+
// use two priorityqueue: O(2nlogn) ; just sort: O(2nlog2n)
51+
public List<result> countRecord(List<record> records){
52+
if (records == null || records.size() == 0){
53+
return new ArrayList<result>();
54+
}
55+
List<result> rst = new ArrayList<result>();
56+
Collections.sort(records, new Comparator<record>(){
57+
@Override
58+
public int compare(record o1, record o2) {
59+
// TODO Auto-generated method stub
60+
return o1.log_time - o2.log_time;
61+
}
62+
});
63+
PriorityQueue<Integer> endheap = new PriorityQueue<Integer>(records.size(),new Comparator<Integer>(){
64+
@Override
65+
public int compare(Integer o1, Integer o2) {
66+
// TODO Auto-generated method stub
67+
return o1-o2;
68+
}
69+
});
70+
PriorityQueue<Integer> startheap = new PriorityQueue<Integer>(records.size(),new Comparator<Integer>(){
71+
@Override
72+
public int compare(Integer o1, Integer o2) {
73+
// TODO Auto-generated method stub
74+
return o1-o2;
75+
}
76+
});
77+
int curr,i;
78+
for (i=0;i<records.size();i++){
79+
record tmp = records.get(i);
80+
startheap.offer(tmp.log_time);
81+
endheap.offer(tmp.logout_time);
82+
}
83+
// output
84+
curr = 0;
85+
while (startheap.size() > 0 || endheap.size() > 0){
86+
int curr1 = startheap.size() > 0 ? startheap.peek() : -1;
87+
int curr2 = endheap.size() > 0 ? endheap.peek() : -1;
88+
if (curr1 < 0 || curr1 > curr2){// only end time left
89+
while (endheap.size() > 0 && endheap.peek() == curr2){
90+
curr--;
91+
endheap.poll();
92+
}
93+
rst.add(new result(curr2,curr));
94+
}
95+
else if (curr2 < 0 || curr1 < curr2){// go with start time
96+
while (startheap.size() > 0 && startheap.peek() == curr1){
97+
curr++;
98+
startheap.poll();
99+
}
100+
rst.add(new result(curr1,curr));
101+
}
102+
else{// curr1 == curr2
103+
while (endheap.size() > 0 && endheap.peek() == curr2){
104+
curr--;
105+
endheap.poll();
106+
}
107+
while (startheap.size() > 0 && startheap.peek() == curr1){
108+
curr++;
109+
startheap.poll();
110+
}
111+
rst.add(new result(curr1,curr));
112+
}
113+
}
114+
115+
return rst;
116+
}
117+
// decode ways: 1-26 => 'A'-'Z'
118+
public int numDecodings(String s){
119+
if (s == null || s.length() == 0){
120+
return 0;
121+
}
122+
int last = 0;
123+
int curr = s.charAt(0) == '0' ? 0 : 1;
124+
int next = last + curr;
125+
int i,copy = 0;
126+
127+
for (i = 0;i < s.length();i++){
128+
next = 0;
129+
if (s.charAt(i) >= '1' && s.charAt(i) <= '9'){
130+
next = curr;
131+
}
132+
int value = copy*10 + s.charAt(i)-'0';
133+
134+
if (value >= 10 && value <= 26){
135+
next += last;
136+
}
137+
copy = value % 10;
138+
139+
last = curr;
140+
curr = next;
141+
}
142+
143+
return next;
144+
}
145+
// wordbreak => [lock,locker, erning] ; lockerning : true, lockern : false
146+
public boolean wordBreak(String s, Set<String> dict){
147+
if (dict == null || s == null || s.length() == 0 || dict.size() == 0){
148+
return false;
149+
}
150+
int m = s.length();
151+
int range = 0;
152+
int i,j;
153+
boolean[] dp = new boolean[m+1];
154+
dp[0] = true;
155+
String curr;
156+
157+
for (i=1;i<=s.length();i++){
158+
for (j=0;j<i;j++){
159+
curr = s.substring(j,i);// current substring
160+
if (dict.contains(curr) && (dp[j] || j < range)){
161+
range = Math.max(range,i-1);
162+
dp[i] = true;
163+
break;
164+
}
165+
}
166+
}
167+
168+
return dp[m];
169+
}
5170
}

0 commit comments

Comments
 (0)