Skip to content
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
10 changes: 10 additions & 0 deletions counting-bits/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# TC: O(n)
# SC: O(n)
# For each number from 1 to n, update the count of set bits for i based on the count for i divided by two
# and the least significant bit (LSB) of i.
class Solution:
def countBits(self, n: int) -> List[int]:
ans = [0] * (n + 1)
for i in range(1, n + 1):
ans[i] = ans[i >> 1] + (i & 1)
return ans
15 changes: 15 additions & 0 deletions group-anagrams/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# TC : O(nwlog(w))
# reason : n being the number of words and w being the length of each word, therefore the total time complexity is n times wlog(w) (time complexity for sorting each word)
# SC : O(w*n)
from collections import defaultdict


class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagrams = defaultdict(list)

# Use sorted word as a string and append it with the original word so the word containing same charactors with the same number of existence can be in the same group
for word in strs:
anagrams[str(sorted(word))].append(word)

return anagrams.values()
14 changes: 14 additions & 0 deletions missing-number/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# TC : O(n)
# SC : O(1)
class Solution:
def missingNumber(self, nums: List[int]) -> int:
n = len(nums)

# The sum of the numbers in the range(0,n)
sumOfNums = (1 + n) * n // 2

# Subtract every element of nums to leave only missing number
for num in nums:
sumOfNums -= num

return sumOfNums
11 changes: 11 additions & 0 deletions number-of-1-bits/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# TC : O(log n)
# reason : bin method internally devides n by 2 while transitioning to binary number
# SC : O(1)
class Solution:
def hammingWeight(self, n: int) -> int:
res = 0
# count each charactor from the first
for bit in bin(n):
if bit == "1":
res += 1
return res
9 changes: 9 additions & 0 deletions reverse-bits/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# TC : O(1)
# SC : O(1)
# Reason : The input is fixed to be 32 bit resulting in time and space complexity being O(1)
class Solution:
def reverseBits(self, n: int) -> int:
binary_string = bin(n)[2:].zfill(32)
reversed_binary_string = binary_string[::-1]

return int(reversed_binary_string, 2)