Skip to content

Commit da7971f

Browse files
authored
Merge pull request #315 from sun912/main
[sun] W1 Solutions
2 parents c705737 + 79de0bc commit da7971f

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

contains-duplicate/sun912.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
Time complexity: O(n)
3+
"""
4+
5+
class Solution:
6+
def containsDuplicate(self, nums: List[int]) -> bool:
7+
nums_set = set(nums)
8+
return len(nums_set) != len(nums)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
8+
"""
9+
TC: O(n) in worst case
10+
SC: O(n) in worst case
11+
"""
12+
class Solution:
13+
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
14+
n = 0
15+
stack = []
16+
current_node = root
17+
18+
while current_node or stack:
19+
while current_node:
20+
stack.append(current_node)
21+
current_node = current_node.left
22+
23+
current_node = stack.pop()
24+
n += 1
25+
if n == k:
26+
return current_node.val
27+
current_node = current_node.right

number-of-1-bits/sun912.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
TC: O(log n)
3+
SC: O(1)
4+
"""
5+
6+
class Solution:
7+
def hammingWeight(self, n: int) -> int:
8+
result = 0
9+
while n > 0:
10+
result += n % 2
11+
n = n//2
12+
13+
return result

palindromic-substrings/sun912.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
TC: O(n^2)
3+
4+
TC in first try was O(n^3).
5+
It is the improved codes from neetcodes
6+
key point is that each character in loop is considered to middle one.
7+
Ant then, check left end and right end.
8+
9+
"""
10+
class Solution:
11+
def countSubstrings(self, s: str) -> int:
12+
result = 0
13+
for i in range(len(s)):
14+
result += self.countPalindrom(s, i, i)
15+
result += self.countPalindrom(s, i, i+1)
16+
return result
17+
18+
19+
def countPalindrom(self, s, left_end, right_end):
20+
result = 0
21+
while left_end >= 0 and right_end < len(s) and s[left_end] == s[right_end]:
22+
result += 1
23+
left_end -= 1
24+
right_end += 1
25+
26+
return result

top-k-frequent-elements/sun912.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
TC : O(n)
3+
SC: O(n)
4+
used bucket sort
5+
index -> count number
6+
list value -> frequent nums
7+
"""
8+
9+
class Solution:
10+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
11+
count = {}
12+
freq = [[] for i in range(len(nums)+1)]
13+
14+
for num in nums:
15+
count[num] = 1 + count.get(num, 0)
16+
for key, val in count.items():
17+
freq[val].append(key)
18+
19+
result = []
20+
for i in range(len(freq) - 1, 0, -1):
21+
for n in freq[i]:
22+
result.append(n)
23+
if len(result) == k:
24+
return result

0 commit comments

Comments
 (0)