Skip to content

[sun] W1 Solutions #315

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 8 commits into from
Aug 17, 2024
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
8 changes: 8 additions & 0 deletions contains-duplicate/sun912.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""
Time complexity: O(n)
"""

class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
nums_set = set(nums)
return len(nums_set) != len(nums)
27 changes: 27 additions & 0 deletions kth-smallest-element-in-a-bst/sun912.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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

"""
TC: O(n) in worst case
SC: O(n) in worst case
"""
class Solution:
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
n = 0
stack = []
current_node = root

while current_node or stack:
while current_node:
stack.append(current_node)
current_node = current_node.left

current_node = stack.pop()
n += 1
if n == k:
return current_node.val
current_node = current_node.right
13 changes: 13 additions & 0 deletions number-of-1-bits/sun912.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
TC: O(log n)
SC: O(1)
"""

class Solution:
def hammingWeight(self, n: int) -> int:
result = 0
while n > 0:
result += n % 2
n = n//2

return result
Copy link
Member

Choose a reason for hiding this comment

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

옷, 파일 마지막에 line break가 빠져있네요.
참고: https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline

Copy link
Contributor Author

Choose a reason for hiding this comment

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

오이잇 감사합니다~=)

Copy link
Contributor

Choose a reason for hiding this comment

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

옷, 파일 마지막에 line break가 빠져있네요. 참고: https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline

저는 Python 컨벤션으로 알고 있었는데, 근본의 UNIX 기원이군요 감사합니다

26 changes: 26 additions & 0 deletions palindromic-substrings/sun912.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
TC: O(n^2)

TC in first try was O(n^3).
It is the improved codes from neetcodes
key point is that each character in loop is considered to middle one.
Ant then, check left end and right end.

"""
class Solution:
def countSubstrings(self, s: str) -> int:
result = 0
for i in range(len(s)):
result += self.countPalindrom(s, i, i)
result += self.countPalindrom(s, i, i+1)
return result


def countPalindrom(self, s, left_end, right_end):
result = 0
while left_end >= 0 and right_end < len(s) and s[left_end] == s[right_end]:
result += 1
left_end -= 1
right_end += 1

return result
24 changes: 24 additions & 0 deletions top-k-frequent-elements/sun912.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
TC : O(n)
SC: O(n)
used bucket sort
index -> count number
list value -> frequent nums
"""

class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
count = {}
freq = [[] for i in range(len(nums)+1)]

for num in nums:
count[num] = 1 + count.get(num, 0)
for key, val in count.items():
freq[val].append(key)

result = []
for i in range(len(freq) - 1, 0, -1):
for n in freq[i]:
result.append(n)
if len(result) == k:
return result
Comment on lines +23 to +24
Copy link
Contributor

Choose a reason for hiding this comment

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

early return으로 실행 속도 이득을 보는 것도 좋지만, 함수 시그니처에 맞춰서 return을 보장해주는 것도 좋다고 생각합니다