post_href stringlengths 57 213 | python_solutions stringlengths 71 22.3k | slug stringlengths 3 77 | post_title stringlengths 1 100 | user stringlengths 3 29 | upvotes int64 -20 1.2k | views int64 0 60.9k | problem_title stringlengths 3 77 | number int64 1 2.48k | acceptance float64 0.14 0.91 | difficulty stringclasses 3
values | __index_level_0__ int64 0 34k |
|---|---|---|---|---|---|---|---|---|---|---|---|
https://leetcode.com/problems/course-schedule-ii/discuss/2795475/Python-Solution-Easy-to-Understand | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
graph = [[] for _ in range(numCourses)]
indegree = [0 for _ in range(numCourses)]
for course in prerequisites:
graph[course[0]].append(course[1])
indegree[course[1]] +=... | course-schedule-ii | Python Solution Easy to Understand | manishchhipa | 0 | 1 | course schedule ii | 210 | 0.481 | Medium | 3,600 |
https://leetcode.com/problems/course-schedule-ii/discuss/2794927/Simple-Depth-First-Search-and-Topological-Sort | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
seen = collections.defaultdict(bool)
pos = collections.defaultdict(int)
graph = collections.defaultdict(list)
def dfs(course) -> bool:
if seen[course] == 2:
... | course-schedule-ii | Simple Depth First Search and Topological Sort | ananth360 | 0 | 2 | course schedule ii | 210 | 0.481 | Medium | 3,601 |
https://leetcode.com/problems/course-schedule-ii/discuss/2763691/Simple-Beginner-Friendly-Approach-or-DFS-or-O(V%2BE) | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
self.graph = defaultdict(list)
for pre in prerequisites:
self.graph[pre[0]].append(pre[1])
self.visited = set()
self.path = set()
self.order = []
for course ... | course-schedule-ii | Simple Beginner Friendly Approach | DFS | O(V+E) | sonnylaskar | 0 | 6 | course schedule ii | 210 | 0.481 | Medium | 3,602 |
https://leetcode.com/problems/course-schedule-ii/discuss/2760808/Using-khan's-algorithm-BFS-striver-solution | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
if numCourses==1:
return [0]
dic=defaultdict(list)
for a,b in prerequisites:
dic[a]+=[b]
vis=[0]*numCourses
for i in range(numCourses):
if... | course-schedule-ii | Using khan's algorithm, BFS, striver solution | aryan3012 | 0 | 4 | course schedule ii | 210 | 0.481 | Medium | 3,603 |
https://leetcode.com/problems/course-schedule-ii/discuss/2741463/simple-pythonor-DFS | class Solution:
def traverse(self, now_point):
if now_point in self.onpath:
self.circle = True
return
if now_point in self.visited or self.circle:
return
self.onpath.append(now_point)
self.visited.append(now_point)
for j in self.graph[now_... | course-schedule-ii | simple python| DFS | lucy_sea | 0 | 2 | course schedule ii | 210 | 0.481 | Medium | 3,604 |
https://leetcode.com/problems/course-schedule-ii/discuss/2716054/Python3-solution-with-detailed-explanation-O(V%2BE)-both-time-and-space-complexity | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
# init the nodes. for each node, first element
# is the state of DFS. 0 means not discovered yet,
# 1 means discovered, 2 means finished. node's
# second element is the adjancent neighb... | course-schedule-ii | Python3 solution with detailed explanation, O(V+E) both time and space complexity | tinmanSimon | 0 | 9 | course schedule ii | 210 | 0.481 | Medium | 3,605 |
https://leetcode.com/problems/course-schedule-ii/discuss/2713174/python3-simple-dfs-iterative-solution | class Solution:
def findOrder(self, numCourses: int, pre: List[List[int]]) -> List[int]:
graph={c:[] for c in range(numCourses)}
for i in pre:
graph[i[1]].append(i[0])
cycle , vis = set(), set()
output = deque([])
for e in graph:
if e not in vis:
... | course-schedule-ii | python3 simple dfs iterative solution | benon | 0 | 9 | course schedule ii | 210 | 0.481 | Medium | 3,606 |
https://leetcode.com/problems/course-schedule-ii/discuss/2710912/PYTHON-SOLUTION | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
indegree=[0]*(numCourses)
graph=defaultdict(list)
res=[]
for courses in prerequisites:
graph[courses[1]].append(courses[0])
indegree[courses[0]]... | course-schedule-ii | PYTHON SOLUTION | shashank_2000 | 0 | 16 | course schedule ii | 210 | 0.481 | Medium | 3,607 |
https://leetcode.com/problems/course-schedule-ii/discuss/2695167/python3-95-topoligical-sort | class Solution:
def findOrder(self, N: int, prerequisites: List[List[int]]) -> List[int]:
if not prerequisites:
return list(range(N))
prereqs_course, prereqs_amount = defaultdict(list), defaultdict(int)
for _, (c, pr) in enumerate(prerequisites):
prereqs_amount[c] += ... | course-schedule-ii | python3 95% topoligical sort | 1ncu804u | 0 | 7 | course schedule ii | 210 | 0.481 | Medium | 3,608 |
https://leetcode.com/problems/course-schedule-ii/discuss/2623419/DFS-BFS-AND-KHAN'S-ALGORITHM | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
# BFS / KAHN'S ALGORITHM
preMap = {i: [] for i in range(numCourses)}
indegree = [0 for _ in range(numCourses)]
"""
numCourses = 4
prerequisite... | course-schedule-ii | DFS / BFS AND KHAN'S ALGORITHM | leomensah | 0 | 26 | course schedule ii | 210 | 0.481 | Medium | 3,609 |
https://leetcode.com/problems/course-schedule-ii/discuss/2597705/Python-Easy-DFS | class Solution:
def findOrder(self, n: int, pre: List[List[int]]) -> List[int]:
res = []
vis = [0]*n
adj = [[] for _ in range(n)]
for p in pre:
adj[p[0]].append(p[1])
for u in range(n):
if vis[u] == 0:
... | course-schedule-ii | Python Easy DFS | lokeshsenthilkumar | 0 | 52 | course schedule ii | 210 | 0.481 | Medium | 3,610 |
https://leetcode.com/problems/course-schedule-ii/discuss/2537428/Course-Schedule-II%3A-Python3-Top-Sort-Kahn-algo | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
def top_sort(DAG: dict): # Time Complexity O(V+E), Space Complexity O(V+E)
# Topological sort: Kahn's algorithm
# step 1: create the incoming_degree dictionary {X: #} to indicate how ma... | course-schedule-ii | Course Schedule II: Python3 Top Sort Kahn algo | NinjaBlack | 0 | 11 | course schedule ii | 210 | 0.481 | Medium | 3,611 |
https://leetcode.com/problems/course-schedule-ii/discuss/2506001/Python-Solution-or-using-DFS-orTopological-sort | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
prereq = {c : [] for c in range(numCourses)}
for crs, pre in prerequisites:
prereq[crs].append(pre)
visit, cycle = set(), set()
output= []
def dfs(crs):
if crs in cycle:
return False
if crs in v... | course-schedule-ii | Python Solution | using DFS |Topological sort | nikhitamore | 0 | 71 | course schedule ii | 210 | 0.481 | Medium | 3,612 |
https://leetcode.com/problems/course-schedule-ii/discuss/2190467/Python3-Topological-Sort-Easy-to-understand | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
preMap = { _ : [] for _ in range(numCourses)}
for cur, pre in prerequisites:
preMap[cur].append(pre)
path = set()
# path to remember all the node on the path we have visited... | course-schedule-ii | Python3 Topological Sort Easy to understand | jethu | 0 | 17 | course schedule ii | 210 | 0.481 | Medium | 3,613 |
https://leetcode.com/problems/course-schedule-ii/discuss/2184681/Topological-Sort-oror-Detecting-Cycle-in-a-Directed-Graph-oror-DFS | class Solution:
def topologicalSort(self, node, graph, visited, stack):
visited[node] = True
for neighbour in graph[node]:
if visited[neighbour] == False:
self.topologicalSort(neighbour, graph, visited, stack)
stack.append(node)
def dfs(... | course-schedule-ii | Topological Sort || Detecting Cycle in a Directed Graph || DFS | Vaibhav7860 | 0 | 42 | course schedule ii | 210 | 0.481 | Medium | 3,614 |
https://leetcode.com/problems/course-schedule-ii/discuss/2153529/210.-Python-solution-with-my-comments | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
graph = [[] for i in range(numCourses)]
visited = [0]*numCourses
res = []
for pair in prerequisites:
x, y = pair
graph[x].append(y)
for i in range(numCou... | course-schedule-ii | 210. Python solution with my comments | JunyiLin | 0 | 34 | course schedule ii | 210 | 0.481 | Medium | 3,615 |
https://leetcode.com/problems/course-schedule-ii/discuss/2118581/Python3-Solution-88 | class Solution:
def __init__(self):
self.graph = {}
self.v = 0
def isCyclicUtil(self, v, visited, recStack, progressStack):
visited[v] = True
recStack[v] = True
if v not in self.graph:
recStack[v]=False
progressStack.append(v)
return F... | course-schedule-ii | Python3 Solution 88% | ComicCoder023 | 0 | 52 | course schedule ii | 210 | 0.481 | Medium | 3,616 |
https://leetcode.com/problems/course-schedule-ii/discuss/2117795/simple-iterative-dfs | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
courses = [[] for _ in range(numCourses)]
for course, pre in prerequisites:
courses[course].append(pre)
visited = {}
order = []
for i ... | course-schedule-ii | simple iterative dfs | gabhinav001 | 0 | 62 | course schedule ii | 210 | 0.481 | Medium | 3,617 |
https://leetcode.com/problems/course-schedule-ii/discuss/2093015/Python3%3A-Simple-DFS-%2B-Backtracking-Solution-using-defaultdict-and-Sets-(no-Topological-Sort) | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
"""
DFS + backtracking
"""
def dfs(course):
visited.add(course)
path.add(course)
# backtracking
for pre in graph[course]:
... | course-schedule-ii | Python3: Simple DFS + Backtracking Solution using defaultdict and Sets (no Topological Sort) | yunglinchang | 0 | 48 | course schedule ii | 210 | 0.481 | Medium | 3,618 |
https://leetcode.com/problems/course-schedule-ii/discuss/2068732/Python-or-Topological-Sorting-or-O(n)-Time-O(n)-space | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
graph = defaultdict(list)
for a, b in prerequisites:
graph[b].append(a)
onPath = [False for _ in range(numCourses)]
visited = [False for _ in range(numCourses)]... | course-schedule-ii | Python | Topological Sorting | O(n) Time, O(n) space | Kiyomi_ | 0 | 44 | course schedule ii | 210 | 0.481 | Medium | 3,619 |
https://leetcode.com/problems/course-schedule-ii/discuss/2041842/course-schedule-oror-python3-oror-topological-oror-DFS | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
# Nothing just modification of course-schedule-1
graph = [[] for _ in range(numCourses)]
visit = [0 for _ in range(numCourses)]
stack=[]
for x, y in prerequisites:
gra... | course-schedule-ii | course schedule || python3 || topological || DFS | Aniket_liar07 | 0 | 30 | course schedule ii | 210 | 0.481 | Medium | 3,620 |
https://leetcode.com/problems/course-schedule-ii/discuss/2036907/Python3-DFS-solution-explained | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
visited = [None] * numCourses
onPath = [None] * numCourses
adjList = [[] for i in range(numCourses)]
res = []
for i in range(numCourses):
adjList.append([])... | course-schedule-ii | Python3 DFS solution explained | TongHeartYes | 0 | 33 | course schedule ii | 210 | 0.481 | Medium | 3,621 |
https://leetcode.com/problems/course-schedule-ii/discuss/2009702/Clean-Python-3-Solution-(Topological-Sorting-%2B-BFS) | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
in_degrees = [0] * numCourses
for v, _ in prerequisites:
in_degrees[v] += 1
q = []
for i in range(len(in_degrees)):
if in_degrees[i] == 0:
q.app... | course-schedule-ii | Clean Python 3 Solution (Topological Sorting + BFS) | Hongbo-Miao | 0 | 83 | course schedule ii | 210 | 0.481 | Medium | 3,622 |
https://leetcode.com/problems/course-schedule-ii/discuss/1970320/Python3-Topological-Sort-Two-Solutions-with-BFS-and-DFS | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
graph = [[] for x in range(numCourses)]
for dest, src in prerequisites:
graph[src].append(dest)
index = [numCourses - 1]
result = [0] * numCourses
seen = [F... | course-schedule-ii | Python3 Topological Sort Two Solutions with BFS & DFS | shtanriverdi | 0 | 50 | course schedule ii | 210 | 0.481 | Medium | 3,623 |
https://leetcode.com/problems/course-schedule-ii/discuss/1914146/Python-DFS-oror-Topological-Sort-oror-Simple-and-Easy | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
preMap = collections.defaultdict(list)
for crs, pre in prerequisites:
preMap[crs].append(pre)
output = []
visit, cycle = set(), set()
def ... | course-schedule-ii | Python - DFS || Topological Sort || Simple and Easy | dayaniravi123 | 0 | 70 | course schedule ii | 210 | 0.481 | Medium | 3,624 |
https://leetcode.com/problems/course-schedule-ii/discuss/1906705/python-topological-sort-solution.-Easy-to-understand | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
incomingNodesCount = [0 for i in range(numCourses)]
graph = {i:[] for i in range(numCourses)}
# create a graph from the given prerequisites
for prereq in prerequisites:
... | course-schedule-ii | python topological sort solution. Easy to understand | karanbhandari | 0 | 96 | course schedule ii | 210 | 0.481 | Medium | 3,625 |
https://leetcode.com/problems/course-schedule-ii/discuss/1890364/Python-Topological-Sorting | class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
ans = []
d = {}
indegree = {}
for i in range(numCourses):
d[i] = []
indegree[i] = 0
for i in prerequisites:
d[i[0]].append(i[1])
... | course-schedule-ii | Python Topological Sorting | DietCoke777 | 0 | 45 | course schedule ii | 210 | 0.481 | Medium | 3,626 |
https://leetcode.com/problems/word-search-ii/discuss/2351408/python3-solution-oror-99-more-faster-oror-39-ms | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
m = len(board)
n = len(board[0])
res = []
d = [[0, 1], [0, -1], [1, 0], [-1, 0]]
ref = set()
for i in range(m):
for j in range(n-1):
ref.add(board... | word-search-ii | python3 solution || 99% more faster || 39 ms | vimla_kushwaha | 3 | 301 | word search ii | 212 | 0.368 | Hard | 3,627 |
https://leetcode.com/problems/word-search-ii/discuss/1061088/Python-AC-Backtracking-in-three-steps%3A-Terminate-Success-Backtrack | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
@functools.lru_cache(None)
def backtrack(i, j, path):
# 1) terminate
if i not in range(0, len(board)): return
if j not ... | word-search-ii | Python AC Backtracking in three steps: Terminate, Success, Backtrack | dev-josh | 3 | 342 | word search ii | 212 | 0.368 | Hard | 3,628 |
https://leetcode.com/problems/word-search-ii/discuss/2779919/or-Python-or-Trie-Easy-Solution | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
res = []
Trie = lambda : defaultdict(Trie)
root = Trie()
def add(s):
cur = root
for c in s: cur = cur[c]
cur['$'] = s
for word in w... | word-search-ii | ✔️ | Python | Trie Easy Solution | code_alone | 2 | 289 | word search ii | 212 | 0.368 | Hard | 3,629 |
https://leetcode.com/problems/word-search-ii/discuss/2781923/Python-or-Trie-with-node-deletion | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
rows, cols = len(board), len(board[0])
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
seen = []
res = set()
trie = {}
for word in words:
current = trie
... | word-search-ii | Python | Trie with node deletion | KevinJM17 | 1 | 38 | word search ii | 212 | 0.368 | Hard | 3,630 |
https://leetcode.com/problems/word-search-ii/discuss/2794297/DP-Approach-More-intuitive-than-Trie | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
if not board:
return []
rtn = []
memo = {}
for word in words:
self.dp(board, word, memo)
if not (-1,-1) in memo[word]:
rtn.append(word)
... | word-search-ii | DP Approach - More intuitive than Trie | nickpavini | 0 | 11 | word search ii | 212 | 0.368 | Hard | 3,631 |
https://leetcode.com/problems/word-search-ii/discuss/2788822/Python3-Faster-than-90-explained-using-dictionaries-and-NO-Tries | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
prefix = {}
word2prefixes = {} #for pruning
for w in words:
word2prefixes[w] = set()
for i in range(1,len(w)+1):
p = w[0:i]
if p in prefix:
... | word-search-ii | Python3 Faster than 90% explained using dictionaries and NO Tries | user9611y | 0 | 11 | word search ii | 212 | 0.368 | Hard | 3,632 |
https://leetcode.com/problems/word-search-ii/discuss/2787688/Python-3-backtracking-%2B-Trie-friendly-to-beginner-no-fancy-syntax | class Solution:
def buildtrie(self, words):
trie = {}
head = trie
for word in words:
for letter in word:
if letter not in trie:
trie[letter] = {}
trie = trie[letter]
trie['$'] = {}
trie = head
ret... | word-search-ii | Python 3 backtracking + Trie, friendly to beginner, no fancy syntax | Arana | 0 | 8 | word search ii | 212 | 0.368 | Hard | 3,633 |
https://leetcode.com/problems/word-search-ii/discuss/2785159/Python-%2B-DFS-with-Trie-Lookup | class Solution:
def __init__(self):
self.res = []
self.m = 0
self.n = 0
self.board = [[]]
def add(self, s:str, root):
cur = root
for c in s: cur = cur[c]
cur['
```] = s
def dfs(self, i:int, j:int, root):
char = self.board[i][j]
... | word-search-ii | Python + DFS with Trie Lookup | ratva0717 | 0 | 21 | word search ii | 212 | 0.368 | Hard | 3,634 |
https://leetcode.com/problems/word-search-ii/discuss/2782513/Help-optimizing-NON-TRIE-based-set-inclusion-method.-Accepted-one-time-TLE-ever-after | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
word_set = set(words)
prefix_set = set()
rows = len(board)
cols = len(board[0])
words_found = []
for word in words:
for i in range(len(word)):
pref... | word-search-ii | Help optimizing NON-TRIE based set-inclusion method. Accepted one time, TLE ever after | cswartzell | 0 | 15 | word search ii | 212 | 0.368 | Hard | 3,635 |
https://leetcode.com/problems/word-search-ii/discuss/2781477/python-solution | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
Words = set()
m = len(board)
n = len(board[0])
map = [[0]*n for i in range(m)]
for i in words:
Words.add(i)
prefix = set()
for it in words:
for ... | word-search-ii | python solution | _saurav_pandey_ | 0 | 12 | word search ii | 212 | 0.368 | Hard | 3,636 |
https://leetcode.com/problems/word-search-ii/discuss/2781476/python-solution | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
Words = set()
m = len(board)
n = len(board[0])
map = [[0]*n for i in range(m)]
for i in words:
Words.add(i)
prefix = set()
for it in words:
for ... | word-search-ii | python solution | _saurav_pandey_ | 0 | 8 | word search ii | 212 | 0.368 | Hard | 3,637 |
https://leetcode.com/problems/word-search-ii/discuss/2780396/Simple-Python-Trie-solution | class Solution:
def findWords(self, board: list[list[str]], words: list[str]) -> list[str]:
hasword = "true"
trie = {}
for word in words:
root = trie
for c in word:
root = root.setdefault(c, {})
root[hasword] = word
outpu... | word-search-ii | Simple Python Trie solution | tinlittle | 0 | 30 | word search ii | 212 | 0.368 | Hard | 3,638 |
https://leetcode.com/problems/word-search-ii/discuss/2780368/Python3-Solution | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
m = len(board)
n = len(board[0])
res = []
d = [[0, 1], [0, -1], [1, 0], [-1, 0]]
ref = set()
for i in range(m):
for j in range(n-1):
ref.add(board... | word-search-ii | Python3 Solution | Motaharozzaman1996 | 0 | 19 | word search ii | 212 | 0.368 | Hard | 3,639 |
https://leetcode.com/problems/word-search-ii/discuss/2780002/Python3-Trim-The-Trie-to-Beat-TLE | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
"""LeetCode 212
What a journey! I have solved this problem before by myself, but the
same method back then no longer worked today, because the test cases
had been enlarged. This means the met... | word-search-ii | [Python3] Trim The Trie to Beat TLE | FanchenBao | 0 | 23 | word search ii | 212 | 0.368 | Hard | 3,640 |
https://leetcode.com/problems/word-search-ii/discuss/2779891/Python3-solution-with-trie-removal | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
def _trie():
return defaultdict(_trie)
_trie.TERMINAL = None
_trie.PARENT = 'pa'
_trie.LETTER = 'le'
#None and 2-character string to avoid overwriting traversal keys
... | word-search-ii | Python3 solution with trie removal | TheDucker1 | 0 | 19 | word search ii | 212 | 0.368 | Hard | 3,641 |
https://leetcode.com/problems/word-search-ii/discuss/2554597/Python-simple-trie-solution | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
class Trie:
def __init__(self) -> None:
self.root={}
def insert(self,word):
cur=self.root
for w in word:
if w not in cur:
... | word-search-ii | Python simple trie solution | shatheesh | 0 | 150 | word search ii | 212 | 0.368 | Hard | 3,642 |
https://leetcode.com/problems/word-search-ii/discuss/2395867/python-trie-and-dfs-or-anyone-knows-why-this-is-slow | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
trie = {}
for w in words:
cur = trie
for c in w:
if c not in cur:
cur[c] = {}
cur = cur[c]
cur[-1] = -1
ans = []... | word-search-ii | python trie and dfs | anyone knows why this is slow? | li87o | 0 | 141 | word search ii | 212 | 0.368 | Hard | 3,643 |
https://leetcode.com/problems/word-search-ii/discuss/1966819/Word-Search-II-Python-3Totally-Math-Method | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
words_out = []
for w_x in words:
inx_words = []
inx_count = []
for xx in range(len(w_x)):
inx_words.append(self.index_all(board, w_x[xx]))
i... | word-search-ii | Word Search II - [Python 3]Totally Math Method | kkimm | 0 | 115 | word search ii | 212 | 0.368 | Hard | 3,644 |
https://leetcode.com/problems/word-search-ii/discuss/1783115/Python3-or-Faster-than-60-Memory-less-than-99 | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
h, w = len(board), len(board[0])
def visit(word, start, i, j, path=[]):
if start >= len(word):
path.append([i, j])
return True
elif len(word)-1 == start... | word-search-ii | Python3 | Faster than 60%, Memory less than 99% | elainefaith0314 | 0 | 165 | word search ii | 212 | 0.368 | Hard | 3,645 |
https://leetcode.com/problems/word-search-ii/discuss/1429990/Simple-Trie-based-solution-35-Lines-(50-50) | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
### ADD to Trie
def add(word):
if word[0] not in trie:
trie[word[0]] = {}
addOn(word[1:],trie[word[0]])
def addOn(word,node):
if n... | word-search-ii | Simple Trie based solution , 35 Lines (50%, 50%) | worker-bee | 0 | 125 | word search ii | 212 | 0.368 | Hard | 3,646 |
https://leetcode.com/problems/word-search-ii/discuss/1242247/Python-DFS-Beats-99-Pass-All-Test-Cases-But-WRONG | class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
# My Effort - Passed all test cases, yet backtracking line doesn't work
cntBoard = collections.Counter(''.join([e for row in board for e in row ]))
cntW = [collections.Counter(w) for w in words]
... | word-search-ii | Python DFS Beats 99% Pass All Test Cases But WRONG | Yan_Air | 0 | 290 | word search ii | 212 | 0.368 | Hard | 3,647 |
https://leetcode.com/problems/house-robber-ii/discuss/2158878/Do-house-robber-twice | class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]
dp = {}
def getResult(a,i):
if i>=len(a):
return 0
if i in dp:
return dp[i]
sum = 0
... | house-robber-ii | 📌 Do house robber twice | Dark_wolf_jss | 4 | 104 | house robber ii | 213 | 0.407 | Medium | 3,648 |
https://leetcode.com/problems/house-robber-ii/discuss/2345845/Success-Details-Runtime%3A-41-ms-oror-Python3-oror-vimla_kushwaha | class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]
a1 = 0
b1 = nums[0]
a2 = 0
b2 = nums[1]
for i in range(1, len(nums) - 1):
a1, b1 = b1, max(a1 + nums[i], b1)
a2, b2 = b2, max(a2 + nums[i + 1],... | house-robber-ii | Success Details Runtime: 41 ms, || Python3 || vimla_kushwaha | vimla_kushwaha | 3 | 94 | house robber ii | 213 | 0.407 | Medium | 3,649 |
https://leetcode.com/problems/house-robber-ii/discuss/2345845/Success-Details-Runtime%3A-41-ms-oror-Python3-oror-vimla_kushwaha | class Solution:
def rob(self, nums: List[int]) -> int:
n=len(nums)
# step 1 or 2
if n<=2:
return max(nums)
return max(self.helper(nums[:-1]),self.helper(nums[1:]))
def helper(self,nums):
dp=[0]*le... | house-robber-ii | Success Details Runtime: 41 ms, || Python3 || vimla_kushwaha | vimla_kushwaha | 3 | 94 | house robber ii | 213 | 0.407 | Medium | 3,650 |
https://leetcode.com/problems/house-robber-ii/discuss/2177085/Python-DP-or-Beats-99-or-Very-Clearly-explained! | class Solution:
def rob(self, houses: List[int]) -> int:
if len(houses) == 1:
return houses[0]
if len(houses) == 2 or len(houses) == 3:
return max(houses)
def find(nums):
n = len(nums)
dp = [0] * n
dp[0], dp[1], dp... | house-robber-ii | ✅Python DP | Beats 99% | Very Clearly explained! | PythonerAlex | 3 | 182 | house robber ii | 213 | 0.407 | Medium | 3,651 |
https://leetcode.com/problems/house-robber-ii/discuss/1468663/Python-or-DP-or-Memoization-or-With-Comments | class Solution:
def hh(self,nums):
dp=nums.copy() #memo table
dp[1]=max(dp[0],dp[1])
#this loop stores the values in memo table as : highest amount that can be stolen upto that house
for i in range(2,len(nums)):
dp[i]=max(dp[i-2]+nums[i],dp[i-1])
return max(dp... | house-robber-ii | Python | DP | Memoization | With Comments | nmk0462 | 2 | 205 | house robber ii | 213 | 0.407 | Medium | 3,652 |
https://leetcode.com/problems/house-robber-ii/discuss/2269240/Python-3-oror-Dynamic-programming | class Solution:
def rob(self, nums: List[int]) -> int:
return max(nums[0], self.helper(nums[1:]), self.helper(nums[:-1]))
def helper(self, nums):
rob1, rob2 = 0, 0
for n in nums:
newRob = max(rob1 + n, rob2)
rob1 = rob2
rob2 = newRob
return rob2 | house-robber-ii | Python 3 || Dynamic programming | sagarhasan273 | 1 | 24 | house robber ii | 213 | 0.407 | Medium | 3,653 |
https://leetcode.com/problems/house-robber-ii/discuss/2034891/Pythonor-Easy-Solution-including-Helper-function-of-House-Robber-1 | class Solution:
def rob(self, nums):
return max(nums[0], self.helper(nums[1:]), self.helper(nums[:-1]))
def helper(self, nums):
rob1, rob2 = 0, 0
for n in nums:
newRob = max(rob1 + n, rob2)
rob1 = rob2
rob2 = newRob
return rob... | house-robber-ii | Python| Easy Solution including Helper function of House Robber 1 | shikha_pandey | 1 | 132 | house robber ii | 213 | 0.407 | Medium | 3,654 |
https://leetcode.com/problems/house-robber-ii/discuss/1505865/Python3-Easy-DFS-%2B-Memoization | class Solution:
def rob(self, nums: List[int]) -> int:
n = len(nums)
@cache
def dfs(i, robbedFirst):
if (i >= n or (i == n - 1 and robbedFirst)):
return 0
return max(dfs(i + 1, robbedFirst), nums[i] + dfs(i + 2, robbedFirst))
return... | house-robber-ii | Python3 - Easy DFS + Memoization ✅ | Bruception | 1 | 222 | house robber ii | 213 | 0.407 | Medium | 3,655 |
https://leetcode.com/problems/house-robber-ii/discuss/1468736/Python-DP-solution | class Solution:
def rob(self, nums: List[int]) -> int:
dp = [0]*(len(nums)-1)
if len(nums)==1:
return nums[0]
if len(nums)==2:
return max(nums[0],nums[1])
else:
# from starting to n-1
dp[0] = nums[0]
dp[1] = max(nums[0],nums... | house-robber-ii | Python DP solution | dhanikshetty | 1 | 182 | house robber ii | 213 | 0.407 | Medium | 3,656 |
https://leetcode.com/problems/house-robber-ii/discuss/1459822/PyPy3-Solution-using-DP-w-comments | class Solution:
def rob(self, nums: List[int]) -> int:
# If array is empty
if not len(nums):
return 0
# If array has no more than three elements,
# as it is a circular array we can only select
# one of the three elements.
if len(nums) <= ... | house-robber-ii | [Py/Py3] Solution using DP w/ comments | ssshukla26 | 1 | 163 | house robber ii | 213 | 0.407 | Medium | 3,657 |
https://leetcode.com/problems/house-robber-ii/discuss/1440067/Simple-Python-dynamic-programming-solution | class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) <= 3:
return max(nums)
def max_profit(money):
dp = [0]*len(money)
for i in range(len(dp)):
prev = dp[i-1] if i > 0 else 0
prevprev = dp[i-2] if i > 1 else ... | house-robber-ii | Simple Python dynamic programming solution | Charlesl0129 | 1 | 121 | house robber ii | 213 | 0.407 | Medium | 3,658 |
https://leetcode.com/problems/house-robber-ii/discuss/893967/A-straightforward-solution-Python-faster-than-87.5 | class Solution:
def rob(self, a: List[int]) -> int:
@lru_cache(maxsize=None)
def g(i):
if i == 0: return 0
if i == 1: return a[1]
return max(g(i-1), g(i-2) + a[i])
@lru_cache(maxsize=None)
def f(i):
if i == 0: return a... | house-robber-ii | A straightforward solution [Python, faster than 87.5%] | dh7 | 1 | 102 | house robber ii | 213 | 0.407 | Medium | 3,659 |
https://leetcode.com/problems/house-robber-ii/discuss/751488/Python-3%3A-O(N)-time-and-O(1)-space-easy-solution. | class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums)==1:
return nums[0]
elif len(nums)==0:
return 0
return max(self.robHelper(nums[1:]),self.robHelper(nums[:-1]))
def robHelper(self, arr):
"""
:type nums: List[int]
... | house-robber-ii | [Python 3]: O(N) time and O(1) space easy solution. | py_js | 1 | 88 | house robber ii | 213 | 0.407 | Medium | 3,660 |
https://leetcode.com/problems/house-robber-ii/discuss/2843265/Dynamic-Programming-Python3. | class Solution:
def rob(self, nums: List[int]) -> int:
n=len(nums)
if (n==1): return nums[0]
if (n==2): return max(nums[0],nums[1])
if (n==3): return max(nums[0],nums[1],nums[2])
def robPlain(nums):
nums[1]=max(nums[0],nums[1])
for i in range(2,len(num... | house-robber-ii | Dynamic Programming, Python3. | minhhoanghectorjack | 0 | 2 | house robber ii | 213 | 0.407 | Medium | 3,661 |
https://leetcode.com/problems/house-robber-ii/discuss/2843180/Python-DP | class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) <= 2:
return max(nums)
DP0, DP1 = 0, 0
for i in range(len(nums) - 1):
DP0, DP1 = DP1, max(DP0 + nums[i], DP1)
res = DP1
DP0, DP1 = 0, 0
for i in range(1, len(nums)):
... | house-robber-ii | [Python] DP | i-hate-covid | 0 | 2 | house robber ii | 213 | 0.407 | Medium | 3,662 |
https://leetcode.com/problems/house-robber-ii/discuss/2822264/Python-solution-or-DP-faster-than-91 | class Solution:
def rob(self, nums: List[int]) -> int:
n = len(nums)
dp1 = [0] * n
dp2 = [0] * n
if n <= 3:
return max(nums)
# take first
dp1[0] = nums[0]
dp1[1] = nums[1]
#take last
dp2[1] = nums[1]
... | house-robber-ii | Python solution | DP faster than 91% | maomao1010 | 0 | 4 | house robber ii | 213 | 0.407 | Medium | 3,663 |
https://leetcode.com/problems/house-robber-ii/discuss/2821933/Python-Using-First-Version-But-Checking-Houses-Length-1 | class Solution:
def rob(self, nums: List[int]) -> int:
'''
Here, the additional condition is that first & last house are "adjacent"
so we can instead take the max of range excluding first vs. excluding last
In case only 1 house, return itself
'''
if len(nums) ... | house-robber-ii | [Python] Using First Version, But Checking Houses Length 1 | graceiscoding | 0 | 3 | house robber ii | 213 | 0.407 | Medium | 3,664 |
https://leetcode.com/problems/house-robber-ii/discuss/2811029/PYTHON-99-Faster-Solution | class Solution:
def rob(self, nums: List[int]) -> int:
bup = nums[:]
nums = nums[:-1]
a, b = 0, 0
for i in nums:
a, b = b, max(i+a, b)
ans = b
nums = bup[1:]
a, b = 0, 0
for i in nums:
a, b = b, max(i+a, b)
return max(b,... | house-robber-ii | PYTHON 99% Faster Solution | hks_007 | 0 | 5 | house robber ii | 213 | 0.407 | Medium | 3,665 |
https://leetcode.com/problems/house-robber-ii/discuss/2794738/Concise-O(1)-space-solution | class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) < 3: return max(nums)
return max(self.robLinear(nums, 1, len(nums)), self.robLinear(nums,0,len(nums)-1))
def robLinear(self, nums: List[int], l: int, r: int) -> int:
c1, c2 = 0, 0
for i in range(l, r):
... | house-robber-ii | Concise O(1) space solution | mittoocode | 0 | 2 | house robber ii | 213 | 0.407 | Medium | 3,666 |
https://leetcode.com/problems/house-robber-ii/discuss/2730934/Veryy-easy-to-understand-beginner-level-Bottom-up | class Solution:
def solve1(self, nums): # exclude first 1 - ()n-1)
n = len(nums)
dp = [0]*n
dp[0] = 0
dp[1] = nums[1]
dp[2] = max(nums[1] , nums[2])
for i in range(2 , n):
take = nums[i]
if i > 2:
take += dp[i-2]
no... | house-robber-ii | Veryy easy to understand , beginner level Bottom-up | rajitkumarchauhan99 | 0 | 8 | house robber ii | 213 | 0.407 | Medium | 3,667 |
https://leetcode.com/problems/house-robber-ii/discuss/2713307/Python-solution-or-dp-and-O(n)-times | class Solution:
def rob(self, nums: List[int]) -> int:
length = len(nums)
if length <= 3:
return max(nums)
dp1 = [0] * length
dp2 = [0] * length
dp1[0] = nums[0]
dp1[1] = nums[1]
dp1[2] = max(dp1[0] + nums[2], dp1[1])
... | house-robber-ii | Python solution | dp & O(n) times | maomao1010 | 0 | 14 | house robber ii | 213 | 0.407 | Medium | 3,668 |
https://leetcode.com/problems/house-robber-ii/discuss/2710477/PYTHON-easy-oror-solution-or-using-DP-bottom-up-approach | class Solution:
def rob(self, nums: List[int]) -> int:
n=len(nums)
if n==1:
return nums[-1]
if n==2:
return max(nums[0],nums[1])
last=[0]*(n-1)
first=[0]*(n-1)
last[0]=nums[0]
last[1]=max(nums[0],nums[1])
first[0]=nums[1]
... | house-robber-ii | PYTHON easy || solution | using DP bottom up approach | tush18 | 0 | 6 | house robber ii | 213 | 0.407 | Medium | 3,669 |
https://leetcode.com/problems/house-robber-ii/discuss/2707603/Python-DP | class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]
def dp(cur, d, nums):
if d[cur] != -1:
return d[cur]
if cur >= len(nums) - 2:
# print(d, nums, cur)
d[cur] = nums[cur]... | house-robber-ii | Python DP | JSTM2022 | 0 | 3 | house robber ii | 213 | 0.407 | Medium | 3,670 |
https://leetcode.com/problems/house-robber-ii/discuss/2665019/Python-or-Dynamic-programming-(with-comments)-or-O(n) | class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]
# For the first iteration we start from 1st house.
# But we can't visit last house so we dont't fill last value of dp1 array
dp1 = [0]*len(nums)
dp1[0], dp1[1] = nums[0], max... | house-robber-ii | Python | Dynamic programming (with comments) | O(n) | LordVader1 | 0 | 30 | house robber ii | 213 | 0.407 | Medium | 3,671 |
https://leetcode.com/problems/house-robber-ii/discuss/2660830/here-is-my-solution-O(n)-greatergreater%3A) | class Solution:
def rob(self, n: List[int]) -> int:
l=len(n)
if(l==1):
return n[0]
elif l==2:
return max(n[0],n[1])
else:
prev2=n[0]
prev1=max(prev2,n[1])
for i in range(2,(l-1)):
pick=n[i]+prev2
... | house-robber-ii | here is my solution O(n)->>:) | re__fresh | 0 | 3 | house robber ii | 213 | 0.407 | Medium | 3,672 |
https://leetcode.com/problems/house-robber-ii/discuss/2652964/Pyhton-easy-oror-DP-oror | class Solution:
def rob(self, A: List[int]) -> int:
if len(A)<4: return max(A)
def solve(A):
dp = [0]*len(A)
dp[0],dp[1] = A[0],max(A[0],A[1])
for i in range(2,len(A)):
dp[i] = max(A[i]+dp[i-2],dp[i-1])
return dp[-1]
return ma... | house-robber-ii | Pyhton easy || DP || | Akash_chavan | 0 | 10 | house robber ii | 213 | 0.407 | Medium | 3,673 |
https://leetcode.com/problems/house-robber-ii/discuss/2597575/Simple-python-code-with-explanation | class Solution:
def rob(self, nums: List[int]) -> int:
return max(nums[0],self.helper(nums[1:]),self.helper(nums[:-1]))
def helper(self,nums):
nums.insert(0,0)
nums.insert(0,0)
rob1= 0
rob2 = 1
for i in ran... | house-robber-ii | Simple python code with explanation | thomanani | 0 | 60 | house robber ii | 213 | 0.407 | Medium | 3,674 |
https://leetcode.com/problems/house-robber-ii/discuss/2587297/Python-Solution-using-DP | class Solution:
def rob(self, nums: List[int]) -> int:
return max(nums[0], self.helper(nums[1:]), self.helper(nums[:-1]))
def helper(self, nums):
rob1, rob2 = 0, 0
for n in nums:
temp = max(n + rob1, rob2)
rob1 = rob2
rob2 = temp
return rob2 | house-robber-ii | Python Solution using DP | nikhitamore | 0 | 25 | house robber ii | 213 | 0.407 | Medium | 3,675 |
https://leetcode.com/problems/house-robber-ii/discuss/2545377/Python-Recursion-or-beats-99.98 | class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]
return max(self.dp(nums[0:len(nums)-1]), self.dp(nums[1:len(nums)]))
def dp(self, subarray: List[int]) -> int:
rob2 = 0
rob1 = 0
for i in subar... | house-robber-ii | Python Recursion | beats 99.98% | amany5642 | 0 | 79 | house robber ii | 213 | 0.407 | Medium | 3,676 |
https://leetcode.com/problems/house-robber-ii/discuss/2519285/Python-%3A-House-Robber-1-Code-Copy-paste | class Solution:
def rob(self, num: List[int]) -> int:
if len(num)==1:
return num[0]
def rob1(nums):
n=len(nums)
# dp=[0 for i in range(n)]
if n==1:
return nums[0]
prev2=nums[0]
prev1=ma... | house-robber-ii | Python : House Robber 1 Code Copy paste | Prithiviraj1927 | 0 | 38 | house robber ii | 213 | 0.407 | Medium | 3,677 |
https://leetcode.com/problems/house-robber-ii/discuss/2518898/Easy-Solution-based-on-House-Rober-I-Python | class Solution:
def rob(self, nums: List[int]) -> int:
# Check if only one element
if len(nums) == 1:
return nums[0]
# First iteration
rob1, rob2 = 0, 0
for i in nums[:-1]:
temp = max(i+rob1, rob2)
rob1 = rob2
rob2 = te... | house-robber-ii | Easy Solution based on House Rober I - Python | cosminpm | 0 | 27 | house robber ii | 213 | 0.407 | Medium | 3,678 |
https://leetcode.com/problems/house-robber-ii/discuss/2407869/Python-3-solution-with-tabulation%2Bspace-optimization-and-29-ms-time | class Solution:
def solveTabSop(self,nums):
curr = 0
prev2 = 0
prev = nums[0]
for i in range(1,len(nums)):
curr = max(prev2+nums[i] , prev)
prev2= prev
prev = curr
return prev
def rob(self,nums):
if(len(nums)==1):
... | house-robber-ii | Python 3 solution with tabulation+space optimization and 29 ms time | Sahil-Chandel | 0 | 26 | house robber ii | 213 | 0.407 | Medium | 3,679 |
https://leetcode.com/problems/house-robber-ii/discuss/2354633/Python-Bottom-Up-Approach-or-Easy-Solution | class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]
if len(nums) == 2:
return max(nums[0], nums[1])
return max(nums[0], self.helper(nums[1:]), self.helper(nums[:-1]))
def helper(self, nums: List[int]):
num... | house-robber-ii | Python Bottom Up Approach | Easy Solution | shreeruparel | 0 | 61 | house robber ii | 213 | 0.407 | Medium | 3,680 |
https://leetcode.com/problems/house-robber-ii/discuss/2352350/DP-or-Python-or-Beats-93.92 | class Solution:
def solve(self, ind, first_ind=0):
if ind >= len(self.nums):
return 0
if first_ind and ind == len(self.nums) - 1 and ind != 0:
return -1e9
if self.dp.get(ind) is not None and self.dp[ind][first_ind] is not None:
return sel... | house-robber-ii | DP | Python | Beats 93.92% | fake_death | 0 | 66 | house robber ii | 213 | 0.407 | Medium | 3,681 |
https://leetcode.com/problems/house-robber-ii/discuss/2328288/Python-Solution-%3A-Reusing-code-of-House-Robber-I | class Solution:
def rob(self, nums: List[int]) -> int:
return max(nums[0], self.helper(nums[1:]), self.helper(nums[:-1]))
def helper(self, nums):
ans1, ans2 = 0, 0
for n in nums:
temp = max(n + ans1, ans2)
ans1 = ans2
ans2 = temp
... | house-robber-ii | Python Solution : Reusing code of House Robber I | zip_demons | 0 | 70 | house robber ii | 213 | 0.407 | Medium | 3,682 |
https://leetcode.com/problems/house-robber-ii/discuss/2327570/SIMPLE-oror-FASTER-THAN-91 | class Solution:
def helper(self,nums):
n=len(nums)
prev,prev2=nums[0],0
for i in range(1,n):
if i>1:
curr=max(nums[i]+prev2,prev)
else:
curr=max(nums[i],prev)
prev2,prev=prev,curr
return prev
def rob(self,nu... | house-robber-ii | SIMPLE || FASTER THAN 91% | vijayvardhan6 | 0 | 31 | house robber ii | 213 | 0.407 | Medium | 3,683 |
https://leetcode.com/problems/house-robber-ii/discuss/2313993/Python-Slower-than-95-but-easy | class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) == 1: return nums[0]
res = [0]*len(nums)
res[0] = 0
res[1] = nums[1]
for i in range(2,len(nums)):
res[i] = nums[i] + max(res[:i-1])
a = max(res[:])
res[0] = nums[0]
for i in range(2,len(nums)):
res[i] = nums[i] + max(res[:i-1])
... | house-robber-ii | [Python] Slower than 95%, but easy | AvocadoDiCaprio | 0 | 11 | house robber ii | 213 | 0.407 | Medium | 3,684 |
https://leetcode.com/problems/house-robber-ii/discuss/2285785/Python-DP-Beats-98-with-full-working-explanation | class Solution:
def rob(self, nums: List[int]) -> int: # Time: O(n) and Space: O(1)
# breaking the list into 1 to n and 0 to n-1, such that 0 and n are adjacent to each other
# so, we can only include one of them and finding the max value from these lists and comparing them with 0 to find the max... | house-robber-ii | Python [DP / Beats 98%] with full working explanation | DanishKhanbx | 0 | 67 | house robber ii | 213 | 0.407 | Medium | 3,685 |
https://leetcode.com/problems/house-robber-ii/discuss/2269100/Python-Dynamic-Programming-Easy-Soln | class Solution:
def rob(self, nums: List[int]) -> int:
n = len(nums)
return max(nums[0], self.helper(0, nums[1:], None), self.helper(0, nums[:n-1], None))
def helper(self, i, nums, lookup=None):
lookup = {} if lookup is None else lookup
... | house-robber-ii | Python Dynamic Programming Easy Soln | logeshsrinivasans | 0 | 66 | house robber ii | 213 | 0.407 | Medium | 3,686 |
https://leetcode.com/problems/house-robber-ii/discuss/2254112/Easy-Python-Solution-or-DP-or-Memoization-or-Faster-than-97 | class Solution:
def rob(self, nums: List[int]) -> int:
# memoization implementation
if len(nums)==1:
return nums[0]
def helper(ind, dp, arr):
if ind==0:
return arr[0]
if ind<0:
return 0
if dp[ind]!=-1:
... | house-robber-ii | Easy Python Solution | DP | Memoization | Faster than 97% | Siddharth_singh | 0 | 103 | house robber ii | 213 | 0.407 | Medium | 3,687 |
https://leetcode.com/problems/house-robber-ii/discuss/2233758/Faster-than-84-of-python-submission-and-68-less-space-usage | class Solution:
def rob(self, nums: List[int]) -> int:
# This below function is also a solution to HouseRobber-I question
def helper(nums):
rob1, rob2 = 0, 0
for value in nums:
newRobValue = max(value+rob1, rob2)
rob1 = ... | house-robber-ii | Faster than 84% of python submission and 68% less space usage | himanshu17__ | 0 | 40 | house robber ii | 213 | 0.407 | Medium | 3,688 |
https://leetcode.com/problems/house-robber-ii/discuss/2227186/Easy-Python-Solution-using-DP-or-House-Robber-II | class Solution:
def helper(self, nums):
rob1, rob2 = 0, 0
for n in nums:
temp = max(rob1 + n, rob2)
rob1 = rob2
rob2 = temp
return rob2
def rob(self, nums: List[int]) -> int:
return max(nums[0],self.helper(nums[1:]),self.helper(nums[:-1])) | house-robber-ii | Easy Python Solution using DP | House Robber II | nishanrahman1994 | 0 | 53 | house robber ii | 213 | 0.407 | Medium | 3,689 |
https://leetcode.com/problems/house-robber-ii/discuss/2218149/House-Robber2-House-Robber1-with-1-extra-variable-or-recursion-with-memoization | class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums)==1:
return nums[0]
def robbing(index,memo,choosed):
if index in memo:return memo[index]
if index==0:return nums[index] if choosed else 0
if index<0:return 0
pick=nums[index... | house-robber-ii | House Robber2 = House Robber1 with 1 extra variable | recursion with memoization | glimloop | 0 | 84 | house robber ii | 213 | 0.407 | Medium | 3,690 |
https://leetcode.com/problems/house-robber-ii/discuss/2066723/Simple-Python-solution-oror-DYNAMIC-PROGRAMMING | class Solution:
def rob(self, nums: List[int]) -> int:
n=len(nums)
if n<=1:
return nums[0]
dp = [[-1]*n for _ in range(2)]
def rob(ind,dp,arr):
if ind == 0:
return arr[ind]
if ind<0:
... | house-robber-ii | Simple Python solution || DYNAMIC PROGRAMMING | testbugsk | 0 | 102 | house robber ii | 213 | 0.407 | Medium | 3,691 |
https://leetcode.com/problems/house-robber-ii/discuss/2033940/Python-Faster-DP-Solution-(O(1)-memory-O(n)-time) | class Solution:
def helper(self, arr, start, end):
prev2 = 0
prev1 = 0
maxRes = 0
for i in range(start, end):
maxRes = max(prev2 + arr[i], prev1)
prev2 = prev1
prev1 = maxRes
return maxRes
def rob... | house-robber-ii | Python Faster DP Solution (O(1) memory, O(n) time) | dbansal18 | 0 | 65 | house robber ii | 213 | 0.407 | Medium | 3,692 |
https://leetcode.com/problems/house-robber-ii/discuss/2024010/Python-or-DP-or-Easy-to-Understand | class Solution:
def rob(self, nums: List[int]) -> int:
n = len(nums)
if n == 1:
return nums[0]
if n == 2:
return max(nums[0], nums[1])
# result1: don't consider the first house
# result2: don't consider the last house
... | house-robber-ii | Python | DP | Easy to Understand | Mikey98 | 0 | 127 | house robber ii | 213 | 0.407 | Medium | 3,693 |
https://leetcode.com/problems/shortest-palindrome/discuss/2157861/No-DP-No-DS-Intuitive-with-comments-oror-Python | class Solution:
def shortestPalindrome(self, s: str) -> str:
end = 0
# if the string itself is a palindrome return it
if(s == s[::-1]):
return s
# Otherwise find the end index of the longest palindrome that starts
# from the first charac... | shortest-palindrome | No DP; No DS; Intuitive with comments || Python | a-myth | 2 | 124 | shortest palindrome | 214 | 0.322 | Hard | 3,694 |
https://leetcode.com/problems/shortest-palindrome/discuss/1665104/Python3-groupby-solution-without-KMP | class Solution1:
def shortestPalindrome(self, s: str) -> str:
"""We create two new arrays to represent the original string. We use
`groupby` to count the number of repeats of each consecutive letters
and record them in two arrays. One is letters, recording all
consecutively distinct ... | shortest-palindrome | [Python3] `groupby` solution without KMP | FanchenBao | 2 | 99 | shortest palindrome | 214 | 0.322 | Hard | 3,695 |
https://leetcode.com/problems/shortest-palindrome/discuss/2820508/100-Faster-Python-Solution-using-KMP-Algorithm | class Solution:
def shortestPalindrome(self, s: str) -> str:
def kmp(txt, patt):
newString = patt + '#' + txt
freqArray = [0 for _ in range(len(newString))]
i = 1
length = 0
while i < len(newString):
if newString[i] == newString[length]:
length += 1
freqArray[i] = length
i += 1
... | shortest-palindrome | 100% Faster Python Solution - using KMP Algorithm | prateekgoel7248 | 1 | 32 | shortest palindrome | 214 | 0.322 | Hard | 3,696 |
https://leetcode.com/problems/shortest-palindrome/discuss/2541808/Python3-Epic-3-Liner | class Solution:
def shortestPalindrome(self, s: str) -> str:
for i in range(len(s), -1, -1):
if s[:i] == s[i-1::-1]:
return s[:i-1:-1] + s | shortest-palindrome | Python3 Epic 3 Liner | ryangrayson | 1 | 155 | shortest palindrome | 214 | 0.322 | Hard | 3,697 |
https://leetcode.com/problems/shortest-palindrome/discuss/2159251/Python-Stupidly-Simple-5-lines-solution | class Solution:
def shortestPalindrome(self, s: str) -> str:
if not s: return ""
for i in reversed(range(len(s))):
if s[0:i+1] == s[i::-1]:
return s[:i:-1] + s | shortest-palindrome | [Python] Stupidly Simple 5 lines solution | Saksham003 | 1 | 175 | shortest palindrome | 214 | 0.322 | Hard | 3,698 |
https://leetcode.com/problems/shortest-palindrome/discuss/739903/Python3-2-line-brute-force-and-9-line-KMP | class Solution:
def shortestPalindrome(self, s: str) -> str:
k = next((i for i in range(len(s), 0, -1) if s[:i] == s[:i][::-1]), 0)
return s[k:][::-1] + s | shortest-palindrome | [Python3] 2-line brute force & 9-line KMP | ye15 | 1 | 92 | shortest palindrome | 214 | 0.322 | Hard | 3,699 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.