-
-
Notifications
You must be signed in to change notification settings - Fork 247
[yhkee0404] WEEK 03 solutions #1792
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 @@ | ||
function combinationSum(candidates: number[], target: number, dp = new Map()): number[][] { | ||
if (target <= 0) { | ||
return []; | ||
} | ||
let ans = dp.get(target); | ||
if (ans !== undefined) { | ||
return ans; | ||
} | ||
ans = []; | ||
for (const candidate of candidates) { | ||
if (target == candidate) { | ||
ans.push([candidate]); | ||
continue; | ||
} | ||
for (const combination of combinationSum(candidates, target - candidate, dp)) { | ||
if (combination[combination.length - 1] > candidate) { | ||
continue; | ||
} | ||
ans.push([...combination, candidate]); | ||
} | ||
} | ||
dp.set(target, ans); | ||
return ans; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
func numDecodings(s string) int { | ||
dp := make([]int, len(s) + 1) | ||
dp[0] = 1 | ||
const cnt = rune('Z') - rune('A') + 1 | ||
for i, c := range s { | ||
a := 0 | ||
if i != 0 { | ||
b := (rune(s[i - 1]) - rune('0')) * 10 + rune(c) - rune('0') | ||
if b > 9 && b <= cnt { | ||
a += dp[i - 1] | ||
} | ||
} | ||
b := rune(c) - rune('0') | ||
if b != 0 && b < cnt { | ||
a += dp[i] | ||
} | ||
dp[i + 1] = a | ||
} | ||
return dp[len(s)] | ||
} |
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. Kadane's Algorithm의 선형 풀이 대신 문제의 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
impl Solution { | ||
pub fn max_sub_array(nums: Vec<i32>) -> i32 { | ||
return Self::solve(&nums, 0, nums.len()).unwrap_or(0); | ||
} | ||
fn solve(nums: &Vec<i32>, l: usize, r: usize) -> Option<i32> { | ||
if l >= r { | ||
return None | ||
} | ||
if l + 1 == r { | ||
return Some(nums[l]) | ||
} | ||
let mid = l + ((r - l) >> 1); | ||
let a = Self::solve(nums, l, mid); | ||
let b = Self::solve(nums, mid, r); | ||
if a.is_none() || b.is_none() { | ||
return a.or(b) | ||
} | ||
let mut ans = a.max(b); | ||
let mut c = 0; | ||
let mut d = 0; | ||
for i in (l..mid).rev() { | ||
c += nums[i]; | ||
d = d.max(c); | ||
} | ||
if d == 0 { | ||
return ans | ||
} | ||
c = d; | ||
for i in mid..r { | ||
c += nums[i]; | ||
d = d.max(c); | ||
} | ||
ans.max(Some(d)) | ||
} | ||
} |
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. 공유해주신 풀이 방법이군요 :) 덕분에 배웠습니다! |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,4 @@ | ||||||||||||||||||||||||||||||||||||||
class Solution: | ||||||||||||||||||||||||||||||||||||||
@lru_cache | ||||||||||||||||||||||||||||||||||||||
def hammingWeight(self, n: int) -> int: | ||||||||||||||||||||||||||||||||||||||
return 1 + self.hammingWeight(n - (n & - n)) if n else 0 | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+2
to
+4
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.
Suggested change
이 문제의 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
function isPalindrome(s: string): boolean { | ||
const isAlpha = x => x.toLowerCase() >= 'a' && x.toLowerCase() <= 'z'; | ||
const isNumeric = x => x >= '0' && x <= '9'; | ||
const isAlphanumeric = x => isAlpha(x) || isNumeric(x); | ||
let i = 0, j = s.length - 1; | ||
while (i < j) { | ||
while (i !== j && ! isAlphanumeric(s[i])) { | ||
i++; | ||
} | ||
while (i !== j && ! isAlphanumeric(s[j])) { | ||
j--; | ||
} | ||
if (s[i].toLowerCase() !== s[j].toLowerCase()) { | ||
return false; | ||
} | ||
i++, j--; | ||
} | ||
return true; | ||
}; |
Uh oh!
There was an error while loading. Please reload this page.
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.
Top-Down DP 풀이인데 최악의 시간 복잡도와 공간 복잡도는 Bottom-UP 풀이와 같습니다: DaleSeo/AlgoDale#29
Top-Down DP 풀이는 부분 해를 완전히 전처리한 뒤 참조하므로 중복 원소 제거를 위해 부등식 검사가 추가로 필요했어요: https://github.com/yhkee0404/leetcode-study/blob/87da9b6db48f37cd4bda21c8b29d88a0d1b79419/combination-sum/yhkee0404.ts#L16
반면에 위와 같이 부분 해가 완성되기 전에 참조하는 경우는 처음 봐서 신기합니다! 덕분에 중복 원소가 제거되네요.
한편 재귀 호출 시 Default Argument인
dp
를 전달하지 않으면 40배 정도 느려졌으니 참고해 주세요.