-
-
Notifications
You must be signed in to change notification settings - Fork 219
[yayyz] WEEK 01 solutions #1159
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
11 commits
Select commit
Hold shift + click to select a range
7863575
two sum solution
yayyz 4914619
added newline
yayyz 0a89e70
contains duplicate solution
yayyz c79c692
added newline
yayyz ef71c9f
product of array except self solution
yayyz 1de52da
longest consecutive sequence solution & added newline
yayyz b74f6ce
house robber solution & added newline
yayyz 9b163e2
added newline
yayyz 4a6c717
added newline
yayyz a234b58
:rotating_light: added newline
yayyz 9475b05
:rotating_light: added new line
yayyz 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,4 @@ | ||
class Solution: | ||
def containsDuplicate(self, nums: List[int]) -> bool: | ||
|
||
return len(nums) != len(set(nums)) |
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,16 @@ | ||
class Solution: | ||
def rob(self, nums: List[int]) -> int: | ||
n = len(nums) | ||
|
||
if n == 1: | ||
return nums[0] | ||
|
||
dp = [0] * n | ||
dp[0] = nums[0] | ||
dp[1] = max(nums[0], nums[1]) | ||
|
||
# i 번째를 털지 않은 조건과 i번째를 털은 조건 중 max를 비교 | ||
for i in range(2, n): | ||
dp[i] = max(dp[i-1], dp[i-2] + nums[i]) | ||
|
||
return dp[-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,21 @@ | ||
class Solution: | ||
def longestConsecutive(self, nums: List[int]) -> int: | ||
if len(nums) == 0: | ||
return 0 | ||
|
||
nums = sorted(set(nums)) | ||
|
||
currLength = 1 | ||
lastNum = nums[0] | ||
result = 1 | ||
|
||
for i in range(1, len(nums)): | ||
if nums[i] == lastNum + 1: | ||
currLength += 1 | ||
else: | ||
currLength = 1 | ||
|
||
result = max(result, currLength) | ||
lastNum = 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,16 @@ | ||
class Solution: | ||
# prefix, postfix probelm :( | ||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
output = [1] * len(nums) | ||
|
||
prefix = 1 | ||
for i in range(len(nums)): | ||
output[i] = prefix | ||
prefix *= nums[i] | ||
|
||
postfix = 1 | ||
for i in range(len(nums) -1, -1, -1): | ||
output[i] *= postfix | ||
postfix *= nums[i] | ||
|
||
return output |
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,10 @@ | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
hashmap = {} | ||
for i, num in enumerate(nums): | ||
complement = target - num | ||
if complement in hashmap: | ||
return [hashmap[complement], i] | ||
hashmap[num] = i | ||
|
||
|
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.
complement
라는 변수명을 직관적으로 잘 작성해주신 것 같습니다 :)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.
감사합니다! 최대한 직관적인 변수명으로 작성하려고 했습니다 :)