Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 6 additions & 0 deletions contains-duplicate/hajunyoo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
string_len = len(nums)
set_len = len(set(nums))

return string_len != set_len
30 changes: 30 additions & 0 deletions kth-smallest-element-in-a-bst/hajunyoo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
from collections import defaultdict


class Solution:
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:

self.count = 0
Copy link
Contributor

Choose a reason for hiding this comment

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

질문) 그냥 count로 선언하는 것과 self.count로 선언하는 것에는 어떤 차이가 있나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

리트코드에서 count를 그냥 선언하면 지역 변수라서 읽어오지를 못했습니다!
global로 선언해도 동일해서, 인스턴수 변수로 관리해보았습니다

self.result = 0

def dfs(node):
if not node:
return

dfs(node.left)

self.count += 1
if self.count == k:
self.result = node.val
return

dfs(node.right)

dfs(root)
return self.result
9 changes: 9 additions & 0 deletions number-of-1-bits/hajunyoo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def hammingWeight(self, n: int) -> int:
n = int(n)
Copy link
Contributor

Choose a reason for hiding this comment

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

안녕하세요 HajunYoo님
n = int(n) 코드를 작성하신 이유가 궁금합니다 :)
제 생각엔 def hammingWeight(self, n: int) -> int:에서 parameter nint로 받고 있기 때문에 굳이 적지 않아도 될 코드 같아요

Copy link
Contributor Author

Choose a reason for hiding this comment

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

이 부분은 저도 의아했는데요
n이 string으로 인식이 되는 에러가 발생했어서, int로 type casting을 해보았습니다
나중에 한번 다시 시도해보겠습니다 ㅎㅎ

cnt = 0
while n > 0:
if n % 2 == 1:
cnt += 1
n = n // 2
return cnt
18 changes: 18 additions & 0 deletions palindromic-substrings/hajunyoo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
def countSubstrings(self, s: str) -> int:
self.count = 0
n = len(s)

def two_pointer_expand(left, right):
while left >= 0 and right < n and s[left] == s[right]:
self.count += 1
left -= 1
right += 1

for i in range(0, n):
# 1, 3, 5 ...
two_pointer_expand(i, i)
# 2, 4, 6 ...
two_pointer_expand(i, i + 1)

return self.count
19 changes: 19 additions & 0 deletions top-k-frequent-elements/hajunyoo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from collections import defaultdict
from typing import List


class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
counter_dict = defaultdict(int)
Copy link
Contributor

Choose a reason for hiding this comment

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

collections의 Counter를 쓰는 건 어떨지 조심스레 제안드립니다 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

리뷰 감사드립니다 ㅎㅎ
사실 Counter 써보려고 했는데, 코딩 테스트에서 중요한 순간에 혹시 해당 라이브러리가 안 떠오를 수도 있고, Counter를 쓰지 않고 라이브 코딩테스트를 요청 받을 수 있을 것 같아 위와 같이 구성해보았습니다.


for n in nums:
counter_dict[n] += 1

count_list = []
for key, val in counter_dict.items():
count_list.append((key, val))

count_list.sort(key=lambda x: x[1], reverse=True)
answer = [a for a, b in count_list[:k]]

return answer