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
12 changes: 12 additions & 0 deletions best-time-to-buy-and-sell-stock/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# idea: -
class Solution:
def maxProfit(self, prices: List[int]) -> int:
max_profit = 0
min_price = prices[0]
for price in prices:
max_profit = max(price - min_price, max_profit)
min_price = min(price, min_price)
return max_profit



13 changes: 13 additions & 0 deletions group-anagrams/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# idea: sorting and dictionary

class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagrams = {}
for word in strs:
sorted_word = str(sorted(word)) #sorted returns list
if sorted_word not in anagrams:
anagrams[sorted_word] = []
anagrams[sorted_word].append(word)
return list(anagrams.values())


45 changes: 45 additions & 0 deletions implement-trie-prefix-tree/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# idea : -

class Node:
def __init__(self, ending=False):
self.children = {}
self.ending = ending

class Trie:
def __init__(self):
self.root = Node(ending= True)

def insert(self, word: str) -> None:
node = self.root
for ch in word:
if ch not in node.children:
node.children[ch] = Node()
node = node.children[ch]
node.ending = True

def search(self, word: str) -> bool:
node = self.root
for ch in word:
if ch not in node.children:
return False
node = node.children[ch]
return node.ending


def startsWith(self, prefix: str) -> bool:
node = self.root
for ch in prefix:
if ch not in node.children:
return False
node = node.children[ch]
return True



# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.search(word)
# param_3 = obj.startsWith(prefix)


18 changes: 18 additions & 0 deletions word-break/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# idea: recursive
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
# TLE > (solution : memorization with @cache decoration)
# @cache : If a function is called more than once with the same arguments, it uses stored memoization results instead of recomputing.
def dfs(start):
if start == len(s):
return True
for word in wordDict:
if s[start:start+len(word)] == word:
if dfs(start+len(word)):
return True
return False
return dfs(0)