Skip to content
This repository was archived by the owner on Feb 22, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions keeprainy/leetcode/128_Longest_Consecutive_Sequence.py
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()
35 changes: 35 additions & 0 deletions keeprainy/leetcode/20_Valid_Parentheses.py
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 = {')': '(', '}': '{', ']': '['}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 방법이 있네요! 배워갑니다!


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오.. 일치하지 않으면 false니까 바로 리턴해도 되네용. 저는 나중에 스택에 남는 거 여부를 검사했는데 이렇게 하는게 훨씬 빠른 것 같아용!

st.pop()

return len(st) == 0


if __name__ == '__main__':
solution = Solution()

36 changes: 36 additions & 0 deletions keeprainy/leetcode/39_Combination_Sum.py
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
Copy link
Member

Choose a reason for hiding this comment

The 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))
40 changes: 40 additions & 0 deletions keeprainy/leetcode/55_Jump_Game.py
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)
35 changes: 35 additions & 0 deletions keeprainy/main.py
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))