-
-
Notifications
You must be signed in to change notification settings - Fork 195
[mandoolala] WEEK 2 solutions #1323
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
Closed
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
868e017
init solution files
5e4c93e
contains duplicate sol
4d40b13
two sum sol
5ceaf52
eol
3c1ea6a
Merge branch 'DaleStudy:main' into main
mandoolala a309b1e
top k elements
4048621
eol
085e297
try using heap solution
90318a2
longest consecutive sequence
a907534
use num set for time limit exceeded
b9cd6b6
house robber
d0f8e0c
Merge branch 'week1'
ea008fb
init sol files
219acf4
valid anagram #218
8175937
Climbing Stairs #230
915c79c
Product of Array Except Self #239
ff4aa6d
3Sum #241
8bd1ce8
Validate Binary Search Tree #251
715db75
Merge branch 'DaleStudy:main' into week2
mandoolala 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,21 @@ | ||
from typing import List | ||
|
||
class Solution: | ||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
answer = set() | ||
sorted_nums = sorted(nums) | ||
|
||
for i in range(len(nums) - 2): | ||
low, high = i + 1, len(nums) - 1 | ||
while low < high: | ||
three_sum = sorted_nums[i] + sorted_nums[low] + sorted_nums[high] | ||
if three_sum == 0: | ||
answer.add((sorted_nums[i], sorted_nums[low], sorted_nums[high])) | ||
low += 1 | ||
high -= 1 | ||
elif three_sum < 0: | ||
low += 1 | ||
elif three_sum > 0: | ||
high -= 1 | ||
return list(answer) | ||
|
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 climbStairs(self, n: int) -> int: | ||
if n == 1: | ||
return 1 | ||
if n == 2: | ||
return 2 | ||
dp = [0]*n | ||
dp[0] = 1 | ||
dp[1] = 2 | ||
for i in range(2, n): | ||
dp[i] = dp[i-1] + dp[i-2] | ||
return dp[n-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,14 @@ | ||
from typing import List | ||
|
||
class Solution: | ||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
answer = [1] * len(nums) | ||
left_product = 1 | ||
for i in range(len(nums) - 1): | ||
left_product *= nums[i] | ||
answer[i + 1] *= left_product | ||
right_product = 1 | ||
for i in range(len(nums) - 1, 0, -1): | ||
right_product *= nums[i] | ||
answer[i - 1] *= right_product | ||
return answer |
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,3 @@ | ||
class Solution: | ||
def isAnagram(self, s: str, t: str) -> bool: | ||
return sorted(s) == sorted(t) |
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 @@ | ||
from typing import Optional | ||
|
||
# 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 traverse(node, low, high): | ||
if not node: | ||
return True | ||
if not (low < node.val < high): | ||
return False | ||
return traverse(node.left, low, node.val) and traverse(node.right, node.val, high) | ||
|
||
return traverse(root, float("-inf"), float("inf")) | ||
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. 오... |
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.
(이미 충분히 빠른 코드이지만) 첫 번째 for 루프에서 0의 갯수를 세어 2개 이상인 경우 바로 0 배열을 반환하면 더욱 빠른 코드가 될 것 같습니다!