Skip to content

Commit e5170bd

Browse files
committed
Updated solutions, added hints Jan 19
1 parent e95038a commit e5170bd

File tree

3 files changed

+8
-19
lines changed

3 files changed

+8
-19
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,13 @@ Solutions <a href = "https://github.com/aliasvishnu/leetcode/tree/master/Solutio
128128
| 249 | Hashing | For each string S, convert it into a Key K by using a concept of offsets. | O(n), string processing is assumed to be O(1) |
129129
| 251 | BF | Keep track of which vector and position you are at. | O(n), n = # total number of elements |
130130
| 252 | Sorting | Sort by start or end time, and check if a previous interval interrupts the next one. | O(n log(n)) |
131-
| 253 | Greedy | Basically interval scheduling problem. | O(n log(n)) |
131+
| 253 | Greedy/Heap | Basically interval scheduling problem. | O(n log(n)) |
132+
| 257 | Inorder traversal | Maintain a path string and add to solution when you hit null. | O(n) |
133+
| 259 | Sorting/2Sum | Sort and do 2 sum like algo. They asked for index triplets, so duplicates aren't an issue. | O(n^2) |
134+
| 261 | DFS | If any chain of exploration hits an already visited node, return false. | O(n) |
135+
| 263 | BF | Divide by 2, 3, 5. You should have 1 in the end. | O(log(n)) in terms of bits. |
136+
| 264 | BF | There are 3 sequences 2x1, 2x2, 2x3....; 3x1, 3x2, 3x3, ...; 5x1, 5x2, 5x3 ...; Keep 3 pointers, at each step, each of these pointers will give the next candidate. The minimum candidate is the next ugly number. | O(n) |
137+
|
132138
| 413 | Array | [1, 2, 3, 4, 5, 7, 9, 11]. can be written as [5, 3] i.e. 5 sequence of difference 1 and 3 sequence of difference 2, you need to figure out how many parts you can split 5 and 3 into. | O(n) |
133139
| 694 | DFS | Keep track of the directions in which DFS proceeds in some form, maybe a string like ddr for down down right. | O(rows*cols) |
134140
| 738 | Array | Find the first time Xi > Xi+1, Xi -= 1 and turn all Xi+k = 9, For eg, 321 becomes 299. Figure out cases like 33332. | O(n) |

Solutions/261-GraphValidTree.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class Solution {
1515
if(node != parent && DFS(node, i) == false) ans = false;
1616
}
1717

18-
visit[0] = false;
1918
return ans;
2019
}
2120

Solutions/263-UglyNumber.cpp

+1-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
class Solution {
22
public:
3-
bool isPrime(int num){
4-
int lim = sqrt(num);
5-
for(int i = 2; i <= lim; i++){
6-
if(num%i == 0) return false;
7-
}
8-
return true;
9-
}
10-
11-
bool check(int num){
12-
if(isPrime(num) && num > 5) return false;
13-
14-
for(int i = 6; i <= num/2; i++){
15-
if(num%i == 0 && isPrime(i)) return false;
16-
}
17-
return true;
18-
}
193

204
bool isUgly(int num) {
215
if(num == 1) return true;
@@ -24,7 +8,7 @@ class Solution {
248
while(num%3 == 0) num/=3;
259
while(num%5 == 0) num/=5;
2610

27-
return check(num);
11+
return num == 1;
2812

2913
}
3014
};

0 commit comments

Comments
 (0)