This repository was archived by the owner on Feb 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
[서폿] 2021.10.03 #373
Merged
Merged
[서폿] 2021.10.03 #373
Changes from all commits
Commits
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,34 @@ | ||
| """ | ||
| # LeetCode | ||
| # ver.Python3 | ||
| # | ||
| # Created by GGlifer | ||
| # | ||
| # Open Source | ||
| """ | ||
|
|
||
| import heapq | ||
| from typing import * | ||
|
|
||
|
|
||
| class Solution: | ||
| def longestConsecutive(self, nums: List[int]) -> int: | ||
| # Set is implemented in hashset at Python3 | ||
| s = set(nums) | ||
|
|
||
| max_len = 0 | ||
| for e in s: | ||
| if e-1 in s: # Check e is first element in sequence | ||
| continue | ||
| else: | ||
| tmp_len = 0 | ||
| while e in s: | ||
| tmp_len += 1 | ||
| e += 1 | ||
| max_len = max(max_len, tmp_len) | ||
|
|
||
| return max_len | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| solution = Solution() |
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,35 @@ | ||
| """ | ||
| # LeetCode | ||
| # ver.Python3 | ||
| # | ||
| # Created by GGlifer | ||
| # | ||
| # Open Source | ||
| """ | ||
|
|
||
| from typing import * | ||
|
|
||
|
|
||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| # Square bracket [] | ||
| # Curly bracket {} | ||
| # Parentheses () | ||
| pair = {')': '(', '}': '{', ']': '['} | ||
|
|
||
| st = [] # stack | ||
|
|
||
| for b in s: | ||
| if b in ['(', '{', '[']: | ||
| st.append(b) | ||
| else: | ||
| if not st or st[-1] != pair[b]: | ||
| return False | ||
|
Comment on lines
+26
to
+27
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. 오.. 일치하지 않으면 |
||
| st.pop() | ||
|
|
||
| return len(st) == 0 | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| solution = Solution() | ||
|
|
||
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 @@ | ||
| """ | ||
| # LeetCode | ||
| # ver.Python3 | ||
| # | ||
| # Created by GGlifer | ||
| # | ||
| # Open Source | ||
| """ | ||
|
|
||
| from typing import * | ||
|
|
||
|
|
||
| class Solution: | ||
| def combinationSum4(self, nums: List[int], target: int) -> int: | ||
| # 1 <= target <= 1000 | ||
| # DP about target | ||
| dp = [0 for _ in range(target+1)] | ||
| for num in nums: | ||
| if num <= target: | ||
| dp[num] += 1 | ||
|
Comment on lines
+18
to
+20
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. 자기 자신에 대한 건 미리 해주셨네용! 저는 if문으로 검사해줬는데 이렇게 해도 괜찮을 것 같네용! |
||
|
|
||
| for i in range(2, target+1): | ||
| for num in nums: | ||
| if i-num >= 1: | ||
| dp[i] += dp[i-num] | ||
|
|
||
| return dp[target] | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| solution = Solution() | ||
|
|
||
| nums = [4, 2, 1] | ||
| target = 32 | ||
|
|
||
| print(solution.combinationSum4(nums, target)) | ||
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 @@ | ||
| """ | ||
| # LeetCode | ||
| # ver.Python3 | ||
| # | ||
| # Created by GGlifer | ||
| # | ||
| # Open Source | ||
| """ | ||
|
|
||
| from typing import * | ||
| from collections import deque | ||
| from itertools import accumulate | ||
|
|
||
|
|
||
| class Solution: | ||
| def canJump(self, nums: List[int]) -> bool: | ||
| if len(nums) == 1: | ||
| return True | ||
|
|
||
| # i+nums[i] -> max idx with index i can reach | ||
| # dp[i] -> max idx with index 0~i can reach | ||
| dp = [i+nums[i] for i in range(len(nums))] | ||
| for i in range(1, len(nums)): | ||
| dp[i] = max(dp[i], dp[i-1]) | ||
|
|
||
| # Jump!!! | ||
| idx = 0 | ||
| while idx < len(nums) and idx < dp[idx]: | ||
| idx = dp[idx] | ||
|
|
||
| # Check max idx with can reach is more than last idx | ||
| return bool(idx >= len(nums)-1) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| solution = Solution() | ||
|
|
||
| nums = [3, 2, 1, 0, 4] | ||
|
|
||
| solution.canJump(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,35 @@ | ||
| """ | ||
| # LeetCode | ||
| # ver.Python3 | ||
| # | ||
| # Created by GGlifer | ||
| # | ||
| # Open Source | ||
| """ | ||
|
|
||
| from typing import * | ||
|
|
||
|
|
||
| class Solution: | ||
| def combinationSum4(self, nums: List[int], target: int) -> int: | ||
|
|
||
| dp = [0 for _ in range(target+1)] | ||
| for num in nums: | ||
| if num <= target: | ||
| dp[num] += 1 | ||
|
|
||
| for i in range(2, target+1): | ||
| for num in nums: | ||
| if i-num >= 1: | ||
| dp[i] += dp[i-num] | ||
|
|
||
| return dp[target] | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| solution = Solution() | ||
|
|
||
| nums = [4, 2, 1] | ||
| target = 32 | ||
|
|
||
| print(solution.combinationSum4(nums, target)) |
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.
이런 방법이 있네요! 배워갑니다!