-
-
Notifications
You must be signed in to change notification settings - Fork 305
[dylan-jung] WEEK 09 Solutions #2257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a5fd65e
linked-list-cycle solution
dylan-jung ffccbb5
pacific-atlantic-water-flow solution
dylan-jung fba069c
maximum-product-subarray solution
dylan-jung 67dfd81
sum-of-two-array solution
dylan-jung 81399a4
minimum-window-substring
dylan-jung dd32ac2
add break
dylan-jung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /** | ||
| * 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 == nullptr || head->next == nullptr) return false; | ||
|
|
||
| ListNode* p1 = head; | ||
| ListNode* p2 = head; | ||
|
|
||
| while (p2 && p2->next) { | ||
| p1 = p1->next; | ||
| p2 = p2->next->next; | ||
|
|
||
| if (p1 == p2) return true; | ||
| } | ||
| return false; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| class Solution { | ||
| public: | ||
| int maxProduct(vector<int>& nums) { | ||
| int n = (int)nums.size(); | ||
| int curMax = nums[0]; | ||
| int curMin = nums[0]; | ||
| int ans = nums[0]; | ||
|
|
||
| for (int i = 1; i < n; i++) { | ||
| int x = nums[i]; | ||
|
|
||
| if (x < 0) swap(curMax, curMin); | ||
|
|
||
| curMax = max(x, curMax * x); | ||
| curMin = min(x, curMin * x); | ||
|
|
||
| ans = max(ans, curMax); | ||
| } | ||
| return ans; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| class Solution { | ||
| public: | ||
| string minWindow(string s, string t) { | ||
| int m = (int)s.size(); | ||
| if (t.empty() || s.empty()) return ""; | ||
|
|
||
| int target[128] = {0}; | ||
| int cnt[128] = {0}; | ||
|
|
||
| int required = 0; | ||
| for (char c : t) { | ||
| unsigned char uc = (unsigned char)c; | ||
| if (target[uc] == 0) required++; | ||
| target[uc]++; | ||
| } | ||
|
|
||
| int formed = 0; | ||
| int l = 0, r = 0; | ||
| int ansl = 0, ansr = 0; | ||
| bool hasAns = false; | ||
|
|
||
| while (l <= r) { | ||
| bool isValid = (formed == required); | ||
|
|
||
| if (isValid) { | ||
| if (!hasAns || (r - l) < (ansr - ansl)) { | ||
| ansl = l; | ||
| ansr = r; | ||
| hasAns = true; | ||
| } | ||
|
|
||
| char cl = s[l]; | ||
| cnt[cl]--; | ||
| if (target[cl] > 0 && cnt[cl] == target[cl] - 1) { | ||
| formed--; | ||
| } | ||
| l++; | ||
| } | ||
| else if (r >= m) { | ||
| char cl = s[l]; | ||
| cnt[cl]--; | ||
| if (target[cl] > 0 && cnt[cl] == target[cl] - 1) { | ||
| formed--; | ||
| } | ||
| l++; | ||
| } | ||
| else { | ||
| char cr = s[r]; | ||
| cnt[cr]++; | ||
| if (target[cr] > 0 && cnt[cr] == target[cr]) { | ||
| formed++; | ||
| } | ||
| r++; | ||
| } | ||
| } | ||
|
|
||
| if (!hasAns) return ""; | ||
| return s.substr(ansl, ansr - ansl); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| class Solution { | ||
| public: | ||
| int m, n; | ||
| int pside[200][200]; | ||
| int aside[200][200]; | ||
|
|
||
| void aflow(vector<vector<int>>& heights, int a, int b) { | ||
| int& val = aside[a][b]; | ||
| if(val == 1) return; | ||
| int dx[4] = {1, -1, 0, 0}; | ||
| int dy[4] = {0, 0, 1, -1}; | ||
| val = 1; | ||
| for(int i = 0; i < 4; i++) { | ||
| int na = a+dx[i]; | ||
| int nb = b+dy[i]; | ||
| if(!(0 <= na && na < m && 0 <= nb && nb < n)) continue; | ||
| if(heights[na][nb] >= heights[a][b]) | ||
| aflow(heights, na, nb); | ||
| } | ||
| } | ||
|
|
||
| void pflow(vector<vector<int>>& heights, int a, int b) { | ||
| int& val = pside[a][b]; | ||
| if(val == 1) return; | ||
| int dx[4] = {1, -1, 0, 0}; | ||
| int dy[4] = {0, 0, 1, -1}; | ||
| val = 1; | ||
| int ans = 0; | ||
| for(int i = 0; i < 4; i++) { | ||
| int na = a+dx[i]; | ||
| int nb = b+dy[i]; | ||
| if(!(0 <= na && na < m && 0 <= nb && nb < n)) continue; | ||
| if(heights[na][nb] >= heights[a][b]) | ||
| pflow(heights, na, nb); | ||
| } | ||
| } | ||
|
|
||
| vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) { | ||
| m = heights.size(); | ||
| n = heights[0].size(); | ||
| fill(&pside[0][0], &pside[0][0]+200*200, 0); | ||
| fill(&aside[0][0], &aside[0][0]+200*200, 0); | ||
| vector<vector<int>> ans; | ||
|
|
||
| for(int i = 0; i < m; i++) aflow(heights, i, n-1); | ||
| for(int i = 0; i < n; i++) aflow(heights, m-1, i); | ||
| for(int i = 0; i < m; i++) pflow(heights, i, 0); | ||
| for(int i = 0; i < n; i++) pflow(heights, 0, i); | ||
|
|
||
| for(int i = 0; i < m; i++) { | ||
| for(int j = 0; j < n; j++) { | ||
| // cout << pside[i][j] << " "; | ||
| if (pside[i][j] && aside[i][j]) { | ||
| ans.push_back({i, j}); | ||
| } | ||
| } | ||
| // cout << "\n"; | ||
| } | ||
| return ans; | ||
| } | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 전 이번 주 이 문제를 못 풀긴 했는데, 역시라면 역시랄까요, 비트 연산을 활용하는 문제군요. 가산기의 원리를 구현할 수 있는 문제인 것으로 보이는데, 좋은 영감이 되었습니다. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| class Solution { | ||
| public: | ||
| int getSum(int a, int b) { | ||
| int ans = 0; | ||
| int carry = 0; | ||
| for(int i = 0; i < 32; i++) { | ||
| ans = ans | ((1 << i) & (a ^ b ^ (carry << i))); | ||
| carry = ((a >> i) & (b >> i) & 1) | (carry & ((a >> i) ^ (b >> i))); | ||
| // cout << carry; | ||
| } | ||
| return ans; | ||
| } | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
일치한다는 것을 발견했을 때 바로 true를 반환하는 것도 좋은 방법이군요! 배워갑니다.