-
-
Notifications
You must be signed in to change notification settings - Fork 304
[radiantchoi] WEEK 02 Solutions #2050
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
8 commits
Select commit
Hold shift + click to select a range
d9dd2d8
ci: remove merge group trigger from integration workflow
DaleSeo 7fb126b
valid anagram solution
radiantchoi b9c6be1
product of array except self solution
radiantchoi 4d421e1
3sum solution
radiantchoi 2a3568b
fix: sync integration file
radiantchoi 6966176
climbing stairs solution
radiantchoi 2d27f84
fix: add additional line for climbing stairs solution
radiantchoi e7ffca0
validating binary search tree solutions
radiantchoi 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,45 @@ | ||
| class Solution: | ||
| def twoSum(self, target: int, nums: List[int], starting: int) -> List[List[int]]: | ||
| left = starting | ||
| right = len(nums) - 1 | ||
|
|
||
| result = [] | ||
|
|
||
| while left < right: | ||
| if nums[left] + nums[right] == target: | ||
| result.append([nums[left], nums[right]]) | ||
| left += 1 | ||
| right -= 1 | ||
|
|
||
| while left < right and nums[left] == nums[left - 1]: | ||
| left += 1 | ||
|
|
||
| while left < right and nums[right] == nums[right + 1]: | ||
| right -= 1 | ||
| elif nums[left] + nums[right] > target: | ||
| right -= 1 | ||
| else: | ||
| left += 1 | ||
|
|
||
| return result | ||
|
|
||
| def kSum(self, n: int, target: int, nums: List[int], starting: int) -> List[List[int]]: | ||
| if n < 2 or starting == len(nums): | ||
| return [] | ||
| elif n == 2: | ||
| return self.twoSum(target, nums, starting) | ||
|
|
||
| result = [] | ||
|
|
||
| for i in range(starting, len(nums) - n + 1): | ||
| if i > starting and nums[i] == nums[i - 1]: | ||
| continue | ||
|
|
||
| for tail in self.kSum(n - 1, target - nums[i], nums, i + 1): | ||
| result.append([nums[i]] + tail) | ||
|
|
||
| return result | ||
|
|
||
| def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
| nums.sort() | ||
| return self.kSum(3, 0, nums, 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,9 @@ | ||
| class Solution: | ||
| def climbStairs(self, n: int) -> int: | ||
| stairs = [1, 1] | ||
|
|
||
| if n > 1: | ||
| for i in range(2, n+1): | ||
| stairs.append(stairs[i - 1] + stairs[i - 2]) | ||
|
|
||
| return stairs[-1] |
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]: | ||
| reduced = 1 | ||
| zeroes = 0 | ||
|
|
||
| for num in nums: | ||
| if num != 0: | ||
| reduced *= num | ||
| else: | ||
| zeroes += 1 | ||
|
|
||
| result = [] | ||
|
|
||
| for num in nums: | ||
| if num == 0: | ||
| result.append(0 if zeroes - 1 > 0 else reduced) | ||
| else: | ||
| result.append(0 if zeroes else reduced // num) | ||
|
|
||
| return result | ||
haxr369 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,12 @@ | ||
| class Solution: | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| s_letters = {} | ||
| t_letters = {} | ||
|
|
||
| for letter in s: | ||
| s_letters[letter] = s_letters.get(letter, 0) + 1 | ||
|
|
||
| for letter in t: | ||
| t_letters[letter] = t_letters.get(letter, 0) + 1 | ||
|
|
||
| return s_letters == t_letters |
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 @@ | ||
| # 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 check(self, root: Optional[TreeNode], minimum: int, maximum: int) -> bool: | ||
| if root is None: | ||
| return True | ||
|
|
||
| if root.val <= minimum or root.val >= maximum: | ||
| return False | ||
|
|
||
| return self.check(root.left, minimum, root.val) and self.check(root.right, root.val, maximum) | ||
|
|
||
| def isValidBST(self, root: Optional[TreeNode]) -> bool: | ||
| minimum_constraint = ((-2) ** 31) - 1 | ||
| maximum_constraint = 2 ** 31 | ||
|
|
||
| return self.check(root, minimum_constraint, maximum_constraint) |
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.
Uh oh!
There was an error while loading. Please reload this page.