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/minimum-cost-homecoming-of-a-robot-in-a-grid/discuss/2714476/Python-O(N-%2B-M)-O(1) | class Solution:
def minCost(self, startPos: List[int], homePos: List[int], rowCosts: List[int], colCosts: List[int]) -> int:
row_cost = sum(rowCosts[min(startPos[0], homePos[0]):max(startPos[0], homePos[0]) + 1]) - rowCosts[startPos[0]]
col_cost = sum(colCosts[min(startPos[1], homePos[1]):max(startPos[1], homePos[1]) + 1]) - colCosts[startPos[1]]
return row_cost + col_cost | minimum-cost-homecoming-of-a-robot-in-a-grid | Python - O(N + M), O(1) | Teecha13 | 0 | 3 | minimum cost homecoming of a robot in a grid | 2,087 | 0.513 | Medium | 28,800 |
https://leetcode.com/problems/minimum-cost-homecoming-of-a-robot-in-a-grid/discuss/1599187/Python-with-explanation-O(n)-time-O(1)-space | class Solution:
def minCost(self, startPos: List[int], homePos: List[int], rowCosts: List[int], colCosts: List[int]) -> int:
rm=0
lm=0
rm=startPos[0]-homePos[0]# number of ups or downs
lm=startPos[1]-homePos[1] # number of lefts or rights
ans=0
if rm<0:#we need to down
a=startPos[0]
while rm!=0:
rm+=1
ans+=rowCosts[a+1]#add costs according to the row numbers
a+=1
elif rm>0:#up
a=startPos[0]
while rm!=0:
rm-=1
ans+=rowCosts[a-1]
a-=1
if lm<0:#left
a=startPos[1]
while lm!=0:
lm+=1
ans+=colCosts[a+1]
a+=1
elif lm>0:#right
a=startPos[1]
while lm!=0:
lm-=1
ans+=colCosts[a-1]
a-=1
return ans | minimum-cost-homecoming-of-a-robot-in-a-grid | Python with explanation O(n) time O(1) space | nmk0462 | 0 | 70 | minimum cost homecoming of a robot in a grid | 2,087 | 0.513 | Medium | 28,801 |
https://leetcode.com/problems/count-fertile-pyramids-in-a-land/discuss/1598873/Python3-just-count | class Solution:
def countPyramids(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
vals = [[inf]*n for _ in range(m)]
for i in range(m):
for j in range(n):
if grid[i][j] == 0: vals[i][j] = 0
elif j == 0: vals[i][j] = 1
else: vals[i][j] = min(vals[i][j], 1 + vals[i][j-1])
if grid[i][~j] == 0: vals[i][~j] = 0
elif j == 0: vals[i][~j] = 1
else: vals[i][~j] = min(vals[i][~j], 1 + vals[i][~j+1])
def fn(vals):
"""Return count of pyramid in given grid."""
ans = 0
for j in range(n):
width = 0
for i in range(m):
if vals[i][j]: width = min(width+1, vals[i][j])
else: width = 0
ans += max(0, width-1)
return ans
return fn(vals) + fn(vals[::-1]) | count-fertile-pyramids-in-a-land | [Python3] just count | ye15 | 5 | 415 | count fertile pyramids in a land | 2,088 | 0.635 | Hard | 28,802 |
https://leetcode.com/problems/count-fertile-pyramids-in-a-land/discuss/1599023/Python-dp-solution | class Solution:
def countPyramids(self, grid: List[List[int]]) -> int:
#checks if there are 3 ones below (x,y)
def check(x,y):
count = 0
to_check = {(1,0),(1,-1),(1,1)}
for dx,dy in to_check:
if(0<=x+dx<len(grid) and 0<=y+dy<len(grid[0]) and grid[x+dx][y+dy]==1):
count += 1
if(count == 3):
return True
else:
return False
memo = {}
#dp returns the number of pyramid levels under (x,y)
def dp(x,y):
if((x,y) in memo):
return memo[(x,y)]
levels = 0
if(check(x,y)==True):
levels += 1
else:
return levels
to_check = {(1,0),(1,-1),(1,1)}
t = float('inf')
#t is the number of additional levels
for dx,dy in to_check:
if(0<=x+dx<len(grid) and 0<=y+dy<len(grid[0]) and grid[x+dx][y+dy]==1):
t = min(t,dp(x+dx,y+dy))
memo[(x,y)] = levels + t
return levels + t
#check number of normal pyramidal plots
ans = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if(grid[i][j] == 1):
ans += dp(i,j)
#check number of inverse pyramidal plots
memo = {}
grid = grid[::-1]
for i in range(len(grid)):
for j in range(len(grid[0])):
if(grid[i][j] == 1):
ans += dp(i,j)
return ans | count-fertile-pyramids-in-a-land | Python dp solution | aakashc | 1 | 64 | count fertile pyramids in a land | 2,088 | 0.635 | Hard | 28,803 |
https://leetcode.com/problems/count-fertile-pyramids-in-a-land/discuss/2609252/Python3-or-DP | class Solution:
def countPyramids(self, grid: List[List[int]]) -> int:
f_ans=0
def countPyramids(grid):
r,c=len(grid),len(grid[0])
dp=copy.deepcopy(grid)
ans=0
for i in range(r-2,-1,-1):
for j in range(1,c-1):
if dp[i][j]:
dp[i][j]=min(dp[i+1][j-1],dp[i+1][j],dp[i+1][j+1])+1
ans+=dp[i][j]-1
return ans
f_ans+=countPyramids(grid)
f_ans+=countPyramids(grid[::-1])
return f_ans | count-fertile-pyramids-in-a-land | [Python3] | DP | swapnilsingh421 | 0 | 5 | count fertile pyramids in a land | 2,088 | 0.635 | Hard | 28,804 |
https://leetcode.com/problems/count-fertile-pyramids-in-a-land/discuss/2490226/Python3-solution%3A-faster-than-97.2-of-other-submissions-oror-Easy-to-understand | class Solution:
def countPyramids(self, grid):
# dp[i][j] represents the number of layers of the largest pyramid with (i, j) as the vertex.
# Note that the 1-level pyramid is invalid in the problem, so it should be removed when summing.
# Note that if grid[i][j] is 0, dp[i][j] will always be 0.
# The dp recurrence formula is dp[i][j] = min(dp[i + 1][j - 1], dp[i + 1][j + 1]) + 1
m, n, dp, cnt = len(grid), len(grid[0]), copy.deepcopy(grid), 0
# triangle
for i in range(m - 2, -1, -1):
for j in range(1, n - 1):
if dp[i][j] > 0 and dp[i + 1][j] > 0:
dp[i][j] = min(dp[i + 1][j - 1], dp[i + 1][j + 1]) + 1
cnt += dp[i][j] - 1
# inverted triangle
dp = grid
for i in range(1, m):
for j in range(1, n - 1):
if dp[i][j] > 0 and dp[i - 1][j] > 0:
dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j + 1]) + 1
cnt += dp[i][j] - 1
return cnt | count-fertile-pyramids-in-a-land | βοΈ Python3 solution: faster than 97.2% of other submissions || Easy to understand | explusar | 0 | 21 | count fertile pyramids in a land | 2,088 | 0.635 | Hard | 28,805 |
https://leetcode.com/problems/count-fertile-pyramids-in-a-land/discuss/2351936/Python3-or-DP-or-Memoization-or-O(m*n) | class Solution:
def countPyramids(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
@cache
def countDown(i, j):
nonlocal m, n
if i + 1 < m and j - 1 > -1 and j + 1 < n and grid[i][j] == 1:
left = countDown(i+1, j-1)
bottom = countDown(i+1, j)
right = countDown(i+1, j+1)
return 1 + min(left, bottom, right)
else:
return grid[i][j]
@cache
def countUp(i, j):
nonlocal m, n
if i - 1 > -1 and j - 1 > -1 and j + 1 < n and grid[i][j] == 1:
left = countUp(i-1, j-1)
top = countUp(i-1, j)
right = countUp(i-1, j+1)
return 1 + min(left, top, right)
else:
return grid[i][j]
ans = 0
for i in range(m):
for j in range(n):
if countDown(i, j) != 0:
ans += countDown(i, j) - 1
if countUp(i, j) != 0:
ans += countUp(i, j) - 1
return ans | count-fertile-pyramids-in-a-land | Python3 | DP | Memoization | O(m*n) | DheerajGadwala | 0 | 14 | count fertile pyramids in a land | 2,088 | 0.635 | Hard | 28,806 |
https://leetcode.com/problems/count-fertile-pyramids-in-a-land/discuss/1655710/Python-oror-2-Solutions-oror-Prefix-Sum-oror-DP | class Solution:
def countPyramids(self, grid: List[List[int]]) -> int:
m,n=len(grid),len(grid[0])
def solve(arr):
left,right=[[0]*n for _ in range(m)],[[0]*n for _ in range(m)]
ans=0
for i in range(m):
for j in range(1,n):
if arr[i][j]==1:
left[i][j]=left[i][j-1]+(1 if arr[i][j-1]==1 else 0)
for j in range(n-2,-1,-1):
if arr[i][j]==1:
right[i][j]=right[i][j+1]+(1 if arr[i][j+1]==1 else 0)
valid=[[0]*n for _ in range(m)]
for i in range(1,m):
for j in range(1,n):
dist=min(left[i][j],right[i][j])
order=valid[i-1][j]
if arr[i][j]==1:
if arr[i-1][j] and order<=dist-1:
ans+=order+1
valid[i][j]=order+1
elif arr[i-1][j] and dist:
ans+=dist
valid[i][j]=dist
return ans
new_grid=[[0]*n for _ in range(m)]
for i in range(m):
for j in range(n):
new_grid[i][j]=grid[m-i-1][n-j-1]
return solve(grid)+solve(new_grid) | count-fertile-pyramids-in-a-land | Python || 2 Solutions || Prefix-Sum || DP | ketan_raut | 0 | 67 | count fertile pyramids in a land | 2,088 | 0.635 | Hard | 28,807 |
https://leetcode.com/problems/count-fertile-pyramids-in-a-land/discuss/1655710/Python-oror-2-Solutions-oror-Prefix-Sum-oror-DP | class Solution:
def countPyramids(self, grid: List[List[int]]) -> int:
m,n=len(grid),len(grid[0])
def solve(arr):
dp=arr
ans=0
for i in range(1,m):
for j in range(1,n-1):
if dp[i][j]:
dp[i][j]+=min(dp[i-1][j],dp[i-1][j-1],dp[i-1][j+1])
ans+=dp[i][j]-1
return ans
new_grid=[[0]*n for _ in range(m)]
for i in range(m):
for j in range(n):
new_grid[i][j]=grid[m-i-1][n-j-1]
return solve(grid)+solve(new_grid) | count-fertile-pyramids-in-a-land | Python || 2 Solutions || Prefix-Sum || DP | ketan_raut | 0 | 67 | count fertile pyramids in a land | 2,088 | 0.635 | Hard | 28,808 |
https://leetcode.com/problems/count-fertile-pyramids-in-a-land/discuss/1599545/Python3%3A-Reduces-to-finding-an-increasing-sequence-in-columns | class Solution:
def countPyramids(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
dp = [[0]*n for _ in range(m)]
# count "1" from left to right
for r in range(m):
for c in range(n):
if grid[r][c] == 0: continue
if c == 0:
dp[r][c] = grid[r][c]
else:
dp[r][c] += dp[r][c - 1] + 1
# count "1" from right to left and take min(left, right) in one loop
for c in range(n-1, -1, -1):
if grid[r][c] == 0: continue
if c == n - 1:
dp[r][c] = min(dp[r][c], grid[r][c])
else:
dp[r][c] = min(dp[r][c], dp[r][c + 1] + 1)
#print(dp)
ans = 0
for r in range(m):
for c in range(n):
if dp[r][c] == 0:
continue
k = 0
while (r + k) < m and dp[r + k][c] >= (k + 1): k += 1
ans += (k-1)
#print(ans)
k = 0
while (r - k) >= 0 and dp[r - k][c] >= k + 1: k += 1
ans += (k-1)
return ans | count-fertile-pyramids-in-a-land | Python3: Reduces to finding an increasing sequence in columns | abuOmar2 | 0 | 20 | count fertile pyramids in a land | 2,088 | 0.635 | Hard | 28,809 |
https://leetcode.com/problems/count-fertile-pyramids-in-a-land/discuss/1599532/python3-recursion-one-testcase-run-into-runtime-limit-though | class Solution(object):
ans = 0
def countPyramids(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
def isPyramidApex(g,i,j,c):
if (j - c) < 0:
return
try:
for x in range(j-c, j+c+1):
if g[i+1][x] != 1:
return
self.ans += 1
isPyramidApex(g, i+1, j, c+1)
except Exception as e:
return
def getPyrimadsCount(g):
n = len(g)
m = len(g[0])
for i in range(n):
for j in range(m):
# each point is a potential pyrmiad apex if it holds value of 1
if g[i][j]:
isPyramidApex(g, i,j, 1)
return
getPyrimadsCount(grid)
# flip array on X then Y direction and tally pyrimads
getPyrimadsCount([list(reversed(x)) for x in reversed(grid)])
return self.ans | count-fertile-pyramids-in-a-land | python3, recursion, one testcase run into runtime limit though | mhd | 0 | 13 | count fertile pyramids in a land | 2,088 | 0.635 | Hard | 28,810 |
https://leetcode.com/problems/count-fertile-pyramids-in-a-land/discuss/1599149/Python3-DP-or-O(MN)-or-100-or-100 | class Solution:
def countPyramids(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
D = [[-1] * n for _ in range(2)] # D[i][j] = the number of pyramids where apex is (i, j)
DI = [[-1] * n for _ in range(2)] # for inverse
ans = 0
turn = 0
for j in range(n):
D[0][j] = 0 if grid[m - 1][j] else -1
DI[0][j] = 0 if grid[0][j] else -1
for i in range(m - 2, -1, -1):
for j in range(n):
if grid[i][j] == 0:
D[not turn][j] = -1
else:
if j == 0 or j == n - 1: D[not turn][j] = 0
else: D[not turn][j] = min(D[turn][j - 1], D[turn][j], D[turn][j + 1]) + 1
if grid[m - i - 1][j] == 0:
DI[not turn][j] = -1
else:
if j == 0 or j == n - 1: DI[not turn][j] = 0
else: DI[not turn][j] = min(DI[turn][j - 1], DI[turn][j], DI[turn][j + 1]) + 1
ans += max(D[not turn][j], 0) + max(DI[not turn][j], 0)
turn = not turn
return ans | count-fertile-pyramids-in-a-land | [Python3] DP | O(MN) | 100% | 100% | hooneken | 0 | 21 | count fertile pyramids in a land | 2,088 | 0.635 | Hard | 28,811 |
https://leetcode.com/problems/count-fertile-pyramids-in-a-land/discuss/1599022/Python3-Short-DP | class Solution:
def countPyramids(self, grid: List[List[int]]) -> int:
m, n, res = len(grid), len(grid[0]), 0
dp = [row[:] for row in grid]
for i, j in product(range(m-2,-1,-1), range(1, n-1)):
if grid[i][j]:
dp[i][j] = min(dp[i+1][k] for k in range(j-1, j+2))+1
res += dp[i][j]-1
dp[0] = grid[0][:]
for i, j in product(range(1, m), range(1, n-1)):
if grid[i][j]:
dp[i][j] = min(dp[i-1][k] for k in range(j-1, j+2))+1
res += dp[i][j]-1
return res | count-fertile-pyramids-in-a-land | [Python3] Short DP | jl1230 | 0 | 24 | count fertile pyramids in a land | 2,088 | 0.635 | Hard | 28,812 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2024498/Python-Solution-%2B-One-Line! | class Solution:
def targetIndices(self, nums, target):
ans = []
for i,num in enumerate(sorted(nums)):
if num == target: ans.append(i)
return ans | find-target-indices-after-sorting-array | Python - Solution + One-Line! | domthedeveloper | 10 | 377 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,813 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2024498/Python-Solution-%2B-One-Line! | class Solution:
def targetIndices(self, nums, target):
return [i for i,num in enumerate(sorted(nums)) if num==target] | find-target-indices-after-sorting-array | Python - Solution + One-Line! | domthedeveloper | 10 | 377 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,814 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2024498/Python-Solution-%2B-One-Line! | class Solution:
def targetIndices(self, nums, target):
idx = cnt = 0
for num in nums:
idx += num < target
cnt += num == target
return list(range(idx, idx+cnt)) | find-target-indices-after-sorting-array | Python - Solution + One-Line! | domthedeveloper | 10 | 377 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,815 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2024498/Python-Solution-%2B-One-Line! | class Solution:
def targetIndices(self, nums, target):
idx = sum(num < target for num in nums)
cnt = sum(num == target for num in nums)
return list(range(idx, idx+cnt)) | find-target-indices-after-sorting-array | Python - Solution + One-Line! | domthedeveloper | 10 | 377 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,816 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1672906/Two-Method-Soln-in-Python-Without-Sorting | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
m=c=0
for i in nums:
if i<target:
m+=1
if i==target:
c+= 1
if target not in nums:
return []
ans=[]
while c > 0:
ans.append(m)
m+=1
c-=1
return ans | find-target-indices-after-sorting-array | Two Method Soln in Python Without Sorting | gamitejpratapsingh998 | 4 | 196 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,817 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1672906/Two-Method-Soln-in-Python-Without-Sorting | class Solution:
import heapq
def targetIndices(self, nums: List[int], target: int) -> List[int]:
heapq.heapify(nums)
i=0
ans=[]
while nums!=[]:
x = heapq.heappop(nums)
if x==target:
ans.append(i)
if x > target:
break
i+=1
return ans | find-target-indices-after-sorting-array | Two Method Soln in Python Without Sorting | gamitejpratapsingh998 | 4 | 196 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,818 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2773394/Simple-Solution-O(n) | class Solution:
def targetIndices(self, N: List[int], T: int) -> List[int]:
c = i = 0
for n in N:
if n < T: i += 1
elif n == T: c += 1
return range(i, i+c) | find-target-indices-after-sorting-array | Simple Solution O(n) | Mencibi | 1 | 81 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,819 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1692419/Python-Binary-Search | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
nums.sort()
min = 0
max = len(nums) - 1
while min <= max:
pos = (min + max) // 2
if target == nums[pos]:
first = last = pos
# find first occurrence
if first - 1 >= 0:
while first != 0 and nums[first] == nums[first-1]:
first -= 1
# find last occurrence
if last != len(nums) - 1:
while last != len(nums) - 1 and nums[last] == nums[last+1]:
last += 1
else:
# only one 'target' was found
return [first]
return list(range(first,last+1))
elif target < nums[pos]:
max = pos - 1
else:
min = pos + 1 | find-target-indices-after-sorting-array | Python Binary Search | manulik | 1 | 399 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,820 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1606050/One-pass-no-sorting-100-speed | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
n_lower = n_target = 0
for n in nums:
if n < target:
n_lower += 1
elif n == target:
n_target += 1
return list(range(n_lower, n_lower + n_target)) if n_target else [] | find-target-indices-after-sorting-array | One pass, no sorting, 100% speed | EvgenySH | 1 | 148 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,821 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1601339/Python3-O(N)-Dijkstra's-3-way-partition | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
lo, mid, hi = 0, 0, len(nums)-1
while mid <= hi:
if nums[mid] < target:
nums[lo], nums[mid] = nums[mid], nums[lo]
lo += 1
mid += 1
elif nums[mid] == target: mid += 1
else:
nums[mid], nums[hi] = nums[hi], nums[mid]
hi -= 1
return range(lo, hi+1) | find-target-indices-after-sorting-array | [Python3] O(N) Dijkstra's 3-way partition | ye15 | 1 | 170 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,822 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1599868/Python-lower-bound-or-upper-bound | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
nums.sort() # sorting array
return list(range(bisect_left(nums, target),bisect_right(nums, target))) #return range from left most occurrence to rightmost occurrence | find-target-indices-after-sorting-array | Python lower bound | upper bound | abkc1221 | 1 | 234 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,823 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2848625/1-line-easy-code-in-Python3 | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
return sorted([k[0] for k in list(enumerate(sorted(nums))) if k[1] == target]) | find-target-indices-after-sorting-array | 1 line easy code in Python3 | DNST | 0 | 1 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,824 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2841292/onliner-python-faster-than-90 | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
# d = sorted(nums)
# c=[]
# for x,y in enumerate(d):
# if y==target:
# c.append(x)
# return(c)
return [x for x, y in enumerate(sorted(nums)) if y==target ] | find-target-indices-after-sorting-array | onliner python faster than 90% | IronmanX | 0 | 1 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,825 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2815042/Easy-solution | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
n=len(nums)
list=[]
nums.sort()
for i in range (n):
if nums[i]==target:
list.append(i)
return list | find-target-indices-after-sorting-array | Easy solution | nishithakonuganti | 0 | 1 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,826 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2812278/2089.-Find-Target-Indices-After-Sorting-Array-with-Python | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
result = []
nums.sort()
for i in range(len(nums)):
if nums[i] == target:
result.append(i)
return result | find-target-indices-after-sorting-array | 2089. Find Target Indices After Sorting Array with Python | Arsalan_Ahmed_07 | 0 | 1 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,827 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2793443/Easy-to-understand-Python-Solution | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
lst = []
nums.sort()
for i in range(len(nums)):
if nums[i] == target:
lst.append(i)
return lst | find-target-indices-after-sorting-array | Easy to understand Python Solution | Aayush3014 | 0 | 1 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,828 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2750459/Python-O(N)-Time | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
if target not in nums:
return []
greater, lower, equal = 0, 0,0
for i,num in enumerate(nums):
if num < target:
lower += 1
elif num > target:
greater += 1
else:
equal += 1
ans = []
ans.append(lower)
for i in range(equal-1):
ans.append(ans[-1] + 1)
return ans | find-target-indices-after-sorting-array | Python O(N) Time | vijay_2022 | 0 | 4 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,829 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2730803/Python3-Short-Solution-!!! | class Solution:
def targetIndices(self, a: List[int], t: int) -> List[int]:
a = sorted(a)
return [i for i, v in enumerate(a) if v == t] | find-target-indices-after-sorting-array | [Python3] Short Solution !!! | kylewzk | 0 | 7 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,830 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2730681/Fast-and-Easy-Solution | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
nums.sort()
back = 0
for i in range(len(nums)):
if nums[i] == target:
nums[back] = i
back += 1
return nums[:back] | find-target-indices-after-sorting-array | Fast and Easy Solution | user6770yv | 0 | 2 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,831 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2690365/Python-Simple-Python-Solution-Using-Sorting | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
result = []
nums = sorted(nums)
for index in range(len(nums)):
if nums[index] > target:
break
if nums[index] == target:
result.append(index)
return result | find-target-indices-after-sorting-array | [ Python ] β
β
Simple Python Solution Using Sorting π₯³βπ | ASHOK_KUMAR_MEGHVANSHI | 0 | 13 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,832 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2622479/Python-Solution-or-Two-Ways-Brute-Force-and-Counter-Based-or-Both-99-Faster | class Solution:
# brute force via sorting and linear search
def targetIndices(self, nums: List[int], target: int) -> List[int]:
nums.sort()
return [idx for idx, num in enumerate(nums) if num == target]
# counter based
def targetIndices(self, nums: List[int], target: int) -> List[int]:
counts = collections.Counter(nums)
smallEles = sum(ele for key, ele in counts.items() if key < target)
return list(range(smallEles, smallEles + counts[target])) | find-target-indices-after-sorting-array | Python Solution | Two Ways - Brute Force and Counter Based | Both 99% Faster | Gautam_ProMax | 0 | 11 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,833 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2568960/SIMPLE-PYTHON3-SOLUTION-O(logN) | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
nums.sort()
res = []
for i in range(nums.count(target)):
index = nums.index(target)
res.append(index)
nums[index] = target+2
return res | find-target-indices-after-sorting-array | β
β SIMPLE PYTHON3 SOLUTION β
β O(logN) | rajukommula | 0 | 33 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,834 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2431185/2089.-Find-Target-Indices-After-Sorting-Array%3A-One-Liner | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
return [index for index,x in enumerate(sorted(nums)) if x == target] | find-target-indices-after-sorting-array | 2089. Find Target Indices After Sorting Array: One Liner | rogerfvieira | 0 | 16 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,835 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2426724/Python-fast-and-easy-solution-using-built-in-function | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
sorted_nums = sorted(nums)
result = []
for i in range(len(sorted_nums)):
if sorted_nums[i] == target:
result.append(i)
return result | find-target-indices-after-sorting-array | Python fast and easy solution using built in function | samanehghafouri | 0 | 12 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,836 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2418558/Using-sort-function-Python | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
res = []
nums.sort()
for i in range(len(nums)):
if nums[i] == target:
res.append(i)
return res | find-target-indices-after-sorting-array | Using sort function Python | ankurbhambri | 0 | 15 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,837 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2385042/Python3-or-Easy-Sorting-Solution | class Solution:
#T.C = O(n)
#S.C = O(n)
def targetIndices(self, nums: List[int], target: int) -> List[int]:
nums.sort()
ans = []
for i in range(len(nums)):
if nums[i] == target:
ans.append(i)
return ans | find-target-indices-after-sorting-array | Python3 | Easy Sorting Solution | JOON1234 | 0 | 18 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,838 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2376640/Python-O(N)-in-Time-and-O(1)-in-space-No-sorting | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
# only need to use two ints to record how many nums less than target, and how many nums equals to target
less = -1
equal = 0
for n in nums:
if n > target:
continue
elif n == target:
equal += 1
else:
less += 1
# if all the nums are large than target, return empty or
# if all the nums are smaller than target, return empty
if equal == 0:
return []
else: # if there are some nums equal target
return range(less+1,less+1+equal)
``` | find-target-indices-after-sorting-array | Python O(N) in Time and O(1) in space, No sorting | meiyaowen | 0 | 20 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,839 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2286379/Simple-Python3-Solution | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
nums.sort()
l = list()
for i in range(len(nums)):
if nums[i] == target:
l.append(i)
return l | find-target-indices-after-sorting-array | Simple Python3 Solution | vem5688 | 0 | 34 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,840 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2174894/Python-oneliner | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
return [x for x in range(sorted(nums).index(target),sorted(nums).index(target)+nums.count(target))] if target in nums else [] | find-target-indices-after-sorting-array | Python oneliner | StikS32 | 0 | 58 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,841 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/2155841/Python3-Solution-with-using-counting | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
less_elems_cnt, more_elems_cnt = 0, 0
for num in nums:
if num < target:
less_elems_cnt += 1
elif num > target:
more_elems_cnt += 1
return [i for i in range(less_elems_cnt, len(nums) - more_elems_cnt)] if less_elems_cnt + more_elems_cnt < len(nums) else [] | find-target-indices-after-sorting-array | [Python3] Solution with using counting | maosipov11 | 0 | 33 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,842 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1988076/Python-Easiest-Solution-With-Explanation-or-88.79-Faster-or-Sorting-or-Beg-to-adv | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
res = [] # taking empty list for storing result
nums.sort() # sorting the provided list, as in the question its says after sorting the array.
for i in range(len(nums)): # traversing the array.
if nums[i] == target: # checking if the current element is equal to the provided target.
res.append(i) # appending index of the element to the resulting list.
return res # returning the list containing our result. | find-target-indices-after-sorting-array | Python Easiest Solution With Explanation | 88.79 % Faster | Sorting | Beg to adv | rlakshay14 | 0 | 90 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,843 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1917559/Python3-Easy-solution-with-98-space-complexity | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
nums.sort()
li =[]
for i in range(len(nums)):
if nums[i]==target:
li.append(i)
return li | find-target-indices-after-sorting-array | Python3 Easy solution with 98% space complexity | VINOD27 | 0 | 82 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,844 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1903078/Python-One-liner-easy-solution | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
return [ i for i,num in enumerate(sorted(nums)) if num==target] | find-target-indices-after-sorting-array | Python One liner easy solution | samuelstephen | 0 | 46 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,845 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1862060/Python-(Simple-Approach-and-Beginner-Friendly) | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
arr = []
nums.sort()
for i,n in enumerate(nums):
if n == target:
arr.append(i)
return arr | find-target-indices-after-sorting-array | Python (Simple Approach and Beginner-Friendly) | vishvavariya | 0 | 56 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,846 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1840535/Python-Easiest-Solution-With-Explanation-or-Beg-to-Adv | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
nums.sort() # as we might get unsorted list, so we have to sort it manually.
res = [] # creating empty list to store the result
for i in range(len(nums)): # checking each element of the giving.
if nums[i] == target: # checking each element if its equal to the target
res.append(i) # if yes append its index in the list we created.
return res # return the list we made, containing index. | find-target-indices-after-sorting-array | Python Easiest Solution With Explanation | Beg to Adv | rlakshay14 | 0 | 65 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,847 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1837071/Simple-Python-Solution | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
nums.sort()
return [i for i in range(len(nums)) if nums[i] == target] | find-target-indices-after-sorting-array | Simple Python Solution | himanshu11sgh | 0 | 33 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,848 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1812659/1-Line-Python-Solution-oror-60-Faster-oror-Memory-less-than-99 | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
return [idx for idx,val in enumerate(sorted(nums)) if val==target] | find-target-indices-after-sorting-array | 1-Line Python Solution || 60% Faster || Memory less than 99% | Taha-C | 0 | 90 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,849 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1780290/Ugly-and-slow-one-liner | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
return [] if target not in nums else [x for x in range(sorted(nums).index(target), sorted(nums).index(target) + sorted(nums).count(target))] | find-target-indices-after-sorting-array | Ugly and slow one liner | rrrares | 0 | 39 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,850 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1739131/Python3-simple-solution | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
ans = []
nums.sort()
for i in range(len(nums)):
if nums[i] == target:
ans.append(i)
return ans | find-target-indices-after-sorting-array | Python3 simple solution | EklavyaJoshi | 0 | 90 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,851 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1720968/Python3-accepted-one-liner-solution | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
return [i for i,x in enumerate(sorted(nums)) if(x==target)] | find-target-indices-after-sorting-array | Python3 accepted one-liner solution | sreeleetcode19 | 0 | 62 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,852 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1702335/Understandable-code-for-beginners-like-me-in-python-!! | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
nums.sort()
answer=[]
nums_len=len(nums)
flag=0
for index in range(nums_len):
if(nums[index]==target):
answer.append(index)
flag=1
elif(nums[index]!=target and flag==1):
break
return answer | find-target-indices-after-sorting-array | Understandable code for beginners like me in python !! | kabiland | 0 | 58 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,853 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1664277/Python-3-binary-search-(or-bisect-like)-solution-which-beats-86.71 | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
if len(nums) == 0:
return []
nums.sort() # O(nlogn)
start = self._binary_search(nums, target, True # O(logn)
end = self._binary_search(nums, target, False) # O(logn)
return [x for x in range(start, end+1)] if start != None else []
# find the first or last index of target in nums
def _binary_search(self, nums, target, is_start):
end = len(nums) - 1
start = 0
while start <= end:
mid = start + (end - start) // 2
pivot = nums[mid]
if pivot == target:
if is_start:
if mid == start or nums[mid - 1] < target:
return mid
else:
end = mid - 1
else:
if mid == end or nums[mid + 1] > target:
return mid
else:
start = mid + 1
elif pivot < target:
start = mid + 1
else:
end = mid - 1
return None | find-target-indices-after-sorting-array | Python 3 binary search (or bisect like) solution which beats 86.71% | MichaelRays | 0 | 114 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,854 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1656545/EASY-SIMPLE-solution | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
nums.sort()
answer =[]
for i in range(len(nums)):
if nums[i]==target:
answer.append(i)
return answer | find-target-indices-after-sorting-array | EASY, SIMPLE solution | Buyanjargal | 0 | 68 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,855 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1646872/Python-O(nlogn)-using-bisect | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
nums.sort()
l, i = [], bisect.bisect_left(nums, target)
while i < len(nums):
if nums[i] == target:
l.append(i)
elif nums[i] > target:
break
i += 1
return l | find-target-indices-after-sorting-array | Python O(nlogn) using bisect | emwalker | 0 | 55 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,856 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1608267/O(n)-time-oror-O(1)-space-oror-Python-solution | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
#count occurence of the target
#count how many numbers are less than target
count = 0
isless = 0
for i in range(len(nums)):
if nums[i] == target:
count += 1
if nums[i] < target:
isless += 1
if count == 0:
return []
res = [isless]
while count > 1:
isless += 1
res.append(isless)
count -= 1
return res | find-target-indices-after-sorting-array | O(n) time || O(1) space || Python solution | s_m_d_29 | 0 | 126 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,857 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1601478/Python-single-pass | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
start = 0
count = 0
for n in nums:
if n < target:
start += 1
elif n == target:
count += 1
return range(start, start + count) | find-target-indices-after-sorting-array | Python, single pass | blue_sky5 | 0 | 91 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,858 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1599990/Python-3-O(n)-wo-sorting | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
start_idx, num_target = 0, 0
for num in nums:
if num < target:
start_idx += 1
elif num == target:
num_target += 1
return list(range(start_idx, start_idx+num_target))
# Time O(n)
# Space O(1) (O(n) w/ output) | find-target-indices-after-sorting-array | [Python 3] O(n) w/o sorting | JosephJia | 0 | 61 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,859 |
https://leetcode.com/problems/find-target-indices-after-sorting-array/discuss/1599813/Python3-Solution | class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
nums.sort()
output = []
for idx in range(len(nums)):
if nums[idx] == target:
output.append(idx)
return output | find-target-indices-after-sorting-array | Python3 Solution | michaelb072 | 0 | 43 | find target indices after sorting array | 2,089 | 0.766 | Easy | 28,860 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/1599973/Python-3-or-Sliding-Window-or-Illustration-with-picture | class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
res = [-1]*len(nums)
left, curWindowSum, diameter = 0, 0, 2*k+1
for right in range(len(nums)):
curWindowSum += nums[right]
if (right-left+1 >= diameter):
res[left+k] = curWindowSum//diameter
curWindowSum -= nums[left]
left += 1
return res | k-radius-subarray-averages | Python 3 | Sliding Window | Illustration with picture | ndus | 48 | 1,300 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,861 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/1599853/Python3-prefix-sum | class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
prefix = [0]
for x in nums: prefix.append(prefix[-1] + x)
ans = [-1]*len(nums)
for i, x in enumerate(nums):
if k <= i < len(nums)-k: ans[i] = (prefix[i+k+1] - prefix[i-k])//(2*k+1)
return ans | k-radius-subarray-averages | [Python3] prefix sum | ye15 | 11 | 411 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,862 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/1599853/Python3-prefix-sum | class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
ans = [-1]*len(nums)
rsm = 0 # range sum
for i, x in enumerate(nums):
rsm += x
if i >= 2*k+1: rsm -= nums[i-(2*k+1)]
if i+1 >= 2*k+1: ans[i-k] = rsm//(2*k+1)
return ans | k-radius-subarray-averages | [Python3] prefix sum | ye15 | 11 | 411 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,863 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/2797823/Python-oror-Easy-oror-Sliding-Window-oror-O(n)-Solution | class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
n=len(nums)
t=2*k+1
if n<t:
return [-1]*n
ans=[-1]*k
s=sum(nums[:t])
avg=s//t
ans.append(avg)
l,r=0,t
for i in range(k+1,n):
if i+k>=n:
ans.append(-1)
else:
s+=nums[r]-nums[l]
avg=s//t
ans.append(avg)
l+=1
r+=1
return ans | k-radius-subarray-averages | Python || Easy || Sliding Window || O(n) Solution | DareDevil_007 | 1 | 34 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,864 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/1999858/python-3-oror-sliding-window | class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
x = 2*k + 1
n = len(nums)
if x > n:
return [-1] * n
s = sum(nums[i] for i in range(x))
res = [-1] * k + [s // x]
for i in range(k + 1, n - k):
s += nums[i + k] - nums[i - k - 1]
res.append(s // x)
res.extend([-1] * k)
return res | k-radius-subarray-averages | python 3 || sliding window | dereky4 | 1 | 67 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,865 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/1999667/Simple-Python-3-Solution-with-Sliding-Window-and-Current-sum | class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
# The idea is quite simple.
# We will have a sliding window of 2k+1 since we need k to left and k to right numbers.
# We will keep track of the current sum
k = 2*k+1
res = [-1] * len(nums)
l, curr = 0, 0
for r, num in enumerate(nums):
curr += num
# r-l+1 == len of curr window
if r - l + 1 > k:
curr -= nums[l]
l += 1
if r - l + 1 == k:
# update the middle of the current window
ind = (l+r)//2
res[ind] = curr // k
return res | k-radius-subarray-averages | Simple Python 3 Solution with Sliding Window and Current sum | toredev | 1 | 39 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,866 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/1599881/Python3-O(n)-with-comments | class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
res = []
n = len(nums)
for i, num in enumerate(nums):
if i-k < 0 or i+k >= n:
res.append(-1)
else: # k<=i<n-k
if i-k == 0:
curSum = sum(nums[:i+k+1])
else:
curSum -= nums[i-k-1] # remove the first element of the previous window
curSum += nums[i+k] # add the new element that is the last element of the current window
res.append(curSum//(2*k+1))
return res | k-radius-subarray-averages | Python3 O(n) with comments | BetterLeetCoder | 1 | 48 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,867 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/2730824/Short-Solution-!!! | class Solution:
def getAverages(self, a: List[int], k: int) -> List[int]:
res = [-1]*len(a)
sum_v, diameter = 0, 2*k+1
for i in range(len(a)):
sum_v += a[i]
if i >= diameter-1:
res[i-k] = sum_v//diameter
sum_v -= a[i-diameter+1]
return res | k-radius-subarray-averages | Short Solution !!! | kylewzk | 0 | 2 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,868 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/1838307/Python-Sliding-Window-Solution | class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
rolling_sum = 0
if len(nums) < 2 * k + 1:
return [-1] * len(nums)
for i in range(0, 2*k + 1):
rolling_sum += nums[i]
ans = []
ans.append(rolling_sum // (2 * k + 1))
left, right = 0, 2 * k + 1
while right < len(nums):
rolling_sum -= nums[left]
rolling_sum += nums[right]
ans.append(rolling_sum // (2 * k + 1))
left += 1
right += 1
return [-1] * k + ans + [-1] * k | k-radius-subarray-averages | Python Sliding Window Solution | Vayne1994 | 0 | 41 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,869 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/1818352/Python-easy-to-read-and-understand | class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
n = len(nums)
t = [-1 for _ in range(n)]
sums, i = 0, 0
radius = 2*k+1
for j in range(n):
sums += nums[j]
if j-i+1 == radius:
t[j-k] = sums//radius
sums -= nums[i]
i = i+1
return t | k-radius-subarray-averages | Python easy to read and understand | sanial2001 | 0 | 54 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,870 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/1695002/python-simple-O(n)-time-sliding-window-solution | class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
n = len(nums)
res = [-1 for _ in range(n)]
if n < 2*k+1:
return res
if k == 0:
return nums
prevsum = sum(nums[:2*k+1])
res[k] = prevsum//(2*k+1)
for i in range(k+1, n-k):
s = prevsum - nums[i-k-1] + nums[i+k]
res[i] = s//(2*k+1)
prevsum = s
return res | k-radius-subarray-averages | python simple O(n) time sliding window solution | byuns9334 | 0 | 62 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,871 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/1623279/python3-sliding-window-solution | class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
if k==0:
return nums
n=len(nums)
if n<=2*k:
return [-1]*(n)
res=[-1]*(n)
s=sum(nums[:2*k+1])
t=2*k+1
res[k]=s//t
for i in range(k+1,n-k):
s=s-nums[i-k-1]+nums[i+k]
res[i]=s//t
for j in range(len(res),len(nums)):
res.append(-1)
return res | k-radius-subarray-averages | python3 sliding window solution | Karna61814 | 0 | 35 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,872 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/1607687/One-pass-with-moving-window-99-speed | class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
if not k:
return nums
len_nums = len(nums)
ans = [-1] * len_nums
len_window = 2 * k + 1
sum_window = sum(nums[: len_window - 1])
for i in range(k, len_nums - k):
sum_window += nums[i + k]
ans[i] = sum_window // len_window
sum_window -= nums[i - k]
return ans | k-radius-subarray-averages | One pass with moving window, 99% speed | EvgenySH | 0 | 60 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,873 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/1602031/Python3Java-Sliding-Window-Succinct | class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
# Accumulate all elements except the last one in the window
acc = sum(nums[0 : 2 * k])
i, result = k, [-1] * len(nums)
for i in range(k, len(nums) - k):
acc += nums[i + k]
result[i] = acc // (2 * k + 1)
acc -= nums[i - k]
return result | k-radius-subarray-averages | [Python3][Java] - Sliding Window - Succinct | mardlucca | 0 | 29 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,874 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/1600433/Sliding-Window-%2B-Prefix-Sum-Simple-approach-O(n)-space-and-time | class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
if k == 0: return nums
n = len(nums)
ans = [-1]*n
prefSum = [0]*n
for i in range(0, n):
if i == 0:
prefSum[0] = nums[i]
else:
prefSum[i] = prefSum[i-1] + nums[i]
#print(prefSum)
for i in range(k, n - k):
if i - k - 1 < 0:
ans[i] = (prefSum[i + k])//(2*k + 1)
else:
ans[i] = (prefSum[i + k] - prefSum[i - k - 1])//(2*k + 1)
return ans | k-radius-subarray-averages | Sliding Window + Prefix Sum - Simple approach- O(n) space and time | abuOmar2 | 0 | 14 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,875 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/1600102/Sliding-Window-oror-For-Beginners-oror-Well-Coded | class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
n = len(nums)
if k*2+1>n:
return [-1]*n
total_len = 2*k+1
s = sum(nums[:2*k])
res = [-1]*n
for i in range(k,n-k):
s+=nums[i+k]
avg = s//total_len
res[i] = avg
s-=nums[i-k]
return res | k-radius-subarray-averages | ππ Sliding Window || For Beginners || Well-Coded π | abhi9Rai | 0 | 37 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,876 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/1600016/Python3-Sliding-Window-Time-O(n) | class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
if k == 0:
return nums
avgs, wid_len, n = [], 2*k+1, len(nums)
for i in range(n):
if i-k < 0 or i+k>n-1:
avgs.append(-1)
else:
if i-k == 0:
running_sum = sum(nums[i-k:i+k+1])
else:
running_sum = running_sum + nums[i+k] - nums[i-k-1]
avgs.append(running_sum//wid_len)
return avgs | k-radius-subarray-averages | [Python3] Sliding-Window Time O(n) | JosephJia | 0 | 25 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,877 |
https://leetcode.com/problems/k-radius-subarray-averages/discuss/1599847/Python3-sliding-window | class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
output = [-1] * len(nums)
running = 0
for idx, i in enumerate(nums):
running += i
if idx >= k*2:
output[idx - k] = running // (k*2+1)
running -= nums[idx - k*2]
return output | k-radius-subarray-averages | Python3 sliding window | michaelb072 | 0 | 20 | k radius subarray averages | 2,090 | 0.426 | Medium | 28,878 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/1599862/Python3-3-candidates | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
imin = nums.index(min(nums))
imax = nums.index(max(nums))
return min(max(imin, imax)+1, len(nums)-min(imin, imax), len(nums)+1+min(imin, imax)-max(imin, imax)) | removing-minimum-and-maximum-from-array | [Python3] 3 candidates | ye15 | 5 | 226 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,879 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/2201682/Basic-Python-Solution | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
n = len(nums)
x = nums.index(min(nums)) + 1
y = nums.index(max(nums)) + 1
res = min(max(n-x+1, n-y+1) , max(x,y)) #minimum of going from right and going from left
if x > y: #exchange if needed so as to do one operation later assuming x is the smaller index
x, y = y, x
option = x + (n - y) + 1 #going left for smaller and right for larger
res = min(res, option)
return res if n > 2 else n | removing-minimum-and-maximum-from-array | Basic Python Solution | nahomn7 | 3 | 96 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,880 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/1609043/Python-3-O(n)-solution | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
min_i = max_i = None
min_num, max_num = math.inf, -math.inf
for i, num in enumerate(nums):
if num < min_num:
min_i, min_num = i, num
if num > max_num:
max_i, max_num = i, num
if min_i > max_i:
min_i, max_i = max_i, min_i
n = len(nums)
return min(min_i + 1 + n - max_i, # delete from left and right
max_i + 1, # delete from left only
n - min_i) # delete from right only | removing-minimum-and-maximum-from-array | Python 3 O(n) solution | dereky4 | 2 | 144 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,881 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/1625106/Python-3-5-lines-oror90-faster-oror-3-diff-methods | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
front_idx_maxi = nums.index(max(nums))
front_idx_mini = nums.index(min(nums))
n = len(nums)
li = sorted([front_idx_maxi,front_idx_mini])
return min(li[1]+1,n-li[0],(li[0]+1)+(n-li[1])) | removing-minimum-and-maximum-from-array | Python 3 - 5 lines ||90% faster || 3 diff methods | ana_2kacer | 1 | 141 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,882 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/2848778/Easy-solution-on-Python3 | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
minVal = min(nums)
maxVal = max(nums)
minIndex = nums.index(minVal)
maxIndex = nums.index(maxVal)
r1 = max(minIndex, maxIndex) + 1
r2 = len(nums) - min(minIndex, maxIndex)
r3 = min(minIndex, maxIndex) + 1 + len(nums) - max(minIndex, maxIndex)
return min(r1,r2,r3) | removing-minimum-and-maximum-from-array | Easy solution on Python3 | DNST | 0 | 2 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,883 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/2839496/Python3-easy-to-understand | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
l = nums.index(min(nums))
h = nums.index(max(nums))
N = len(nums)
if len(nums) == 1: return 1
if len(nums) == 2: return 2
return min(max(l + 1, h + 1), max(N - l, N - h), min(l + 1, len(nums) - l) + min(h + 1, len(nums) - h)) | removing-minimum-and-maximum-from-array | Python3 - easy to understand | mediocre-coder | 0 | 2 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,884 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/2817731/python-super-easy-to-understand-O(n) | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
min_val = float("inf")
max_val = float("-inf")
min_index = None
max_index = None
for i in range(len(nums)):
if nums[i] < min_val:
min_val = nums[i]
min_index = i
if nums[i] > max_val:
max_val = nums[i]
max_index = i
return min(len(nums) - abs(max_index - min_index) + 1, max(min_index, max_index) + 1, len(nums) - min(min_index, max_index)) | removing-minimum-and-maximum-from-array | python super easy to understand O(n) | harrychen1995 | 0 | 1 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,885 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/2812587/Python-clean-and-easy-code | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
max_v, min_v, max_i, min_i = -float('inf'), float('inf'), 0, 0
for i in range(len(nums)):
if nums[i] > max_v:
max_v, max_i = nums[i], i
if nums[i] < min_v:
min_v, min_i = nums[i], i
x, y = (max_i, min_i) if max_i < min_i else (min_i, max_i)
# delete from first ele to the last target ele, or vice versa,
# or from frist ele to the first target ele and back from last to the second target ele.
# the min of above is the answer
min_dnum = min(y+1, len(nums)-x, x+1 + len(nums)-y )
return min_dnum | removing-minimum-and-maximum-from-array | Python clean and easy code | jsyx1994 | 0 | 1 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,886 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/2730851/Python3-Short-Solution-!!! | class Solution:
def minimumDeletions(self, A: List[int]) -> int:
i, j, n = A.index(min(A)), A.index(max(A)), len(A)
return min(max(i+1, j+1), max(n-i, n-j), i+1+n-j, j+1+n-i) | removing-minimum-and-maximum-from-array | [Python3] Short Solution !!! | kylewzk | 0 | 5 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,887 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/2669739/begginers-friendly-code...python | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
mini=nums.index(min(nums))#taking out the index of minimum element.
maxi=nums.index(max(nums))#taking out the element of maximum element.
a1=max(mini,maxi)+1 # calculate the no of steps removing from the left.
a2=max(len(nums)-mini,len(nums)-maxi)#calculate the no of steps removing from right.
a3=min(mini,maxi)+1+len(nums)-max(mini,maxi)#calculate the no of steps removing from both left and right
return min(a1,a2,a3)# at last find the minimum of all 3 | removing-minimum-and-maximum-from-array | begginers friendly code...#python | insane_me12 | 0 | 3 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,888 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/2441444/Python-two-pointers | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
if len(nums) == 1:
return 1
left, right = 0, 0
l, r = 0, len(nums) - 1
maximum = max(nums)
minimum = min(nums)
bothFind = 0
twoSide = 0
while l <= (len(nums)-1) and r >= 0:
if nums[l] == maximum or nums[l] == minimum:
left = l + 1
bothFind += 1
if bothFind <= 2:
twoSide += left
if nums[r] == maximum or nums[r] == minimum:
right = len(nums) - r
bothFind += 1
if bothFind <= 2:
twoSide += right
l += 1
r -= 1
return min(left, right, twoSide) | removing-minimum-and-maximum-from-array | Python two-pointers | scr112 | 0 | 33 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,889 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/2339001/Python3-or-Spelled-out-for-readability | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
N = len(nums)
min_pos = nums.index(min(nums))
max_pos = nums.index(max(nums))
del_mn_from_left = min_pos + 1
del_mx_from_left = max_pos + 1
del_mn_from_right = N - min_pos
del_mx_from_right = N - max_pos
# Possible ways to deletet 'min & max'
ll = max(del_mn_from_left, del_mx_from_left)
rr = max(del_mn_from_right, del_mx_from_right)
lr = del_mn_from_left + del_mx_from_right
rl = del_mx_from_left + del_mn_from_right
return min(ll, rr, lr, rl) | removing-minimum-and-maximum-from-array | Python3 | Spelled out for readability | Ploypaphat | 0 | 53 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,890 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/2207610/Python-O(N)O(1) | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
min_idx = max_idx = 0
min_value = max_value = nums[0]
for i, n in enumerate(nums):
if n < min_value:
min_idx = i
min_value = n
if n > max_value:
max_idx = i
max_value = n
if min_idx > max_idx:
min_idx, max_idx = max_idx, min_idx
return min(min_idx + len(nums) - max_idx + 1, max_idx + 1, len(nums) - min_idx) | removing-minimum-and-maximum-from-array | Python, O(N)/O(1) | blue_sky5 | 0 | 21 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,891 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/2199390/Simple-Logical-Solution-oror-Covering-all-3-possibilities | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
n = len(nums)
if n == 1:
return 1
maxIdx = nums.index(max(nums))
minIdx = nums.index(min(nums))
if maxIdx > minIdx:
a = maxIdx + 1
else:
a = minIdx + 1
revMaxIdx = n - 1 - maxIdx
revMinIdx = n - 1 - minIdx
if revMaxIdx > revMinIdx:
b = revMaxIdx + 1
else:
b = revMinIdx + 1
maxIdxFront = maxIdx
minIdxBack = revMinIdx
maxIdxBack = revMaxIdx
minIdxFront = minIdx
c = maxIdxFront + minIdxBack + 2
d = maxIdxBack + minIdxFront + 2
e = min(c, d)
return min(a, min(b, e)) | removing-minimum-and-maximum-from-array | Simple Logical Solution || Covering all 3 possibilities | Vaibhav7860 | 0 | 16 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,892 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/2164194/Simple-Logic-(Python) | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
i, j , n = nums.index(min(nums)), nums.index(max(nums)),len(nums)
return min(max(i+1,j+1), max(n-i,n-j), j+1+n-i, i+1+n-j) | removing-minimum-and-maximum-from-array | Simple Logic (Python) | writemeom | 0 | 50 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,893 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/2035134/Python-easy-to-read-and-understand | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
x, y = nums.index(max(nums)), nums.index(min(nums))
n = len(nums)
return min(x+1 + n-y, #if max on left and min on right
y+1 + n-x, #if min on left and max on right
max(x, y)+1, #deleting from left only
n-min(x, y)) #deleting from right only | removing-minimum-and-maximum-from-array | Python easy to read and understand | sanial2001 | 0 | 68 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,894 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/1838328/Python-Math-Solution | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
s1 = sorted(nums)
L = len(nums)
max_index, min_index = 0, 0
min_num, max_num = s1[0], s1[-1]
for index in range(len(nums)):
if nums[index] == min_num:
min_index = index
elif nums[index] == max_num:
max_index = index
else:
continue
return min(max(max_index + 1, min_index + 1), max(L- max_index, L- min_index), min(min_index + 1, max_index + 1) + L - max(min_index, max_index)) | removing-minimum-and-maximum-from-array | Python Math Solution | Vayne1994 | 0 | 23 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,895 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/1798659/Python3-oror-O(n)-time-oror-O(1)-space | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
#we have three possibilities for each number:
#we can del only from left, only from right, both
min_idx, max_idx = 0, 0
min_val, max_val = nums[0], nums[0]
for idx in range(1,len(nums)):
if nums[idx] < min_val:
min_idx = idx
min_val = nums[idx]
if nums[idx] > max_val:
max_idx = idx
max_val = nums[idx]
size = len(nums)
idx1, idx2 = min(min_idx,max_idx), max(min_idx,max_idx)
left = idx2 + 1
right = size - idx1
both = idx1 + 1 + (size - idx2)
return min(left,right,both) | removing-minimum-and-maximum-from-array | Python3 || O(n) time || O(1) space | s_m_d_29 | 0 | 47 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,896 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/1600056/Simple-and-Straight-oror-Well-Explained-and-Clean-Coded | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
mi,ma = min(nums),max(nums)
n = len(nums)
miind, maind = nums.index(mi), nums.index(ma) # index of minimum element and index of maximum element
mirind,marind = n-miind-1, n-maind-1 # index of minimum element and index of maximum element from backside
return min(max(miind,maind)+1,max(mirind,marind)+1,miind+marind+2,maind+mirind+2) | removing-minimum-and-maximum-from-array | ππ Simple and Straight || Well-Explained and Clean Coded π | abhi9Rai | 0 | 44 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,897 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/1600025/Python3-O(n)-Time-Without-build-in-functions | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
n = len(nums)
min_element, min_idx, max_element, max_idx = math.inf, -1, -math.inf, -1
for idx, num in enumerate(nums):
if num < min_element:
min_element, min_idx = num, idx
if num > max_element:
max_element, max_idx = num, idx
return min(max(min_idx+1, max_idx+1),
max(n-min_idx, n-max_idx),
min_idx + 1 + n-max_idx,
max_idx + 1 + n-min_idx)
# Time O(n)
# Space O(1) | removing-minimum-and-maximum-from-array | [Python3] O(n) Time Without build-in functions | JosephJia | 0 | 34 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,898 |
https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/1599898/Python-check-possibilities-of-minimum-or-easy | class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
min_arr = max_arr = nums[0]
min_i = max_i = 0
n = len(nums)
# finding max and min indices
for i in range(1, n):
if nums[i] > max_arr:
max_arr = nums[i]
max_i = i
if nums[i] < min_arr:
min_arr = nums[i]
min_i = i
# checking possibilities
if min_i <= max_i:
return min(min_i+1+n-max_i, max_i+1, n-min_i)
else:
return min(max_i+1+n-min_i, min_i+1, n-max_i) | removing-minimum-and-maximum-from-array | Python check possibilities of minimum | easy | abkc1221 | 0 | 36 | removing minimum and maximum from array | 2,091 | 0.566 | Medium | 28,899 |
Subsets and Splits