Skip to content
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

[kayden] Week 01 Solutions #327

Merged
merged 9 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
5 changes: 5 additions & 0 deletions contains-duplicate/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 시간복잡도: O(N)
# 공간복잡도: O(N)
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return len(set(nums)) != len(nums)
32 changes: 32 additions & 0 deletions kth-smallest-element-in-a-bst/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 시간복잡도: O(N)
# 공간복잡도: O(1)
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right


class Solution:

def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
self.result = 0
self.count = 0

def dfs(node):
if node is None:
return

dfs(node.left)

self.count += 1

if self.count == k:
self.result = node.val
return

dfs(node.right)

dfs(root)

return self.result
5 changes: 5 additions & 0 deletions number-of-1-bits/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 시간복잡도: O(log N)
# 공간복잡도: O(log N)
class Solution:
def hammingWeight(self, n: int) -> int:
return bin(n).count("1")
22 changes: 22 additions & 0 deletions palindromic-substrings/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 시간복잡도: O(N^2)
# 공간복잡도: O(N^2)
class Solution:
def countSubstrings(self, s: str) -> int:
n = len(s)
count = n
isPalindrome = [[False for _ in range(n)] for _ in range(n)]

for i in range(n):
isPalindrome[i][i] = True
if i < n-1 and s[i] == s[i+1]:
isPalindrome[i][i+1] = True
count += 1

for length in range(3, n + 1):
for i in range(n - length + 1):
j = i + length - 1
if isPalindrome[i+1][j-1] and s[i] == s[j]:
isPalindrome[i][j] = True
count += 1

return count
7 changes: 7 additions & 0 deletions top-k-frequent-elements/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 시간복잡도: O(Nlog N)
# 공간복잡도: O(N)
from collections import Counter

class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
return [num for num, count in Counter(nums).most_common(k)]