diff --git a/contains-duplicate/sun912.py b/contains-duplicate/sun912.py new file mode 100644 index 000000000..c86fb22de --- /dev/null +++ b/contains-duplicate/sun912.py @@ -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) diff --git a/kth-smallest-element-in-a-bst/sun912.py b/kth-smallest-element-in-a-bst/sun912.py new file mode 100644 index 000000000..bbfceff85 --- /dev/null +++ b/kth-smallest-element-in-a-bst/sun912.py @@ -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 diff --git a/number-of-1-bits/sun912.py b/number-of-1-bits/sun912.py new file mode 100644 index 000000000..0df9a5d29 --- /dev/null +++ b/number-of-1-bits/sun912.py @@ -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 \ No newline at end of file diff --git a/palindromic-substrings/sun912.py b/palindromic-substrings/sun912.py new file mode 100644 index 000000000..325f61e65 --- /dev/null +++ b/palindromic-substrings/sun912.py @@ -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 diff --git a/top-k-frequent-elements/sun912.py b/top-k-frequent-elements/sun912.py new file mode 100644 index 000000000..5fe78ea9c --- /dev/null +++ b/top-k-frequent-elements/sun912.py @@ -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