Skip to content

[Raft] Week 01 solutions #318

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 6 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
12 changes: 12 additions & 0 deletions contains-duplicate/Raft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
appeared = defaultdict(int)
for n in nums:
if appeared[n] > 0:
return True
appeared[n] += 1

return False
# T: O(n)
# S: O(n)

16 changes: 16 additions & 0 deletions kth-smallest-element-in-a-bst/Raft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
stack = []

while True:
while root:
stack.append(root)
root = root.left
root = stack.pop()
k -= 1
if not k:
return root.val
root = root.right
Comment on lines +1 to +13
Copy link
Member

Choose a reason for hiding this comment

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

์™€, ์ด ํ’€์ด ์™„์ „ ์‹ ์„ ํ•ฉ๋‹ˆ๋‹ค. ํ˜น์‹œ ์ด๋ฒˆ ์ฃผ ๋ชจ์ž„์— ์ฐธ์„ํ•˜์‹ ๋‹ค๋ฉด ๋‹ค๋ฅธ ๋ถ„๋“ค๊ป˜ ๋‹ต์•ˆ ์†Œ๊ฐœํ•ด์ฃผ์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

# T: O(n)
# S: O(n)

10 changes: 10 additions & 0 deletions number-of-1-bits/Raft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def hammingWeight(self, n: int) -> int:
res = 0
while n:
res += n % 2
n = n >> 1
return res
# T: logn
# S: 1

18 changes: 18 additions & 0 deletions palindromic-substrings/Raft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
def countSubstrings(self, s: str) -> int:
count = 0
for i in range(len(s)):
count += self.countPalindrome(s, i, i)
count += self.countPalindrome(s, i, i + 1)
return count

def countPalindrome(self, s, l, r):
count = 0
while r < len(s) and l >= 0 and s[l] == s[r]:
count += 1
l -= 1
r += 1
return count
# T: O(n^2)
# S: O(n^2)
Copy link
Member

Choose a reason for hiding this comment

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

๊ณต๊ฐ„ ๋ณต์žก๋„๊ฐ€ ์–ด๋–ป๊ฒŒ O(n^2)์œผ๋กœ ๋ถ„์„ ๋˜์…จ๋Š”์ง€ ์„ค๋ช… ์ข€ ๋ถ€ํƒ๋“œ๋ ค๋„ ๋ ๊นŒ์š”?

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๋ฅผ n^2 ๋ฒˆ๋งŒํผ ์ €์žฅํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๋‹ค์‹œ๋ณด๋‹ˆ O(n) ๊ฐ™์•„๋ณด์ด๋„ค์š”..!


44 changes: 44 additions & 0 deletions top-k-frequent-elements/raft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
# Frequency
freq = defaultdict(int)
for n in nums:
freq[n] += 1

freq_list = []
for key, val in freq.items():
freq_list.append((-1 * val, key))

heapq.heapify(freq_list)

res = []
for _ in range(k):
res.append(heapq.heappop(freq_list)[1])

return res

# T: nlogn
# S: n

def topKFrequent2(self, nums: List[int], k: int) -> List[int]:
res = []
# Frequency
count = defaultdict(int)
for n in nums:
count[n] += 1

# Convert to frequency
frequency = [[] for _ in range(len(nums) + 1)]
for key, freq in count.items():
frequency[freq].append(key)

# top K
for freq in range(len(nums), 0, -1):
for n in frequency[freq]:
res.append(n)

if len(res) == k:
return res
# T: n
# S: n