-
-
Notifications
You must be signed in to change notification settings - Fork 195
[hi-rachel] WEEK 03 solutions #1325
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
d3ef18f
valid palindrome solution(py, ts)
hi-rachel 0199703
number-of-1-bits solution (py, ts)
hi-rachel 78849ad
combination-sum solution(py)
hi-rachel f3496fb
decode-ways solution(py)
hi-rachel 09e54d4
maximum-subarray solution (py)
hi-rachel ad5aee5
fix linelint
hi-rachel 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,20 @@ | ||
# DFS + 백트래킹 | ||
# 중복 조합 문제 | ||
# O(2^t) time, O(재귀 깊이 (t) + 결과 조합의 수 (len(result))) space | ||
|
||
class Solution: | ||
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
nums, result = [], [] | ||
def dfs(start, total): | ||
if total > target: | ||
return | ||
if total == target: | ||
result.append(nums[:]) | ||
for i in range(start, len(candidates)): | ||
num = candidates[i] | ||
nums.append(num) | ||
dfs(i, total + num) | ||
nums.pop() | ||
|
||
dfs(0, 0) | ||
return result |
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,19 @@ | ||
# 디코드 가능 1 ~ 26 | ||
# 숫자의 첫 번째 자리가 0이라면 decode x | ||
# O(n) time, O(n) space | ||
|
||
class Solution: | ||
def numDecodings(self, s: str) -> int: | ||
memo = {len(s): 1} # 문자열 끝에 도달했을 때는 경우의 수 1 | ||
|
||
def dfs(start): | ||
if start in memo: # 이미 계산한 위치 재계산 x | ||
return memo[start] | ||
if s[start] == "0": | ||
memo[start] = 0 | ||
elif start + 1 < len(s) and int(s[start:start + 2]) < 27: # 두 자리로 해석 가능할 때 | ||
memo[start] = dfs(start + 1) + dfs(start + 2) # 첫 한 자리만 decode 경우 + 두 자리 한꺼번에 decode 경우 | ||
else: | ||
memo[start] = dfs(start + 1) # 두 자리로 decode 불가능할 때 -> 한 자리만 decode | ||
return memo[start] | ||
return dfs(0) |
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,35 @@ | ||
# 최대 부분 배열 합 문제 | ||
|
||
# O(n^2) time, O(1) space | ||
# Time Limit Exceeded | ||
|
||
class Solution: | ||
def maxSubArray(self, nums: List[int]) -> int: | ||
max_total = nums[0] | ||
for i in range(len(nums)): | ||
total = 0 | ||
for j in range(i, len(nums)): | ||
total += nums[j] | ||
max_total = max(total, max_total) | ||
return max_total | ||
|
||
# 개선 풀이 | ||
# O(n) time, O(1) space | ||
class Solution: | ||
def maxSubArray(self, nums: List[int]) -> int: | ||
max_total = nums[0] | ||
total = nums[0] | ||
for i in range(1, len(nums)): | ||
total = max(nums[i], total + nums[i]) | ||
max_total = max(total, max_total) | ||
return max_total | ||
|
||
# DP 풀이 | ||
# O(n) time, O(n) space | ||
class Solution: | ||
def maxSubArray(self, nums: List[int]) -> int: | ||
dp = [0] * len(nums) | ||
dp[0] = nums[0] | ||
for i in range(1, len(nums)): | ||
dp[i] = max(nums[i], dp[i - 1] + nums[i]) | ||
return max(dp) |
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,26 @@ | ||
# O(log n) time, O(1) space | ||
# % 나머지, // 몫 | ||
|
||
class Solution: | ||
def hammingWeight(self, n: int) -> int: | ||
cnt = 0 | ||
|
||
while n > 0: | ||
if (n % 2) == 1: | ||
cnt += 1 | ||
n //= 2 | ||
|
||
return cnt | ||
|
||
|
||
# O(log n) time, O(log n) space | ||
class Solution: | ||
def hammingWeight(self, n: int) -> int: | ||
return bin(n).count("1") | ||
|
||
|
||
# TS 풀이 | ||
# O(log n) time, O(log n) space | ||
# function hammingWeight(n: number): number { | ||
# return n.toString(2).split('').filter(bit => bit === '1').length; | ||
# }; |
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,20 @@ | ||
# O(n) time, O(1) space | ||
# isalnum() -> 문자열이 영어, 한글 혹은 숫자로 되어있으면 참 리턴, 아니면 거짓 리턴. | ||
|
||
class Solution: | ||
def isPalindrome(self, s: str) -> bool: | ||
l = 0 | ||
r = len(s) - 1 | ||
|
||
while l < r: | ||
if not s[l].isalnum(): | ||
l += 1 | ||
elif not s[r].isalnum(): | ||
r -= 1 | ||
elif s[l].lower() == s[r].lower(): | ||
l += 1 | ||
r -= 1 | ||
else: | ||
return False | ||
return True | ||
|
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 @@ | ||
// O(n) time, O(1) space | ||
|
||
function isPalindrome(s: string): boolean { | ||
let low = 0, | ||
high = s.length - 1; | ||
|
||
while (low < high) { | ||
while (low < high && !s[low].match(/[0-9a-zA-Z]/)) { | ||
low++; | ||
} | ||
while (low < high && !s[high].match(/[0-9a-zA-Z]/)) { | ||
high--; | ||
} | ||
if (s[low].toLowerCase() !== s[high].toLowerCase()) { | ||
return false; | ||
} | ||
low++; | ||
high--; | ||
} | ||
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.
bin()
함수는 처음 보았는데 흥미롭네요 😃