-
-
Notifications
You must be signed in to change notification settings - Fork 304
[daiyongg-kim] WEEK 02 solutions #2033
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
7 commits
Select commit
Hold shift + click to select a range
2f62efd
adding valid-anagram
daiyongg-kim f2b5a10
adding climbing stairs
daiyongg-kim 6d0e4b4
adding product-of-array-except-self
daiyongg-kim bb52a49
adding climbing stairs
daiyongg-kim ac0da2e
adding climbing stairs
daiyongg-kim 86c5f80
adding isValidBST
daiyongg-kim ba5559a
adding 3sum
daiyongg-kim 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,36 @@ | ||
| class Solution: | ||
| def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
| nums.sort() | ||
|
|
||
| result = [] | ||
| n = len(nums) | ||
|
|
||
| for i in range(n - 2): | ||
|
|
||
| if i > 0 and nums[i] == nums[i - 1]: | ||
| continue | ||
|
|
||
| left = i + 1 | ||
| right = n - 1 | ||
| while left < right: | ||
| current_sum = nums[i] + nums[left] + nums[right] | ||
|
|
||
| if current_sum == 0: | ||
| result.append([nums[i], nums[left], nums[right]]) | ||
|
|
||
| while left < right and nums[left] == nums[left + 1]: | ||
| left += 1 | ||
|
|
||
| while left < right and nums[right] == nums[right - 1]: | ||
| right -= 1 | ||
|
|
||
| left += 1 | ||
| right -= 1 | ||
|
|
||
| elif current_sum < 0: | ||
| left += 1 | ||
|
|
||
| else: | ||
| right -= 1 | ||
|
|
||
| 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,13 @@ | ||
| class Solution: | ||
| def climbStairs(self, n: int) -> int: | ||
| if n == 1: | ||
| return 1 | ||
|
|
||
| prev, curr = 1, 2 | ||
|
|
||
| for i in range(3, n+1): | ||
| next = prev + curr | ||
| prev = curr | ||
| curr = next | ||
|
|
||
| return curr |
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 @@ | ||
| class Solution: | ||
| def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
|
|
||
| size = len(nums) | ||
|
|
||
| #initialize array as 1 | ||
| result = [1] * size | ||
|
|
||
| left_pass = 1 | ||
|
|
||
| for i in range(size):A | ||
| result[i] *= left_pass | ||
| left_pass *= nums[i] | ||
|
|
||
| right_pass = 1 | ||
| for i in range(size-1, -1, -1): | ||
| result[i] *= right_pass | ||
| right_pass *= nums[i] | ||
|
|
||
| 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,44 @@ | ||
| class Solution: | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| first = {} | ||
| second = {} | ||
|
|
||
| for c in s: | ||
| if c in first: | ||
| first[c] += 1 | ||
| else: | ||
| first[c] = 1 | ||
|
|
||
| for c in t: | ||
| if c in second: | ||
| second[c] += 1 | ||
| else: | ||
| second[c] = 1 | ||
|
|
||
| return first == second | ||
|
|
||
|
|
||
| # class Solution: | ||
| # def isAnagram(self, s: str, t: str) -> bool: | ||
| # first = {} | ||
|
|
||
| # if len(s) != len(t): | ||
| # return False | ||
|
|
||
| # for i in range(len(s)): | ||
| # if s[i] in first: | ||
| # first[s[i]] += 1 | ||
| # else: | ||
| # first[s[i]] = 1 | ||
|
|
||
| # for i in range(len(t)): | ||
| # if t[i] in first: | ||
| # first[t[i]] -= 1 | ||
| # else: | ||
| # return False | ||
|
|
||
| # for i in first.values(): | ||
| # if i != 0: | ||
| # 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,18 @@ | ||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| class Solution: | ||
| def isValidBST(self, root: Optional[TreeNode]) -> bool: | ||
|
|
||
| def checkBST(root, min_val, max_val): | ||
| if not root: | ||
| return True | ||
|
|
||
| if not (min_val < root.val < max_val): | ||
| return False | ||
| return checkBST(root.left, min_val, root.val) and checkBST(root.right, root.val, max_val) | ||
|
|
||
| return checkBST(root, float('-inf'), float('inf')) |
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.
JavaScript에서 이 둘을 다르다고 리턴할텐데, Python의 딕셔너리는 동일하다고 리턴을 하는군요..!