Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions linked-list-cycle/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
// class Solution {
// public:
// bool hasCycle(ListNode *head) {
// if(!head)
// return false;
// set<ListNode*> check;

// check.insert(head);
// while(head) {
// if(head->next && check.find(head->next) == check.end())
// check.insert(head);
// else if(head->next && check.find(head->next) != check.end())
// return true;
// head = head->next;
// }

// return false;
// }
// };

// class Solution {
// public:
// bool hasCycle(ListNode *head) {
// while(head) {
// if(head->val) {
// if(head->val == INT_MAX)
// return true;
// head->val = INT_MAX;
// }
// head = head->next;
// }

// return false;
// }
// };

class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode* slow = head;
ListNode* fast = head;

while(fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
return true;
}

return false;
}
};

18 changes: 18 additions & 0 deletions maximum-product-subarray/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
int maxProduct(vector<int>& nums) {
int prod_min = 1, prod_max = 1, ans = nums[0];

for(int num : nums) {
int tmp = prod_min;
prod_min = min(prod_min * num, prod_max * num);
prod_min = min(prod_min, num);
prod_max = max(tmp * num, prod_max * num);
prod_max = max(prod_max, num);
ans = max(ans, prod_max);
}

return ans;
}
};

36 changes: 36 additions & 0 deletions minimum-window-substring/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
public:
string minWindow(string s, string t) {
int l = 0, substr = 0, min_l = 0, min_r = s.size();
map<char, int> cnt;
for(char c : t)
cnt[c]++;

for(int r = 0; r < s.size(); r++) {
if(cnt.find(s[r]) != cnt.end()) {
if(cnt[s[r]] > 0)
substr++;
cnt[s[r]]--;
}

while(substr == t.size()) {
if(r - l < min_r - min_l) {
min_l = l;
min_r = r;
}

if(cnt.find(s[l]) != cnt.end()) {
cnt[s[l]]++;
if(cnt[s[l]] > 0)
substr--;
}

l++;
}
}

string ans = (min_r < s.size() ? s.substr(min_l, min_r - min_l + 1) : "");
return ans;
}
};

123 changes: 123 additions & 0 deletions pacific-atlantic-water-flow/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// class Solution {
// public:
// vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
// int m = heights.size(), n = heights[0].size();
// int dx[] = {-1, 0, 1, 0};
// int dy[] = {0, 1, 0, -1};

// vector<vector<int>> ans;

// for(int i = 0; i < m; i++) {
// for(int j = 0; j < n; j++) {
// bool pacific = false, atlantic = false;
// queue<pair<int, pair<int, int>>> que;
// vector<vector<bool>> check(m, vector<bool> (n, false));

// que.push({heights[i][j], {i, j}});
// check[i][j] = true;
// while(!que.empty()) {
// int h = que.front().first;
// int x = que.front().second.first;
// int y = que.front().second.second;
// que.pop();

// for(int k = 0; k < 4; k++) {
// int new_x = x + dx[k];
// int new_y = y + dy[k];

// if(new_x == -1 || new_y == -1)
// pacific = true;
// if(new_x == m || new_y == n)
// atlantic = true;

// 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) {
// que.push({heights[new_x][new_y], {new_x, new_y}});
// check[new_x][new_y] = true;
// }
// }

// if(pacific && atlantic)
// break;
// }
// if(pacific && atlantic)
// ans.push_back(vector<int> {i, j});
// }
// }
// return ans;
// }
// };

class Solution {
public:
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int m = heights.size(), n = heights[0].size();
vector<vector<bool>> atlantic(m, vector<bool> (n, false)), pacific(m, vector<bool> (n, false));
queue<pair<int, int>> que;

for(int i = 0; i < m; i++) {
que.push({i, 0});
pacific[i][0] = true;
}

for(int i = 1; i < n; i++) {
que.push({0, i});
pacific[0][i] = true;
}

while(!que.empty()) {
int x = que.front().first;
int y = que.front().second;
que.pop();

for(int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(nx < 0 || nx == m || ny < 0 || ny == n)
continue;

if(heights[x][y] <= heights[nx][ny] && !pacific[nx][ny]) {
pacific[nx][ny] = true;
que.push({nx, ny});
}
}
}

for(int i = 0; i < m; i++) {
que.push({i, n - 1});
atlantic[i][n - 1] = true;
}

for(int i = 0; i < n - 1; i++) {
que.push({m - 1, i});
atlantic[m - 1][i] = true;
}

while(!que.empty()) {
int x = que.front().first;
int y = que.front().second;
que.pop();

for(int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(nx < 0 || nx == m || ny < 0 || ny == n)
continue;

if(heights[x][y] <= heights[nx][ny] && !atlantic[nx][ny]) {
atlantic[nx][ny] = true;
que.push({nx, ny});
}
}
}

vector<vector<int>> ans;
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
if(pacific[i][j] && atlantic[i][j])
ans.push_back({i, j});
return ans;
}
};

12 changes: 12 additions & 0 deletions sum-of-two-integers/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
int getSum(int a, int b) {
while(b) {
int t = a ^ b;
b = (a & b) << 1;
a = t;
}
return a;
}
};