diff --git a/best-time-to-buy-and-sell-stock/njngwn.py b/best-time-to-buy-and-sell-stock/njngwn.py new file mode 100644 index 0000000000..0a07f6bfb7 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/njngwn.py @@ -0,0 +1,12 @@ +class Solution: + # Time Complexity: O(n), n: len(prices) + # Space Complexity: O(1) + def maxProfit(self, prices: List[int]) -> int: + min_price = prices[0] + profit = 0 + + for i in range(1, len(prices)): + min_price = min(min_price, prices[i]) + profit = max(profit, prices[i] - min_price) + + return profit diff --git a/climbing-stairs/njngwn.py b/climbing-stairs/njngwn.py new file mode 100644 index 0000000000..1d2fb959cb --- /dev/null +++ b/climbing-stairs/njngwn.py @@ -0,0 +1,12 @@ +class Solution: + # dynamic programming with bottom-up + # Time Complexity: O(n) + # Space Complexity: O(1) + def climbStairs(self, n: int) -> int: + if n <= 2: return n + prev1, prev2 = 2, 1 + + for i in range(3, n+1): + prev1, prev2 = prev2 + prev1, prev1 + + return prev1 diff --git a/encode-and-decode-strings/njngwn.py b/encode-and-decode-strings/njngwn.py new file mode 100644 index 0000000000..1d72058f64 --- /dev/null +++ b/encode-and-decode-strings/njngwn.py @@ -0,0 +1,26 @@ +class Solution: + + def encode(self, strs: List[str]) -> str: + encoded = '' + + for s in strs: + encoded += str(len(s)) + '#' + s + + return encoded + + def decode(self, s: str) -> List[str]: + decoded = [] + + i = 0 + while i < len(s): + j = i # j is a pointer for beginning of string + # find '#' in string + while s[j] != '#': + j += 1 + + length = int(s[i:j]) + word = s[j + 1:j + length + 1] + decoded.append(word) + i = j + length + 1 + + return decoded diff --git a/group-anagrams/njngwn.py b/group-anagrams/njngwn.py new file mode 100644 index 0000000000..087589153a --- /dev/null +++ b/group-anagrams/njngwn.py @@ -0,0 +1,14 @@ +class Solution: + # Time Complexity: O(n), n: len(strs) + # Space Complexity: O(n), n: len(strs) + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + anagram_groups = dict() # key: string in an alphabetical order, values: strings + + for s in strs: + word = ''.join(sorted(s)) + if word in anagram_groups: + anagram_groups[word].extend([s]) + else: + anagram_groups[word] = [s] + + return list(anagram_groups.values()) diff --git a/valid-anagram/njngwn.py b/valid-anagram/njngwn.py new file mode 100644 index 0000000000..da47ffffe5 --- /dev/null +++ b/valid-anagram/njngwn.py @@ -0,0 +1,7 @@ +from collections import Counter + +class Solution: + # Time Complexity: O(n), n: max(len(s), len(t)) + # Space Complexity: O(k), k: number of letters + def isAnagram(self, s: str, t: str) -> bool: + return Counter(s) == Counter(t) diff --git a/word-break/njngwn.py b/word-break/njngwn.py new file mode 100644 index 0000000000..f15acb142a --- /dev/null +++ b/word-break/njngwn.py @@ -0,0 +1,16 @@ +from functools import cache + + +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + @cache + def check(cur): + if cur == len(s): + return True + for word in wordDict: + if s[cur: cur + len(word)] == word: + if check(cur + len(word)): + return True + return False + + return check(0)