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/nearest-exit-from-entrance-in-maze/discuss/1814959/Python-BFS-75
|
class Solution:
def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int:
rows, cols = len(maze), len(maze[0])
directions = ((0,1), (0,-1), (1,0), (-1,0))
def withinBounds(r, c):
return 0 <= r < rows and 0 <= c < cols
q = deque([(entrance[0], entrance[1])])
visited = set([(entrance[0], entrance[1])])
path = 0
while q:
for _ in range(len(q)):
r, c = q.popleft()
for dr, dc in directions:
dr, dc = dr + r, dc + c
if not withinBounds(dr, dc):
if (r, c) != (entrance[0], entrance[1]):
return path
continue
if (dr, dc) not in visited and maze[dr][dc] == ".":
visited.add((dr, dc))
q.append((dr, dc))
path += 1
return -1
|
nearest-exit-from-entrance-in-maze
|
Python BFS 75%
|
Rush_P
| 0
| 26
|
nearest exit from entrance in maze
| 1,926
| 0.49
|
Medium
| 27,200
|
https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/discuss/1707803/Why-is-this-BFS-giving-TLE-can-anyone-help
|
class Solution:
def nearestExit(self, maze, entrance) -> int:
'''
for val in maze:
print(val)
'''
m, n = len(maze), len(maze[0])
q, ans = [entrance], 0
while q:
num = len(q)
for i in range(num):
x, y = q[0]
if (x == 0 or y == 0 or x == m - 1 or y == n - 1) and ans > 0:
return ans
q.pop(0)
maze[x][y] = '+'
if x > 0 and maze[x - 1][y] == '.':
q.append([x - 1, y])
if y > 0 and maze[x][y - 1] == '.':
q.append([x, y - 1])
if x < m - 1 and maze[x + 1][y] == '.':
q.append([x + 1, y])
if y < n - 1 and maze[x][y + 1] == '.':
q.append([x, y + 1])
ans += 1
return -1
|
nearest-exit-from-entrance-in-maze
|
Why is this BFS giving TLE, can anyone help
|
sanial2001
| 0
| 41
|
nearest exit from entrance in maze
| 1,926
| 0.49
|
Medium
| 27,201
|
https://leetcode.com/problems/sum-game/discuss/1330360/Python-3-or-Simple-Math-or-Explanation
|
class Solution:
def sumGame(self, num: str) -> bool:
n = len(num)
q_cnt_1 = s1 = 0
for i in range(n//2): # get digit sum and question mark count for the first half of `num`
if num[i] == '?':
q_cnt_1 += 1
else:
s1 += int(num[i])
q_cnt_2 = s2 = 0
for i in range(n//2, n): # get digit sum and question mark count for the second half of `num`
if num[i] == '?':
q_cnt_2 += 1
else:
s2 += int(num[i])
s_diff = s1 - s2 # calculate sum difference and question mark difference
q_diff = q_cnt_2 - q_cnt_1
return not (q_diff % 2 == 0 and q_diff // 2 * 9 == s_diff) # When Bob can't win, Alice wins
|
sum-game
|
Python 3 | Simple Math | Explanation
|
idontknoooo
| 16
| 396
|
sum game
| 1,927
| 0.469
|
Medium
| 27,202
|
https://leetcode.com/problems/sum-game/discuss/1336163/Python3-greedy
|
class Solution:
def sumGame(self, num: str) -> bool:
diff = qm = 0
for i, ch in enumerate(num):
if ch == "?": qm += 1 if i < len(num)//2 else -1
else: diff += int(ch) if i < len(num)//2 else -int(ch)
return diff * 2 + qm * 9 != 0
|
sum-game
|
[Python3] greedy
|
ye15
| 0
| 60
|
sum game
| 1,927
| 0.469
|
Medium
| 27,203
|
https://leetcode.com/problems/minimum-cost-to-reach-destination-in-time/discuss/2841255/Python-Dijkstra's-Algorithm%3A-36-time-8-space
|
class Solution:
def minCost(self, maxTime: int, edges: List[List[int]], passingFees: List[int]) -> int:
n = len(passingFees)
mat = {}
for x, y, time in edges:
if x not in mat: mat[x] = set()
if y not in mat: mat[y] = set()
mat[x].add((y, time))
mat[y].add((x, time))
h = [(passingFees[0], 0, 0)]
visited = set()
while h:
fees, time_so_far, city = heappop(h)
if time_so_far > maxTime: continue
if city == n - 1: return fees
if (city, time_so_far) in visited: continue
visited.add((city, time_so_far))
for nxt, time_to_travel in mat[city]:
# Check if we are retracing a visited path
if (nxt, time_so_far - time_to_travel) in visited: continue
heappush(h, (fees + passingFees[nxt], time_so_far + time_to_travel, nxt))
return -1
|
minimum-cost-to-reach-destination-in-time
|
Python Dijkstra's Algorithm: 36% time, 8% space
|
hqz3
| 0
| 2
|
minimum cost to reach destination in time
| 1,928
| 0.374
|
Hard
| 27,204
|
https://leetcode.com/problems/minimum-cost-to-reach-destination-in-time/discuss/1336167/Python3-Dijkstra'-algo
|
class Solution:
def minCost(self, maxTime: int, edges: List[List[int]], passingFees: List[int]) -> int:
graph = {}
for u, v, t in edges:
graph.setdefault(u, []).append((v, t))
graph.setdefault(v, []).append((u, t))
pq = [(passingFees[0], 0, 0)]
dist = {0: 0}
while pq:
cost, k, t = heappop(pq)
if k == len(passingFees)-1: return cost
for kk, tt in graph.get(k, []):
if t + tt <= maxTime and t + tt < dist.get(kk, inf):
dist[kk] = t + tt
heappush(pq, (cost + passingFees[kk], kk, t + tt))
return -1
|
minimum-cost-to-reach-destination-in-time
|
[Python3] Dijkstra' algo
|
ye15
| 0
| 85
|
minimum cost to reach destination in time
| 1,928
| 0.374
|
Hard
| 27,205
|
https://leetcode.com/problems/minimum-cost-to-reach-destination-in-time/discuss/1329543/Python-3-Heap-%2B-BFS-(496-ms)
|
class Solution:
def minCost(self, maxTime: int, edges: List[List[int]], fee: List[int]) -> int:
g = defaultdict(lambda: defaultdict(lambda: float('inf')))
n = len(fee)
# select edges with minimal time
for u, v, t in edges:
if t < g[u][v]:
g[u][v] = t
if t < g[v][u]:
g[v][u] = t
vis = defaultdict(tuple, {0: (0, fee[0])})
q = [(fee[0], 0, 0)]
while q:
f, t, cur = heappop(q)
if cur == n - 1:
return f
for nei in g[cur]:
nf = f + fee[nei]
nt = t + g[cur][nei]
if nt > maxTime: continue
# global vis set (either less time or fare)
if not vis[nei] or (nt < vis[nei][0] or nf < vis[nei][1]):
if not vis[nei]:
vis[nei] = (nt, nf)
else:
vis[nei] = (min(nt, vis[nei][0]), min(nf, vis[nei][1]))
heappush(q, (nf, nt, nei))
return -1
|
minimum-cost-to-reach-destination-in-time
|
[Python 3] Heap + BFS (496 ms)
|
chestnut890123
| 0
| 119
|
minimum cost to reach destination in time
| 1,928
| 0.374
|
Hard
| 27,206
|
https://leetcode.com/problems/concatenation-of-array/discuss/2044719/Easy-Python-two-liner-code
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
nums.extend(nums)
return nums
|
concatenation-of-array
|
Easy Python two liner code
|
Shivam_Raj_Sharma
| 10
| 743
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,207
|
https://leetcode.com/problems/concatenation-of-array/discuss/1434007/Python3-solution
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
n=len(nums)
r=[]
for i in range(0,2*n):
if i<n:
r.append(nums[i])
else:
r.append(nums[i-n])
return r
|
concatenation-of-array
|
Python3 solution
|
Pranav447
| 9
| 1,900
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,208
|
https://leetcode.com/problems/concatenation-of-array/discuss/2023062/Python-One-Line-(x4)!-%2B-Multiple-Solutions!
|
class Solution:
def getConcatenation(self, nums):
return nums + nums
|
concatenation-of-array
|
Python - One Line (x4)! + Multiple Solutions!
|
domthedeveloper
| 5
| 228
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,209
|
https://leetcode.com/problems/concatenation-of-array/discuss/2023062/Python-One-Line-(x4)!-%2B-Multiple-Solutions!
|
class Solution:
def getConcatenation(self, nums):
return nums * 2
|
concatenation-of-array
|
Python - One Line (x4)! + Multiple Solutions!
|
domthedeveloper
| 5
| 228
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,210
|
https://leetcode.com/problems/concatenation-of-array/discuss/2023062/Python-One-Line-(x4)!-%2B-Multiple-Solutions!
|
class Solution:
def getConcatenation(self, nums):
return [*nums, *nums]
|
concatenation-of-array
|
Python - One Line (x4)! + Multiple Solutions!
|
domthedeveloper
| 5
| 228
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,211
|
https://leetcode.com/problems/concatenation-of-array/discuss/2023062/Python-One-Line-(x4)!-%2B-Multiple-Solutions!
|
class Solution:
def getConcatenation(self, nums):
return [nums[i % len(nums)] for i in range(len(nums)*2)]
|
concatenation-of-array
|
Python - One Line (x4)! + Multiple Solutions!
|
domthedeveloper
| 5
| 228
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,212
|
https://leetcode.com/problems/concatenation-of-array/discuss/2023062/Python-One-Line-(x4)!-%2B-Multiple-Solutions!
|
class Solution:
def getConcatenation(self, nums):
ans = []
for num in nums: ans.append(num)
for num in nums: ans.append(num)
return ans
|
concatenation-of-array
|
Python - One Line (x4)! + Multiple Solutions!
|
domthedeveloper
| 5
| 228
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,213
|
https://leetcode.com/problems/concatenation-of-array/discuss/2023062/Python-One-Line-(x4)!-%2B-Multiple-Solutions!
|
class Solution:
def getConcatenation(self, nums):
ans = []
for _ in range(2):
for num in nums:
ans.append(num)
return ans
|
concatenation-of-array
|
Python - One Line (x4)! + Multiple Solutions!
|
domthedeveloper
| 5
| 228
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,214
|
https://leetcode.com/problems/concatenation-of-array/discuss/2023062/Python-One-Line-(x4)!-%2B-Multiple-Solutions!
|
class Solution:
def getConcatenation(self, nums):
ans = nums.copy()
for num in nums: ans.append(num)
return ans
|
concatenation-of-array
|
Python - One Line (x4)! + Multiple Solutions!
|
domthedeveloper
| 5
| 228
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,215
|
https://leetcode.com/problems/concatenation-of-array/discuss/2685750/Python-1-lineror-or-92.77-speed-or-64.96-memory
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums+nums
|
concatenation-of-array
|
Python 1-liner| | 92.77% speed, | 64.96% memory
|
MA_Khan
| 4
| 1,100
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,216
|
https://leetcode.com/problems/concatenation-of-array/discuss/2250173/Python3-Cirular-Array-trick
|
class Solution:
# O(n) || O(n)
# Runtime: 135ms 43.91% memory: 14.2mb 24.06%
def getConcatenation(self, nums: List[int]) -> List[int]:
newArray = [0] * (len(nums)*2)
size = len(nums)
for i in range(len(nums) * 2):
index = i % size
newArray[i] = nums[index]
return newArray
|
concatenation-of-array
|
Python3 Cirular Array trick
|
arshergon
| 4
| 148
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,217
|
https://leetcode.com/problems/concatenation-of-array/discuss/1714679/1929.-Concatenation-of-Array-python-solution-easy
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
for i in range(len(nums)):
nums.append(nums[i])
return nums
|
concatenation-of-array
|
1929. Concatenation of Array python solution easy
|
apoorv11jain
| 2
| 165
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,218
|
https://leetcode.com/problems/concatenation-of-array/discuss/2366565/Easy-Python-One-Liner!
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums + nums
|
concatenation-of-array
|
Easy Python One Liner! ✅🐍
|
qing306037
| 1
| 65
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,219
|
https://leetcode.com/problems/concatenation-of-array/discuss/2366565/Easy-Python-One-Liner!
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums * 2
|
concatenation-of-array
|
Easy Python One Liner! ✅🐍
|
qing306037
| 1
| 65
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,220
|
https://leetcode.com/problems/concatenation-of-array/discuss/2187353/Python-solution-multiple-approach-for-beginners-by-beginner.
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums+nums
|
concatenation-of-array
|
Python solution multiple approach for beginners by beginner.
|
EbrahimMG
| 1
| 53
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,221
|
https://leetcode.com/problems/concatenation-of-array/discuss/2187353/Python-solution-multiple-approach-for-beginners-by-beginner.
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
a = []
for i in nums:
a.append(i)
for i in nums:
a.append(i)
return a
|
concatenation-of-array
|
Python solution multiple approach for beginners by beginner.
|
EbrahimMG
| 1
| 53
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,222
|
https://leetcode.com/problems/concatenation-of-array/discuss/2187353/Python-solution-multiple-approach-for-beginners-by-beginner.
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums*2
|
concatenation-of-array
|
Python solution multiple approach for beginners by beginner.
|
EbrahimMG
| 1
| 53
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,223
|
https://leetcode.com/problems/concatenation-of-array/discuss/1865898/Python-1-line-simple-solution-or-100-Faster
|
class Solution(object):
def getConcatenation(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
nums.extend(nums)
return nums
|
concatenation-of-array
|
Python 1 line simple solution | 100% Faster
|
leonhwlee9
| 1
| 205
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,224
|
https://leetcode.com/problems/concatenation-of-array/discuss/1741543/Python-3-95-faster-Multiple-Solutions-Easy-to-understand
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums + nums
|
concatenation-of-array
|
[Python 3] 95% faster - Multiple Solutions - Easy to understand
|
flatwhite
| 1
| 171
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,225
|
https://leetcode.com/problems/concatenation-of-array/discuss/1741543/Python-3-95-faster-Multiple-Solutions-Easy-to-understand
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums.extend(nums)
|
concatenation-of-array
|
[Python 3] 95% faster - Multiple Solutions - Easy to understand
|
flatwhite
| 1
| 171
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,226
|
https://leetcode.com/problems/concatenation-of-array/discuss/1741543/Python-3-95-faster-Multiple-Solutions-Easy-to-understand
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums * 2
|
concatenation-of-array
|
[Python 3] 95% faster - Multiple Solutions - Easy to understand
|
flatwhite
| 1
| 171
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,227
|
https://leetcode.com/problems/concatenation-of-array/discuss/1351042/Python-simplest-solution-with-explanation
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums + nums
|
concatenation-of-array
|
[Python] simplest solution with explanation
|
rodrigogiraoserrao
| 1
| 400
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,228
|
https://leetcode.com/problems/concatenation-of-array/discuss/1338513/Python-%3A-Explained-with-faster-approach.-Faster-than-95-and-lesser-space.
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
"""
Time complexity: O(n)
Space Complexity: O(n)
"""
for i in range(len(nums)):
nums.append(nums[i])
return nums
#2nd solution: One liner
return nums+nums
#3rd solution: Using inbuilt function
nums.extend(nums)
return nums
|
concatenation-of-array
|
Python : Explained with faster approach. Faster than 95% and lesser space.
|
er1shivam
| 1
| 270
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,229
|
https://leetcode.com/problems/concatenation-of-array/discuss/2839328/Two-pointer-python-solution-TC-O(n2)
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
l = 0
r = len(nums) - 1
ans = [None] * len(nums) * 2
N = len(nums)
while l <= r:
ans[l] = ans[N + l] = nums[l]
ans[r] = ans[N + r] = nums[r]
l += 1
r -= 1
return ans
|
concatenation-of-array
|
Two pointer python solution TC O(n/2)
|
Charan_coder
| 0
| 2
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,230
|
https://leetcode.com/problems/concatenation-of-array/discuss/2830890/Very-Easy-Python-Solution-for-Beginners
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
b=nums
c=nums+b
return c
|
concatenation-of-array
|
Very Easy Python Solution for Beginners
|
VidishaPandey
| 0
| 2
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,231
|
https://leetcode.com/problems/concatenation-of-array/discuss/2815395/Fastest-and-Simplest-Solution-Python-(One-Liner-Code)
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums + nums
|
concatenation-of-array
|
Fastest and Simplest Solution - Python (One-Liner Code)
|
PranavBhatt
| 0
| 5
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,232
|
https://leetcode.com/problems/concatenation-of-array/discuss/2812802/Python-solution
|
class Solution(object):
def getConcatenation(self, nums):
for i in range(len(nums)):
nums.append(nums[i])
return nums
|
concatenation-of-array
|
Python solution
|
tegast
| 0
| 3
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,233
|
https://leetcode.com/problems/concatenation-of-array/discuss/2804907/Simple-Python-Beats-96-Memory-84-Runtime
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
ans = []
for i in nums * 2:
ans.append(i)
return ans
|
concatenation-of-array
|
Simple Python Beats 96% Memory, 84% Runtime
|
rdfabian
| 0
| 2
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,234
|
https://leetcode.com/problems/concatenation-of-array/discuss/2802227/Python-Solution-No-Multiplication-addition-is-best
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
nums = nums + nums
return nums
|
concatenation-of-array
|
Python Solution No Multiplication addition is best
|
sscswapnil
| 0
| 2
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,235
|
https://leetcode.com/problems/concatenation-of-array/discuss/2802145/The-easiest-way-to-solve-this-problem-in-python3.
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
n = len(nums)
ans = list(range(n * 2))
for i in range(n):
ans[i] = nums[i]
ans[i + n] = nums[i]
return ans
|
concatenation-of-array
|
The easiest way to solve this problem in python3.
|
arnoldaguila04
| 0
| 2
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,236
|
https://leetcode.com/problems/concatenation-of-array/discuss/2800809/PYTHON3-BETTER
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
n=len(nums)
r=[]
for i in range(0,2*n):
if i<n:
r.append(nums[i])
else:
r.append(nums[i-n])
return r
|
concatenation-of-array
|
PYTHON3 BETTER
|
Gurugubelli_Anil
| 0
| 2
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,237
|
https://leetcode.com/problems/concatenation-of-array/discuss/2795080/Faster-than-96.92-of-Python3-online-submissions-for-Concatenation-of-Array.
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
ans = []
length = (len(nums))*2 #double actual of length
n = len(nums)
for i in range(length):
if i < n:
ans.append(nums[i])
else:
mod = i % n # because modulus would be always less than "n"
ans.append(nums[mod])
return ans
|
concatenation-of-array
|
Faster than 96.92% of Python3 online submissions for Concatenation of Array.
|
IlsaAfzaal
| 0
| 7
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,238
|
https://leetcode.com/problems/concatenation-of-array/discuss/2785781/Solution
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums*2
|
concatenation-of-array
|
Solution
|
vovatoshev1986
| 0
| 1
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,239
|
https://leetcode.com/problems/concatenation-of-array/discuss/2778484/Python-or-LeetCode-or-1929.-Concatenation-of-Array
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
ans = []
ans.extend(nums)
ans.extend(nums)
return ans
|
concatenation-of-array
|
Python | LeetCode | 1929. Concatenation of Array
|
UzbekDasturchisiman
| 0
| 1
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,240
|
https://leetcode.com/problems/concatenation-of-array/discuss/2778484/Python-or-LeetCode-or-1929.-Concatenation-of-Array
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
nums.extend(nums)
return nums
|
concatenation-of-array
|
Python | LeetCode | 1929. Concatenation of Array
|
UzbekDasturchisiman
| 0
| 1
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,241
|
https://leetcode.com/problems/concatenation-of-array/discuss/2778484/Python-or-LeetCode-or-1929.-Concatenation-of-Array
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums + nums
|
concatenation-of-array
|
Python | LeetCode | 1929. Concatenation of Array
|
UzbekDasturchisiman
| 0
| 1
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,242
|
https://leetcode.com/problems/concatenation-of-array/discuss/2778484/Python-or-LeetCode-or-1929.-Concatenation-of-Array
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums*2
|
concatenation-of-array
|
Python | LeetCode | 1929. Concatenation of Array
|
UzbekDasturchisiman
| 0
| 1
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,243
|
https://leetcode.com/problems/concatenation-of-array/discuss/2764662/one-line-solution-python
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums + nums
|
concatenation-of-array
|
one line solution python
|
ft3793
| 0
| 3
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,244
|
https://leetcode.com/problems/concatenation-of-array/discuss/2744042/Python-1-line-code
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return 2*nums;
|
concatenation-of-array
|
Python 1 line code
|
kumar_anand05
| 0
| 3
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,245
|
https://leetcode.com/problems/concatenation-of-array/discuss/2743533/Python-Solution-using-one-for-loop
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
ans = []
n = len(nums)
for i in range(2 * n):
ans.append(nums[i % n])
return ans
|
concatenation-of-array
|
Python Solution using one for loop
|
Furat
| 0
| 3
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,246
|
https://leetcode.com/problems/concatenation-of-array/discuss/2717480/Python-1-line-GEN-way-solution
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
ans = [nums[i] if i < len(nums) else nums[i-len(nums)] for i in range(len(nums)*2) ]
return ans
|
concatenation-of-array
|
Python 1 line GEN way solution
|
ensur89
| 0
| 4
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,247
|
https://leetcode.com/problems/concatenation-of-array/discuss/2710846/Python-easy-to-understand
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
n = len(nums)
ans = [0]* 2*n
for i in range(n):
ans[i] = nums[i]
ans[i + n] = nums[i]
return ans
|
concatenation-of-array
|
Python easy to understand
|
piyush_54
| 0
| 5
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,248
|
https://leetcode.com/problems/concatenation-of-array/discuss/2694006/Python-C%2B%2B-and-JavaScript-solutions
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums + nums
|
concatenation-of-array
|
Python, C++ and JavaScript solutions
|
anandanshul001
| 0
| 3
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,249
|
https://leetcode.com/problems/concatenation-of-array/discuss/2674671/Python-solution
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
for i in range(len(nums)):
nums.append(nums[i])
return(nums)
|
concatenation-of-array
|
Python solution
|
VijayarajP
| 0
| 53
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,250
|
https://leetcode.com/problems/concatenation-of-array/discuss/2654791/one-liner-easy-and-faster
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums+nums
|
concatenation-of-array
|
one liner easy and faster
|
Raghunath_Reddy
| 0
| 6
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,251
|
https://leetcode.com/problems/concatenation-of-array/discuss/2643464/Easy-way-to-do-this-problem-using-built-in-python.
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
ans = []
for r in nums:
ans.append(r)
ans.extend(nums)
return ans
|
concatenation-of-array
|
Easy way to do this problem using built in python.
|
Simicsoftware
| 0
| 1
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,252
|
https://leetcode.com/problems/concatenation-of-array/discuss/2615883/Python-easy-single-line-solution-TC-O(k)
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
nums.extend(nums)
return nums
|
concatenation-of-array
|
Python easy single line solution [TC-O(k)]
|
Shreya_sg_283
| 0
| 19
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,253
|
https://leetcode.com/problems/concatenation-of-array/discuss/2567447/Python3-easy-one-liner-solution
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums * 2 #or return nums + nums
|
concatenation-of-array
|
Python3 easy one liner solution
|
khushie45
| 0
| 70
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,254
|
https://leetcode.com/problems/concatenation-of-array/discuss/2564401/iterative-O(n)
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
# get the size of nums n
# create an arr 2n filled with 0s
# iterate over nums
# store the value at nums[i] and nums[i + 1]
# Time O(n) Space O(n)
n = len(nums)
arr = [0] * 2 * n
for i in range(n):
num = nums[i]
arr[i], arr[i + n] = num, num
return arr
|
concatenation-of-array
|
iterative O(n)
|
andrewnerdimo
| 0
| 38
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,255
|
https://leetcode.com/problems/concatenation-of-array/discuss/2564401/iterative-O(n)
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
# cheeky way
return nums + nums
|
concatenation-of-array
|
iterative O(n)
|
andrewnerdimo
| 0
| 38
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,256
|
https://leetcode.com/problems/concatenation-of-array/discuss/2509776/Python3-EASY-WAY
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
finish = []
for i in nums:
finish.append(i)
for i in nums:
finish.append(i)
return finish
|
concatenation-of-array
|
Python3 - EASY WAY
|
xfuxad00
| 0
| 45
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,257
|
https://leetcode.com/problems/concatenation-of-array/discuss/2494455/Python-Solution-or-100-Faster-or-One-Liner-or-Just-Do-nums-*-2
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums*2
|
concatenation-of-array
|
Python Solution | 100% Faster | One Liner | Just Do nums * 2
|
Gautam_ProMax
| 0
| 132
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,258
|
https://leetcode.com/problems/concatenation-of-array/discuss/2427489/.or-python-one-liner-or.
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums+nums
|
concatenation-of-array
|
.| python one liner |.
|
keertika27
| 0
| 75
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,259
|
https://leetcode.com/problems/concatenation-of-array/discuss/2409770/Using-Insert-in-List-Python
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
# list.insert(position, element)
# It accepts a position and an element and inserts the element at given position in the list.
l1=[]
for i in range(len(nums)):
l1.insert(i,nums[i])
l1.insert(i+len(nums),nums[i])
return l1
|
concatenation-of-array
|
Using Insert in List Python
|
Sri_Balaji98
| 0
| 29
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,260
|
https://leetcode.com/problems/concatenation-of-array/discuss/2398524/simple-python-code
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums+nums
|
concatenation-of-array
|
simple python code
|
thomanani
| 0
| 105
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,261
|
https://leetcode.com/problems/concatenation-of-array/discuss/2380455/EASY-PYTHON-SOLUTION-USING-2-FOR-LOOPS
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
arr=[]
for i in nums:
arr.append(i)
for i in nums:
arr.append(i)
return arr
|
concatenation-of-array
|
EASY PYTHON SOLUTION USING 2 FOR LOOPS
|
keertika27
| 0
| 68
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,262
|
https://leetcode.com/problems/concatenation-of-array/discuss/2348214/Concatenation-of-array
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
ans = nums + nums
return ans
|
concatenation-of-array
|
Concatenation of array
|
samanehghafouri
| 0
| 30
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,263
|
https://leetcode.com/problems/concatenation-of-array/discuss/2256415/Very-Easy-Python-3-solution-using-%22*%22-mathematical-operator
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
ans = 2 * nums
return ans
|
concatenation-of-array
|
Very Easy Python 3 solution using "*" mathematical operator
|
Vaishnav-Dahake
| 0
| 40
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,264
|
https://leetcode.com/problems/concatenation-of-array/discuss/2214174/faster-than-85.88Python-simple-Solution
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
ans = nums
for i in range(len(nums)):
ans.append(nums[i])
return ans
|
concatenation-of-array
|
faster than 85.88%[Python simple Solution]
|
salsabilelgharably
| 0
| 86
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,265
|
https://leetcode.com/problems/concatenation-of-array/discuss/2116008/Python-Three-easy-solutions-with-complexities
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
for i in range(0,len(nums)):
nums.append(nums[i])
return nums
# time O(N)
# space O(N)
|
concatenation-of-array
|
[Python] Three easy solutions with complexities
|
mananiac
| 0
| 194
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,266
|
https://leetcode.com/problems/concatenation-of-array/discuss/2116008/Python-Three-easy-solutions-with-complexities
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums[:] + nums[:]
# time O(N)
# space O(N)
|
concatenation-of-array
|
[Python] Three easy solutions with complexities
|
mananiac
| 0
| 194
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,267
|
https://leetcode.com/problems/concatenation-of-array/discuss/2116008/Python-Three-easy-solutions-with-complexities
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums*2
# time O(N)
# space O(N)
|
concatenation-of-array
|
[Python] Three easy solutions with complexities
|
mananiac
| 0
| 194
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,268
|
https://leetcode.com/problems/concatenation-of-array/discuss/2102749/JUST-TWO-WORDS-CODE-100-working
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums*2
|
concatenation-of-array
|
JUST TWO WORDS CODE - 100% working
|
T1n1_B0x1
| 0
| 87
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,269
|
https://leetcode.com/problems/concatenation-of-array/discuss/2097855/array-concatenation
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums+nums
|
concatenation-of-array
|
array concatenation
|
anil5829354
| 0
| 58
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,270
|
https://leetcode.com/problems/concatenation-of-array/discuss/2089088/python-very-easy-to-understand-one-liner
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums + nums
|
concatenation-of-array
|
python-very easy to understand one liner
|
Coconut0727
| 0
| 68
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,271
|
https://leetcode.com/problems/concatenation-of-array/discuss/2088434/Python-3
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
nums.extend(nums)
return nums
|
concatenation-of-array
|
Python 3
|
iamirulofficial
| 0
| 43
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,272
|
https://leetcode.com/problems/concatenation-of-array/discuss/1959396/Python3-Soulution
|
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums * 2
|
concatenation-of-array
|
Python3 Soulution
|
AprDev2011
| 0
| 85
|
concatenation of array
| 1,929
| 0.912
|
Easy
| 27,273
|
https://leetcode.com/problems/unique-length-3-palindromic-subsequences/discuss/1330186/easy-python-solution
|
class Solution(object):
def countPalindromicSubsequence(self, s):
d=defaultdict(list)
for i,c in enumerate(s):
d[c].append(i)
ans=0
for el in d:
if len(d[el])<2:
continue
a=d[el][0]
b=d[el][-1]
ans+=len(set(s[a+1:b]))
return(ans)
|
unique-length-3-palindromic-subsequences
|
easy python solution
|
aayush_chhabra
| 32
| 1,100
|
unique length 3 palindromic subsequences
| 1,930
| 0.515
|
Medium
| 27,274
|
https://leetcode.com/problems/unique-length-3-palindromic-subsequences/discuss/1330340/Python-solution-faster-than-200-ms
|
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
if len(s) < 3:
return 0
elif len(s) == 3:
return 1 if s[0]==s[2] else 0
else:
num_of_palindromes = 0
unique = list(set(s))
for char in unique:
count = s.count(char)
if count > 1:
# find first and last index of char in s
a_index = s.index(char)
c_index = s.rindex(char)
# find num of unique chars between the two indeces
between = s[a_index+1:c_index]
num_of_palindromes += len(list(set(between)))
return num_of_palindromes
|
unique-length-3-palindromic-subsequences
|
Python solution, faster than 200 ms
|
njain07
| 13
| 952
|
unique length 3 palindromic subsequences
| 1,930
| 0.515
|
Medium
| 27,275
|
https://leetcode.com/problems/unique-length-3-palindromic-subsequences/discuss/1331480/Simple-and-Easy-oror-100-faster-oror-Well-Explained-oror-6-lines-of-code
|
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
res = 0
unq_str = set(s)
for ch in unq_str:
st = s.find(ch)
ed = s.rfind(ch)
if st<ed:
res+=len(set(s[st+1:ed]))
return res
|
unique-length-3-palindromic-subsequences
|
🐍 Simple & Easy || 100% faster || Well-Explained || 6 lines of code 📌
|
abhi9Rai
| 5
| 288
|
unique length 3 palindromic subsequences
| 1,930
| 0.515
|
Medium
| 27,276
|
https://leetcode.com/problems/unique-length-3-palindromic-subsequences/discuss/1330314/Python-Simple-Solution
|
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
ctr = 0
for i in range(97,123):
first = s.find(chr(i))
if f!=-1:
last = s.rfind(chr(i))
ctr += len(set(s[first+1:last])) # count of unique characters between first and last character
return ctr
|
unique-length-3-palindromic-subsequences
|
Python Simple Solution
|
lokeshsenthilkumar
| 3
| 270
|
unique length 3 palindromic subsequences
| 1,930
| 0.515
|
Medium
| 27,277
|
https://leetcode.com/problems/unique-length-3-palindromic-subsequences/discuss/1346614/Python-O(N*26)
|
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
indices = {}
for i in range(len(s)):
if s[i] in indices:
indices[s[i]][1] = i
else:
indices[s[i]] = [i, i]
count = 0
#indices[char] denotes first and last occurrence of char in the given string
for c in indices:
if indices[c][0] == indices[c][1]:
#if the character occurs only once in the given string
pass
else:
tempAdded = set()
for i in range(indices[c][0]+1, indices[c][1], 1):
#counts the number of distinct middle character in the three lettered palindrome that could be formed with c at either ends
tempAdded.add(s[i])
count += len(tempAdded)
return count
|
unique-length-3-palindromic-subsequences
|
Python O(N*26)
|
srnarayanaa
| 2
| 223
|
unique length 3 palindromic subsequences
| 1,930
| 0.515
|
Medium
| 27,278
|
https://leetcode.com/problems/unique-length-3-palindromic-subsequences/discuss/2814249/Python-super-simple-10-lines
|
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
res = 0
for letter in set(s):
a, b = 0, len(s) - 1
#find first occurrence
while s[a] != letter:
a += 1
#find last occurrence
while s[b] != letter:
b -=1
if b>a:
res += len(set(s[a+1:b]))
return res
|
unique-length-3-palindromic-subsequences
|
Python super simple, 10 lines
|
__1
| 0
| 11
|
unique length 3 palindromic subsequences
| 1,930
| 0.515
|
Medium
| 27,279
|
https://leetcode.com/problems/unique-length-3-palindromic-subsequences/discuss/2384847/python-3-or-hash-map-and-hash-sets-or-O(n)O(1)
|
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
chars = {}
for i, c in enumerate(s):
if c not in chars:
chars[c] = [i + 1, -1]
else:
chars[c][1] = i
res = 0
for start, end in chars.values():
res += len({s[i] for i in range(start, end)})
return res
|
unique-length-3-palindromic-subsequences
|
python 3 | hash map and hash sets | O(n)/O(1)
|
dereky4
| 0
| 393
|
unique length 3 palindromic subsequences
| 1,930
| 0.515
|
Medium
| 27,280
|
https://leetcode.com/problems/unique-length-3-palindromic-subsequences/discuss/1338652/Python3-binary-search
|
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
locs = defaultdict(list)
for i, ch in enumerate(s): locs[ch].append(i)
ans = 0
for x in ascii_lowercase:
if len(locs[x]) > 1:
if len(locs[x]) > 2: ans += 1
for xx in ascii_lowercase:
if x != xx and bisect_left(locs[xx], locs[x][0]) != bisect_left(locs[xx], locs[x][-1]): ans += 1
return ans
|
unique-length-3-palindromic-subsequences
|
[Python3] binary search
|
ye15
| 0
| 76
|
unique length 3 palindromic subsequences
| 1,930
| 0.515
|
Medium
| 27,281
|
https://leetcode.com/problems/unique-length-3-palindromic-subsequences/discuss/1336941/Python3%3A-single-forward-pass-state-machines-O(26*n)
|
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
st = [[0]*26 for _ in range(26)]
for c in s:
n = ord(c)-ord('a')
for m in range(26):
if n==m:
st[n][n] += 1
continue
st[m][n] += st[m][n]&1
st[n][m] += 1-(st[n][m]&1)
return sum(sum(1 for v in row if v>=3) for row in st)
|
unique-length-3-palindromic-subsequences
|
Python3: single forward pass, state machines, O(26*n)
|
vsavkin
| 0
| 65
|
unique length 3 palindromic subsequences
| 1,930
| 0.515
|
Medium
| 27,282
|
https://leetcode.com/problems/unique-length-3-palindromic-subsequences/discuss/1331438/Python-3-binary-search-(156ms)
|
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
ans = set()
loc = defaultdict(list)
for i, x in enumerate(s):
loc[x].append(i)
ans = 0
for x in ascii_lowercase:
if len(loc[x]) < 2: continue
# triple same letters
if len(loc[x]) >= 3: ans += 1
# use longest range
l, r = loc[x][0], loc[x][-1]
for y in ascii_lowercase:
if x == y: continue
loc_l = bisect.bisect_left(loc[y], l)
loc_r = bisect.bisect_right(loc[y], r)
if loc_l != loc_r:
ans += 1
return ans
|
unique-length-3-palindromic-subsequences
|
[Python 3] binary search (156ms)
|
chestnut890123
| 0
| 67
|
unique length 3 palindromic subsequences
| 1,930
| 0.515
|
Medium
| 27,283
|
https://leetcode.com/problems/unique-length-3-palindromic-subsequences/discuss/1330229/Pythn3-Count-unique-characters-between-first-and-last-occurrence-of-each-alphabet
|
class Solution:
def countPalindromicSubsequence(self, s: str) -> int:
first = {}
last = {}
for i,c in enumerate(s):
if c not in first:
first[c] = i
last[c] = i
ans = 0
for c in "abcdefghijklmnopqrstuvwxyz":
if c not in last:
continue
uniq = set()
for i in range(first[c]+1,last[c]):
uniq.add(s[i])
ans += len(uniq)
return ans
|
unique-length-3-palindromic-subsequences
|
Pythn3 - Count unique characters between first and last occurrence of each alphabet
|
ampl3t1m3
| 0
| 92
|
unique length 3 palindromic subsequences
| 1,930
| 0.515
|
Medium
| 27,284
|
https://leetcode.com/problems/painting-a-grid-with-three-different-colors/discuss/1338695/Python3-top-down-dp
|
class Solution:
def colorTheGrid(self, m: int, n: int) -> int:
@cache
def fn(i, j, mask):
"""Return number of ways to color grid."""
if j == n: return 1
if i == m: return fn(0, j+1, mask)
ans = 0
for x in 1<<2*i, 1<<2*i+1, 0b11<<2*i:
mask0 = mask ^ x
if mask0 & 0b11<<2*i and (i == 0 or (mask0 >> 2*i) & 0b11 != (mask0 >> 2*i-2) & 0b11):
ans += fn(i+1, j, mask0)
return ans % 1_000_000_007
return fn(0, 0, 0)
|
painting-a-grid-with-three-different-colors
|
[Python3] top-down dp
|
ye15
| 1
| 263
|
painting a grid with three different colors
| 1,931
| 0.57
|
Hard
| 27,285
|
https://leetcode.com/problems/painting-a-grid-with-three-different-colors/discuss/1332318/Equivalent-to-finding-the-number-of-length-n-paths-in-a-graph
|
class Solution:
def colorTheGrid(self, m: int, n: int) -> int:
M = 10**9 + 7
ans = 3
if m == 1:
for i in range(n-1):
ans *= 2
ans %= M
return ans % M
def IsValid(a):
return all ( a[i] != a[i+1] for i in range(len(a) - 1) )
if m >= 2:
# AllStates contains all colorings of a single row of length m using 3 colors (0, 1, 2), hence of size 3^m
# For example, m = 2, AllStates = [(0,0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
AllStates = [[0], [1], [2]]
colors = [0, 1, 2]
for j in range(m-1):
L = len(AllStates)
for i in range(L):
x = AllStates.pop(0)
for c in colors:
AllStates.append(tuple([*x, c]))
# v contains all valid colorings of a single row of length m using 3 colors
v = set()
for a in AllStates:
if IsValid(a):
v.add(a)
d = {i:a for i, a in enumerate(v)}
# construct a graph where vertices are valid single row colorings
# an edge is present if two colorings satisfy the constraints
numStates = len(v)
def IsValidNeigbor(a, b):
if a not in v or b not in v:
return False
else:
for i in range(len(a)):
if a[i] == b[i]:
return False
return True
adj = {i:[] for i in range(numStates)}
for i in range(numStates):
for j in range(i+1, numStates):
if IsValidNeigbor(d[i], d[j]):
adj[i].append(j)
adj[j].append(i)
# then the problem becomes: finding the number of paths of length n in the constructed graph,
# which can be done by an easy-to-understand dp.
dp = [[0 for _ in range(n+1)] for _ in range(numStates)]
for i in range(numStates):
dp[i][1] = 1
for j in range(2, n+1):
for i in range(numStates):
for k in adj[i]:
dp[i][j] += dp[k][j-1]
dp[i][j] %= M
return sum(dp[i][n] for i in range(numStates)) % M
|
painting-a-grid-with-three-different-colors
|
Equivalent to finding the number of length-n paths in a graph
|
CommCode
| 0
| 145
|
painting a grid with three different colors
| 1,931
| 0.57
|
Hard
| 27,286
|
https://leetcode.com/problems/merge-bsts-to-create-single-bst/discuss/1410066/Python3-Recursive-tree-building-solution
|
class Solution:
def canMerge(self, trees: List[TreeNode]) -> TreeNode:
roots, leaves, loners, n = {}, {}, set(), len(trees)
if n == 1:
return trees[0]
for tree in trees:
if not tree.left and not tree.right:
loners.add(tree.val)
continue
roots[tree.val] = tree
for node in [tree.left, tree.right]:
if node:
if node.val in leaves:
return None
leaves[node.val] = node
for loner in loners:
if loner not in leaves and loner not in roots:
return None
orphan = None
for val, tree in roots.items():
if val not in leaves:
if orphan:
return None
orphan = tree
if not orphan:
return None
def build(node, small, big):
nonlocal roots
if not node:
return True
if small >= node.val or node.val >= big:
return False
if node.val in roots:
node.left, node.right = roots[node.val].left, roots[node.val].right
del roots[node.val]
return build(node.left, small, node.val) and build(node.right, node.val, big)
del roots[orphan.val]
result = build(orphan.left, -inf, orphan.val) and build(orphan.right, orphan.val, inf)
return orphan if result and not roots.keys() else None
|
merge-bsts-to-create-single-bst
|
Python3 Recursive tree building solution
|
yiseboge
| 0
| 134
|
merge bsts to create single bst
| 1,932
| 0.353
|
Hard
| 27,287
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1355349/Easy-Fast-Python-Solutions-(2-Approaches-28ms-32ms-Faster-than-93)
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
text = text.split()
length = len(text)
brokenLetters = set(brokenLetters)
for word in text:
for char in word:
if char in brokenLetters:
length -= 1
break
return length
|
maximum-number-of-words-you-can-type
|
Easy, Fast Python Solutions (2 Approaches - 28ms, 32ms; Faster than 93%)
|
the_sky_high
| 10
| 624
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,288
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1355349/Easy-Fast-Python-Solutions-(2-Approaches-28ms-32ms-Faster-than-93)
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
text = text.split()
length = len(text)
brokenLetters = list(brokenLetters)
for i in text:
temp = 0
for j in i:
if j in brokenLetters:
temp -= 1
break
if temp < 0:
length -= 1
return length
|
maximum-number-of-words-you-can-type
|
Easy, Fast Python Solutions (2 Approaches - 28ms, 32ms; Faster than 93%)
|
the_sky_high
| 10
| 624
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,289
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1860723/Python-45-ms-solution-good-for-beginners
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
words = text.split()
count = 0
flag = 0
for i in words:
for j in brokenLetters:
if j in i:
flag = 1
break
if flag == 0:
count += 1
flag = 0
return count
|
maximum-number-of-words-you-can-type
|
Python 45 ms solution, good for beginners
|
alishak1999
| 2
| 124
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,290
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1685655/Simple-python-Solution
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
s=[]
text=text.split()
for i in text:
for j in i:
if j in brokenLetters:
break
else:
s.append(i)
return len(s)
|
maximum-number-of-words-you-can-type
|
Simple python Solution
|
FaizanAhmed_
| 2
| 125
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,291
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/2208909/Python-1-Liner!!
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
return len([i for i in text.split(' ') if len(set(i).intersection(brokenLetters))==0])
|
maximum-number-of-words-you-can-type
|
Python 1-Liner!!
|
XRFXRF
| 1
| 75
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,292
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1507535/2-line-solutions-in-Python
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
text = text.split()
return len(text) - sum(any(c in w for c in brokenLetters) for w in text)
|
maximum-number-of-words-you-can-type
|
2-line solutions in Python
|
mousun224
| 1
| 142
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,293
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1507535/2-line-solutions-in-Python
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
bls = set(brokenLetters)
return sum(not set(w) & bls for w in text.split())
|
maximum-number-of-words-you-can-type
|
2-line solutions in Python
|
mousun224
| 1
| 142
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,294
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/2832369/python3
|
class Solution:
def canBeTypedWords(self, text: str, b: str) -> int:
output = 0
text = text.split()
brokenLetters = list(b)
for elem in text:
if any(v in brokenLetters for v in elem):
continue
else:
output += 1
return output
|
maximum-number-of-words-you-can-type
|
python3
|
Sneh713
| 0
| 2
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,295
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/2796126/Python3-Fast-One-Liner-with-set.intersection-(beats-89-time-95-memory)
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
return sum(not set(word).intersection(brokenLetters) for word in text.split(' '))
|
maximum-number-of-words-you-can-type
|
[Python3] Fast One-Liner with set.intersection (beats 89% time, 95% memory)
|
ivnvalex
| 0
| 1
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,296
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/2666078/Python-Solution-Without-Using-Split
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
brokenSet, numWords, voided = set(brokenLetters), 1, False
for char in text:
if char == ' ':
numWords += 1
voided = False
elif not voided and char in brokenSet:
numWords -= 1
voided = True
return numWords
|
maximum-number-of-words-you-can-type
|
Python Solution Without Using Split
|
kcstar
| 0
| 10
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,297
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/2659041/Python-easy-to-understand-solution-or-Sets
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
words = text.split(' ')
brokenLetters = set(brokenLetters)
count = len(words)
for word in words:
for letter in word:
if letter in brokenLetters:
count -= 1
break
return count
|
maximum-number-of-words-you-can-type
|
Python easy to understand solution | Sets
|
KevinJM17
| 0
| 3
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,298
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/2656047/Simple-and-Easy-to-Understand-or-Beginner's-Friendly-or-Python
|
class Solution(object):
def canBeTypedWords(self, text, brokenLetters):
flag, ans = True, 0
for ch in text:
if flag:
if ch in brokenLetters: flag = False
if ch == ' ': ans += 1
else:
if ch == ' ': flag = True
return ans + 1 if flag else ans
|
maximum-number-of-words-you-can-type
|
Simple and Easy to Understand | Beginner's Friendly | Python
|
its_krish_here
| 0
| 16
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,299
|
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.