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/find-unique-binary-string/discuss/1899599/Python-oror-Runtime-36ms-faster-than-86 | class Solution:
def findDifferentBinaryString(self, nums: List[str]) -> str:
ans, l = '', len(nums[0])
con = False
def helper(curr):
nonlocal ans,con
if ans:
return
if len(curr) == l:
... | find-unique-binary-string | Python || Runtime 36ms, faster than 86 % | MS1301 | 0 | 69 | find unique binary string | 1,980 | 0.643 | Medium | 27,700 |
https://leetcode.com/problems/find-unique-binary-string/discuss/1892162/Python3-Solution | class Solution:
def findDifferentBinaryString(self, nums: List[str]) -> str:
k=len(nums[0])
num=set([int(_,2) for _ in nums])
maxnum=2**k
for i in range(maxnum):
if i not in num:
l=str(bin(i)[2:])
return ''.join(['0' for _ in range(k-len(l))])+l | find-unique-binary-string | Python3 Solution | eaux2002 | 0 | 54 | find unique binary string | 1,980 | 0.643 | Medium | 27,701 |
https://leetcode.com/problems/find-unique-binary-string/discuss/1747325/Runtime%3A-44-ms-faster-than-55.33-Memory-Usage%3A-less-than-80.20-using-python-and-backtracking | class Solution:
def findDifferentBinaryString(self, nums: List[str]) -> str:
self.n = len(nums)
self.lock = False
self.res = ""
def solve(com):
if(len(com) == self.n):
if(com not in nums):
self.res = com
self.lock = True
return
# for i in:
if(self.lock):
return
solve(com+"0")
... | find-unique-binary-string | Runtime: 44 ms, faster than 55.33% Memory Usage: less than 80.20% using python and backtracking | jagdishpawar8105 | 0 | 54 | find unique binary string | 1,980 | 0.643 | Medium | 27,702 |
https://leetcode.com/problems/find-unique-binary-string/discuss/1700378/Python3-accepted-solution | class Solution:
def findDifferentBinaryString(self, nums: List[str]) -> str:
for i in range(2**(len(nums[0])-1), 2**(len(nums[0]))):
if(bin(i).replace("0b","") not in nums):
return bin(i).replace("0b","")
elif("0"+bin(i).replace("0b","")[1:] not in nums):
... | find-unique-binary-string | Python3 accepted solution | sreeleetcode19 | 0 | 70 | find unique binary string | 1,980 | 0.643 | Medium | 27,703 |
https://leetcode.com/problems/find-unique-binary-string/discuss/1609958/Python-Easy-Solution-or-Simple-Trick | class Solution:
def findDifferentBinaryString(self, nums: List[str]) -> str:
bit = 1 << len(nums[0])
bitLen = len(nums[0])
res = [i for i in range(bit)]
for i in range(len(nums)):
nums[i] = int(nums[i], 2)
res = [num for num in res if num not in nums]
bit = len(bin(res[0])[2:])
if bit != bitLen:
re... | find-unique-binary-string | Python Easy Solution | Simple Trick ✔ | leet_satyam | 0 | 92 | find unique binary string | 1,980 | 0.643 | Medium | 27,704 |
https://leetcode.com/problems/find-unique-binary-string/discuss/1523325/WEEB-DOES-PYTHON-BFS | class Solution:
def findDifferentBinaryString(self, nums: List[str]) -> str:
queue = deque([["0"] * len(nums[0])])
return self.bfs(queue, nums)
def bfs(self, queue, nums):
visited = set(nums)
deadset = set()
while queue:
curCombo = queue.popleft()
curStr = "".join(curCombo)
... | find-unique-binary-string | WEEB DOES PYTHON BFS | Skywalker5423 | 0 | 66 | find unique binary string | 1,980 | 0.643 | Medium | 27,705 |
https://leetcode.com/problems/find-unique-binary-string/discuss/1519942/Python3-Two-kind-of-solutions | class Solution:
def backtracking(self, _set, lst, idx):
s = ''.join(lst)
if s not in _set:
return s
for i in range(idx, len(lst)):
lst[i] = '0'
res = self.backtracking(_set, lst, i + 1)
if len(res) != 0:
retur... | find-unique-binary-string | [Python3] Two kind of solutions | maosipov11 | 0 | 120 | find unique binary string | 1,980 | 0.643 | Medium | 27,706 |
https://leetcode.com/problems/find-unique-binary-string/discuss/1508778/Search-in-set-of-integers-85-speed | class Solution:
def findDifferentBinaryString(self, nums: List[str]) -> str:
n = len(nums[0])
set_nums = set(int(num, 2) for num in nums)
for i in range(pow(2, n) + 1):
if i not in set_nums:
return f"{bin(i)[2:]:0>{n}}" | find-unique-binary-string | Search in set of integers, 85% speed | EvgenySH | 0 | 63 | find unique binary string | 1,980 | 0.643 | Medium | 27,707 |
https://leetcode.com/problems/find-unique-binary-string/discuss/1496328/Intuitive-approach-by-looping-all-possible-integer | class Solution:
def findDifferentBinaryString(self, nums: List[str]) -> str:
num_set, num_size = set(nums), len(nums)
bs_format = "{" + f":0{num_size}b" + "}"
for i in range(pow(2, num_size)):
ans = bs_format.format(i)
if ans not in num_set:
return ans | find-unique-binary-string | Intuitive approach by looping all possible integer | puremonkey2001 | 0 | 28 | find unique binary string | 1,980 | 0.643 | Medium | 27,708 |
https://leetcode.com/problems/find-unique-binary-string/discuss/1495972/Python-O(16)-one-liner-solution%3A-flip-one-digit-for-each-num-to-build-the-answer | class Solution:
def findDifferentBinaryString(self, nums: List[str]) -> str:
flip = lambda a: '0' if a == '1' else '1'
return ''.join([flip(nums[i][i]) for i in range(len(nums))]) | find-unique-binary-string | [Python] O(16) one-liner solution: flip one digit for each num to build the answer | licpotis | 0 | 61 | find unique binary string | 1,980 | 0.643 | Medium | 27,709 |
https://leetcode.com/problems/find-unique-binary-string/discuss/1495972/Python-O(16)-one-liner-solution%3A-flip-one-digit-for-each-num-to-build-the-answer | class Solution:
def findDifferentBinaryString(self, nums: List[str]) -> str:
return ''.join(['0' if nums[i][i] == '1' else '1' for i in range(len(nums))]) | find-unique-binary-string | [Python] O(16) one-liner solution: flip one digit for each num to build the answer | licpotis | 0 | 61 | find unique binary string | 1,980 | 0.643 | Medium | 27,710 |
https://leetcode.com/problems/find-unique-binary-string/discuss/1488946/Python3-We-can-solve-it-simply-by-counting-1! | class Solution:
def findDifferentBinaryString(self, nums: List[str]) -> str:
# ---------------------------------------------------------------------------
# Notice that nums[i].length == n,
# It indicates that you will have many valid answers &
# Even we merely look number of 1 ... | find-unique-binary-string | Python3 - We can solve it simply by counting 1! | tkuo-tkuo | 0 | 70 | find unique binary string | 1,980 | 0.643 | Medium | 27,711 |
https://leetcode.com/problems/find-unique-binary-string/discuss/1439997/Python3-or-Simple-math-%2B-String-Functions | class Solution:
def findDifferentBinaryString(self, nums: List[str]) -> str:
n=len(nums)
hset=set(nums)
for i in range(n+1):
s=bin(i)[2:]
s=("0")*(n-len(s))+s
if s not in hset:
return s | find-unique-binary-string | [Python3] | Simple math + String Functions | swapnilsingh421 | 0 | 73 | find unique binary string | 1,980 | 0.643 | Medium | 27,712 |
https://leetcode.com/problems/find-unique-binary-string/discuss/1429483/Python3-solution-without-recusion | class Solution:
def findDifferentBinaryString(self, nums: List[str]) -> str:
n = len(nums[0])
for i in range(len(nums)):
nums[i] = int(nums[i],2)
for i in range(2**n):
if i not in nums:
return bin(i)[2:].zfill(n) | find-unique-binary-string | Python3 solution without recusion | EklavyaJoshi | 0 | 33 | find unique binary string | 1,980 | 0.643 | Medium | 27,713 |
https://leetcode.com/problems/minimize-the-difference-between-target-and-chosen-elements/discuss/1418634/100-efficient-or-Pruning-%2B-Memoization-or-Dynamic-Programming-or-Explanation | class Solution:
def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:
# store the mxn size of the matrix
m = len(mat)
n = len(mat[0])
dp = defaultdict(defaultdict)
# Sorting each row of the array for more efficient pruning
... | minimize-the-difference-between-target-and-chosen-elements | ✅ 100% efficient | Pruning + Memoization | Dynamic Programming | Explanation | CaptainX | 24 | 2,200 | minimize the difference between target and chosen elements | 1,981 | 0.323 | Medium | 27,714 |
https://leetcode.com/problems/minimize-the-difference-between-target-and-chosen-elements/discuss/2345711/Python-3-oror-Using-Leetcode-Given-hints | class Solution:
def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:
rows = len(mat)
old_hash_set = set({0})
for i in range(rows):
hash_set = set()
max_val = None
for elem in old_hash_set:
for row_val in sorted(mat[i]):
if max_val is not None and elem + row_val >= max_val... | minimize-the-difference-between-target-and-chosen-elements | Python 3 || Using Leetcode Given hints | sagarhasan273 | 0 | 61 | minimize the difference between target and chosen elements | 1,981 | 0.323 | Medium | 27,715 |
https://leetcode.com/problems/minimize-the-difference-between-target-and-chosen-elements/discuss/1424118/Python3-2-solutions-dp-and-bitset | class Solution:
def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:
m, n = len(mat), len(mat[0])
mn = [min(row) for row in mat]
@cache
def fn(i, x):
"""Return minimum absolute difference."""
if i == m: return abs(x)
... | minimize-the-difference-between-target-and-chosen-elements | [Python3] 2 solutions - dp & bitset | ye15 | 0 | 101 | minimize the difference between target and chosen elements | 1,981 | 0.323 | Medium | 27,716 |
https://leetcode.com/problems/minimize-the-difference-between-target-and-chosen-elements/discuss/1424118/Python3-2-solutions-dp-and-bitset | class Solution:
def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:
mask = 0b1
for row in mat:
temp = 0
for x in row: temp |= mask << x
mask = temp
for x in range(5000):
if mask >> (target+x) & 1 or x <= t... | minimize-the-difference-between-target-and-chosen-elements | [Python3] 2 solutions - dp & bitset | ye15 | 0 | 101 | minimize the difference between target and chosen elements | 1,981 | 0.323 | Medium | 27,717 |
https://leetcode.com/problems/find-array-given-subset-sums/discuss/1431457/Easy-Explanation-for-Noobs-%2B-Python-code-with-comments | class Solution:
def recoverArray(self, n: int, sums: List[int]) -> List[int]:
res = [] # Result set
sums.sort()
while len(sums) > 1:
num = sums[-1] - sums[-2] # max - secondMax
countMap = Counter(sums) # Get count of each elements
excluding = [] ... | find-array-given-subset-sums | Easy Explanation for Noobs + Python code with comments | sumit686215 | 57 | 1,500 | find array given subset sums | 1,982 | 0.489 | Hard | 27,718 |
https://leetcode.com/problems/find-array-given-subset-sums/discuss/1424193/Python3-greedy | class Solution:
def recoverArray(self, n: int, sums: List[int]) -> List[int]:
sums.sort()
ans = []
for _ in range(n):
diff = sums[1] - sums[0]
ss0, ss1 = [], []
freq = defaultdict(int)
on = False
for i, x in enumerate(sums):
... | find-array-given-subset-sums | [Python3] greedy | ye15 | 0 | 178 | find array given subset sums | 1,982 | 0.489 | Hard | 27,719 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/1433298/Python3-greedy-2-line | class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
nums.sort()
return min(nums[i+k-1]-nums[i] for i in range(len(nums)-k+1)) | minimum-difference-between-highest-and-lowest-of-k-scores | [Python3] greedy 2-line | ye15 | 2 | 151 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,720 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/2659161/Python3-solutions | class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
nums.sort()
res = 100000
for i in range(len(nums) - k + 1):
arr = nums[i:i + k]
res = min(res, arr[-1] - arr[0])
return res | minimum-difference-between-highest-and-lowest-of-k-scores | Python3 solutions | mediocre-coder | 1 | 277 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,721 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/2659161/Python3-solutions | class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
nums.sort()
arr = nums[:k]
res = arr[-1] - arr[0]
for i in range(k, len(nums)):
arr.pop(0)
arr.append(nums[i])
res = min(res, arr[-1] - arr[0])
... | minimum-difference-between-highest-and-lowest-of-k-scores | Python3 solutions | mediocre-coder | 1 | 277 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,722 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/1513200/Python-O(nlogn)-solution-oror-sort-first | class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
n = len(nums)
nums.sort()
minv = nums[-1]-nums[0]
for i in range(n-k+1):
minv = min(minv, nums[i+k-1]-nums[i])
return minv | minimum-difference-between-highest-and-lowest-of-k-scores | Python O(nlogn) solution || sort first | byuns9334 | 1 | 163 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,723 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/1432037/Python-or-Sort-or-2-Lines | class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
nums.sort()
return min(nums[i+k-1] - nums[i] for i in range(len(nums) - k + 1)) | minimum-difference-between-highest-and-lowest-of-k-scores | Python | Sort | 2 Lines | leeteatsleep | 1 | 121 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,724 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/1432037/Python-or-Sort-or-2-Lines | class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
nums.sort()
min_diff = float('inf')
for i in range(len(nums) - k + 1):
min_diff = min(min_diff, nums[i+k-1] - nums[i])
return min_diff | minimum-difference-between-highest-and-lowest-of-k-scores | Python | Sort | 2 Lines | leeteatsleep | 1 | 121 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,725 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/2851026/Easy-python-Solution-beats-95.. | class Solution:
def minimumDifference(self, nums, k):
if k==1:return 0
nums.sort()
nums.reverse()
fin =[]
l=-1
for i in range(len(nums)):
l+=1
r = l+k-1
if ((l <len(nums )) and (r < len(nums))):
fin.append(nums[l]-nu... | minimum-difference-between-highest-and-lowest-of-k-scores | Easy python Solution beats 95%.. | Varshan_cr | 0 | 1 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,726 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/2365476/Python3-or-O(nlogn)-or-Sorting | class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
nums.sort()
ans,n=float('inf'),len(nums)
for i in range(n-k+1):
minVal=nums[i]
maxVal=nums[i+k-1]
ans=min(ans,maxVal-minVal)
return ans | minimum-difference-between-highest-and-lowest-of-k-scores | [Python3] | O(nlogn) | Sorting | swapnilsingh421 | 0 | 28 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,727 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/2305475/Python3-Solution-or-Easy-Sliding-Window-technique! | class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
#sliding window technique!
L, R = 0,0
counter = 0
ans = float(inf)
#sort the array so every group of k elements, the min and max is closest to each other
#as possible!
nums.sort()
... | minimum-difference-between-highest-and-lowest-of-k-scores | Python3 Solution | Easy Sliding Window technique! | JOON1234 | 0 | 22 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,728 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/2174598/Python-one-line-solution | class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
nums = sorted(nums)
return min(b-a for a, b in zip(nums, nums[k-1:])) | minimum-difference-between-highest-and-lowest-of-k-scores | Python one line solution | writemeom | 0 | 62 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,729 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/1880535/Python-dollarolution | class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
nums = sorted(nums,reverse = True)
m = 10**5 + 1
for i in range(len(nums)-k+1):
x = nums[i] - nums[i+k-1]
if x < m:
m = x
return m | minimum-difference-between-highest-and-lowest-of-k-scores | Python $olution | AakRay | 0 | 41 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,730 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/1794100/5-Lines-Python-Solution-oror-50-Faster-oror-Memory-less-than-80 | class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
if k==1: return 0
nums, ans = sorted(nums), 100000
for i in range(len(nums)-k+1):
if nums[i+k-1] - nums[i] < ans: ans = nums[i+k-1] - nums[i]
return ans | minimum-difference-between-highest-and-lowest-of-k-scores | 5-Lines Python Solution || 50% Faster || Memory less than 80% | Taha-C | 0 | 98 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,731 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/1757004/Easy-Python-Solution | class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
nums.sort()
mdiff = inf
for i in range(k-1,len(nums)):
mdiff = min(mdiff,nums[i]-nums[i-k+1])
return mdiff | minimum-difference-between-highest-and-lowest-of-k-scores | Easy Python Solution | MengyingLin | 0 | 35 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,732 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/1713592/Python-or-Sort-and-Slide | class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
n = len(nums)
if n == 1:
return 0
nums.sort()
res = math.inf
for i in range(k-1, n):
res = min(res, nums[i] - nums[i-(k-1)])
... | minimum-difference-between-highest-and-lowest-of-k-scores | Python | Sort and Slide | jgroszew | 0 | 107 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,733 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/1617534/Python-3-sorting-solution | class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
nums.sort()
return min(nums[i] - nums[i-k+1] for i in range(k-1, len(nums))) | minimum-difference-between-highest-and-lowest-of-k-scores | Python 3 sorting solution | dereky4 | 0 | 131 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,734 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/1455782/Python3-Sliding-Window-Commented | class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
L = len(nums)
# catch fringe case
if L == 1:
return 0
# sorting simplifies this problem significantly
nums.sort()
# consider a sliding window o... | minimum-difference-between-highest-and-lowest-of-k-scores | Python3 Sliding Window Commented | manuel_lemos | 0 | 87 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,735 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/1435590/Python-sort.-Time%3A-O(N-log-N) | class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
nums.sort()
return min(nums[i+k-1] - nums[i] for i in range(len(nums)-k+1)) | minimum-difference-between-highest-and-lowest-of-k-scores | Python, sort. Time: O(N log N) | blue_sky5 | 0 | 67 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,736 |
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/discuss/1432539/Linear-TIme-(n%2Bm)-python-solution.-Counting-sort. | class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
minimal=float('inf')
counter=defaultdict(int)
maximum=float('-inf')
minimum=float('inf')
sortedArray=[]
for num in nums:
counter[num]+=1
maximum=ma... | minimum-difference-between-highest-and-lowest-of-k-scores | Linear TIme (n+m) python solution. Counting sort. | hasham | 0 | 90 | minimum difference between highest and lowest of k scores | 1,984 | 0.536 | Easy | 27,737 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/1432093/Python-or-The-Right-Way-during-Interview-or-Comparators | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
nums = sorted(map(int, nums), reverse=True)
return str(nums[k-1]) | find-the-kth-largest-integer-in-the-array | Python | The Right Way during Interview | Comparators | malraharsh | 28 | 1,800 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,738 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/1434018/Python3-3-approaches | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
return sorted(nums, key=int)[-k] | find-the-kth-largest-integer-in-the-array | [Python3] 3 approaches | ye15 | 6 | 617 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,739 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/1434018/Python3-3-approaches | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
pq = [] # min-heap
for x in nums:
heappush(pq, int(x))
if len(pq) > k: heappop(pq)
return str(pq[0]) | find-the-kth-largest-integer-in-the-array | [Python3] 3 approaches | ye15 | 6 | 617 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,740 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/1434018/Python3-3-approaches | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
nums = [int(x) for x in nums]
shuffle(nums)
def part(lo, hi):
"""Return partition of nums[lo:hi]."""
i, j = lo+1, hi-1
while i <= j:
if nums[i] < nums[lo... | find-the-kth-largest-integer-in-the-array | [Python3] 3 approaches | ye15 | 6 | 617 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,741 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/1431830/Python-1-line | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
return sorted(nums, key = int, reverse = True)[k-1] | find-the-kth-largest-integer-in-the-array | Python - 1 line | ajith6198 | 3 | 327 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,742 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/1537835/Python-One-Line-Only | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
res = []
for i in nums:
res.append(int(i))
res.sort(reverse = True)
return str(res[k-1]) | find-the-kth-largest-integer-in-the-array | Python One Line Only | aaffriya | 2 | 146 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,743 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/2185139/Super-simple-python-one-liner-solution | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
return str(sorted(list(map(int, nums)), reverse=True)[k-1]) | find-the-kth-largest-integer-in-the-array | Super simple python one-liner solution | pro6igy | 1 | 66 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,744 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/1827521/Simple-Python-solution-or-Two-lines-of-code-or-80-faster | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
nums = sorted(list(map(int,nums)))
return str(nums[-k]) | find-the-kth-largest-integer-in-the-array | ✔Simple Python solution | Two lines of code | 80% faster | Coding_Tan3 | 1 | 84 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,745 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/2851403/Python-oror-1-Liner-oror-94.90-Faster-oror-Sorting | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
return sorted(nums, key= lambda i : int(i))[-k] | find-the-kth-largest-integer-in-the-array | Python || 1 Liner || 94.90% Faster || Sorting | DareDevil_007 | 0 | 1 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,746 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/2808193/One-line-code-in-Python | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
return str(sorted([int(k) for k in nums], reverse=True)[k-1]) | find-the-kth-largest-integer-in-the-array | One line code in Python | DNST | 0 | 4 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,747 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/2798370/simple-python-solution-using-list-approach | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
v=[]
for i in range(0,len(nums)):
v.append(int(nums[i]))
v.sort()
return str(v[len(v)-k]) | find-the-kth-largest-integer-in-the-array | simple python solution using list approach | namangupta07729 | 0 | 1 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,748 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/2779161/python-Min-Heap | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
# O(n*log(k)), O(k)
nums = [int(i) for i in nums]
# heapq.heapify(nums)
# while len(nums) > k:
# heapq.heappop(nums)
# return str(nums[0])
return str(heapq.nlargest(k, nums)[-1]) | find-the-kth-largest-integer-in-the-array | python Min Heap | sahilkumar158 | 0 | 2 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,749 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/2755755/easy-heap-solution-in-python | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
nums = [int(i) for i in nums]
minhp = []
for i in nums:
heapq.heappush(minhp, i)
if len(minhp) > k:
heapq.heappop(minhp)
return str(minhp[0]) | find-the-kth-largest-integer-in-the-array | easy heap solution in python | ankurbhambri | 0 | 12 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,750 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/2645452/Heap-limited-at-K | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
pq = []
for e in nums:
heappush(pq, int(e))
if len(pq) > k:
heappop(pq)
return str(pq[0]) | find-the-kth-largest-integer-in-the-array | Heap limited at K | tomfran | 0 | 8 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,751 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/2362359/Python3-Solution | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
nums.sort(key=int,reverse = True)
for i in range(len(nums)-1, -1, -1):
if k-1 == i:
return nums[i] | find-the-kth-largest-integer-in-the-array | Python3 Solution | bharg4vi | 0 | 38 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,752 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/2180928/Python-solution-or-Sorting-or-Indexing | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
# Converting all elements in integers
for i in range(len(nums)):
nums[i] = int(nums[i])
# Sorting them
nums.sort()
# Returning the kth element from the back
retur... | find-the-kth-largest-integer-in-the-array | Python solution | Sorting | Indexing | yashkumarjha | 0 | 36 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,753 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/2134060/python-min-heap-solution-with-easy-understanding | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
minHeap = []
for i in nums:
heapq.heappush(minHeap, int(i))
if len(minHeap) > k:
heapq.heappop(minHeap)
return str(heapq.heappop(minHeap)) | find-the-kth-largest-integer-in-the-array | python min-heap solution with easy understanding | writemeom | 0 | 50 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,754 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/1953612/Python-Simple-Sorting-With-Lambda-or-Beats-93-Solution | class Solution(object):
def kthLargestNumber(self, nums, k):
"""
:type nums: List[str]
:type k: int
:rtype: str
"""
nums.sort(reverse = True, key=lambda x:int(x))
return nums[k-1] | find-the-kth-largest-integer-in-the-array | Python Simple Sorting With Lambda | Beats 93% Solution | AkashHooda | 0 | 62 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,755 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/1701910/Python3-Solution-oror-Using-map-and-sorted-oror | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
nums = sorted(map(int, nums), reverse = True)
return str(nums[k-1]) | find-the-kth-largest-integer-in-the-array | [Python3] Solution || Using map and sorted || | Cheems_Coder | 0 | 56 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,756 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/1538346/Python-easy-to-understand-heap-solution | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
import heapq
heap = []
for num in nums:
heapq.heappush(heap, -1*int(num))
for _ in range(k):
v = heapq.heappop(heap)
return str(-1*v) | find-the-kth-largest-integer-in-the-array | Python easy-to-understand heap solution | byuns9334 | 0 | 174 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,757 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/1450159/Brute-Force-oror-Easy-to-Understand-with-Complexity-Analysis | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
arr = []
for i in nums:
arr.append(int(i))
arr.sort()
n = len(nums)
return str(arr[n-k]) | find-the-kth-largest-integer-in-the-array | Brute Force || Easy to Understand with Complexity Analysis | aarushsharmaa | 0 | 110 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,758 |
https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/1431841/Python-Straightforward-method | class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
for i in range(len(nums)):
nums[i] = int(nums[i])
nums.sort()
nums.reverse()
return str(nums[k-1]) | find-the-kth-largest-integer-in-the-array | [Python] Straightforward method | hyj1116 | 0 | 32 | find the kth largest integer in the array | 1,985 | 0.447 | Medium | 27,759 |
https://leetcode.com/problems/minimum-number-of-work-sessions-to-finish-the-tasks/discuss/1433054/Python-or-Backtracking-or-664ms-or-100-time-and-space-or-Explanation | class Solution:
def minSessions(self, tasks: List[int], sessionTime: int) -> int:
subsets = []
self.ans = len(tasks)
def func(idx):
if len(subsets) >= self.ans:
return
if idx == len(tasks):
self.ans = min(self.ans,... | minimum-number-of-work-sessions-to-finish-the-tasks | Python | Backtracking | 664ms | 100% time and space | Explanation | detective_dp | 22 | 1,000 | minimum number of work sessions to finish the tasks | 1,986 | 0.331 | Medium | 27,760 |
https://leetcode.com/problems/minimum-number-of-work-sessions-to-finish-the-tasks/discuss/1433595/Python-recursion-%2B-tuple-memoization-no-bitmask-simple-64ms | class Solution:
def minSessions(self, tasks: List[int], sessionTime: int) -> int:
tasks = sorted(tasks)
@lru_cache(None)
def recur_fn(x,tasks):
if len(tasks) == 0:
return 1
ans = 0
result = []
if tasks[0] > x:
a... | minimum-number-of-work-sessions-to-finish-the-tasks | [Python] recursion + tuple memoization, no bitmask, simple 64ms | RJMCMC | 2 | 221 | minimum number of work sessions to finish the tasks | 1,986 | 0.331 | Medium | 27,761 |
https://leetcode.com/problems/minimum-number-of-work-sessions-to-finish-the-tasks/discuss/1536913/Python-backtracking-about-280ms-and-very-low-space-complexity | class Solution:
def minSessions(self, tasks: List[int], sessionTime: int) -> int:
# if sum fully divisible, the answer can be directly calculated
total_tasks = sum(tasks)
quotient, remainder = divmod(total_tasks, sessionTime)
sessions = []
ans = len(tasks) # cant ... | minimum-number-of-work-sessions-to-finish-the-tasks | Python backtracking about 280ms and very low space complexity | shabie | 1 | 374 | minimum number of work sessions to finish the tasks | 1,986 | 0.331 | Medium | 27,762 |
https://leetcode.com/problems/minimum-number-of-work-sessions-to-finish-the-tasks/discuss/1433314/Python3-bit-mask-dp | class Solution:
def minSessions(self, tasks: List[int], sessionTime: int) -> int:
@cache
def fn(mask, rem):
"""Return minimum work sessions to finish tasks indicated by set bits in mask."""
if not mask: return 0 # done
ans = inf
for i, x in ... | minimum-number-of-work-sessions-to-finish-the-tasks | [Python3] bit-mask dp | ye15 | 1 | 144 | minimum number of work sessions to finish the tasks | 1,986 | 0.331 | Medium | 27,763 |
https://leetcode.com/problems/minimum-number-of-work-sessions-to-finish-the-tasks/discuss/1431953/Python3-Greedy-%2B-DP | class Solution:
def minSessions(self, tasks: List[int], sessionTime: int) -> int:
# dp[ntasks+1][sesstionTime+1]
# Put large tasks first
tasks.sort(reverse=True)
tasks_ = [tasks[i] for i in range(len(tasks))]
nSession = 0
while len(tasks_) > 0:
# Put as... | minimum-number-of-work-sessions-to-finish-the-tasks | Python3, Greedy + DP | wu1meng2 | 1 | 307 | minimum number of work sessions to finish the tasks | 1,986 | 0.331 | Medium | 27,764 |
https://leetcode.com/problems/minimum-number-of-work-sessions-to-finish-the-tasks/discuss/2841842/Eliminate-overlapping-calculation-top-down-memo-python-simple-solution | class Solution:
def minSessions(self, tasks: List[int], sessionTime: int) -> int:
memo = dict()
def dfs(candidate, remain):
if not candidate:
return 1
if min(candidate) > remain:
return dfs(candidate, sessionTime) + 1
... | minimum-number-of-work-sessions-to-finish-the-tasks | Eliminate overlapping calculation / top-down memo / python simple solution | Lara_Craft | 0 | 3 | minimum number of work sessions to finish the tasks | 1,986 | 0.331 | Medium | 27,765 |
https://leetcode.com/problems/minimum-number-of-work-sessions-to-finish-the-tasks/discuss/2826718/Python-or-DFS-%2B-Binary-Search-or-bitmaskDP-or-Faster-than-99 | class Solution:
def minSessions(self, tasks: List[int], sessionTime: int) -> int:
def canFinish(i):
if i == nTask:
return True
for j in range(mid):
if remainTime[j] >= tasks[i]:
remainTime[j] -= tasks[i]
if canFi... | minimum-number-of-work-sessions-to-finish-the-tasks | Python | DFS + Binary Search | ❌bitmask❌DP | Faster than 99% | ryandoren | 0 | 3 | minimum number of work sessions to finish the tasks | 1,986 | 0.331 | Medium | 27,766 |
https://leetcode.com/problems/minimum-number-of-work-sessions-to-finish-the-tasks/discuss/1432907/Python-3-DP-and-bitmask-(2820ms) | class Solution:
def minSessions(self, tasks: List[int], sessionTime: int) -> int:
n = len(tasks)
@lru_cache(None)
def dp(mask, t):
if mask == (1 << n) - 1:
return 0
ans = n
for i in range(n):
if mask & (1 << i): continu... | minimum-number-of-work-sessions-to-finish-the-tasks | [Python 3] DP and bitmask (2820ms) | chestnut890123 | 0 | 158 | minimum number of work sessions to finish the tasks | 1,986 | 0.331 | Medium | 27,767 |
https://leetcode.com/problems/number-of-unique-good-subsequences/discuss/1433355/Python3-bit-mask-dp | class Solution:
def numberOfUniqueGoodSubsequences(self, binary: str) -> int:
@cache
def fn(i, mask, v):
"""Return # unique good subsequences starting with 1."""
if i == len(binary) or not mask: return v
x = int(binary[i])
if not mask & (... | number-of-unique-good-subsequences | [Python3] bit-mask dp | ye15 | 1 | 139 | number of unique good subsequences | 1,987 | 0.524 | Hard | 27,768 |
https://leetcode.com/problems/number-of-unique-good-subsequences/discuss/1433355/Python3-bit-mask-dp | class Solution:
def numberOfUniqueGoodSubsequences(self, binary: str) -> int:
f0 = f1 = 0
for ch in binary:
if ch == "0": f0 += f1
else: f1 += f0 + 1
return (f0 + f1 + int("0" in binary)) % 1_000_000_007 | number-of-unique-good-subsequences | [Python3] bit-mask dp | ye15 | 1 | 139 | number of unique good subsequences | 1,987 | 0.524 | Hard | 27,769 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/2321632/Python-98.85-faster-or-Simplest-solution-with-explanation-or-Beg-to-Adv-or-Prefix-Sum | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
left = 0 # nums[0] + nums[1] + ... + nums[middleIndex-1]
right = sum(nums) # nums[middleIndex+1] + nums[middleIndex+2] + ... + nums[nums.length-1]
for i, num in enumerate(nums): # we can use normal for loop as well.
... | find-the-middle-index-in-array | Python 98.85% faster | Simplest solution with explanation | Beg to Adv | Prefix Sum | rlakshay14 | 8 | 224 | find the middle index in array | 1,991 | 0.673 | Easy | 27,770 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/1444181/Python-Prefix-sum | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
A = [0] + list(accumulate(nums)) + [0]
total, n = sum(nums), len(nums)
for i in range(n):
if A[i] == total - A[i] - nums[i]:
return i
return -1 | find-the-middle-index-in-array | Python - Prefix sum | ajith6198 | 5 | 558 | find the middle index in array | 1,991 | 0.673 | Easy | 27,771 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/2175411/PYTHON-Solution-oror-Beginner-friendly | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
total = sum(nums)
size = len(nums)
for i in range(size):
if (sum(nums[:i]) == sum(nums[i+1:])) and i < (size - 1) :
return i
elif i == (size - 1) and (total-nums[-1]) == 0:
... | find-the-middle-index-in-array | PYTHON Solution || Beginner friendly | Shivam_Raj_Sharma | 3 | 114 | find the middle index in array | 1,991 | 0.673 | Easy | 27,772 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/1444092/Python3-prefix-sum | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
total = sum(nums)
prefix = 0
for i, x in enumerate(nums):
if 2*prefix == total - x: return i
prefix += x
return -1 | find-the-middle-index-in-array | [Python3] prefix sum | ye15 | 3 | 79 | find the middle index in array | 1,991 | 0.673 | Easy | 27,773 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/2544957/Python-Commented-Rolling-Window | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
# find the sum
right_sum = sum(nums)
left_sum = 0
for idx in range(len(nums)):
# subtract current num from right sum
right_sum -= nums[idx]
... | find-the-middle-index-in-array | [Python] - Commented Rolling Window | Lucew | 1 | 24 | find the middle index in array | 1,991 | 0.673 | Easy | 27,774 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/1548703/Python%3A-Left-sum-and-Right-sum | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
lsum,rsum=0,sum(nums)
for i in range(len(nums)):
rsum-=nums[i]
if lsum==rsum:
return i
lsum+=nums[i]
return -1 | find-the-middle-index-in-array | Python: Left sum and Right sum | mesiddyy | 1 | 421 | find the middle index in array | 1,991 | 0.673 | Easy | 27,775 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/2805728/Python-very-easy-solution-using-String-Slice | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
for i in range(0,len(nums)):
if sum(nums[0:i]) == sum(nums[i+1:]):
return i
return -1 | find-the-middle-index-in-array | Python very easy solution using String Slice | khanismail_1 | 0 | 2 | find the middle index in array | 1,991 | 0.673 | Easy | 27,776 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/2798482/Easy-approach | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
n=len(nums)
for i in range (n):
a=sum(nums[:i])
b=sum(nums[i+1:])
if a==b:
return i
else :
return -1 | find-the-middle-index-in-array | Easy approach | nishithakonuganti | 0 | 1 | find the middle index in array | 1,991 | 0.673 | Easy | 27,777 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/2751388/Simple-Python-Solution | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
n = len(nums)
running_sum = [0 for _ in range(n)]
for i in range(n):
running_sum[i] = running_sum[i - 1] + nums[i] if i > 0 else nums[i]
for i in range(n):
left = running_... | find-the-middle-index-in-array | Simple Python Solution | mansoorafzal | 0 | 4 | find the middle index in array | 1,991 | 0.673 | Easy | 27,778 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/2689592/97-Accepted-Solution-or-Easy-to-Understand-or-Python | class Solution(object):
def findMiddleIndex(self, nums):
rSum = sum(nums)
lSum = 0
for i in range(len(nums)):
rSum -= nums[i]
if lSum == rSum: return i
lSum += nums[i]
return -1 | find-the-middle-index-in-array | 97% Accepted Solution | Easy to Understand | Python | its_krish_here | 0 | 4 | find the middle index in array | 1,991 | 0.673 | Easy | 27,779 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/2641729/Beats-81-oror-Clean-Python-code-oror-Intuitive | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
if len(nums) == 1:
return 0
leftSum = 0
rightSum = sum(nums)
for i in range(len(nums)):
rightSum -= nums[i]
if leftSum == rightSum:
return i
leftSum += ... | find-the-middle-index-in-array | Beats 81% || Clean Python code || Intuitive | mayankleetcode | 0 | 3 | find the middle index in array | 1,991 | 0.673 | Easy | 27,780 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/2631067/Python3-or-Solved-Using-Prefix-Sum-in-O(N)-Time-and-Space! | class Solution:
#Let n = len(nums)!
#Time-Complexity: O(n + n * 1) -> O(n)
#Space-Complexity: O(n)
def findMiddleIndex(self, nums: List[int]) -> int:
#Approach: Keep track of prefix sums using an array. Then, for each index pos, get the left
#and right sum! Once we have a match, we can i... | find-the-middle-index-in-array | Python3 | Solved Using Prefix Sum in O(N) Time and Space! | JOON1234 | 0 | 7 | find the middle index in array | 1,991 | 0.673 | Easy | 27,781 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/2505724/Simple-Python-Solution-faster-than-95-percent | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
total=sum(nums)
left_sum=0
right_sum=0
for i in range(len(nums)):
if i==0:
left_sum=0
right_sum=total-left_sum-nums[i]
else:
left_sum=sum(nums[:i... | find-the-middle-index-in-array | Simple Python Solution [faster than 95 percent] | deepanshu704281 | 0 | 18 | find the middle index in array | 1,991 | 0.673 | Easy | 27,782 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/2309897/Python3-Simple-solution... | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
# Calculate the sum of all elements
sumElements = sum(nums)
sumSoFar = 0
for i in range(len(nums)):
if i - 1 >= 0:
sumSoFar += nums[i-1]
if sumElements - nums[i] == 2 * sumSoFar... | find-the-middle-index-in-array | [Python3] Simple solution... | Gp05 | 0 | 28 | find the middle index in array | 1,991 | 0.673 | Easy | 27,783 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/2306756/Python3-Simple-Solution | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
N = len(nums)
if N == 1: return 0
i, arrL, arrR = 1, [0]*N, [0]*N
arrL[0], arrR[-1] = nums[0], nums[-1]
while i < N:
arrL[i] = arrL[i-1] + nums[i]
arrR[N - i - 1] = arrR[N - i... | find-the-middle-index-in-array | Python3 Simple Solution | mediocre-coder | 0 | 21 | find the middle index in array | 1,991 | 0.673 | Easy | 27,784 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/2303897/Python-95-Faster | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
def pivotIndex(nums):
total_sum = sum(nums)
left_sum = 0
for i in range(0,len(nums)):
if total_sum-left_sum-nums[i] == left_sum:
return i
left_sum+= nums... | find-the-middle-index-in-array | Python 95% Faster | Abhi_009 | 0 | 19 | find the middle index in array | 1,991 | 0.673 | Easy | 27,785 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/2233517/Python3-Solution-with-using-prefix-sum | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
right_sum = sum(nums)
left_sum = 0
for i in range(len(nums)):
right_sum -= nums[i]
if left_sum == right_sum:
return i
left_sum += nums[i]
... | find-the-middle-index-in-array | [Python3] Solution with using prefix sum | maosipov11 | 0 | 25 | find the middle index in array | 1,991 | 0.673 | Easy | 27,786 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/2143781/Very-Simple-python-solution-(one-pass-with-single-if-statement) | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
for i in range(len(nums)):
if sum(nums[:i]) == sum(nums[i+1:]):
return i
return -1 | find-the-middle-index-in-array | Very Simple python solution (one pass with single if statement) | shree050102 | 0 | 47 | find the middle index in array | 1,991 | 0.673 | Easy | 27,787 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/2044576/Python-simple-solution | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
for i in range(len(nums)):
if sum(nums[:i]) == sum(nums[i+1:]):
return i
return -1 | find-the-middle-index-in-array | Python simple solution | StikS32 | 0 | 60 | find the middle index in array | 1,991 | 0.673 | Easy | 27,788 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/1919111/Python-easy-solution-with-memory-less-than-97 | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
for i in range(len(nums)):
if sum(nums[:i]) == sum(nums[i+1:]):
return i
return -1 | find-the-middle-index-in-array | Python easy solution with memory less than 97% | alishak1999 | 0 | 68 | find the middle index in array | 1,991 | 0.673 | Easy | 27,789 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/1880567/Python-dollarolution | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
leftsum = 0
rightsum = sum(nums)
for i in range(len(nums)):
x = nums[i]
rightsum -= x
if leftsum == rightsum:
return i
leftsum += x
return -1 | find-the-middle-index-in-array | Python $olution | AakRay | 0 | 50 | find the middle index in array | 1,991 | 0.673 | Easy | 27,790 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/1869880/Cumulative-Sum-solution | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
if len(nums) == 1:
return 0
if sum(nums[1:]) == 0:
return 0
res = [nums[0]]
for i in range(1, len(nums)):
res.append(nums[i] + res[-1]) # cumulative sum
fo... | find-the-middle-index-in-array | Cumulative Sum solution | yuranusduke | 0 | 22 | find the middle index in array | 1,991 | 0.673 | Easy | 27,791 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/1806354/3-Lines-Python-Solution-oror-80-Faster-oror-Memory-less-than-60 | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
for i in range(len(nums)):
if sum(nums[:i])==sum(nums[i+1:]): return i
return -1 | find-the-middle-index-in-array | 3-Lines Python Solution || 80% Faster || Memory less than 60% | Taha-C | 0 | 56 | find the middle index in array | 1,991 | 0.673 | Easy | 27,792 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/1787783/Python3-accepted-solution | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
for i in range(len(nums)):
if(sum(nums[:i]) == sum(nums[i+1:])):return i
return -1 | find-the-middle-index-in-array | Python3 accepted solution | sreeleetcode19 | 0 | 37 | find the middle index in array | 1,991 | 0.673 | Easy | 27,793 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/1757019/Python-Easy-sOLUTION | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
sumn = sum(nums)
for i in range(len(nums)):
ls = sum(nums[:i])
if ls == sumn-ls - nums[i]:
return i
return -1 | find-the-middle-index-in-array | Python Easy sOLUTION | MengyingLin | 0 | 41 | find the middle index in array | 1,991 | 0.673 | Easy | 27,794 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/1689605/Python3-Memory-Less-Than-98.10 | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
before, after = 0, sum(nums)
for i in range(len(nums)):
after -= nums[i]
if before == after:
return i
before += nums[i]
return -1 | find-the-middle-index-in-array | Python3 Memory Less Than 98.10% | Hejita | 0 | 62 | find the middle index in array | 1,991 | 0.673 | Easy | 27,795 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/1609849/Python-oror-O(n)-time-oror-O(n)-space | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
prefix_sum = [0] * len(nums)
prefix_sum[0] = nums[0]
for i in range(1,len(nums)):
prefix_sum[i] = prefix_sum[i-1] + nums[i]
for i in range(len(nums)):
if i == 0:
left =... | find-the-middle-index-in-array | Python || O(n) time || O(n) space | s_m_d_29 | 0 | 67 | find the middle index in array | 1,991 | 0.673 | Easy | 27,796 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/1570460/Using-accumulate-97-speed | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
accu = list(accumulate(nums))
len_nums1 = len(nums) - 1
for i, n in enumerate(nums):
if i == 0:
if accu[-1] - nums[0] == 0:
return 0
elif i == len_nums1:
... | find-the-middle-index-in-array | Using accumulate, 97% speed | EvgenySH | 0 | 48 | find the middle index in array | 1,991 | 0.673 | Easy | 27,797 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/1451015/simple-python-solution | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
middle = float(inf)
for i in range(len(nums)):
if sum(nums[:i])-sum(nums[i+1:])==0:
middle = min(i,middle)
return middle if middle!=float(inf) else -1 | find-the-middle-index-in-array | simple python solution | pheobhe | 0 | 49 | find the middle index in array | 1,991 | 0.673 | Easy | 27,798 |
https://leetcode.com/problems/find-the-middle-index-in-array/discuss/1444997/Intuitive-approach-by-sliding-window | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
left_sum, right_sum, num_len = 0, sum(nums[1:]), len(nums)
if left_sum == right_sum:
return 0
for i in range(1, num_len):
left_sum += nums[i-1]
right_sum -= nums[i]
... | find-the-middle-index-in-array | Intuitive approach by sliding window | puremonkey2001 | 0 | 13 | find the middle index in array | 1,991 | 0.673 | Easy | 27,799 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.