Skip to content

Commit e3b2068

Browse files
authored
Merge pull request #2259 from ys-han00/main
[ys-han00] WEEK 09 solutions
2 parents 8dadc2f + 197e091 commit e3b2068

5 files changed

Lines changed: 250 additions & 0 deletions

File tree

linked-list-cycle/ys-han00.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
// class Solution {
10+
// public:
11+
// bool hasCycle(ListNode *head) {
12+
// if(!head)
13+
// return false;
14+
// set<ListNode*> check;
15+
16+
// check.insert(head);
17+
// while(head) {
18+
// if(head->next && check.find(head->next) == check.end())
19+
// check.insert(head);
20+
// else if(head->next && check.find(head->next) != check.end())
21+
// return true;
22+
// head = head->next;
23+
// }
24+
25+
// return false;
26+
// }
27+
// };
28+
29+
// class Solution {
30+
// public:
31+
// bool hasCycle(ListNode *head) {
32+
// while(head) {
33+
// if(head->val) {
34+
// if(head->val == INT_MAX)
35+
// return true;
36+
// head->val = INT_MAX;
37+
// }
38+
// head = head->next;
39+
// }
40+
41+
// return false;
42+
// }
43+
// };
44+
45+
class Solution {
46+
public:
47+
bool hasCycle(ListNode *head) {
48+
ListNode* slow = head;
49+
ListNode* fast = head;
50+
51+
while(fast && fast->next) {
52+
slow = slow->next;
53+
fast = fast->next->next;
54+
if(slow == fast)
55+
return true;
56+
}
57+
58+
return false;
59+
}
60+
};
61+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int maxProduct(vector<int>& nums) {
4+
int prod_min = 1, prod_max = 1, ans = nums[0];
5+
6+
for(int num : nums) {
7+
int tmp = prod_min;
8+
prod_min = min(prod_min * num, prod_max * num);
9+
prod_min = min(prod_min, num);
10+
prod_max = max(tmp * num, prod_max * num);
11+
prod_max = max(prod_max, num);
12+
ans = max(ans, prod_max);
13+
}
14+
15+
return ans;
16+
}
17+
};
18+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
string minWindow(string s, string t) {
4+
int l = 0, substr = 0, min_l = 0, min_r = s.size();
5+
map<char, int> cnt;
6+
for(char c : t)
7+
cnt[c]++;
8+
9+
for(int r = 0; r < s.size(); r++) {
10+
if(cnt.find(s[r]) != cnt.end()) {
11+
if(cnt[s[r]] > 0)
12+
substr++;
13+
cnt[s[r]]--;
14+
}
15+
16+
while(substr == t.size()) {
17+
if(r - l < min_r - min_l) {
18+
min_l = l;
19+
min_r = r;
20+
}
21+
22+
if(cnt.find(s[l]) != cnt.end()) {
23+
cnt[s[l]]++;
24+
if(cnt[s[l]] > 0)
25+
substr--;
26+
}
27+
28+
l++;
29+
}
30+
}
31+
32+
string ans = (min_r < s.size() ? s.substr(min_l, min_r - min_l + 1) : "");
33+
return ans;
34+
}
35+
};
36+
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// class Solution {
2+
// public:
3+
// vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
4+
// int m = heights.size(), n = heights[0].size();
5+
// int dx[] = {-1, 0, 1, 0};
6+
// int dy[] = {0, 1, 0, -1};
7+
8+
// vector<vector<int>> ans;
9+
10+
// for(int i = 0; i < m; i++) {
11+
// for(int j = 0; j < n; j++) {
12+
// bool pacific = false, atlantic = false;
13+
// queue<pair<int, pair<int, int>>> que;
14+
// vector<vector<bool>> check(m, vector<bool> (n, false));
15+
16+
// que.push({heights[i][j], {i, j}});
17+
// check[i][j] = true;
18+
// while(!que.empty()) {
19+
// int h = que.front().first;
20+
// int x = que.front().second.first;
21+
// int y = que.front().second.second;
22+
// que.pop();
23+
24+
// for(int k = 0; k < 4; k++) {
25+
// int new_x = x + dx[k];
26+
// int new_y = y + dy[k];
27+
28+
// if(new_x == -1 || new_y == -1)
29+
// pacific = true;
30+
// if(new_x == m || new_y == n)
31+
// atlantic = true;
32+
33+
// if(-1 < new_x && new_x < m && -1 < new_y && new_y < n && check[new_x][new_y] == false && heights[new_x][new_y] <= h) {
34+
// que.push({heights[new_x][new_y], {new_x, new_y}});
35+
// check[new_x][new_y] = true;
36+
// }
37+
// }
38+
39+
// if(pacific && atlantic)
40+
// break;
41+
// }
42+
// if(pacific && atlantic)
43+
// ans.push_back(vector<int> {i, j});
44+
// }
45+
// }
46+
// return ans;
47+
// }
48+
// };
49+
50+
class Solution {
51+
public:
52+
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
53+
int dx[] = {-1, 0, 1, 0};
54+
int dy[] = {0, 1, 0, -1};
55+
int m = heights.size(), n = heights[0].size();
56+
vector<vector<bool>> atlantic(m, vector<bool> (n, false)), pacific(m, vector<bool> (n, false));
57+
queue<pair<int, int>> que;
58+
59+
for(int i = 0; i < m; i++) {
60+
que.push({i, 0});
61+
pacific[i][0] = true;
62+
}
63+
64+
for(int i = 1; i < n; i++) {
65+
que.push({0, i});
66+
pacific[0][i] = true;
67+
}
68+
69+
while(!que.empty()) {
70+
int x = que.front().first;
71+
int y = que.front().second;
72+
que.pop();
73+
74+
for(int i = 0; i < 4; i++) {
75+
int nx = x + dx[i];
76+
int ny = y + dy[i];
77+
if(nx < 0 || nx == m || ny < 0 || ny == n)
78+
continue;
79+
80+
if(heights[x][y] <= heights[nx][ny] && !pacific[nx][ny]) {
81+
pacific[nx][ny] = true;
82+
que.push({nx, ny});
83+
}
84+
}
85+
}
86+
87+
for(int i = 0; i < m; i++) {
88+
que.push({i, n - 1});
89+
atlantic[i][n - 1] = true;
90+
}
91+
92+
for(int i = 0; i < n - 1; i++) {
93+
que.push({m - 1, i});
94+
atlantic[m - 1][i] = true;
95+
}
96+
97+
while(!que.empty()) {
98+
int x = que.front().first;
99+
int y = que.front().second;
100+
que.pop();
101+
102+
for(int i = 0; i < 4; i++) {
103+
int nx = x + dx[i];
104+
int ny = y + dy[i];
105+
if(nx < 0 || nx == m || ny < 0 || ny == n)
106+
continue;
107+
108+
if(heights[x][y] <= heights[nx][ny] && !atlantic[nx][ny]) {
109+
atlantic[nx][ny] = true;
110+
que.push({nx, ny});
111+
}
112+
}
113+
}
114+
115+
vector<vector<int>> ans;
116+
for(int i = 0; i < m; i++)
117+
for(int j = 0; j < n; j++)
118+
if(pacific[i][j] && atlantic[i][j])
119+
ans.push_back({i, j});
120+
return ans;
121+
}
122+
};
123+

sum-of-two-integers/ys-han00.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int getSum(int a, int b) {
4+
while(b) {
5+
int t = a ^ b;
6+
b = (a & b) << 1;
7+
a = t;
8+
}
9+
return a;
10+
}
11+
};
12+

0 commit comments

Comments
 (0)