Skip to content

[i-mprovising] Week 01 solutions #1167

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 6 commits into from
Apr 5, 2025
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
11 changes: 11 additions & 0 deletions contains-duplicate/i-mprovising.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
O(n) complexity
"""


class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
nums_set = set(nums)
if len(nums) == len(nums_set):
return False
return True
Copy link
Member

Choose a reason for hiding this comment

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

Set을 이용해서 중복을 없앤뒤 기존 배열과 비교해서 처리한 부분이 저랑 비슷한 풀이같네요👍

21 changes: 21 additions & 0 deletions house-robber/i-mprovising.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Time complexity O(n)
"""

class Solution:
def rob(self, nums: List[int]) -> int:
n = len(nums)
if n == 1:
return nums[0]
dp = [nums[0], max([nums[0], nums[1]])]
if n == 2:
return dp[1]

for i in range(2, n):
num = nums[i]
tmp = [
dp[i-2] + num,
dp[i-1]
]
dp.append(max(tmp))
return dp[-1]
19 changes: 19 additions & 0 deletions longest-consecutive-sequence/i-mprovising.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
Time complexity O(n)
"""

class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
num_set = set(nums) # hash table
longest = 0

for n in num_set:
if n-1 in num_set:
continue
cur_len = 1
while n + cur_len in num_set:
cur_len += 1

longest = max(longest, cur_len)

return longest
11 changes: 11 additions & 0 deletions top-k-frequent-elements/i-mprovising.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
"""O(nlogn) complexity"""
frequency = defaultdict(int)
for n in nums:
frequency[n] += 1
sorted_frequency = sorted(frequency.items(), key=lambda x:x[1], reverse=True)
answer = []
for i in range(k):
answer.append(sorted_frequency[i][0])
return answer
12 changes: 12 additions & 0 deletions two-sum/i-mprovising.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
hash table
O(n) complexity
"""

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
table = {num:idx for idx, num in enumerate(nums)}
for i, x in enumerate(nums):
y = target - x
if (y in table) and table[y] != i:
return [i, table[y]]
Copy link
Member

@hsskey hsskey Apr 5, 2025

Choose a reason for hiding this comment

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

첫 번째 루프에서 table을 미리 만드는 것보다는, enumerate로 순회하면서 동시에 dict에 저장해서
개선해 볼 수 있을거같아
의견 남깁니다.

ex)

table = {}
        for i, num in enumerate(nums):
            complement = target - num
            if complement in table:
                return [table[complement], i]
            table[num] = i

Copy link
Contributor Author

Choose a reason for hiding this comment

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

상세한 리뷰 감사합니다!
굳이 루프를 더 쓸 필요가 없네요.