-
-
Notifications
You must be signed in to change notification settings - Fork 304
[dylan-jung] WEEK 03 solutions #2091
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> ans; | ||
| vector<int> candids; | ||
| int t; | ||
|
|
||
| void dfs(int idx, int sum, vector<int> s) { | ||
| if(sum > t) return; | ||
| else if (sum == t) ans.push_back(s); | ||
|
|
||
| for(int i = idx; i < candids.size(); i++) { | ||
| auto next = vector<int>(s); | ||
|
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. 매 호출마다 벡터를 복사하면 주어진 |
||
| next.push_back(candids[i]); | ||
| dfs(i, sum+candids[i], next); | ||
| } | ||
| }; | ||
|
|
||
| vector<vector<int>> combinationSum(vector<int>& candidates, int target) { | ||
| candids = candidates; | ||
|
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. 추가로 주어진 |
||
| t = target; | ||
| dfs(0, 0, {}); | ||
| return ans; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| class Solution { | ||
| public: | ||
| int mem[100]; | ||
| string str; | ||
|
|
||
| bool isValid(string s) { | ||
| if(s.size() == 1) { | ||
| return '1' <= s[0] && s[0] <= '9'; | ||
| } | ||
| else if(s.size() == 2) { | ||
| if(s[0] == '1') return '0' <= s[1] && s[1] <= '9'; | ||
| else if(s[0] == '2') return '0' <= s[1] && s[1] <= '6'; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| int dfs(int idx) { | ||
| if(idx >= str.size()) return 1; | ||
|
|
||
| int& ret = mem[idx]; | ||
| if(ret != -1) return ret; | ||
| ret = 0; | ||
|
|
||
| if (isValid(str.substr(idx, 1))){ | ||
| ret += dfs(idx+1); | ||
| } | ||
| if (idx < str.size() - 1 && isValid(str.substr(idx, 2))){ | ||
| ret += dfs(idx+2); | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| int numDecodings(string s) { | ||
| str = s; | ||
| fill(mem, mem+100, -1); | ||
| return dfs(0); | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class Solution { | ||
| public: | ||
| int maxSubArray(vector<int>& nums) { | ||
| int dp[100000]; | ||
| dp[0] = max(nums[0], -(1<<30)); | ||
| for(int i = 1; i < nums.size(); i++) { | ||
| dp[i] = max(dp[i-1] + nums[i], nums[i]); | ||
|
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.
|
||
| } | ||
| int m = dp[0]; | ||
| for(int i = 1; i < nums.size(); i++) { | ||
| m = max(dp[i], m); | ||
| } | ||
| return m; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| class Solution { | ||
| public: | ||
| int hammingWeight(int n) { | ||
| return popcount((unsigned int)n); | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| class Solution { | ||
| public: | ||
| bool isPalindrome(string s) { | ||
| string p; | ||
| for(char const& c: s) { | ||
| if('A' <= c && c <= 'Z') { | ||
| p.push_back(c - 'A' + 'a'); | ||
| } | ||
| else if('a' <= c && c <= 'z') { | ||
| p.push_back(c); | ||
| } | ||
| else if ('0' <= c && c <= '9') { | ||
| p.push_back(c); | ||
| } | ||
| } | ||
| int start = 0, end = p.size()-1; | ||
| while(start <= end) { | ||
| if(p[start++] != p[end--]) return false; | ||
| } | ||
| return true; | ||
| } | ||
| }; |
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.
sum == t일 때도 곧바로return하는 것이 더 좋을 것 같습니다!