diff --git a/contains-duplicate/6kitty.py b/contains-duplicate/6kitty.py new file mode 100644 index 0000000000..a5a33a9986 --- /dev/null +++ b/contains-duplicate/6kitty.py @@ -0,0 +1,20 @@ +# +# @lc app=leetcode id=217 lang=python3 +# +# [217] Contains Duplicate +# + +# @lc code=start +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + # list로 하니까 시간 초과 떠서 set으로 변경 + idx=set() + for i in nums: + if i in idx: + return True + idx.add(i) + # null 방지 + return False + +# @lc code=end + diff --git a/house-robber/6kitty.py b/house-robber/6kitty.py new file mode 100644 index 0000000000..8a1b65a470 --- /dev/null +++ b/house-robber/6kitty.py @@ -0,0 +1,17 @@ +# +# @lc app=leetcode id=198 lang=python3 +# +# [198] House Robber +# + +# @lc code=start +class Solution: + def rob(self, nums: List[int]) -> int: + pre=0 + cur=0 + for num in nums: + pre, cur = cur, max(num + pre, cur) + return cur + +# @lc code=end + diff --git a/longest-consecutive-sequence/6kitty.py b/longest-consecutive-sequence/6kitty.py new file mode 100644 index 0000000000..249cfbf91d --- /dev/null +++ b/longest-consecutive-sequence/6kitty.py @@ -0,0 +1,28 @@ +# +# @lc app=leetcode id=128 lang=python3 +# +# [128] Longest Consecutive Sequence +# +from typing import List +# @lc code=start +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + nums.sort() + long=0 + + leng=1 + for i in range(len(nums)-1): + if nums[i]==nums[i+1]: + continue + if nums[i]+1==nums[i+1]: + leng+=1 + else: + long=max(long,leng) + leng=1 + # 아래를 추가해야 for문 다 돌았을 때 leng이 최고인 경우 반영 + long=max(long,leng) + return long + + +# @lc code=end + diff --git a/top-k-frequent-elements/6kitty.py b/top-k-frequent-elements/6kitty.py new file mode 100644 index 0000000000..5d9c94f5a2 --- /dev/null +++ b/top-k-frequent-elements/6kitty.py @@ -0,0 +1,25 @@ +# +# @lc app=leetcode id=347 lang=python3 +# +# [347] Top K Frequent Elements +# + +# @lc code=start +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + sol = {} + for i in nums: + sol[i] = sol.get(i, 0) + 1 + + sorted_items = sorted(sol.items(), key=lambda x: x[1], reverse=True) + + ssol = [] + for i in range(k): + ssol.append(sorted_items[i][0]) + + return ssol + + + +# @lc code=end + diff --git a/two-sum/6kitty.py b/two-sum/6kitty.py new file mode 100644 index 0000000000..0e3012ea16 --- /dev/null +++ b/two-sum/6kitty.py @@ -0,0 +1,16 @@ +# +# @lc app=leetcode id=1 lang=python3 +# +# [1] Two Sum +# + +# @lc code=start +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + for i,num in enumerate(nums): + for j in range(i+1,len(nums)): + if num + nums[j] == target: + return [i,j] + +# @lc code=end +