-
Notifications
You must be signed in to change notification settings - Fork 126
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
[BEMELON][WEEK 01](Python) 리트코드 문제 풀이 #300
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5b0fc61
[LC] contains-duplicate
BEMELON 5639aa5
style: add empty line
BEMELON 0bc5262
Solve LC > numbers-of-1-bits
BEMELON 2af581d
style: add empty line
BEMELON 4b0908b
Solve LC > top-k-frequent-elements
BEMELON dd0d0b4
Solve LC > kth-smallest-element-in-a-bst
BEMELON 4341deb
Solve LC > palindromic substrings
BEMELON 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 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,10 @@ | ||
class Solution: | ||
def containsDuplicate(self, nums: List[int]) -> bool: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
seen = set() | ||
for num in nums: | ||
if num in seen: | ||
return True | ||
seen.add(num) | ||
return False |
This file contains 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 @@ | ||
class TreeNode: | ||
def __init__(self, val=0, left=None, right=None): | ||
self.val = val | ||
self.left = left | ||
self.right = right | ||
|
||
class Solution: | ||
""" | ||
Time Complexity: O(n) | ||
Space Complexity: O(k) | ||
""" | ||
|
||
def inOrder(self, root: TreeNode, asc_sorted_list: list[int], k: int) -> None: | ||
if root.left: | ||
self.inOrder(root.left, asc_sorted_list, k) | ||
|
||
asc_sorted_list.append(root.val) | ||
|
||
if len(asc_sorted_list) < k and root.right: | ||
self.inOrder(root.right, asc_sorted_list, k) | ||
|
||
def kthSmallest(self, root: TreeNode, k: int) -> int: | ||
asc_sorted_list = [] | ||
self.inOrder(root, asc_sorted_list, k) | ||
|
||
return asc_sorted_list[k - 1] |
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. 재귀는 생각하지 못했는데, bit 연산을 최적화하여 잘 사용하신것 같습니다! |
This file contains 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,18 @@ | ||
class Solution: | ||
def hammingWeight_v1(self, n: int) -> int: | ||
# Recursive solution | ||
# Time complexity: O(log n) | ||
# Space complexity: O(log n) | ||
if n == 0: return 0 | ||
elif n % 2 == 0: return self.hammingWeight(n // 2) | ||
else: return self.hammingWeight(n // 2) + 1 | ||
|
||
def hammingWeight(self, n: int) -> int: | ||
# Iterative solution | ||
# Time complexity: O(log n) | ||
# Space complexity: O(1) | ||
set_bit_cnt = 0 | ||
while n > 0: | ||
set_bit_cnt += n & 1 | ||
n >>= 1 | ||
return set_bit_cnt |
This file contains 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,49 @@ | ||
class Solution: | ||
def countSubstrings_v1(self, s: str) -> int: | ||
def isPalindrome(self, substr: str) -> bool: | ||
return len(substr) <= 1 or (substr[0] == substr[-1] and self.isPalindrome(substr[1:-1])) | ||
|
||
# Brute-Force Solution - TLE | ||
count = 0 | ||
for l in range(1, len(s) + 1): | ||
for start in range(0, len(s)): | ||
if start + l > len(s): continue | ||
|
||
substr = s[start: start + l] | ||
if (self.isPalindrome(substr)): | ||
count += 1 | ||
return count | ||
|
||
def countSubstrings(self, s: str) -> int: | ||
""" | ||
Dynamic Programming Solution | ||
Time Complexity: O(N^2) | ||
Space Complexity: O(N^2) | ||
""" | ||
n = len(s) | ||
|
||
# isPalindrome[i][j] => Palindrome at s[i:j]? | ||
isPalindrome = [[False] * n for _ in range(n)] | ||
answer = 0 | ||
# 1. "a", "b", "c" are all Palindrome | ||
for i in range(n): | ||
isPalindrome[i][i] = True | ||
answer += 1 | ||
|
||
# 2. "a{x}" are Palindrome if a == {x} | ||
for i in range(n - 1): | ||
if s[i] == s[i + 1]: | ||
isPalindrome[i][i + 1] = True | ||
answer += 1 | ||
|
||
# 3. else) str[i:j] is Palindrome if str[i + 1: j - 1] ... is Palinedrome | ||
for size in range(3, n + 1): | ||
for start in range(n - size + 1): | ||
end = start + size - 1 | ||
|
||
if s[start] == s[end] and isPalindrome[start + 1][end - 1]: | ||
isPalindrome[start][end] = True | ||
answer += 1 | ||
|
||
return answer | ||
|
This file contains 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,22 @@ | ||
from collections import defaultdict | ||
|
||
class Solution: | ||
def topKFrequent(self, nums: list[int], k: int) -> list[int]: | ||
""" | ||
Time complexity: O(nlogn) | ||
Space complexity: O(n) | ||
""" | ||
if len(nums) == k: | ||
return list(set(nums)) | ||
|
||
num_cnt = defaultdict(int) | ||
for num in nums: | ||
num_cnt[num] += 1 | ||
|
||
return list( | ||
sorted( | ||
num_cnt, | ||
key=lambda x: num_cnt[x], | ||
reverse=True | ||
) | ||
)[:k] |
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.
Set 활용이 잘 된것 같습니다!