-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
[sun] W1 Solutions #315
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6d8c598
contains-duplicate solution
sun912 f74db69
Time complexity added
sun912 e0ae448
number-of-1-bits solution
sun912 4841b41
top-k-frequent-elements solution
sun912 eb1c165
top-k-frequent-elements solve
sun912 8b22e3f
comment changed
sun912 eda20ca
solved palindromic-substrings
sun912 79de0bc
kth-smallest-element-in-a-bst solution
sun912 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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. early return으로 실행 속도 이득을 보는 것도 좋지만, 함수 시그니처에 맞춰서 return을 보장해주는 것도 좋다고 생각합니다 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
옷, 파일 마지막에 line break가 빠져있네요.
참고: https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline
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.
오이잇 감사합니다~=)
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.
저는 Python 컨벤션으로 알고 있었는데, 근본의 UNIX 기원이군요 감사합니다