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/task-scheduler-ii/discuss/2388794/Python3-O(n)-with-last-time-updated-and-simulation | class Solution:
def taskSchedulerII(self, tasks: List[int], space: int) -> int:
time = 0
n = len(tasks)
ht = {}
for i in range(n):
if ht.get(tasks[i]) is None or (time-ht[tasks[i]]-1)>=space:
time = time + 1
ht[tasks[i]] = time
... | task-scheduler-ii | [Python3] O(n) with last time updated and simulation | dntai | 0 | 3 | task scheduler ii | 2,365 | 0.462 | Medium | 32,400 |
https://leetcode.com/problems/task-scheduler-ii/discuss/2388467/Simple-Python-Solution-using-Dictionary | class Solution:
def taskSchedulerII(self, tasks: List[int], space: int) -> int:
ld={}
d=0
for t in tasks:
ld[t]=0
for t in tasks:
if ld[t]==0:
d+=1
ld[t]=d
else:
nd=ld[t]+space
ld[t]=m... | task-scheduler-ii | Simple Python Solution using Dictionary | shreyasjain0912 | 0 | 4 | task scheduler ii | 2,365 | 0.462 | Medium | 32,401 |
https://leetcode.com/problems/minimum-replacements-to-sort-the-array/discuss/2388550/Python-oror-One-reversed-pass-oror-Easy-Approaches | class Solution:
def minimumReplacement(self, nums: List[int]) -> int:
n = len(nums)
k = nums[n - 1]
ans = 0
for i in reversed(range(n - 1)):
if nums[i] > k:
l = nums[i] / k
if l == int(l):
ans += int(l... | minimum-replacements-to-sort-the-array | ✅Python || One reversed pass || Easy Approaches | chuhonghao01 | 2 | 84 | minimum replacements to sort the array | 2,366 | 0.399 | Hard | 32,402 |
https://leetcode.com/problems/minimum-replacements-to-sort-the-array/discuss/2409182/Python3-loop-backward | class Solution:
def minimumReplacement(self, nums: List[int]) -> int:
ans = 0
prev = 1_000_000_001
for x in reversed(nums):
d = ceil(x/prev)
ans += d-1
prev = x//d
return ans | minimum-replacements-to-sort-the-array | [Python3] loop backward | ye15 | 0 | 63 | minimum replacements to sort the array | 2,366 | 0.399 | Hard | 32,403 |
https://leetcode.com/problems/minimum-replacements-to-sort-the-array/discuss/2408985/python-3-or-simple-one-pass-solution-or-O(n)O(1) | class Solution:
def minimumReplacement(self, nums: List[int]) -> int:
res = 0
prev = nums[-1]
for i in range(len(nums) - 2, -1, -1):
if nums[i] <= prev:
prev = nums[i]
continue
q, r = divmod(nums[i], prev)
ops = q if r else ... | minimum-replacements-to-sort-the-array | python 3 | simple one pass solution | O(n)/O(1) | dereky4 | 0 | 49 | minimum replacements to sort the array | 2,366 | 0.399 | Hard | 32,404 |
https://leetcode.com/problems/minimum-replacements-to-sort-the-array/discuss/2394555/Python3-Optimal-Time-O(n)-Space-O(1) | class Solution:
def minimumReplacement(self, nums: List[int]) -> int:
ret = 0
i = len(nums) - 1
R = float('inf')
while i >= 0:
num_of_splits = (nums[i] - 1) // R
ret += num_of_splits
R = nums[i] // (num_of_splits + 1)
i -= 1
ret... | minimum-replacements-to-sort-the-array | [Python3] Optimal - Time O(n), Space O(1) | leet_aiml | 0 | 10 | minimum replacements to sort the array | 2,366 | 0.399 | Hard | 32,405 |
https://leetcode.com/problems/minimum-replacements-to-sort-the-array/discuss/2390349/Easy-optimization-problem-or-Google-Interview-Problem-or-Python3-or-division-with-remainder | class Solution:
def minimumReplacement(self, nums: List[int]) -> int:
ref = nums[-1]
n = len(nums)
res = 0
for i in range(n-2, -1, -1):
if nums[i] <= ref:
ref = nums[i]
continue
q, r = divmod(nums[i], ref)
... | minimum-replacements-to-sort-the-array | Easy optimization problem | Google Interview Problem? | Python3 | division with remainder | wxy0925 | 0 | 17 | minimum replacements to sort the array | 2,366 | 0.399 | Hard | 32,406 |
https://leetcode.com/problems/minimum-replacements-to-sort-the-array/discuss/2388953/Python-3Hint-solution | class Solution:
def minimumReplacement(self, nums: List[int]) -> int:
prev = nums[-1]
ans = 0
for num in nums[:-1][::-1]:
if num > prev:
# equally divide num to make part as large as possible
# prev = 6, num = 15
# then ... | minimum-replacements-to-sort-the-array | [Python 3]Hint solution | chestnut890123 | 0 | 19 | minimum replacements to sort the array | 2,366 | 0.399 | Hard | 32,407 |
https://leetcode.com/problems/minimum-replacements-to-sort-the-array/discuss/2388186/Python-easy-understanding-solution | class Solution:
def minimumReplacement(self, nums: List[int]) -> int:
if len(nums) == 1:
return 0
def helper(n, to_deal): # return (times needed to divide, biggest left-most num)
if n <= to_deal:
return (0, n)
if n % to_dea... | minimum-replacements-to-sort-the-array | Python easy-understanding solution | byroncharly3 | 0 | 16 | minimum replacements to sort the array | 2,366 | 0.399 | Hard | 32,408 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2395275/Python-oror-Easy-Approach | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
ans = 0
n = len(nums)
for i in range(n):
if nums[i] + diff in nums and nums[i] + 2 * diff in nums:
ans += 1
return ans | number-of-arithmetic-triplets | ✅Python || Easy Approach | chuhonghao01 | 19 | 1,200 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,409 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2393465/Python-easy-understand-solution-O(n)-space-O(n)-time | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
s = set(nums)
count = 0
for num in nums:
if (num + diff) in s and (num + diff + diff) in s:
count += 1
return count | number-of-arithmetic-triplets | Python easy understand solution O(n) space, O(n) time | amikai | 6 | 499 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,410 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2498311/python-95-fast-and-97-memory | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
dic = {} # store nums[i]
quest = {} # store require number you possible find after nums[i]
count = 0 # count answer
for i in nums:
dic[i] = True
if i in quest: count += 1 # meet ... | number-of-arithmetic-triplets | python 95% fast and 97% memory | TUL | 3 | 234 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,411 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2391706/Python-easy-solution-for-beginners-using-slightly-optimized-brute-force | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
res = 0
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[j] - nums[i] == diff:
for k in range(j+1, len(nums)):
if nums[k] - nums[... | number-of-arithmetic-triplets | Python easy solution for beginners using slightly optimized brute force | alishak1999 | 3 | 113 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,412 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2415108/Python-Elegant-and-Short-or-Two-solutions-or-O(n)-and-O(n*log(n))-or-Binary-search | class Solution:
"""
Time: O(n*log(n))
Memory: O(1)
"""
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
count = 0
left, right = 0, len(nums) - 1
for j, num in enumerate(nums):
if self.binary_search(nums, num - diff, left, j - 1) != -1 and \
self.binary_search(nums, num + diff, j ... | number-of-arithmetic-triplets | Python Elegant & Short | Two solutions | O(n) and O(n*log(n)) | Binary search | Kyrylo-Ktl | 2 | 138 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,413 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2390909/Secret-Python-Answer-Binary-Search-Right-and-Left | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
t = len(nums)
def binary_search(arr, low, high, x):
if high >= low:
mid = (high + low) // 2
if arr[mid] == x:
return mid
elif arr[mi... | number-of-arithmetic-triplets | [Secret Python Answer🤫🐍👌😍] Binary Search Right and Left | xmky | 2 | 141 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,414 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2587110/Python-or-Easy-or-O(n)-Solution | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
dict_ = {}
for num in nums:
dict_[num] = dict_.get(num,0) + 1 ##To keep the count of each num's occurence
count = 0
for num in nums:
if dict_.get(num+diff) and dict_.get(... | number-of-arithmetic-triplets | Python | Easy | O(n) Solution | anurag899 | 1 | 130 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,415 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2392027/Python-Simple-Python-Solution-Using-Bute-Force | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
check = []
for index1 in range(len(nums) - 2):
for index2 in range(index1 + 1, len(nums) - 1):
current_diffrence = nums[index2] - nums[index1]
if current_diffrence == diff:
check.append([index1, index2 , current_... | number-of-arithmetic-triplets | [ Python ] ✅✅ Simple Python Solution Using Bute Force 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 1 | 62 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,416 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2850945/Python-O(n)-solution-with-explanation | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
ans = 0
nums_to_pos = {num:index for index, num in enumerate(nums)}
for i in range(len(nums)):
a = nums[i]
b, c = a + diff, a + 2 * diff
if b in nums_to_pos and c in nums_to_p... | number-of-arithmetic-triplets | Python O(n) solution with explanation | besiobu | 0 | 2 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,417 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2849788/COMPACT-Python-solution | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
ans=0
for i in range(-1,-1*len(nums)-1,-1):
n1=nums[i]
n2=n1-diff
n3=n2-diff
if n2 in nums:
if n3 in nums:
ans+=1
... | number-of-arithmetic-triplets | [COMPACT] Python solution | hanifrizal | 0 | 1 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,418 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2844634/Clean-and-fast-one-liner | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
return sum(1 if {n + diff, n + 2*diff}.issubset(nums) else 0 for n in nums) | number-of-arithmetic-triplets | Clean and fast one-liner | user4248gf | 0 | 1 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,419 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2824582/PYTHON3-BEST | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
a = set()
b = 0
for i in nums:
if i - diff in a and i - diff * 2 in a:
b += 1
a.add(i)
return b | number-of-arithmetic-triplets | PYTHON3 BEST | Gurugubelli_Anil | 0 | 2 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,420 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2812553/Easiest-way | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
res=0
n=len(nums)
for i in range (n):
for j in range (i+1,n):
if nums[j]-nums[i]==diff:
for k in range (j+1,n):
if nums[k]-nums[j]==diff:
... | number-of-arithmetic-triplets | Easiest way | nishithakonuganti | 0 | 3 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,421 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2798365/simple-AP-concept-48ms | class Solution:
def arithmeticTriplets(self, nums, diff):
cnt = 0
for i in range(len(nums)):
if i < len(nums)-1 and (nums[i] + diff) in nums[i+1:] and (nums[i]-diff) in nums[:i]:
cnt += 1
return cnt | number-of-arithmetic-triplets | simple AP concept-48ms | alamwasim29 | 0 | 2 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,422 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2797253/Python-easy-code-solution. | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
l = len(nums)
count = 0
for i in range(l):
for j in range(i+1,l):
for k in range(j+1,l):
if(((nums[j] - nums[i]) == diff) and ((nums[k] - nums[j]) == diff)):
... | number-of-arithmetic-triplets | Python easy code solution. | anshu71 | 0 | 7 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,423 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2769661/Python-solution | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
ans = 0
n = len(nums)
for i in range(n):
for j in range(i+1, n):
for k in range(j+1, n):
if nums[j] - nums[i] == nums[k] - nums[j] == diff:
... | number-of-arithmetic-triplets | Python solution | kruzhilkin | 0 | 1 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,424 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2746875/EASY-PYTHON-SOLUTION-WITH-EXPLANATION | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
ans = 0
n = len(nums)
for i in range(n):
if nums[i] + diff in nums and nums[i] + 2 * diff in nums:
ans += 1
return ans | number-of-arithmetic-triplets | EASY PYTHON SOLUTION WITH EXPLANATION | sowmika_chaluvadi | 0 | 3 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,425 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2688015/Python3-Solution-one-line-solution | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
return sum([1 for i in nums if i-diff in nums and i+diff in nums]) | number-of-arithmetic-triplets | Python3 Solution - one-line solution | sipi09 | 0 | 4 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,426 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2653124/python-easy-solution-using-loop | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
c=0
for i in range(len(nums)):
o=nums[i]
for j in range(i,len(nums)):
if abs(nums[j]-o)==diff:
t=nums[j]
for k in range(j+1,len(nums)):
... | number-of-arithmetic-triplets | python easy solution using loop | lalli307 | 0 | 2 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,427 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2619104/Simple-code-easy-to-understand | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
c=0
for i in range(len(nums)):
a=nums[i]+diff
if a in nums:
b=a+diff
if b in nums:
c=c+1
return c | number-of-arithmetic-triplets | Simple code easy to understand | Prabal_Nair | 0 | 34 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,428 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2535471/Python-Two-Solutions-Linear-Time-or-Constant-Space | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
# make a set from numbers
numset = set()
result = 0
# check whether the other two numbers exist
for num in nums:
if (num-2*diff) in numset and (num-diff) in numset:
... | number-of-arithmetic-triplets | [Python] - Two Solutions - Linear Time or Constant Space | Lucew | 0 | 51 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,429 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2535471/Python-Two-Solutions-Linear-Time-or-Constant-Space | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
# get the array highest integer
result = 0
# we could do this using binary search
for index, num in enumerate(nums):
if binary_search(nums[index+1:], num+2*diff) and binary_... | number-of-arithmetic-triplets | [Python] - Two Solutions - Linear Time or Constant Space | Lucew | 0 | 51 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,430 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2530238/Python-easy-and-fast-solution | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
count = [0]*201
res = 0
for n in nums:
res += count[n-diff] and count[n-diff*2]
count[n] = True
return res | number-of-arithmetic-triplets | [Python] easy and fast solution | yhc22593 | 0 | 24 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,431 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2464065/Solution | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
ans = 0
for i in range(len(nums)):
num = 0
num = nums[i] + diff
if ((num in nums) and (num + diff in nums)):
ans += 1
return ans | number-of-arithmetic-triplets | Solution | fiqbal997 | 0 | 23 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,432 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2446767/The-simplest-solution-in-python | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
visited = set(nums)
res = 0
for n in nums:
if n + diff in visited and n + 2*diff in visited:
res += 1
return res | number-of-arithmetic-triplets | The simplest solution in python | byuns9334 | 0 | 38 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,433 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2446636/python-solution | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
c=0
for i in range(0,len(nums)-2):
for j in range(i+1,len(nums)-1):
for k in range(0,len(nums)):
if nums[j]-nums[i]==diff and nums[k] - nums[j] == diff:
c=c+1
return c | number-of-arithmetic-triplets | python solution | keertika27 | 0 | 26 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,434 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2402581/Python-brute-force-solution | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
ans = 0
ln = len(nums)
for i in range(ln):
for j in range(i+1, ln):
for k in range(j+1, ln):
if nums[j] - nums[i] == diff and nums[k] - nums[j] == diff:
... | number-of-arithmetic-triplets | Python brute force solution | StikS32 | 0 | 15 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,435 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2394452/Python-Faster-Solution-oror-Documented | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
# convert list into dictionary
dct = {}
for i, num in enumerate(nums):
dct[num] = i
# for each num, count when num+diff and num+diff+diff exist in dct
cnt = 0
for num in num... | number-of-arithmetic-triplets | [Python] Faster Solution || Documented | Buntynara | 0 | 6 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,436 |
https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/2390517/Python3-hash-set | class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
ans = 0
seen = set()
for x in nums:
if x-diff in seen and x-2*diff in seen: ans += 1
seen.add(x)
return ans | number-of-arithmetic-triplets | [Python3] hash set | ye15 | 0 | 31 | number of arithmetic triplets | 2,367 | 0.837 | Easy | 32,437 |
https://leetcode.com/problems/reachable-nodes-with-restrictions/discuss/2395280/Python-oror-Graph-oror-UnionFind-oror-Easy-Approach | class Solution:
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
restrictedSet = set(restricted)
uf = UnionFindSet(n)
for edge in edges:
if edge[0] in restrictedSet or edge[1] in restrictedSet:
continue
e... | reachable-nodes-with-restrictions | ✅Python || Graph || UnionFind || Easy Approach | chuhonghao01 | 3 | 152 | reachable nodes with restrictions | 2,368 | 0.574 | Medium | 32,438 |
https://leetcode.com/problems/reachable-nodes-with-restrictions/discuss/2402181/Python3%3A-BFT-Using-Sets.-Comments-and-Explanation. | class Solution:
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
#Convert restricted list into restricted set to increase processing speed
#Create a dictionary of non-restricted nodes
#Thus the key is the node and the value is the adjacency set for th... | reachable-nodes-with-restrictions | Python3: BFT Using Sets. Comments & Explanation. | tinyspidey | 1 | 54 | reachable nodes with restrictions | 2,368 | 0.574 | Medium | 32,439 |
https://leetcode.com/problems/reachable-nodes-with-restrictions/discuss/2390856/Secret-Python-Answer-DFS | class Solution:
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
e = defaultdict(set)
restricted = set(restricted)
for ed in edges:
if ed[1] not in restricted and ed[0] not in restricted:
e[ed[0]].add(ed[1])
... | reachable-nodes-with-restrictions | [Secret Python Answer🤫🐍👌😍] DFS | xmky | 1 | 46 | reachable nodes with restrictions | 2,368 | 0.574 | Medium | 32,440 |
https://leetcode.com/problems/reachable-nodes-with-restrictions/discuss/2390664/Python3-dfs | class Solution:
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
graph = [[] for _ in range(n)]
for u, v in edges:
graph[u].append(v)
graph[v].append(u)
ans = 0
seen = set(restricted)
stack = [0]
while s... | reachable-nodes-with-restrictions | [Python3] dfs | ye15 | 1 | 17 | reachable nodes with restrictions | 2,368 | 0.574 | Medium | 32,441 |
https://leetcode.com/problems/reachable-nodes-with-restrictions/discuss/2733693/python-non-recursive-DFS-(faster-than-93-of-python-solutions) | class Solution(object):
def reachableNodes(self, n, edges, restricted):
"""
:type n: int
:type edges: List[List[int]]
:type restricted: List[int]
:rtype: int
"""
restricted = set(restricted)
adj_list = {i: [] for i in range(n)}
for (u,... | reachable-nodes-with-restrictions | python non-recursive DFS (faster than 93% of python solutions) | juliejiang112 | 0 | 1 | reachable nodes with restrictions | 2,368 | 0.574 | Medium | 32,442 |
https://leetcode.com/problems/reachable-nodes-with-restrictions/discuss/2686644/Python3-or-Simple-DFS | class Solution:
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
def dfs(node):
nonlocal ans
for it in adj[node]:
if it in res:
continue
elif it not in vis:
ans+=1
... | reachable-nodes-with-restrictions | [Python3] | Simple DFS | swapnilsingh421 | 0 | 7 | reachable nodes with restrictions | 2,368 | 0.574 | Medium | 32,443 |
https://leetcode.com/problems/reachable-nodes-with-restrictions/discuss/2536108/Python-easy-to-read-and-understand-or-DFS | class Solution:
def dfs(self, g, node, visit, restricted):
# print(node)
self.cnt += 1
for nei in g[node]:
if nei not in visit and nei not in restricted:
visit.add(nei)
self.dfs(g, nei, visit, restricted)
def reachableNodes(self, n: int, edges... | reachable-nodes-with-restrictions | Python easy to read and understand | DFS | sanial2001 | 0 | 18 | reachable nodes with restrictions | 2,368 | 0.574 | Medium | 32,444 |
https://leetcode.com/problems/reachable-nodes-with-restrictions/discuss/2532131/python3-or-Clear-and-concise-dfs-AND-bfs-solutions | class Solution:
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
restricted = set(restricted)
graph = defaultdict(set)
visited = set()
for source, destination in edges:
graph[source].add(destination)
graph[destina... | reachable-nodes-with-restrictions | [python3] | Clear and concise dfs AND bfs solutions | _snake_case | 0 | 13 | reachable nodes with restrictions | 2,368 | 0.574 | Medium | 32,445 |
https://leetcode.com/problems/reachable-nodes-with-restrictions/discuss/2532131/python3-or-Clear-and-concise-dfs-AND-bfs-solutions | class Solution:
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
restricted = set(restricted)
graph = defaultdict(set)
visited = set()
reachable = 0
queue = collections.deque([0])
for source, destination in edges:
... | reachable-nodes-with-restrictions | [python3] | Clear and concise dfs AND bfs solutions | _snake_case | 0 | 13 | reachable nodes with restrictions | 2,368 | 0.574 | Medium | 32,446 |
https://leetcode.com/problems/reachable-nodes-with-restrictions/discuss/2418231/python-simple-dfs-solution | class Solution:
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
res = 1
restricted = set(restricted)
visited = {0}
graph = defaultdict(set)
for x, y in edges:
graph[x].add(y)
graph[y].add(x)
... | reachable-nodes-with-restrictions | python simple dfs solution | byuns9334 | 0 | 65 | reachable nodes with restrictions | 2,368 | 0.574 | Medium | 32,447 |
https://leetcode.com/problems/reachable-nodes-with-restrictions/discuss/2407639/Count-visited-with-sets | class Solution:
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
neighbors = defaultdict(set)
for a, b in edges:
neighbors[a].add(b)
neighbors[b].add(a)
set_restricted = set(restricted)
visited = {0}
frontier = n... | reachable-nodes-with-restrictions | Count visited with sets | EvgenySH | 0 | 21 | reachable nodes with restrictions | 2,368 | 0.574 | Medium | 32,448 |
https://leetcode.com/problems/reachable-nodes-with-restrictions/discuss/2394644/Python-Accurate-Solution-Adjacency-List-oror-Documented | class Solution:
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
# Generate Adjacency List representing graph
adjList = [[] for i in range(n)]
for a, b in edges:
adjList[a].append(b) # we can move from a to b
adjList[b].... | reachable-nodes-with-restrictions | [Python] Accurate Solution - Adjacency List || Documented | Buntynara | 0 | 11 | reachable nodes with restrictions | 2,368 | 0.574 | Medium | 32,449 |
https://leetcode.com/problems/reachable-nodes-with-restrictions/discuss/2394024/Python3-or-Easy-Fix-to-Avoid-TLE(Typecast-restricted-from-list-to-set) | class Solution:
#T.C = O(n + n + n-1), in worst case total number of vistable nodes from 0 is n-1, where
#only one node from 0...n-1th node is restricted! -> O(n)
#S.C = O(n*n-1 + n-1 + n-1) -> O(n^2)
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
a... | reachable-nodes-with-restrictions | Python3 | Easy Fix to Avoid TLE(Typecast restricted from list to set) | JOON1234 | 0 | 9 | reachable nodes with restrictions | 2,368 | 0.574 | Medium | 32,450 |
https://leetcode.com/problems/reachable-nodes-with-restrictions/discuss/2393925/Python3-or-Mad-that-my-approach-not-meeting-Time-Constraint | class Solution:
#I tried two approaches:
#1. Generic processing each and every edge in edges set!
#2. Performing generic bfs starting from node 0 with queue DS!
#3. Used a dfs helper function that will only visit every non-restricted and previously unvisited nodes!
#-> Both resulted in TLE?? -> What... | reachable-nodes-with-restrictions | Python3 | Mad that my approach not meeting Time Constraint | JOON1234 | 0 | 11 | reachable nodes with restrictions | 2,368 | 0.574 | Medium | 32,451 |
https://leetcode.com/problems/reachable-nodes-with-restrictions/discuss/2391308/Python-DFS | class Solution:
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
def dfs(node, prev):
self.result += 1
for neighbor in adj[node]:
if neighbor != prev and neighbor not in restricted:
dfs(neighbor, node)
... | reachable-nodes-with-restrictions | Python, DFS | blue_sky5 | 0 | 27 | reachable nodes with restrictions | 2,368 | 0.574 | Medium | 32,452 |
https://leetcode.com/problems/reachable-nodes-with-restrictions/discuss/2390452/Python3-Iterative-DFS | class Solution:
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
g = defaultdict(list)
restrict = set(restricted)
for u, v in edges:
g[u].append(v)
g[v].append(u)
seen = set()
stack = [0]
... | reachable-nodes-with-restrictions | [Python3] Iterative DFS | 0xRoxas | 0 | 25 | reachable nodes with restrictions | 2,368 | 0.574 | Medium | 32,453 |
https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/discuss/2390552/Python3-Iterative-DFS | class Solution:
def validPartition(self, nums: List[int]) -> bool:
idxs = defaultdict(list)
n = len(nums)
#Find all doubles
for idx in range(1, n):
if nums[idx] == nums[idx - 1]:
idxs[idx - 1].append(idx + 1)
#Find all tri... | check-if-there-is-a-valid-partition-for-the-array | [Python3] Iterative DFS | 0xRoxas | 2 | 106 | check if there is a valid partition for the array | 2,369 | 0.401 | Medium | 32,454 |
https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/discuss/2403935/Python3-state-machine-(accepted)-and-regular-expression-(TLE) | class Solution:
def validPartition(self, nums: List[int]) -> bool:
f3,f2,f1 = False,False,True
for i,v in enumerate(nums):
f = f2 and (v==nums[i-1])
f = f or f3 and (v==nums[i-1]==nums[i-2])
f = f or f3 and (v==nums[i-1]+1==nums[i-2]+2)
f3,f2,f1 = f2,f... | check-if-there-is-a-valid-partition-for-the-array | Python3 state machine (accepted) and regular expression (TLE) | vsavkin | 1 | 22 | check if there is a valid partition for the array | 2,369 | 0.401 | Medium | 32,455 |
https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/discuss/2813842/Python-(Simple-Dynamic-Programming) | class Solution:
def validPartition(self, nums):
n = len(nums)
dp = [False]*(n+1)
dp[0] = True
for i in range(2,n+1):
dp[i] |= nums[i-1] == nums[i-2] and dp[i-2]
dp[i] |= i>2 and nums[i-1] == nums[i-2] == nums[i-3] and dp[i-3]
dp[i] |= i>2 and num... | check-if-there-is-a-valid-partition-for-the-array | Python (Simple Dynamic Programming) | rnotappl | 0 | 1 | check if there is a valid partition for the array | 2,369 | 0.401 | Medium | 32,456 |
https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/discuss/2419691/Python3-O(n)-DP-Solution | class Solution:
def validPartition(self, nums: List[int]) -> bool:
grid = [0] * (len(nums) + 1)
grid[0] = 1
if nums[0] == nums[1]:
grid[2] = 1
for t in range(3, len(nums)+1):
if nums[t-1] == nums[t-2]:
grid[t] = (grid[t-2] or grid[t])
... | check-if-there-is-a-valid-partition-for-the-array | Python3 O(n) DP Solution | xxHRxx | 0 | 61 | check if there is a valid partition for the array | 2,369 | 0.401 | Medium | 32,457 |
https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/discuss/2391048/Python3-O(n)-Dynamic-Programming-1D | class Solution:
def validPartition(self, nums: List[int]) -> bool:
a = nums
dp = [False] * len(a)
for i in range(1, len(a)):
ret = False
if i-1>=0 and a[i]==a[i-1]:
ret = ret or (True if i-2<0 else dp[i-2])
if i-2>=0 and a[i]==a[i-1] and a... | check-if-there-is-a-valid-partition-for-the-array | [Python3] O(n) Dynamic Programming 1D | dntai | 0 | 14 | check if there is a valid partition for the array | 2,369 | 0.401 | Medium | 32,458 |
https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/discuss/2390606/Python3-dp | class Solution:
def validPartition(self, nums: List[int]) -> bool:
@cache
def fn(i):
if i == len(nums): return True
if i+1 < len(nums) and nums[i] == nums[i+1] and fn(i+2): return True
if i+2 < len(nums) and nums[i] == nums[i+1] == nums[i+2] and fn(i+3... | check-if-there-is-a-valid-partition-for-the-array | [Python3] dp | ye15 | 0 | 15 | check if there is a valid partition for the array | 2,369 | 0.401 | Medium | 32,459 |
https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/discuss/2390606/Python3-dp | class Solution:
def validPartition(self, nums: List[int]) -> bool:
dp = [False]*(len(nums)+1)
dp[-1] = True
for i in range(len(nums)-1, -1, -1):
if i+1 < len(nums) and dp[i+2] and nums[i] == nums[i+1] \
or i+2 < len(nums) and dp[i+3] and (nums[i] == nums[i+1] == num... | check-if-there-is-a-valid-partition-for-the-array | [Python3] dp | ye15 | 0 | 15 | check if there is a valid partition for the array | 2,369 | 0.401 | Medium | 32,460 |
https://leetcode.com/problems/longest-ideal-subsequence/discuss/2390471/DP | class Solution:
def longestIdealString(self, s: str, k: int) -> int:
dp = [0] * 26
for ch in s:
i = ord(ch) - ord("a")
dp[i] = 1 + max(dp[max(0, i - k) : min(26, i + k + 1)])
return max(dp) | longest-ideal-subsequence | DP | votrubac | 34 | 2,300 | longest ideal subsequence | 2,370 | 0.379 | Medium | 32,461 |
https://leetcode.com/problems/longest-ideal-subsequence/discuss/2489080/Python-3.-O(n)-Solution-DP | class Solution:
def longestIdealString(self, s: str, k: int) -> int:
# For storing the largest substring ending at that character
psum=[0]*26
ans=1
for i in range(len(s)):
element=ord(s[i])-97
# Checking for k characters left to current ... | longest-ideal-subsequence | Python 3. O(n) Solution - DP | Harsha138 | 1 | 76 | longest ideal subsequence | 2,370 | 0.379 | Medium | 32,462 |
https://leetcode.com/problems/longest-ideal-subsequence/discuss/2808439/Python3-DP-O(n)-time-beats-90-space-beats-96 | class Solution:
def longestIdealString(self, s: str, k: int) -> int:
lis = [0] * 26
for ch in s:
idx = ord(ch) - ord("a")
mi, ma = max(0, idx - k), min(25, idx + k)
last_lis = max(lis[mi: ma + 1])
lis[idx] = last_lis + 1
return max(lis) | longest-ideal-subsequence | [Python3] DP O(n), time beats 90%, space beats 96% | huangweijing | 0 | 3 | longest ideal subsequence | 2,370 | 0.379 | Medium | 32,463 |
https://leetcode.com/problems/longest-ideal-subsequence/discuss/2691172/Python3-O(n)-Dynamic-Programming-Solution | class Solution:
def longestIdealString(self, s: str, k: int) -> int:
grid = [0] * 26
data = [ord(_) - 97 for _ in s]
for element in data:
mini, maxi = max(0, element - k), min(25, element + k)
longest = 0
for t in range(mini, maxi+1):
... | longest-ideal-subsequence | Python3 O(n) Dynamic Programming Solution | xxHRxx | 0 | 5 | longest ideal subsequence | 2,370 | 0.379 | Medium | 32,464 |
https://leetcode.com/problems/longest-ideal-subsequence/discuss/2448962/Python-Solution-or-Memoization-or-Tabulation-or-DP-or-TLE | class Solution:
def longestIdealString(self, s: str, k: int) -> int:
n=len(s)
# Memoization
# dp=[[-1]*(n+1) for i in range(n+1)]
# def helper(ind, prev_ind):
# if ind==n:
# return 0
# if dp[ind][prev_ind]!=-1:
# return dp[ind][pre... | longest-ideal-subsequence | Python Solution | Memoization | Tabulation | DP | TLE | Siddharth_singh | 0 | 106 | longest ideal subsequence | 2,370 | 0.379 | Medium | 32,465 |
https://leetcode.com/problems/longest-ideal-subsequence/discuss/2424999/python3-Hashmap-solution-for-reference | class Solution:
def longestIdealString(self, s: str, k: int) -> int:
N = len(s)
c = "abcdefghijklmnopqrstuvwxyz"
zipped = zip(c, range(26))
h = defaultdict(int)
for a,v in zipped:
h[a] = v
O = [0]*len(c)
... | longest-ideal-subsequence | [python3] Hashmap solution for reference | vadhri_venkat | 0 | 51 | longest ideal subsequence | 2,370 | 0.379 | Medium | 32,466 |
https://leetcode.com/problems/longest-ideal-subsequence/discuss/2409282/Python3-dp | class Solution:
def longestIdealString(self, s: str, k: int) -> int:
dp = [0]*26
for ch in s:
i = ord(ch)-97
dp[i] = 1 + max(dp[max(0, i-k) : i+k+1], default=0)
return max(dp) | longest-ideal-subsequence | [Python3] dp | ye15 | 0 | 14 | longest ideal subsequence | 2,370 | 0.379 | Medium | 32,467 |
https://leetcode.com/problems/longest-ideal-subsequence/discuss/2393736/Python-3One-dimension-DP-O(n-*-(2-*-k-%2B-1)) | class Solution:
def longestIdealString(self, s: str, k: int) -> int:
n = len(s)
dp = [0] * 26
for i in range(n):
loc = ord(s[i]) - ord('a')
new_dp = dp[:]
for j in range(loc - k, loc + k + 1):
new_dp[loc] = max(new_dp[loc], 1 + dp[j % 26])
... | longest-ideal-subsequence | [Python 3]One-dimension DP O(n * (2 * k + 1)) | chestnut890123 | 0 | 16 | longest ideal subsequence | 2,370 | 0.379 | Medium | 32,468 |
https://leetcode.com/problems/longest-ideal-subsequence/discuss/2390838/Secret-Python-AnswerHashmap-to-Remember-the-current-max-set-size-for-each-letter-seen | class Solution:
def longestIdealString(self, s: str, k: int) -> int:
s = [ord(c) - ord('a') for c in s]
m = 0
hm = {}
for i,v in enumerate(s) :
m = 1
for h in hm:
if abs(v - h) <= k:
... | longest-ideal-subsequence | [Secret Python Answer🤫🐍👌😍]Hashmap to Remember the current max set size for each letter seen | xmky | 0 | 26 | longest ideal subsequence | 2,370 | 0.379 | Medium | 32,469 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2422191/Python3-simulation | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
n = len(grid)
ans = [[0]*(n-2) for _ in range(n-2)]
for i in range(n-2):
for j in range(n-2):
ans[i][j] = max(grid[ii][jj] for ii in range(i, i+3) for jj in range(j, j+3))
... | largest-local-values-in-a-matrix | [Python3] simulation | ye15 | 14 | 1,200 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,470 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2422317/Python-oror-Easy-Approach-oror-Brute-force | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
n = len(grid)
ans = []
for i in range(n - 2):
res = []
for j in range(n - 2):
k = []
k.append(grid[i][j])
k.append(grid[i][j + 1])
... | largest-local-values-in-a-matrix | ✅Python || Easy Approach || Brute force | chuhonghao01 | 9 | 803 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,471 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2428294/Python-Elegant-and-Short-or-100-faster | class Solution:
"""
Time: O(n^2)
Memory: O(1)
"""
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
n = len(grid)
return [[self.local_max(grid, r, c, 1) for c in range(1, n - 1)] for r in range(1, n - 1)]
@staticmethod
def local_max(grid: List[List[int]], row: int, col: int, radius: int) ... | largest-local-values-in-a-matrix | Python Elegant & Short | 100% faster | Kyrylo-Ktl | 6 | 698 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,472 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2422186/Python-Two-loop-solution | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
n = len(grid)
matrix = [[1]* (n-2) for i in range(n-2)]
for i in range(1, n - 1):
for j in range(1, n - 1):
matrix[i-1][j-1] = max(grid[i-1][j-1], grid[i-1][j], grid[i-1][j+1],
... | largest-local-values-in-a-matrix | ✅ [Python] Two loop solution | amikai | 6 | 370 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,473 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2425602/Python3-oror-6-lines-chain-oror-TM%3A-145-ms14.2-MB | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
n = len(grid)-2
ans = [[0]*n for _ in range(n)]
for i in range(n):
for j in range(n):
ans[i][j] = max(chain(grid[i ][j:j+3],
grid[i+1][j:j+... | largest-local-values-in-a-matrix | Python3 || 6 lines, chain || T/M: 145 ms/14.2 MB | warrenruud | 3 | 211 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,474 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2824565/PYTHON3-BEST | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
n = len(grid)
ans = [[0]*(n-2) for _ in range(n-2)]
for i in range(n-2):
for j in range(n-2):
ans[i][j] = max(grid[ii][jj] for ii in range(i, i+3) for jj in range(j, j+3))
... | largest-local-values-in-a-matrix | PYTHON3 BEST | Gurugubelli_Anil | 1 | 12 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,475 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2814349/Python-or-BRUTAL-FORCE-w-thoughts-on-better-implementation | class Solution:
def largestLocal(self, grid: List[List[int]]):
'''
THOUGHTS THAT CAME LATER:
Ultimately the amount of comparisions can be massively
Reduced. If you've just looked at a 3x3 section and then
Only increment over by one, then you're readdressing a
2x3 sec... | largest-local-values-in-a-matrix | Python | BRUTAL FORCE w/ thoughts on better implementation | jessewalker2010 | 0 | 5 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,476 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2801766/Python-multiple-loops | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
n = len(grid)
x = n-2
res = []
answer = []
i_ = 0
j_ = 0
while i_ < x:
while j_ < x:
max_ = 0
for i in range(i_, i_+3):
... | largest-local-values-in-a-matrix | Python multiple loops | mani-cash | 0 | 4 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,477 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2784835/python-image-deep-learning | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
rows, cols = len(grid), len(grid[0])
res = [[0] * (cols - 2) for i in range(rows - 2)]
for i in range(rows - 2):
for j in range(cols - 2):
local = float('-inf')
for i... | largest-local-values-in-a-matrix | python image deep learning | JasonDecode | 0 | 2 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,478 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2743683/Python3-one-liner | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
return [[max(grid[i][j] for j in range(c -1, c + 2) for i in range(r -1, r + 2)) for c in range(1, len(grid) - 1)] for r in range(1, len(grid) - 1)] | largest-local-values-in-a-matrix | [Python3] one-liner | avishaitsur | 0 | 6 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,479 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2741410/PYTHON | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
N = len(grid)
ans = [[0] * (N-2) for _ in range(N-2)]
for i in range(N-2):
for j in range(N-2):
for di in range(3):
for dj in range(3):
... | largest-local-values-in-a-matrix | PYTHON🐍 | shubhamdraj | 0 | 11 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,480 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2713759/Python-or-Original-Solution-or-Easy-to-understand | class Solution:
def largestLocal(self, grid: list[list[int]]) -> list[list[int]]:
maxVal = 0
row = []
col = []
n = len(grid[0])
endRow = 3
endCol = 3
startRow = 0
startCol = 0
# [[9,9],[8,6]]
while True:
for i in range(s... | largest-local-values-in-a-matrix | Python | Original Solution | Easy to understand | flufe | 0 | 8 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,481 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2706112/Python-Simple-brute-force-solution | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
res = []
for r in range(0, len(grid) - 2):
mx = []
for c in range(0, len(grid[0]) - 2):
v1 = max(grid[r][c], grid[r][c + 1], grid[r][c + 2])
v2 = max(grid[r + 1][... | largest-local-values-in-a-matrix | [Python] Simple brute force solution | casshsu | 0 | 9 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,482 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2678057/Python3-Solution | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
size = len(grid)
ans = []
i = 0
while i + 3 <= size:
j = 0
a = []
while j + 3 <= size:
a.append(max([max(g[j:j+3]) for g in grid[i:i+3]]))
... | largest-local-values-in-a-matrix | Python3 Solution | sipi09 | 0 | 5 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,483 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2669689/python-solution-easy | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
m = len(grid)
n = len(grid)
def findMax(x, y):
print(x, y)
maxi = -1
for i in range(3):
maxi = max(maxi, max(grid[x + i][y:3 + y]))
return maxi
... | largest-local-values-in-a-matrix | python solution easy | MaryLuz | 0 | 3 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,484 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2652201/Python3-One-Liner-With-List-Comprehension | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
return [[max([grid[i+o1][j+o2] for o1 in range(-1,2) for o2 in range(-1, 2)]) for j in range(1,len(grid)-1)] for i in range(1,len(grid)-1)] | largest-local-values-in-a-matrix | Python3 One Liner With List Comprehension | godshiva | 0 | 7 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,485 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2587521/Primitive-solution-or-Python-3 | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
# print(grid[0]) -> [9,9,8,1]
# print(grid[0][0]) -> 9
# print(grid[3][3]) -> 2
# print(len(grid)) -> 4
# print(max(grid[0])) -> 9
newgrid = []
... | largest-local-values-in-a-matrix | Primitive solution | Python 3 | aglona | 0 | 37 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,486 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2513130/Python-3-greater-Elegant-way-that-avoids-repetition-as-much-as-possible | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
result= []
for row in range(len(grid)-2):
self.countMap = collections.defaultdict(int)
temp = []
for col in range(len(grid[0])):
self.addColumn(grid, row, co... | largest-local-values-in-a-matrix | Python 3 -> Elegant way that avoids repetition as much as possible | mybuddy29 | 0 | 29 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,487 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2472350/Python3-or-with-assert | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
length = len(grid) - 2
res_matrix = [[0 for x in range(length)] for x in range(length)]
for i in range(length):
for j in range(length):
max_line_matrix_3x3 = [max(grid[x][j:j+3]) for... | largest-local-values-in-a-matrix | Python3 | with assert | Sergei_Gusev | 0 | 26 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,488 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2445224/Largest-Local-Values-in-a-Matrix | class Solution:
def maximum(self ,arr):
m = 0
for i in arr:
for j in i:
if j>m:
m = j
return m
def largestLocal(self, arr: List[List[int]]) -> List[List[int]]:
n = len(arr)
out = [["#" for i in range(n-2)] for i in range(n-... | largest-local-values-in-a-matrix | Largest Local Values in a Matrix | dhananjayaduttmishra | 0 | 34 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,489 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2425971/Python-Accurate-and-Faster-Solution | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
localN = len(grid)-2
mat = [[0]*localN for _ in range(localN)]
for ii in range(localN):
for jj in range(localN):
mat[ii][jj] = max([grid[i][j] for i in range(ii, ii+3) for j in rang... | largest-local-values-in-a-matrix | [Python] Accurate and Faster Solution | Buntynara | 0 | 13 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,490 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2425956/O(n)-one-liner | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
return [[max([max([grid[mrow][mcol] for mcol in range(col - 1, col + 2)]) for mrow in range(row - 1, row + 2)])
for col in range(1, len(grid[0]) - 1)] for row in range(1, len(grid) - 1)] | largest-local-values-in-a-matrix | O(n) one-liner | ahmedyarub | 0 | 30 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,491 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2425318/Commented-out-python-solution-100-fast | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
#make an empty 2d arrray to fill in later
ret = []
for i in range(len(grid) - 2):
ret.append([])
#for each matrix row in our return matrix...
for i in range(len(ret))... | largest-local-values-in-a-matrix | Commented out python solution, 100% fast | igorkaluga | 0 | 21 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,492 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2425173/List-comprehension-75-speed | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
n1 = len(grid) - 1
return [[max(grid[i][j] for i in range(r - 1, r + 2)
for j in range(c - 1, c + 2))
for c in range(1, n1)] for r in range(1, n1)] | largest-local-values-in-a-matrix | List comprehension, 75% speed | EvgenySH | 0 | 14 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,493 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2422487/Secret-Python-Answer-9-Lines-O(n2) | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
n = len(grid)
res = [[0 for i in range(n-2)] for j in range(n-2)]
for i in range(1,n-1):
for j in range(1,n-1):
m = 0
... | largest-local-values-in-a-matrix | [Secret Python Answer🤫🐍👌😍] 9 Lines O(n^2) | xmky | 0 | 33 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,494 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2422458/Python3-Monotonic-Queue | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
n = len(grid)
res = [[0] * (n - 2) for _ in range(n - 2)]
for i in range(n - 2):
queue = collections.deque()
num0 = max(grid[i][0], grid[i + 1][0], grid[i + 2][0])
n... | largest-local-values-in-a-matrix | [Python3] Monotonic Queue | celestez | 0 | 26 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,495 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2422359/python3-straightforward-naive-way | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
n = len(grid)
dir8 = [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
output = []
# iterate over grid but not the stuff on the edges
for i in range(1, n - 1):
row ... | largest-local-values-in-a-matrix | python3 straightforward naive way | Pinfel | 0 | 10 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,496 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2422321/Python-3-Simple-solution | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
n = len(grid)
ret = [[None] * (n - 2) for _ in range(n - 2)]
for i in range(1, n-1):
for j in range(1, n-1):
ret[i - 1][j - 1] = max(grid[i-1][j-1],
... | largest-local-values-in-a-matrix | [Python 3] Simple solution | leet_aiml | 0 | 34 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,497 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2422318/Python-or-Brute-Force | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
n = len(grid)
m = len(grid[0])
ans = [[0] * (n-2) for i in range(n-2)]
for p in range(n-2):
for q in range(n-2):
maxi = max(grid[p+1][q+1], grid[p+... | largest-local-values-in-a-matrix | Python | Brute Force | LittleMonster23 | 0 | 17 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,498 |
https://leetcode.com/problems/largest-local-values-in-a-matrix/discuss/2422293/Python-Simple-Python-Solution-Using-Brute-Force-or-100-Faster | class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
result = [[0 for _ in range(len(grid)-2)] for _ in range(len(grid)-2)]
def get_max_value(r,c):
max_value = max( grid[r][c : c + 3] + grid[r + 1][ c : c + 3] + grid[r + 2][c : c + 3])
return max_value
for row in range(le... | largest-local-values-in-a-matrix | [ Python ] ✅✅ Simple Python Solution Using Brute Force | 100 % Faster 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 59 | largest local values in a matrix | 2,373 | 0.839 | Easy | 32,499 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.