Skip to content
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
20 changes: 20 additions & 0 deletions contains-duplicate/6kitty.py
Original file line number Diff line number Diff line change
@@ -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

17 changes: 17 additions & 0 deletions house-robber/6kitty.py
Original file line number Diff line number Diff line change
@@ -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

28 changes: 28 additions & 0 deletions longest-consecutive-sequence/6kitty.py
Original file line number Diff line number Diff line change
@@ -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

25 changes: 25 additions & 0 deletions top-k-frequent-elements/6kitty.py
Original file line number Diff line number Diff line change
@@ -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

16 changes: 16 additions & 0 deletions two-sum/6kitty.py
Original file line number Diff line number Diff line change
@@ -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]
Comment on lines +10 to +13
Copy link
Member

Choose a reason for hiding this comment

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

for 문을 2중 루프로 돌아서 해결하는 방법도 있지만 달레님 해설에도 나온 것 처럼 해시 테이블이나 아니면 left, right 를 사용해서 1번의 for 문 으로도 해결할 수 있을 것 같습니다.

나머지 문제들은 정석대로 잘 풀어주신 것 같습니다!


# @lc code=end