Skip to content

Commit 8dadc2f

Browse files
authored
Merge pull request #2257 from dylan-jung/main
[dylan-jung] WEEK 09 Solutions
2 parents ee5ccfd + dd32ac2 commit 8dadc2f

5 files changed

Lines changed: 180 additions & 0 deletions

File tree

linked-list-cycle/dylan-jung.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 == nullptr || head->next == nullptr) return false;
13+
14+
ListNode* p1 = head;
15+
ListNode* p2 = head;
16+
17+
while (p2 && p2->next) {
18+
p1 = p1->next;
19+
p2 = p2->next->next;
20+
21+
if (p1 == p2) return true;
22+
}
23+
return false;
24+
}
25+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int maxProduct(vector<int>& nums) {
4+
int n = (int)nums.size();
5+
int curMax = nums[0];
6+
int curMin = nums[0];
7+
int ans = nums[0];
8+
9+
for (int i = 1; i < n; i++) {
10+
int x = nums[i];
11+
12+
if (x < 0) swap(curMax, curMin);
13+
14+
curMax = max(x, curMax * x);
15+
curMin = min(x, curMin * x);
16+
17+
ans = max(ans, curMax);
18+
}
19+
return ans;
20+
}
21+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Solution {
2+
public:
3+
string minWindow(string s, string t) {
4+
int m = (int)s.size();
5+
if (t.empty() || s.empty()) return "";
6+
7+
int target[128] = {0};
8+
int cnt[128] = {0};
9+
10+
int required = 0;
11+
for (char c : t) {
12+
unsigned char uc = (unsigned char)c;
13+
if (target[uc] == 0) required++;
14+
target[uc]++;
15+
}
16+
17+
int formed = 0;
18+
int l = 0, r = 0;
19+
int ansl = 0, ansr = 0;
20+
bool hasAns = false;
21+
22+
while (l <= r) {
23+
bool isValid = (formed == required);
24+
25+
if (isValid) {
26+
if (!hasAns || (r - l) < (ansr - ansl)) {
27+
ansl = l;
28+
ansr = r;
29+
hasAns = true;
30+
}
31+
32+
char cl = s[l];
33+
cnt[cl]--;
34+
if (target[cl] > 0 && cnt[cl] == target[cl] - 1) {
35+
formed--;
36+
}
37+
l++;
38+
}
39+
else if (r >= m) {
40+
char cl = s[l];
41+
cnt[cl]--;
42+
if (target[cl] > 0 && cnt[cl] == target[cl] - 1) {
43+
formed--;
44+
}
45+
l++;
46+
}
47+
else {
48+
char cr = s[r];
49+
cnt[cr]++;
50+
if (target[cr] > 0 && cnt[cr] == target[cr]) {
51+
formed++;
52+
}
53+
r++;
54+
}
55+
}
56+
57+
if (!hasAns) return "";
58+
return s.substr(ansl, ansr - ansl);
59+
}
60+
};
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class Solution {
2+
public:
3+
int m, n;
4+
int pside[200][200];
5+
int aside[200][200];
6+
7+
void aflow(vector<vector<int>>& heights, int a, int b) {
8+
int& val = aside[a][b];
9+
if(val == 1) return;
10+
int dx[4] = {1, -1, 0, 0};
11+
int dy[4] = {0, 0, 1, -1};
12+
val = 1;
13+
for(int i = 0; i < 4; i++) {
14+
int na = a+dx[i];
15+
int nb = b+dy[i];
16+
if(!(0 <= na && na < m && 0 <= nb && nb < n)) continue;
17+
if(heights[na][nb] >= heights[a][b])
18+
aflow(heights, na, nb);
19+
}
20+
}
21+
22+
void pflow(vector<vector<int>>& heights, int a, int b) {
23+
int& val = pside[a][b];
24+
if(val == 1) return;
25+
int dx[4] = {1, -1, 0, 0};
26+
int dy[4] = {0, 0, 1, -1};
27+
val = 1;
28+
int ans = 0;
29+
for(int i = 0; i < 4; i++) {
30+
int na = a+dx[i];
31+
int nb = b+dy[i];
32+
if(!(0 <= na && na < m && 0 <= nb && nb < n)) continue;
33+
if(heights[na][nb] >= heights[a][b])
34+
pflow(heights, na, nb);
35+
}
36+
}
37+
38+
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
39+
m = heights.size();
40+
n = heights[0].size();
41+
fill(&pside[0][0], &pside[0][0]+200*200, 0);
42+
fill(&aside[0][0], &aside[0][0]+200*200, 0);
43+
vector<vector<int>> ans;
44+
45+
for(int i = 0; i < m; i++) aflow(heights, i, n-1);
46+
for(int i = 0; i < n; i++) aflow(heights, m-1, i);
47+
for(int i = 0; i < m; i++) pflow(heights, i, 0);
48+
for(int i = 0; i < n; i++) pflow(heights, 0, i);
49+
50+
for(int i = 0; i < m; i++) {
51+
for(int j = 0; j < n; j++) {
52+
// cout << pside[i][j] << " ";
53+
if (pside[i][j] && aside[i][j]) {
54+
ans.push_back({i, j});
55+
}
56+
}
57+
// cout << "\n";
58+
}
59+
return ans;
60+
}
61+
};

sum-of-two-integers/dylan-jung.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int getSum(int a, int b) {
4+
int ans = 0;
5+
int carry = 0;
6+
for(int i = 0; i < 32; i++) {
7+
ans = ans | ((1 << i) & (a ^ b ^ (carry << i)));
8+
carry = ((a >> i) & (b >> i) & 1) | (carry & ((a >> i) ^ (b >> i)));
9+
// cout << carry;
10+
}
11+
return ans;
12+
}
13+
};

0 commit comments

Comments
 (0)