Skip to content

Commit 21f39ce

Browse files
More leetcode
1 parent 4ffa2c0 commit 21f39ce

File tree

5 files changed

+346
-26
lines changed

5 files changed

+346
-26
lines changed

Data_Struct_Implementation/memoryMap/memory_map_io.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,8 @@ typedef struct {
7272
_IO uint32_t BRR,
7373
_IO uint32_t ASCR,
7474
} GPIO_REG;
75-
```
75+
```
76+
77+
## Reference
78+
79+
[Yifeng zhu - Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and C: Third Edition](https://www.youtube.com/watch?v=aT5XMOrid7Y&list=PLRJhV4hUhIymmp5CCeIFPyxbknsdcXCc8&index=5&ab_channel=EmbeddedSystemswithARMCortex-MMicrocontrollersinAssemblyLanguageandC)

Interview/Algorithm/Array.md

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
## Problems
2+
3+
1. Running Sum of 1d Array
4+
2. Continuous Subarray Sum
5+
3. Random Pick with Weight
6+
4. Friends Of Appropriate Ages
7+
8+
9+
## Implementation
10+
11+
### **Running Sum of 1d Array**
12+
13+
***Big O:*** O(n) speed, O(1) space
14+
```
15+
Tips:
16+
17+
Use the sum of previous item.
18+
```
19+
```c++
20+
class Solution {
21+
public:
22+
vector<int> runningSum(vector<int>& nums) {
23+
vector<int> running_sum(nums.size(), 0);
24+
running_sum[0] = nums[0];
25+
26+
for (int i = 1; i < nums.size(); i++) {
27+
running_sum[i] = nums[i] + running_sum[i-1];
28+
}
29+
30+
return running_sum;
31+
}
32+
};
33+
```
34+
35+
### **Continuous Subarray Sum**
36+
37+
***Big O:*** O(n) speed, O(n) space
38+
```
39+
Tips:
40+
41+
a%k = x
42+
b%k = x
43+
(a - b) %k = x -x = 0
44+
here a - b = the sum between i and j.
45+
```
46+
```c++
47+
class Solution {
48+
public:
49+
bool checkSubarraySum(vector<int>& nums, int k) {
50+
if(nums.size()<2)
51+
return false;
52+
53+
unordered_map<int, int> mp;
54+
55+
// <0,-1> can allow it to return true when the runningSum%k=0,
56+
mp[0]=-1;
57+
58+
int runningSum=0;
59+
for(int i=0;i<nums.size();i++) {
60+
runningSum+=nums[i];
61+
62+
if(k!=0)
63+
runningSum = runningSum%k;
64+
65+
//check if the runningsum already exists in the hashmap
66+
if(mp.find(runningSum)!=mp.end()) {
67+
//if it exists, then the current location minus the previous location must be greater than1
68+
if(i-mp[runningSum]>1)
69+
return true;
70+
}
71+
else
72+
mp[runningSum]=i;
73+
}
74+
return false;
75+
}
76+
};
77+
```
78+
79+
### **Random Pick with Weight**
80+
81+
***Big O:*** O(log(n)) speed, O(n) space
82+
```
83+
Tips:
84+
85+
Prefix Sums with Binary Search.
86+
```
87+
```c++
88+
class Solution {
89+
vector<int> sums;
90+
91+
public:
92+
Solution(vector<int>& w) {
93+
sums = vector<int>(w.size(), 0);
94+
sums[0] = w[0];
95+
96+
for (int i = 1; i < sums.size(); i++) {
97+
sums[i] = w[i] + sums[i-1];
98+
}
99+
}
100+
101+
int pickIndex() {
102+
int w_index = rand()%(sums.back());
103+
104+
int st = 0;
105+
int end = sums.size() - 1;
106+
int ret;
107+
108+
while (st < end) {
109+
int mid = st + (end-st)/2;
110+
111+
if (sums[mid] <= w_index) {
112+
st = mid + 1;
113+
} else{
114+
end = mid;
115+
}
116+
}
117+
118+
return st;
119+
}
120+
};
121+
122+
```
123+
124+
### **Friends Of Appropriate Ages**
125+
126+
***Big O:*** O(nlog(n)) speed, O(n) space
127+
```
128+
Tips:
129+
Approache 1:
130+
131+
Calculate the target value and then do a binary search. For ages we already went through, simply add the number of requests for that age.
132+
133+
Approach 2:
134+
135+
Instead of processing all 20000 people, we can process pairs of (age, count) representing how many people are that age. Since there are only 120 possible ages, this is a much faster loop.
136+
```
137+
```c++
138+
// Approach 2
139+
class Solution {
140+
public:
141+
int numFriendRequests(vector<int>& ages) {
142+
int count[121] = {};
143+
for (int age: ages) count[age]++;
144+
145+
int ans = 0;
146+
for (int ageA = 0; ageA <= 120; ageA++) {
147+
int countA = count[ageA];
148+
for (int ageB = 0; ageB <= 120; ageB++) {
149+
int countB = count[ageB];
150+
if (ageA/2 + 7 >= ageB) continue;
151+
if (ageA < ageB) continue;
152+
if (ageA < 100 && 100 < ageB) continue;
153+
ans += (countA * countB);
154+
if (ageA == ageB) ans -= countA;
155+
}
156+
}
157+
158+
return ans;
159+
}
160+
};
161+
162+
// Approach 1
163+
class Solution {
164+
public:
165+
unordered_map <int,int> map; // key: age, val: FriendRequestCount.
166+
167+
int findRequests (vector<int> & ages, int index) {
168+
169+
if (map.find(ages[index]) != map.end()) {
170+
return map[ages[index]];
171+
}
172+
173+
int left = 0;
174+
int right = index-1;
175+
double target = (double) (0.5*ages[index]) + 7; // find ages >= target.
176+
177+
while (left <= right) {
178+
int mid = left + (right-left)/2;
179+
if (ages[mid] <= target) {
180+
left = mid+1;
181+
} else {
182+
right = mid-1;
183+
}
184+
}
185+
186+
map[ages[index]] = index-left;
187+
return index-left; // len between index-1 and left.
188+
}
189+
190+
int numFriendRequests(vector<int>& ages) {
191+
192+
sort (ages.begin(), ages.end());
193+
194+
int count = 0;
195+
196+
for (int i = ages.size()-1; i >= 0 ; i--) {
197+
count += findRequests (ages, i);
198+
}
199+
return count;
200+
}
201+
};
202+
```

Interview/Algorithm/LeetCode_for_embedded_advanced.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
15. Find the Duplicate Number v
6565
16. Container With Most Water v
6666
17. Verifying an Alien Dictionary v
67+
18. Running Sum of 1d Array v
68+
19. Continuous Subarray Sum v
69+
20. Random Pick with Weight v
70+
21. Friends Of Appropriate Ages v
6771

6872
***Math:***
6973
1. Add Binary v
@@ -72,6 +76,9 @@
7276
4. Fibonacci Number v
7377
5. Pow(x, n)
7478
6. Divide Two Integers v
79+
7. Reverse Integer without long long v
80+
8. Palindrome Number v
81+
9. Dot Product of Two Sparse Vectors v
7582

7683
***Matirx:***
7784
1. Give the center of a matrix and then draw circle

Interview/Algorithm/math.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
## Problems
2+
3+
1. Reverse Integer without using long long
4+
2. Palindrome Number
5+
3. Dot Product of Two Sparse Vectors
6+
7+
8+
## Implementation
9+
### **Dot Product of Two Sparse Vectors**
10+
11+
***Big O:*** O(k) speed - k is the size of the array with smaller length, O(n) space
12+
```
13+
Tips:
14+
15+
Use hashmap to store <index, value>. While performing the dot product, iterate through the hashmap of the more sparse array and do the sum.
16+
```
17+
```c++
18+
class SparseVector {
19+
public:
20+
unordered_map<int, int> umap;
21+
22+
SparseVector(vector<int> &nums) {
23+
for (int i = 0; i < nums.size(); i++) {
24+
if (nums[i] != 0)
25+
umap[i] = nums[i];
26+
}
27+
}
28+
29+
// Return the dotProduct of two sparse vectors
30+
int dotProduct(SparseVector& vec) {
31+
int sum = 0;
32+
33+
if (vec.umap.size() < this->umap.size()) {
34+
for (auto it : vec.umap) {
35+
if (umap.find(it.first) != umap.end())
36+
sum += umap[it.first]*it.second;
37+
}
38+
} else {
39+
for (auto it : umap) {
40+
if (vec.umap.find(it.first) != vec.umap.end())
41+
sum += vec.umap[it.first]*it.second;
42+
}
43+
}
44+
45+
return sum;
46+
}
47+
};
48+
49+
```
50+
51+
### **Reverse Integer without using long long**
52+
53+
***Big O:*** O(n) speed, O(1) space
54+
```
55+
Tips:
56+
57+
Check overflow by comparing with boundary value divided by 10.
58+
59+
Another approach could be first casting the integer to string and then do the comparison on strings.
60+
```
61+
```c++
62+
class Solution {
63+
public:
64+
int reverse(int x) {
65+
int y=0;
66+
while(x){
67+
if(y>INT_MAX/10 || y<INT_MIN/10){
68+
return 0;
69+
}else{
70+
y=y*10 +x%10;
71+
x=x/10;
72+
}
73+
}
74+
return y;
75+
}
76+
};
77+
```
78+
79+
## **Palindrome Number**
80+
81+
***Big O:*** O(n) speed, O(1) space
82+
```
83+
Tips:
84+
85+
Reverse integer and compare it with the original one. Return true if equal. Be careful with overflow. Use long long for the reversed number variable.
86+
```
87+
```c++
88+
class Solution {
89+
public:
90+
bool isPalindrome(int x) {
91+
if (x < 0)
92+
return false;
93+
94+
long long temp = x, dummy = 0;
95+
96+
while (temp) {
97+
dummy *= 10;
98+
dummy += temp%10;
99+
temp /= 10;
100+
}
101+
102+
return (long long) x == dummy;
103+
}
104+
};
105+
106+
```

0 commit comments

Comments
 (0)