-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter_5.tex
168 lines (144 loc) · 4.18 KB
/
chapter_5.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
\section{Chapter 5}
\subsection{Delete A Node In BST}
Recursive is the best, remember to devide and conquer. Keep deleteing what you copied. {\color{blue} \textbf{time complexity O(log(n)), mem O(log(n))}}
\begin{lstlisting}
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(!root) return NULL;
if(root->val < key){
root->right = deleteNode(root->right, key);
}
else if(root->val > key){
root->left = deleteNode(root->left, key);
}
else{
if(!root->left || !root->right){
root = root->left ? root->left: root->right;
}
else{
auto temp = root->right;
while(temp->left)
temp = temp->left;
root->val = temp->val;
// key of the recursion
root->right = deleteNode(root->right, temp->val);
}
}
return root;
}
};
\end{lstlisting}
\subsection{Minimum Number of Arrows to Burst Balloons}
Greedy Algo, very similar to merge intervals. {\color{blue}\textbf{time complexity $O(Nlog(N))$}}, {\color{blue}\textbf{space is $O(1)$}}
\begin{lstlisting}
class Solution {
public:
int findMinArrowShots(vector<pair<int, int>>& points) {
sort(points.begin(), points.end());
int count = 0;
int right = 0;
for(int i=0; i<points.size(); i++){
if(i==0){
right = points[i].second;
count++;
continue;
}
if(points[i].first <= right)
right = min(right, points[i].second);
else{
count++;
right = points[i].second;
}
}
return count;
}
};
\end{lstlisting}
\subsection{Minimum Moves to Equal Array Elements}
Find minimum first, {\color{blue}\textbf{time complexity $O(N)$}}, {\color{blue}\textbf{space is $O(1)$}}. A set of good function in \textbf{algorithm} to be used
\begin{enumerate}
\item nth\_element(a.begin(), a.begin()+n, a.end())
\item *min\_element(a.begin(), a.end())
\item *max\_element(a.begin(), a.end())
\end{enumerate}
\begin{lstlisting}
class Solution {
public:
int minMoves(vector<int>& nums) {
int m = *min_element(nums.begin(), nums.end());
long long res = 0;
for(auto x: nums)
res += abs((long long)x - m);
return res;
}
};
\end{lstlisting}
\subsection{4 Sum Two}
Use unordered\_map. {\color{blue}\textbf{time complexity $O(N^2)$}}, {\color{blue}\textbf{space is $O(N^2)$}}
\begin{lstlisting}
class Solution {
public:
int fourSumCount(
vector<int>& A,
vector<int>& B,
vector<int>& C,
vector<int>& D
) {
unordered_map<int, int> a, b;
for(auto x: A) for(auto y: B) a[x+y]++;
for(auto x: C) for(auto y: D) b[x+y]++;
int res = 0;
for(auto x: a){
if(b.count(-x.first))
res += x.second*b[-x.first];
}
return res;
}
};
\end{lstlisting}
\subsection{Assign Cookie}
Sort and double pointer.{\color{blue}\textbf{time complexity O(Nlog(N))}}, {\color{blue}\textbf{space is O(1)}}
\begin{lstlisting}
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int i=0, j=0, count=0;
while(i < g.size() && j < s.size()){
if(g[i] <= s[j]){
i++;
j++;
count++;
}
else{
j++;
}
}
return count;
}
};
\end{lstlisting}
\subsection{132 Pattern}
Using reverse stack, keep track of the second largest. {\color{blue}\textbf{time complexity O(N)}}, {\color{blue}\textbf{space is O(N)}}
\begin{lstlisting}
class Solution {
public:
bool find132pattern(vector<int>& nums) {
stack<int> s;
int s2 = INT_MIN;
for(int i=nums.size()-1; i>-1; i--){
if(nums[i] < s2)
return true;
while(!s.empty() && s.top() < nums[i]){
s2 = s.top();
s.pop();
}
s.push(nums[i]);
}
return false;
}
};
\end{lstlisting}
\subsection{}