Skip to content

Commit 90c9d85

Browse files
More leetcode
1 parent 4906bf9 commit 90c9d85

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

Interview/Algorithm/Array.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
6. House Robber
99
7. Degree of an array
1010
8. Decode Ways
11+
9. Best Time to sell stocks II
12+
10. Monotonic Array
13+
11. Longest Continuous Increasing Subsequence
14+
12. Maximum Product Subarray
1115

1216

1317
## Implementation
@@ -377,4 +381,111 @@ public:
377381
return dp[len - 1];
378382
}
379383
};
384+
```
385+
### **Best Time to sell stocks II**
386+
387+
***Big O:*** O(n) speed, O(1) space
388+
```
389+
Tips:
390+
391+
Greedy. Buy low, Sell high.
392+
```
393+
```c++
394+
class Solution {
395+
public:
396+
int maxProfit(vector<int>& prices) {
397+
int prev = prices[0], res = 0;
398+
for (int curr: prices) {
399+
if (prev < curr) res += curr - prev;
400+
prev = curr;
401+
}
402+
return res;
403+
}
404+
};
405+
```
406+
407+
### **Monotonic Array**
408+
409+
***Big O:*** O(n) speed, O(1) space
410+
```
411+
Tips:
412+
413+
Self evident.
414+
```
415+
```c++
416+
class Solution {
417+
public:
418+
bool isMonotonic(vector<int>& A) {
419+
bool increase = true;
420+
bool decrease = true;
421+
for(int i = 0; i < A.size() - 1; i++) {
422+
if(A[i] > A[i+1]) increase = false;
423+
if(A[i] < A[i+1]) decrease = false;
424+
if(increase == false && decrease == false) return false;
425+
}
426+
return true;
427+
}
428+
};
429+
```
430+
431+
### **Longest Continuous Increasing Subsequence**
432+
433+
***Big O:*** O(n) speed, O(1) space
434+
```
435+
Tips:
436+
437+
Self evident.
438+
```
439+
```c++
440+
class Solution {
441+
public:
442+
int findLengthOfLCIS(vector<int>& nums) {
443+
if(nums.size()<=1)return nums.size();
444+
int answer=1,count=1;
445+
for(int i=0;i<nums.size()-1;i++){
446+
if(nums[i]<nums[i+1]){
447+
count++;
448+
answer=max(answer,count);
449+
}
450+
else{
451+
count=1;
452+
}
453+
}
454+
return answer;
455+
}
456+
};
457+
```
458+
459+
### **Maximum Product Subarray**
460+
461+
***Big O:*** O(n) speed, O(1) space
462+
```
463+
Tips:
464+
465+
Dynamic Programming. While going through numbers in nums, we will have to keep track of the maximum product up to that number (we will call max_so_far) and minimum product up to that number (we will call min_so_far). The reason behind keeping track of max_so_far is to keep track of the accumulated product of positive numbers. The reason behind keeping track of min_so_far is to properly handle negative numbers.
466+
```
467+
```c++
468+
class Solution {
469+
public:
470+
471+
int maxProduct(vector<int>& nums) {
472+
if (nums.size() == 0) return 0;
473+
474+
int max_so_far = nums[0];
475+
int min_so_far = nums[0];
476+
int result = max_so_far;
477+
478+
for (int i = 1; i < nums.size(); i++) {
479+
int curr = nums[i];
480+
int temp_max = max(curr, max(max_so_far * curr, min_so_far * curr));
481+
min_so_far = min(curr, min(max_so_far * curr, min_so_far * curr));
482+
483+
max_so_far = temp_max;
484+
485+
result = max(max_so_far, result);
486+
}
487+
488+
return result;
489+
}
490+
};
380491
```

Interview/Algorithm/LeetCode_for_embedded_advanced.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@
7272
23. House Robber v
7373
24. Degree of an array v
7474
25. Decode Ways v
75+
26. Best time to sell stocks II v
76+
27. Monotonic Array v
77+
28. Longest Continuous Increasing Subsequence v
78+
29. Maximum Product Subarray v
7579

7680
***Math:***
7781
1. Add Binary v
@@ -83,6 +87,7 @@
8387
7. Reverse Integer without long long v
8488
8. Palindrome Number v
8589
9. Dot Product of Two Sparse Vectors v
90+
10. Ugly Number v
8691

8792
***Matirx:***
8893
1. Give the center of a matrix and then draw circle

Interview/Algorithm/math.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
2. Palindrome Number
55
3. Dot Product of Two Sparse Vectors
66
4. Pow(x, n)
7+
5. Ugly Number
78

89

910
## Implementation
@@ -133,4 +134,26 @@ public:
133134
return ans;
134135
}
135136
};
137+
```
138+
139+
## **Pow(x, n)**
140+
141+
***Big O:*** O(log(n)) speed, O(1) space
142+
```
143+
Tips:
144+
145+
The idea is simple; you have a base of 3 prime numbers conveniently stored in primes, you loop through them, progressively reducing the initially provided number, if and only as long each of the primes is a divisor of it.
146+
147+
At the end of the run, if what you are left with is == 1, then you had an ugly number, false otherwise (and note that it would also rule out non-positive numbers, but I prefer to just save computation and check initially for it).
148+
```
149+
```c++
150+
class Solution {
151+
public:
152+
vector<int> primes = {2, 3, 5};
153+
bool isUgly(int n) {
154+
if (n < 1) return false;
155+
for (int p: primes) while (n % p == 0) n /=p;
156+
return n == 1;
157+
}
158+
};
136159
```

0 commit comments

Comments
 (0)