Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 7 additions & 4 deletions contains-duplicate/rivkode.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
# The size of the set for storing deduplicated elements is proportional to the length of the input list.

class Solution:
def containsDuplicate(self, nums: list[int]) -> bool:
list_len = len(nums)
set_len = len(set(nums))
def containsDuplicate(self, nums):
list_num = len(nums)
set_num = len(set(nums))

return list_len != set_len
if list_num != set_num:
return True
else:
return False

if __name__ == "__main__":
solution = Solution()
Expand Down
35 changes: 26 additions & 9 deletions top-k-frequent-elements/rivkode.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,35 @@
# - and when sorting takes O(n), hash[x] occupy O(1)

class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
hash = dict()
def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""

# for loop nums to set count for each element in hash(dictionary)
for num in nums:
if num in hash:
hash[num] += 1
dic = {}

for v in nums:
if v in dic:
cur = dic[v]
cur += 1
dic[v] = cur
else:
hash[num] = 1
dic[v] = 1
Copy link
Contributor

Choose a reason for hiding this comment

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

for v in nums:
    dic[v] = dic.get(v, 0) + 1

이런 식으로 간결하기 줄일 수도 있을거 같아요~!

Copy link
Member Author

Choose a reason for hiding this comment

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

넵 감사합니다 ~

Copy link
Member Author

Choose a reason for hiding this comment

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

get(v, 0) 과 같이 기본값을 사용하는 것은 좋은 방법인 것 같습니다 ㅎㅎ

Copy link
Member Author

Choose a reason for hiding this comment

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

@shinheekim 알려주신 방법대로 수정했습니다~


reverse_desc = sorted(dic.items(), key=lambda item: item[1], reverse=True)

# sort (TimSort), using lambda function to set a sorting key which is a count
return sorted(hash, key=lambda x: hash[x], reverse=True)[:k]
n = 0
result = []
for v in reverse_desc:
if n == k:
break

result.append(v[0])
n += 1

return result

if __name__ == "__main__":
solution = Solution()
Expand Down
18 changes: 18 additions & 0 deletions two-sum/rivkode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution(object):
def twoSum(self, nums, target):

"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""

dic = {}

for i, v in enumerate(nums):
complement = target - v

if complement in dic:
return [i, dic[complement]]

dic[v] = i
Copy link
Contributor

Choose a reason for hiding this comment

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

파일의 마지막에 개행문자를 추가해서 Posix 표준 준수해주시면 좋을 거 같습니다.

Loading