-
-
Notifications
You must be signed in to change notification settings - Fork 231
[sungjinwi] Week03 #1324
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
[sungjinwi] Week03 #1324
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2488680
#220 valid-palindrome solution
sungjinwi 277e417
#232 number-of-1-bits solution
sungjinwi 4a067a3
#254 combination-sum solution
sungjinwi d69715d
#268 decode-ways solution
sungjinwi d684b59
#275 maximum-subarray solution
sungjinwi 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,38 @@ | ||
/* | ||
풀이 : | ||
target + 1개의 크기를 가지는 삼중벡터 dp를 만든다 | ||
dp[n] = dp[n - candidate]의 각 조합에 candidate를 추가하는 로직으로 쌓아나갈 것이다 | ||
dp[n - c]가 [comb1, comb2]일 때 dp[n]은 [comb1.push_back(c), comb2.push_back[2]] | ||
|
||
dp[0]은 연산을 위해 빈 이중 벡터로 초기화 ( dp[n] = dp[n - n] = dp[0] --> [[].push_back(n)]) | ||
|
||
target크기 : T, candidate 갯수 : N | ||
|
||
TC : O(T * N) | ||
|
||
SC : O(T * N) | ||
*/ | ||
|
||
#include <vector> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
vector<vector<int>> combinationSum(vector<int>& candidates, int target) { | ||
vector<vector<vector<int>>> dp(target + 1); | ||
dp[0] = {{}}; | ||
for (int candidate : candidates) | ||
{ | ||
for (int num = candidate; num <= target; num++) | ||
{ | ||
for (auto& combination : dp[num - candidate]) | ||
{ | ||
vector<int> new_comb = combination; | ||
new_comb.push_back(candidate); | ||
dp[num].push_back(new_comb); | ||
} | ||
} | ||
} | ||
return dp[target]; | ||
} | ||
}; |
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,52 @@ | ||
/* | ||
풀이 : | ||
i 번째 연산을 시작 전 cur는 i + 1에서 시작하는 경우의 수, nxt에는 i + 2에서 시작하는 경우의 수 저장돼있다 | ||
i 번째 연산이 끝난 후 cur는 i에서 시작하는 경우, nxt에는 i + 1에서 시작하는 경우의 수 저장되도록 한다 | ||
s의 길이가 1일 때 무조건 1개의 경우의 수를 가지므로 cur 1로 초기화 | ||
|
||
세가지 경우의 수 | ||
1. s[i]가 '0' 일때 0으로 시작하는 문자열은 해석가능한 수가 없으므로 cur를 0으로 한다 | ||
2. s[i]로 시작하는 두 자리 수가 숫자로 변환하면 27보다 작으면, 1자리로 변환하는 경우의 수(cur) + 2자리로 변환하는 경우의 수(nxt)로 cur 변경 | ||
3. 그 외에는 1자리로 변환하는 경우의 수 밖에 없으므로 cur 그대로 | ||
|
||
문자열 끝에서 조건에 맞춰 업데이트 하면서 문자열 처음까지 순회하고 cur 리턴한다 | ||
|
||
문자열 길이 N | ||
|
||
TC : O(N) | ||
문자열 한번 순회 | ||
|
||
SC : O(1) | ||
*/ | ||
|
||
#include <string> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int numDecodings(string s) { | ||
int cur = 1; | ||
int nxt = 0; | ||
int tmp; | ||
|
||
for (int i = s.size() - 1; i >= 0; i--) | ||
{ | ||
tmp = nxt; | ||
if (s[i] == '0') | ||
{ | ||
nxt = cur; | ||
cur = 0; | ||
} | ||
else if(i < s.size() - 1 && stoi(s.substr(i, 2)) < 27) | ||
{ | ||
nxt = cur; | ||
cur = cur + tmp; | ||
} | ||
else | ||
{ | ||
nxt = cur; | ||
} | ||
} | ||
return cur; | ||
} | ||
}; |
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,37 @@ | ||
/* | ||
풀이 : | ||
max_sum은 INT_MIN으로 초기화 | ||
|
||
수를 순회하면서 sum에 num을 더한다(subarray의 합) | ||
max_sum과 현재 subarray합을 비교해 업데이트한다 | ||
|
||
subarray의 합이 0보다 작으면 새로운 subarray를 시작하기 위해 sum을 0으로 초기화한다 | ||
|
||
nums의 길이 N | ||
|
||
TC : O(N) | ||
|
||
SC : O(1) | ||
*/ | ||
|
||
#include <vector> | ||
#include <limits.h> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int maxSubArray(vector<int>& nums) { | ||
int max_sum = INT_MIN; | ||
int sum = 0; | ||
|
||
for (auto& num : nums) | ||
{ | ||
sum += num; | ||
if (max_sum < sum) | ||
max_sum = sum; | ||
if (sum < 0) | ||
sum = 0; | ||
} | ||
return max_sum; | ||
} | ||
}; |
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,23 @@ | ||
/* | ||
풀이 : | ||
쉬프트 연산으로 n을 감소 시키면서 n 과 1의 & 연산이 true인 갯수를 세서 리턴 | ||
|
||
TC : O(1) | ||
n이 커도 최대 32번의 반복문 | ||
|
||
SC : O(1) | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
int hammingWeight(int n) { | ||
int cnt = 0; | ||
while (n > 0) | ||
{ | ||
if (n & 1) | ||
cnt++; | ||
n = n >> 1; | ||
} | ||
return cnt; | ||
} | ||
}; |
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,38 @@ | ||
/* | ||
풀이 : | ||
alnum인 문자들만 추출해서 소문자로 만들어 alnu_s를 만듬 | ||
양 끝이 같은 문자일 동안 left++, right-- 실시 | ||
같지 않으면 false, 양 끝이 수렴되서 서로 만난다면 true | ||
|
||
문자 길이 N | ||
|
||
TC : O(N) | ||
|
||
SC : O(N) | ||
*/ | ||
|
||
#include <string> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
bool isPalindrome(string s) { | ||
string alnu_s = ""; | ||
|
||
for (char c : s) | ||
{ | ||
if (isalnum(c)) | ||
alnu_s += tolower(c); | ||
} | ||
int left = 0; | ||
int right = alnu_s.size() - 1; | ||
while (left < right) | ||
{ | ||
if (alnu_s[left] != alnu_s[right]) | ||
return false; | ||
left++; | ||
right--; | ||
} | ||
return true; | ||
} | ||
}; |
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.
설명 너무 깔끔하고 잘 정리하셨네요. 변수명도 다 잘 읽혀서 쉽게 이해했습니다!