-
Notifications
You must be signed in to change notification settings - Fork 126
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
[HaJunYoo][WEEK 01] 리트코드 문제 풀이 #303
Changes from 6 commits
6d1ee5a
a2d1621
ca0a2b4
833fda5
6139c9c
0c8f197
b9ae750
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Solution: | ||
# Time complexity: O(n) | ||
def containsDuplicate(self, nums: List[int]) -> bool: | ||
string_len = len(nums) | ||
set_len = len(set(nums)) | ||
|
||
return string_len != set_len |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# 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: | ||
# Time complexity: O(n) | ||
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: | ||
|
||
self.count = 0 | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Solution: | ||
# Time complexity: O(log n) | ||
def hammingWeight(self, n: int) -> int: | ||
n = int(n) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 안녕하세요 HajunYoo님 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분은 저도 의아했는데요 |
||
cnt = 0 | ||
while n > 0: | ||
if n % 2 == 1: | ||
cnt += 1 | ||
n = n // 2 | ||
return cnt |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution: | ||
# Time complexity: O(n^2) = O(n) * O(n) | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from collections import defaultdict | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
# Time complexity: O(nlogn) -> O(n) + O(nlogn) + O(k) | ||
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
counter_dict = defaultdict(int) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. collections의 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 리뷰 감사드립니다 ㅎㅎ |
||
|
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
질문) 그냥
count
로 선언하는 것과self.count
로 선언하는 것에는 어떤 차이가 있나요?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
리트코드에서 count를 그냥 선언하면 지역 변수라서 읽어오지를 못했습니다!
global로 선언해도 동일해서, 인스턴수 변수로 관리해보았습니다