-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Changes from all commits
a3b23af
19151db
703cbe0
f770374
052d112
f10b34d
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,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) | ||
|
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 | ||
# T: O(n) | ||
# S: O(n) | ||
|
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 | ||
|
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) | ||
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. ๊ณต๊ฐ ๋ณต์ก๋๊ฐ ์ด๋ป๊ฒ 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. ๋จ์ํ๊ฒ count๋ฅผ n^2 ๋ฒ๋งํผ ์ ์ฅํ๋ค๊ณ ์๊ฐํ๋๋ฐ ๋ค์๋ณด๋ O(n) ๊ฐ์๋ณด์ด๋ค์..! |
||
|
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 | ||
|
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.
์, ์ด ํ์ด ์์ ์ ์ ํฉ๋๋ค. ํน์ ์ด๋ฒ ์ฃผ ๋ชจ์์ ์ฐธ์ํ์ ๋ค๋ฉด ๋ค๋ฅธ ๋ถ๋ค๊ป ๋ต์ ์๊ฐํด์ฃผ์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.