-
-
Notifications
You must be signed in to change notification settings - Fork 245
[mkwkw] WEEK 02 Solutions #1733
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
7cdbccc
342e4ab
ac62e45
f580dd4
e9e6e9f
bc363f1
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 @@ | ||
//set-up |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//Dynamic Programming | ||
//dp[n] = dp[n-1]+dp[n-2] | ||
class Solution { | ||
public: | ||
int climbStairs(int n) { | ||
int dp[46]; | ||
|
||
dp[0] = 0; | ||
dp[1] = 1; | ||
dp[2] = 2; | ||
|
||
for(int i=3; i<=n; i++) | ||
Comment on lines
+10
to
+12
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.
|
||
{ | ||
dp[i] = dp[i-1]+dp[i-2]; | ||
} | ||
|
||
return dp[n]; | ||
|
||
} | ||
}; |
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. @SamTheKorean 이 파일처럼 비었지만 Merge 된 경우가 더 있네요: #1756 (comment) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
//set-up |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//map: key:alphabet, value:count | ||
//if t is an anagram of s | ||
// 1. same length | ||
// 2. same content of map | ||
class Solution { | ||
public: | ||
bool isAnagram(string s, string t) { | ||
|
||
//1. different length -> false | ||
if(s.length()!=t.length()) | ||
{ | ||
return false; | ||
} | ||
|
||
//2. different map -> false | ||
map<char, int> sMap, tMap; | ||
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. 배열이나 |
||
for(int i=0; i<s.length(); i++) | ||
{ | ||
sMap[s[i]]++; | ||
tMap[t[i]]++; | ||
} | ||
|
||
for(auto c : sMap) | ||
{ | ||
if(tMap.find(c.first)==tMap.end()) | ||
{ | ||
return false; | ||
} | ||
if(tMap[c.first] != c.second) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
//Optimize complexity | ||
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. 아래 Division 풀이의 시간 복잡도와 공간 복잡도에 비해 더 최적화된 점이 없지 않나요? |
||
class Solution { | ||
public: | ||
vector<int> productExceptSelf(vector<int>& nums) { | ||
|
||
vector<int> answer(nums.size(), 1); | ||
//production before nums[i] from index 0 | ||
int before = 1; | ||
|
||
for(int i=0; i<nums.size()-1; i++) | ||
{ | ||
before *= nums[i]; | ||
answer[i+1] *= before; | ||
} | ||
|
||
//nums[i] 기준 앞에서 production 해놓은 것에 nums[i] 기준 뒤에 것들을 production from index n-1 | ||
//production after nums[i] to answer[i-1] | ||
// answer[i-1] * after | ||
int after = 1; | ||
for(int i=nums.size()-1; i>0; i--) | ||
{ | ||
after *= nums[i]; | ||
answer[i-1] *= after; | ||
} | ||
|
||
return answer; | ||
} | ||
}; | ||
|
||
|
||
|
||
//using division operation | ||
/* | ||
class Solution { | ||
public: | ||
vector<int> productExceptSelf(vector<int>& nums) { | ||
int zeroCnt = 0; | ||
int allProduct = 1; | ||
vector<int> answer(nums.size()); | ||
|
||
for(int i=0; i<nums.size(); i++) | ||
{ | ||
allProduct *= nums[i]; | ||
if(nums[i]==0) | ||
{ | ||
zeroCnt++; | ||
} | ||
} | ||
|
||
if(zeroCnt == 0) | ||
{ | ||
for(int i=0; i<nums.size(); i++) | ||
{ | ||
answer[i] = allProduct/nums[i]; | ||
} | ||
} | ||
else if(zeroCnt == 1) | ||
{ | ||
int zeroIdx = 0; | ||
int allProductWithoutZero = 1; | ||
|
||
for(int i=0; i<nums.size(); i++) | ||
{ | ||
if(nums[i]==0) | ||
{ | ||
zeroIdx = i; | ||
} | ||
else | ||
{ | ||
allProductWithoutZero *=nums[i]; | ||
answer[i] = 0; | ||
} | ||
} | ||
|
||
answer[zeroIdx] = allProductWithoutZero; | ||
} | ||
|
||
return answer; | ||
} | ||
}; | ||
*/ |
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.
@SamTheKorean 이 파일처럼 비었지만 Merge 된 경우가 더 있네요: #1756 (comment)