-
-
Notifications
You must be signed in to change notification settings - Fork 227
[ayosecu] WEEK 03 solutions #1282
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
5 commits
Select commit
Hold shift + click to select a range
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,40 @@ | ||
from typing import List | ||
|
||
class Solution: | ||
""" | ||
- Time Complexity: O(2^t), t = target | ||
- This combination makes binary tree (t = Height) | ||
- The number of node in binary tree is 2^t | ||
- Space Complexity: O(t) => O(1) | ||
- backtrack function calls stack size (Tree's Height = t) | ||
- 1 <= t <= 40 => O(t) => O(40) => O(1) | ||
""" | ||
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
result = [] | ||
|
||
def backtrack(path, target, start): | ||
if target == 0: | ||
result.append(list(path)) | ||
return | ||
if target < 0: | ||
return | ||
|
||
for i in range(start, len(candidates)): | ||
path.append(candidates[i]) | ||
backtrack(path, target - candidates[i], i) | ||
path.pop() | ||
|
||
backtrack([], target, 0) | ||
|
||
return result | ||
|
||
tc = [ | ||
([2,3,6,7], 7, [[2,2,3],[7]]), | ||
([2,3,5], 8, [[2,2,2,2],[2,3,3],[3,5]]), | ||
([2], 1, []) | ||
] | ||
|
||
for i, (c, t, e) in enumerate(tc, 1): | ||
sol = Solution() | ||
r = sol.combinationSum(c, t) | ||
print(f"TC {i} is Passed!" if r == e else f"TC {i} is Failed! - Expected: {e}, Result: {r}") |
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,57 @@ | ||
class Solution: | ||
""" | ||
- Time Complexity: O(n), n = len(s) | ||
- Space Complexity: O(n) | ||
""" | ||
def numDecodingsDP(self, s: str) -> int: | ||
if s[0] == "0": | ||
return 0 | ||
|
||
n = len(s) | ||
dp = [0] * (n + 1) | ||
# dp[0] => empty string => 1 case | ||
# dp[1] => not 0 => 1 case (single digit) | ||
dp[0], dp[1] = 1, 1 | ||
|
||
for i in range(2, n + 1): | ||
one_digit = int(s[i - 1:i]) | ||
two_digit = int(s[i - 2:i]) | ||
|
||
if 1 <= one_digit <= 9: | ||
dp[i] += dp[i - 1] | ||
if 10 <= two_digit <= 26: | ||
dp[i] += dp[i - 2] | ||
|
||
return dp[n] | ||
|
||
""" | ||
- Time Complexity: O(n), n = len(s) | ||
- Space Complexity: O(1) | ||
""" | ||
def numDecodings(self, s: str) -> int: | ||
if s[0] == "0": | ||
return 0 | ||
|
||
# Using two variables for checking single and double digit | ||
prev2, prev1 = 1, 1 | ||
|
||
for i in range(1, len(s)): | ||
current = 0 | ||
if s[i] != "0": | ||
current += prev1 | ||
if 10 <= int(s[i - 1:i + 1]) <= 26: | ||
current += prev2 | ||
prev2, prev1 = prev1, current | ||
|
||
return prev1 # prev1 = current | ||
|
||
tc = [ | ||
("12", 2), | ||
("226", 3), | ||
("06", 0) | ||
] | ||
|
||
for i, (s, e) in enumerate(tc, 1): | ||
sol = Solution() | ||
r = sol.numDecodings(s) | ||
print(f"TC {i} is Passed!" if r == e else f"TC {i} is Failed! - Expected: {e}, Result: {r}") |
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,31 @@ | ||
from typing import List | ||
|
||
class Solution: | ||
""" | ||
- Time Complexity: O(n) | ||
- Space Complexity: O(1) | ||
""" | ||
def maxSubArray(self, nums: List[int]) -> int: | ||
max_sum = float("-inf") | ||
|
||
curr_sum = 0 | ||
for num in nums: | ||
curr_sum += num | ||
if curr_sum < num: | ||
# if current sum is less than current number | ||
# current sum is replaced with current number | ||
curr_sum = num | ||
max_sum = max(max_sum, curr_sum) | ||
|
||
return max_sum | ||
|
||
tc = [ | ||
([-2,1,-3,4,-1,2,1,-5,4], 6), | ||
([1], 1), | ||
([5,4,-1,7,8], 23) | ||
] | ||
|
||
for i, (nums, e) in enumerate(tc, 1): | ||
sol = Solution() | ||
r = sol.maxSubArray(nums) | ||
print(f"TC {i} is Passed!" if r == e else f"TC {i} is Failed! - Expected: {e}, Result: {r}") |
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,27 @@ | ||
class Solution: | ||
""" | ||
- Time Complexity: O(1) | ||
- n is 32bit integer | ||
- while loop never exceed 32 times | ||
- Space Complexity: O(1) | ||
""" | ||
def hammingWeight(self, n: int) -> int: | ||
# check the most right bit, and shift right | ||
count = 0 | ||
while n > 0: | ||
if n & 1: | ||
count += 1 | ||
n >>= 1 | ||
|
||
return count | ||
|
||
tc = [ | ||
(11, 3), | ||
(128, 1), | ||
(2147483645, 30) | ||
] | ||
|
||
for i, (n, e) in enumerate(tc, 1): | ||
sol = Solution() | ||
r = sol.hammingWeight(n) | ||
print(f"TC {i} is Passed!" if r == e else f"TC {i} is Failed! - Expected: {e}, Result: {r}") |
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,36 @@ | ||
class Solution: | ||
""" | ||
- Time Complexity: O(n), n = len(s) | ||
- Space Complexity: O(1) | ||
""" | ||
def isPalindrome(self, s: str) -> bool: | ||
# to lower and check two pointers | ||
s = s.lower() | ||
l, r = 0, len(s) - 1 | ||
|
||
while l <= r: | ||
if not s[l].isalnum(): | ||
l += 1 | ||
continue | ||
if not s[r].isalnum(): | ||
r -= 1 | ||
continue | ||
|
||
if s[l] != s[r]: | ||
return False | ||
l += 1 | ||
r -= 1 | ||
|
||
return True | ||
|
||
|
||
tc = [ | ||
("A man, a plan, a canal: Panama", True), | ||
("race a car", False), | ||
(" ", True) | ||
] | ||
|
||
for i, (s, e) in enumerate(tc, 1): | ||
sol = Solution() | ||
r = sol.isPalindrome(s) | ||
print(f"TC {i} is Passed!" if r == e else f"TC {i} is Failed! - Expected: {e}, Result: {r}") |
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.
한 주간 수고 많으셨습니다. 🎉 리트코드 예제를 활용해 테스트 케이스를 간단히 정리하신 방식이 인상적이었습니다. 코드를 보는데 도움이 되네요.