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(startP... | 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 ne... | 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
... | 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... | 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[... | 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 alw... | 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)
... | 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 ar... | 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... | 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 =... | 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):
... | 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 ra... | 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))+... | 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 ... | 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:
... | 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... | 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_t... | 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[m... | 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:
... | 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... | 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:
... | 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 fo... | 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... | 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... | 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(... | 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)
... | 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
... | 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
... | 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... | 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
curWindow... | 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)
... | 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:
... | 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-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... | 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(num... | 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]
... | 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-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
... | 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)
... | 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 ra... | 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_w... | 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]
resu... | 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:
pre... | 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:
... | 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... | 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... | 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 = ... | 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,... | 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... | 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, ... | 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
... | 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[... | 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... | 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:... | 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... | 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... | 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
... | 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... | 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
... | 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[i... | 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... | 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 > m... | 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... | 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
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.