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/maximum-points-you-can-obtain-from-cards/discuss/2201249/Python3-or-Oneline-solution
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: ans = sum(cardPoints[:k]) ma = ans for i in range(k): ma += cardPoints[-i-1] - cardPoints[k-1-i] ans = max(ma, ans) return ans
maximum-points-you-can-obtain-from-cards
Python3 | Oneline solution
n124345679976
0
9
maximum points you can obtain from cards
1,423
0.523
Medium
21,300
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2201249/Python3-or-Oneline-solution
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: return max(0, max(itertools.accumulate(cardPoints[-i-1] - cardPoints[k-1-i] for i in range(k)))) + sum(cardPoints[:k])
maximum-points-you-can-obtain-from-cards
Python3 | Oneline solution
n124345679976
0
9
maximum points you can obtain from cards
1,423
0.523
Medium
21,301
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2201146/Python-Recursion-%2B-Memoization-%2B-Sliding-Window
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: return self.helper(cardPoints, k, 0, len(cardPoints) - 1) def helper(self, arr, k, start, end): # Base Cases if k <= 0: return 0 if start > end: return 0 # Score calculated by taking the score from start score1 = arr[start] + self.helper(arr, k - 1, start + 1, end) # Score calculated by taking the score from end score2 = arr[end] + self.helper(arr, k - 1, start, end - 1) # Calculating Max Score return max(score1, score2)
maximum-points-you-can-obtain-from-cards
Python Recursion + Memoization + Sliding Window
zippysphinx
0
35
maximum points you can obtain from cards
1,423
0.523
Medium
21,302
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2201146/Python-Recursion-%2B-Memoization-%2B-Sliding-Window
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: memo = [[[-1 for _ in range(k + 1)] for i in range(len(cardPoints) + 1)] for _ in range(len(cardPoints) + 1)] return self.helper(cardPoints, k, 0, len(cardPoints) - 1, memo) def helper(self, arr, k, start, end, memo): if k <= 0: return 0 if start > end: return 0 if memo[start][end][k] != -1: return memo[start][end][k] score1 = arr[start] + self.helper(arr, k - 1, start + 1, end, memo) score2 = arr[end] + self.helper(arr, k - 1, start, end - 1, memo) memo[start][end][k] = max(score1, score2) return memo[start][end][k]
maximum-points-you-can-obtain-from-cards
Python Recursion + Memoization + Sliding Window
zippysphinx
0
35
maximum points you can obtain from cards
1,423
0.523
Medium
21,303
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2201146/Python-Recursion-%2B-Memoization-%2B-Sliding-Window
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: # Instead of finding max Sum from k elements from start or end # we can find minimum Sum of a subarray of length (len(CardPoints) - k) # them, if we subtract that minimum value from totalSum, it # will automatically give maximum values from start and end subarrayLengthToFind = len(cardPoints) - k minSubArraySum = curr = sum(cardPoints[:subarrayLengthToFind]) for i in range(len(cardPoints) - subarrayLengthToFind): curr += cardPoints[subarrayLengthToFind + i] - cardPoints[i] minSubArraySum = min(minSubArraySum, curr) return sum(cardPoints) - minSubArraySum
maximum-points-you-can-obtain-from-cards
Python Recursion + Memoization + Sliding Window
zippysphinx
0
35
maximum points you can obtain from cards
1,423
0.523
Medium
21,304
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2200205/Walk-once-and-beats-92
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: l = len(cardPoints) window_size = l - k all_sum = cur_win_sum = win_sum = sum(cardPoints[:window_size])`` for idx in range(window_size, l): cur_win_sum += - cardPoints[idx - window_size] + cardPoints[idx] all_sum += cardPoints[idx] win_sum = min(cur_win_sum, win_sum) return all_sum - win_sum
maximum-points-you-can-obtain-from-cards
Walk once and beats 92%
coolshinji
0
4
maximum points you can obtain from cards
1,423
0.523
Medium
21,305
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2200071/Simple-Python-Solutions-with-Explanation
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: new_k = len(cardPoints) - k total_sum, k_sum = 0, 0 # we can do total_sum = sum(cardPoints) # k_sum = sum(cardPoints[:new_k]) for i in range(len(cardPoints)): total_sum += cardPoints[i] if i < new_k: k_sum += cardPoints[i] res = total_sum - k_sum for i in range(1, k+1): k_sum -= cardPoints[i-1] k_sum += cardPoints[i+new_k-1] res = max(res, total_sum - k_sum) return res
maximum-points-you-can-obtain-from-cards
Simple Python Solutions with Explanation
atiq1589
0
12
maximum points you can obtain from cards
1,423
0.523
Medium
21,306
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2200070/Python-Solution
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: sum1 = sum(cardPoints[:k]) sum1_temp = sum1 for i in range(k): sub_left = k-i-1 add_right = len(cardPoints)-i-1 sum1_temp = sum1_temp-cardPoints[sub_left] sum1_temp = sum1_temp+cardPoints[add_right] sum1 = max(sum1, sum1_temp) return sum1
maximum-points-you-can-obtain-from-cards
Python Solution
yashkumarjha
0
16
maximum points you can obtain from cards
1,423
0.523
Medium
21,307
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2199494/Python-Simple-Python-Solution-Using-Sliding-Window
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: total_sum = sum(cardPoints) length = len(cardPoints) - k sum_subarray = sum(cardPoints[:length]) result = total_sum - sum_subarray for i in range(1, len(cardPoints)-length+1): sum_subarray = sum_subarray + cardPoints[i+length-1] - cardPoints[i-1] result = max(result, total_sum - sum_subarray) return result
maximum-points-you-can-obtain-from-cards
[ Python ] ✅✅ Simple Python Solution Using Sliding Window 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
0
41
maximum points you can obtain from cards
1,423
0.523
Medium
21,308
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2199340/Easy-Sol.-clean-readable-code-Maximum-Points-You-Can-Obtain-from-Cards.
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: if len(cardPoints)==k: return sum(cardPoints) total=sum(cardPoints) size=len(cardPoints)-k curr=sum(cardPoints[:size]) j=0 ans=curr for i in range(size,len(cardPoints)): curr+=cardPoints[i] curr-=cardPoints[j] ans=min(ans,curr) j+=1 return total-ans
maximum-points-you-can-obtain-from-cards
Easy Sol. clean readable code Maximum Points You Can Obtain from Cards.
udaysinghania20
0
2
maximum points you can obtain from cards
1,423
0.523
Medium
21,309
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2198736/Sliding-window
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: n, score = len(cardPoints), sum(cardPoints[:k]) if k == n: return score max_score, start, end = score, n - 1, k - 1 while end >= 0: max_score = max(max_score, score := score + cardPoints[start] - cardPoints[end]) start, end = start - 1, end - 1 return max_score
maximum-points-you-can-obtain-from-cards
Sliding window
Xie-Fangyuan
0
8
maximum points you can obtain from cards
1,423
0.523
Medium
21,310
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2198711/Python3-oror-Sliding-Window-oror-Self-Explanatory-oror-Easy-Understanding
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: if k==len(cardPoints): return sum(cardPoints) total_sum=sum(cardPoints) p1=0 p2=len(cardPoints)-k ongoing_sum=sum(cardPoints[p1:p2]) prev_sum=ongoing_sum while p1<=p2 and p2<len(cardPoints): if ongoing_sum<prev_sum: prev_sum=ongoing_sum ongoing_sum-=cardPoints[p1] ongoing_sum+=cardPoints[p2] p1+=1 p2+=1 if ongoing_sum<prev_sum: prev_sum=ongoing_sum return total_sum-prev_sum
maximum-points-you-can-obtain-from-cards
Python3 || Sliding Window || Self-Explanatory || Easy-Understanding
bug_buster
0
15
maximum points you can obtain from cards
1,423
0.523
Medium
21,311
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2198709/Python3-10-lines-Super-easy-or-Sliding-window-or-Efficient
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: l = 0 r = len(cardPoints) - k total = sum(cardPoints[r:]) # Intial total result = total while r < len(cardPoints): total = total + cardPoints[l] - cardPoints[r] result = max(result, total) l += 1 r += 1 return result
maximum-points-you-can-obtain-from-cards
✅Python3 - 10 lines Super easy | Sliding window | Efficient
thesauravs
0
6
maximum points you can obtain from cards
1,423
0.523
Medium
21,312
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2198601/Python-Easy-Solution-using-sliding-window-or-Time-O(k)-or-Space-O(1)
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: leftPointer, rightPointer = 0, len(cardPoints) - k total = sum(cardPoints[rightPointer:]) result = total while rightPointer < len(cardPoints): total += (cardPoints[leftPointer] - cardPoints[rightPointer]) result = max(total,result) leftPointer += 1 rightPointer += 1 return result
maximum-points-you-can-obtain-from-cards
Python Easy Solution using sliding window | Time O(k) | Space O(1)
nishanrahman1994
0
9
maximum points you can obtain from cards
1,423
0.523
Medium
21,313
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2198581/Python-Easy-Solution-Easy-To-Understand-100
class Solution: def maxScore(self, card: List[int], k: int) -> int: sm=sum(card[:k]) mx=sm for i in range(1,k+1): sm=sm+card[-i]-card[k-i] mx=max(mx,sm) return mx ```
maximum-points-you-can-obtain-from-cards
Python Easy Solution, Easy To Understand 100%
vaibhav0077
0
9
maximum points you can obtain from cards
1,423
0.523
Medium
21,314
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2197804/python-3-or-simple-O(k)O(1)-solution
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: n = len(cardPoints) maxScore = score = sum(cardPoints[i] for i in range(n - k, n)) for i in range(k): score += cardPoints[i] score -= cardPoints[n - k + i] maxScore = max(maxScore, score) return maxScore
maximum-points-you-can-obtain-from-cards
python 3 | simple O(k)/O(1) solution
dereky4
0
26
maximum points you can obtain from cards
1,423
0.523
Medium
21,315
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2197798/Python-sliding-window.-O(k)O(1)
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: result = s = sum(cardPoints[:k]) for i in range(k): s += -cardPoints[k-1-i] + cardPoints[-1-i] result = max(result, s) return result
maximum-points-you-can-obtain-from-cards
Python, sliding window. O(k)/O(1)
blue_sky5
0
8
maximum points you can obtain from cards
1,423
0.523
Medium
21,316
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2197734/Python-or-Easy-to-Understand-or-With-Explanation-or-Sliding-Window
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: # problem transformation: # let's assume the length of the cardPoints is n # to find the maximum points we can obtain from cards # we just need to find the (n - k) contiguous cards that have the minimum points # Sliding Window GoGoGo!!! s = sum(cardPoints) n = len(cardPoints) length = n - k min_sum = cur_sum = sum(cardPoints[:length]) for i in range(length, n): cur_sum = cur_sum + cardPoints[i] - cardPoints[i-length] min_sum = min(cur_sum, min_sum) return s - min_sum
maximum-points-you-can-obtain-from-cards
Python | Easy to Understand | With Explanation | Sliding Window
Mikey98
0
16
maximum points you can obtain from cards
1,423
0.523
Medium
21,317
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2182452/Recursive-%2B-DP-Solution-oror-Easy-to-Understand
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: n = len(cardPoints) if k == 1: return max(cardPoints[0], cardPoints[-1]) maximumScore = max(cardPoints[0] + self.maxScore(cardPoints[1:], k - 1), cardPoints[n - 1] + self.maxScore(cardPoints[:n - 1], k - 1)) return maximumScore
maximum-points-you-can-obtain-from-cards
Recursive + DP Solution || Easy to Understand
Vaibhav7860
0
96
maximum points you can obtain from cards
1,423
0.523
Medium
21,318
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2182452/Recursive-%2B-DP-Solution-oror-Easy-to-Understand
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: dpArray = [0 for i in range(k + 1)] dpArray[0] = sum(cardPoints[:k]) for i in range(1, k + 1): dpArray[i] = dpArray[i - 1] - cardPoints[k - i] + cardPoints[-i] return max(dpArray)
maximum-points-you-can-obtain-from-cards
Recursive + DP Solution || Easy to Understand
Vaibhav7860
0
96
maximum points you can obtain from cards
1,423
0.523
Medium
21,319
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/2035234/Python-easy-to-read-and-understand-or-sliding-window
class Solution: def maxScore(self, nums: List[int], k: int) -> int: n = len(nums) window = n-k total = sum(nums) i, j = 0, window sums = sum(nums[:window]) ans = total-sums while j < n: sums = sums + (nums[j]-nums[i]) ans = max(ans, total-sums) i, j = i+1, j+1 return ans
maximum-points-you-can-obtain-from-cards
Python easy to read and understand | sliding-window
sanial2001
0
46
maximum points you can obtain from cards
1,423
0.523
Medium
21,320
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/1988560/Find-minimum-sum-sub-array-of-size-len-k
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: if k==len(cardPoints): return sum(cardPoints) m = len(cardPoints)-k i = 0 j = m-1 s = sum(cardPoints[0:m]) min_sum = s while j<len(cardPoints)-1: s-=cardPoints[i] i+=1 j+=1 s+=cardPoints[j] if s<min_sum: min_sum = s return sum(cardPoints)-min_sum
maximum-points-you-can-obtain-from-cards
Find minimum sum sub-array of size len-k
Brillianttyagi
0
40
maximum points you can obtain from cards
1,423
0.523
Medium
21,321
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/1450547/Python-O(k)-time-O(1)-space-super-easy-to-understand
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: n = len(cardPoints) left, right = (-k) % n, (-1) % n s = sum(cardPoints[left:right+1]) maxv = s for i in range(k): s = s - cardPoints[left%n] + cardPoints[(right+1)%n] maxv = max(maxv, s) left += 1 right += 1 return maxv
maximum-points-you-can-obtain-from-cards
Python O(k) time, O(1) space, super easy to understand
byuns9334
0
138
maximum points you can obtain from cards
1,423
0.523
Medium
21,322
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/1433700/PythonPython3-Solution-using-sliding-window
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: if k == 0: return 0 if k >= len(cardPoints): return sum(cardPoints) # Only considered those points which are in range, i.e. k cards # from begining and k cards from end cardPoints = cardPoints[:k] + cardPoints[-k:] # array changed here # Varibales to handle slinding window add_k = 0 # include ith element sub_k = 0 # exclude (k+i)th element left_k = sum(cardPoints[:k]) # sum of Left most k element right_k = sum(cardPoints[-k:]) # sum of right most k element maxsum = max(left_k, right_k) # possible max value # sliding window, from rigth most window, i.e. consider rotation for i in range(k): add_k += cardPoints[i] sub_k += cardPoints[k+i] maxsum = max(right_k - sub_k + add_k, maxsum) return maxsum
maximum-points-you-can-obtain-from-cards
[Python/Python3] Solution using sliding window
ssshukla26
0
109
maximum points you can obtain from cards
1,423
0.523
Medium
21,323
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/1194771/Python-3-faster-than-94.56
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: left = k right = len(cardPoints) total = sum(cardPoints[:left]) sum_left = total sum_right = 0 while True: left -= 1 right -= 1 sum_left -= cardPoints[left] sum_right += cardPoints[right] total = total if total > sum_left + sum_right else sum_left + sum_right if left == 0: break return total
maximum-points-you-can-obtain-from-cards
Python 3 faster than 94.56%
troubleboi
0
69
maximum points you can obtain from cards
1,423
0.523
Medium
21,324
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/1119390/Python-Sliding-window
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: cur = sum(cardPoints[:k]) best = cur for i in range(k - 1, -1, -1): cur = cur - cardPoints[i] + cardPoints[i - k] best = max(best, cur) return best
maximum-points-you-can-obtain-from-cards
[Python] Sliding window
modusV
0
90
maximum points you can obtain from cards
1,423
0.523
Medium
21,325
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/1062761/Elegant-Python-solution-faster-than-99.97
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: k_ = len(cardPoints)-k total = sum(cardPoints[:k_]) minimum = total for i in range(len(cardPoints)-k_): total += cardPoints[i+k_] - cardPoints[i] if total < minimum: minimum = total return sum(cardPoints)-minimum
maximum-points-you-can-obtain-from-cards
Elegant Python solution, faster than 99.97%
111989
0
175
maximum points you can obtain from cards
1,423
0.523
Medium
21,326
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/993001/Python-easy-O(k)-solution-with-explanation
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: ''' The optimal score for this game is going to have us take L elements from the left, and R elements from the right, where L+R = k. We can start with a full (length k) group on the left, then shrink the left group and expand the right group until left is empty and right is full. We have now covered every possible score for this game. Track the max as you go. Time: O(k), k to to create the first left group and k to compute all scores. Space: O(1), we just use 5 integer variables. ''' # Note: this uses the sum of a generator and is O(1) space, whereas sum(cardPoints[:k]) creates a new list. left_sum = sum(cardPoints[i] for i in range(k)) right_sum = 0 left_pointer = k-1 right_pointer = len(cardPoints) - 1 answer = left_sum while left_pointer >= 0: left_sum -= cardPoints[left_pointer] right_sum += cardPoints[right_pointer] answer = max(answer, left_sum + right_sum) left_pointer -= 1 right_pointer -= 1 return answer
maximum-points-you-can-obtain-from-cards
Python easy O(k) solution with explanation
gins1
0
89
maximum points you can obtain from cards
1,423
0.523
Medium
21,327
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/844861/intuitively-3-lines-with-short-comments
class Solution: def maxScore(self, cardPoints, k): if len(cardPoints) <= k: return sum(cardPoints) # window move is increasing from head: cardPoints[0] and decreasing from tail window = (cardPoints[i] - cardPoints[-k+1*i] for i in range(k)) # slide window start from tail, initial sum is cardPoints[-k:] return max(accumulate(window, initial = sum(cardPoints[-k:])))
maximum-points-you-can-obtain-from-cards
intuitively 3 lines with short comments
MarcoChang
0
91
maximum points you can obtain from cards
1,423
0.523
Medium
21,328
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/597915/Python3-Sliding-window-easy-to-understand.-O(k)-time-and-O(1)-memory.
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: M = sum(cardPoints[:k]) temp = M for i in range(1, k+1): temp += cardPoints[-i] temp -= cardPoints[k-i] if temp > M: M = temp return M
maximum-points-you-can-obtain-from-cards
[Python3] Sliding window, easy to understand. O(k) time and O(1) memory.
icespeech
0
62
maximum points you can obtain from cards
1,423
0.523
Medium
21,329
https://leetcode.com/problems/diagonal-traverse-ii/discuss/1866412/Python-easy-to-read-and-understand-or-hashmap
class Solution: def findDiagonalOrder(self, nums: List[List[int]]) -> List[int]: d = collections.defaultdict(list) for i in range(len(nums)): for j in range(len(nums[i])): d[(i+j)].append(nums[i][j]) ans = [] for key in d: ans += d[key][::-1] return ans
diagonal-traverse-ii
Python easy to read and understand | hashmap
sanial2001
0
58
diagonal traverse ii
1,424
0.504
Medium
21,330
https://leetcode.com/problems/diagonal-traverse-ii/discuss/597934/Python3-sorting
class Solution: def findDiagonalOrder(self, nums: List[List[int]]) -> List[int]: idx, val = [], [] for i, row in enumerate(nums): for j, num in enumerate(row): idx.append((i+j, -i)) val.append(num) _, ans = zip(*sorted(zip(idx, val))) return ans
diagonal-traverse-ii
[Python3] sorting
ye15
0
18
diagonal traverse ii
1,424
0.504
Medium
21,331
https://leetcode.com/problems/constrained-subsequence-sum/discuss/2672473/Python-or-Deque
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: n = len(nums) dp = [0]*n q = deque() for i, num in enumerate(nums): if i > k and q[0] == dp[i-k-1]: q.popleft() dp[i] = max(q[0] if q else 0, 0)+num while q and q[-1] < dp[i]: q.pop() q.append(dp[i]) return max(dp)
constrained-subsequence-sum
Python | Deque
jainsiddharth99
0
6
constrained subsequence sum
1,425
0.474
Hard
21,332
https://leetcode.com/problems/constrained-subsequence-sum/discuss/2664591/Python-monotonic-decreasing-queue-time-O(N)
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: n = len(nums) dp, mono_dec_q, res = [nums[0]] * n, deque([0]), nums[0] for i in range(1, n): dp[i] = nums[i] + max(0, dp[mono_dec_q[0]]) while mono_dec_q and dp[i] >= dp[mono_dec_q[-1]]: mono_dec_q.pop() if mono_dec_q and i - mono_dec_q[0] == k: mono_dec_q.popleft() mono_dec_q.append(i) res = max(res, dp[i]) return res
constrained-subsequence-sum
Python monotonic decreasing queue time O(N)
JasonDecode
0
5
constrained subsequence sum
1,425
0.474
Hard
21,333
https://leetcode.com/problems/constrained-subsequence-sum/discuss/2652789/Monotonic-queue-in-Python-faster-than-98.47
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: q = deque() for i, v in enumerate(nums): if len(q): nums[i] += nums[q[0]] if q and i - q[0] >= k: q.popleft() if nums[i] > 0: while q and nums[q[-1]] <= nums[i]: q.pop() q.append(i) return max(nums)
constrained-subsequence-sum
Monotonic queue in Python, faster than 98.47%
metaphysicalist
0
8
constrained subsequence sum
1,425
0.474
Hard
21,334
https://leetcode.com/problems/constrained-subsequence-sum/discuss/2330842/Python3-Decreasing-monotonic-deque-without-copying-the-original-array
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: max_dq = deque() max_sum = float('-inf') for i in range(len(nums)): local_max = nums[i] local_max += max_dq[0][1] if max_dq else 0 max_sum = max(max_sum, local_max) while max_dq and max_dq[-1][1] < local_max: max_dq.pop() # max_dq can be empty. In this case, the new coming candidate is the new coming number itself if local_max > 0: max_dq.append((i, local_max)) # remove updated element if max_dq and max_dq[0][0] == i-k: max_dq.popleft() return max_sum
constrained-subsequence-sum
[Python3] Decreasing monotonic deque without copying the original array
bymyself1208
0
25
constrained subsequence sum
1,425
0.474
Hard
21,335
https://leetcode.com/problems/constrained-subsequence-sum/discuss/2273270/Python3-or-Decreasing-Deque-or-O(N)
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: sw=deque() n=len(nums) currSum=0 ans=-float('inf') for i in range(n): if sw and i-k>sw[0][0]: sw.popleft() if len(sw): currSum=max(nums[i],nums[i]+sw[0][1]) else: currSum=nums[i] #print(currSum,sw) while sw and sw[-1][1]<=currSum: sw.pop() sw.append([i,currSum]) ans=max(ans,currSum) return ans
constrained-subsequence-sum
[Python3] | Decreasing Deque | O(N)
swapnilsingh421
0
16
constrained subsequence sum
1,425
0.474
Hard
21,336
https://leetcode.com/problems/constrained-subsequence-sum/discuss/2048733/python-monotonic-deque-solution-faster-than-99.11
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: q = deque() for i in range(len(nums)) : nums[i] += q[0] if q else 0 while q and q[-1] < nums[i] : q.pop() if nums[i] > 0 : q.append(nums[i]) if i >= k and q and q[0] == nums[i-k] : q.popleft() # print(nums) return max(nums)
constrained-subsequence-sum
python monotonic deque solution faster than 99.11%
runtime-terror
0
45
constrained subsequence sum
1,425
0.474
Hard
21,337
https://leetcode.com/problems/constrained-subsequence-sum/discuss/924239/'Subarray-with-gaps'-intuition-O(N)-time-O(k)-space
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: stk = [] subsum = 0 best = float('-inf') for idx in range(len(nums)): while stk and nums[stk[-1]] < 0 and (len(stk) == 1 or idx - stk[-2] <= k) and nums[stk[-1]] < nums[idx]: subsum -= nums[stk.pop()] # kick out more expensive negative bridge elements if nums[idx] > 0 and subsum < 0: stk = [idx] subsum = nums[idx] # don't have a negative start else: stk.append(idx) subsum += nums[idx] best = max(best, subsum) return best
constrained-subsequence-sum
'Subarray with gaps' intuition, O(N) time, O(k) space
celmar_stone
0
140
constrained subsequence sum
1,425
0.474
Hard
21,338
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2269844/Python-3-ONE-LINER
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: return [x+extraCandies >= max(candies) for x in candies]
kids-with-the-greatest-number-of-candies
Python 3 [ONE LINER]
omkarxpatel
6
143
kids with the greatest number of candies
1,431
0.875
Easy
21,339
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/609954/Two-python-O(n)-sol.-w-Comment
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: result, maximum = [], max( candies ) for candy in candies: # check whether current kid can achieve maximum by adding extra candies if (candy + extraCandies) >= maximum: result.append( True ) else: result.append( False ) return result
kids-with-the-greatest-number-of-candies
Two python O(n) sol. [w/ Comment ]
brianchiang_tw
5
1,600
kids with the greatest number of candies
1,431
0.875
Easy
21,340
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/609954/Two-python-O(n)-sol.-w-Comment
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: maximum = max( candies ) # check whether current kid can achieve maximum by adding extra candies # output by list comprehension return [ (candy + extraCandies) >= maximum for candy in candies ]
kids-with-the-greatest-number-of-candies
Two python O(n) sol. [w/ Comment ]
brianchiang_tw
5
1,600
kids with the greatest number of candies
1,431
0.875
Easy
21,341
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2759684/Simple-Python-solution-0(n)
1st Solution Time complexity - O(n) Space complexity - O(n) class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: n = len(candies) res, richest = [None] * n, max(candies) for i in range(n): res[i] = candies[i] + extraCandies >= richest return res 2nd Solution Time complexity - O(n) Space complexity - O(1) class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: richest = max(candies) for i in range(len(candies)): candies[i] = candies[i] + extraCandies >= richest return candies
kids-with-the-greatest-number-of-candies
Simple Python solution, 0(n)
tragob
3
367
kids with the greatest number of candies
1,431
0.875
Easy
21,342
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/969088/Python-In-place-Memory-Solution
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: minRequiredCandies = max(candies) - extraCandies for idx, kid in enumerate(candies): candies[idx] = kid >= minRequiredCandies return candies
kids-with-the-greatest-number-of-candies
Python In-place Memory Solution
JuanRodriguez
3
303
kids with the greatest number of candies
1,431
0.875
Easy
21,343
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2823368/Python3-One-Liner
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: return [candies[i]+extraCandies >= max(candies) for i in range(len(candies))]
kids-with-the-greatest-number-of-candies
Python3 One-Liner
kschaitanya2001
1
24
kids with the greatest number of candies
1,431
0.875
Easy
21,344
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2313743/Python-3-using-lambda...-faster-than-92-in-python-submissions
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: m = max(candies) candies = candies f = lambda x: x + extraCandies >= m return list(map(f, candies)) ````
kids-with-the-greatest-number-of-candies
Python 3 using lambda... faster than 92% in python submissions
Notysoty
1
82
kids with the greatest number of candies
1,431
0.875
Easy
21,345
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2250372/Python3-O(n)-oror-O(1)-Runtime%3A-54ms-64.17-memory%3A-13.9mb-62.75
class Solution: # O(n) || O(1) # Runtime: 54ms 64.17% memory: 13.9mb 62.75% def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: maxCandie = max(candies) for idx, val in enumerate(candies): # candies[idx] = True if not (val + extraCandies) < maxCandie else False candies[idx] = not (val + extraCandies) < maxCandie return candies
kids-with-the-greatest-number-of-candies
Python3 O(n) || O(1) # Runtime: 54ms 64.17% memory: 13.9mb 62.75%
arshergon
1
49
kids with the greatest number of candies
1,431
0.875
Easy
21,346
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/1971279/easy-python-code
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: maxi = max(candies) output = [] for i in candies: if i+extraCandies >= maxi: output.append(True) else: output.append(False) return output
kids-with-the-greatest-number-of-candies
easy python code
dakash682
1
72
kids with the greatest number of candies
1,431
0.875
Easy
21,347
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/1880078/PYTHON-DIRECT-solution-(40ms)
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: #Find the current non adjust max number of candies non_adjusted_max = max( candies ); LENGTH = len ( candies ); #Make an array for each kid at index i bool_array = [ None for _ in range( LENGTH ) ]; #Go through each candy count for i in range( LENGTH ): #Adjust the current number by the extra candies current_adjusted = candies[ i ] + extraCandies; #See if the adjusted is greater than the max #If it is, set the bool_array at the ith index to True if current_adjusted >= non_adjusted_max: bool_array[ i ] = True; #Otherwise, set it to False else: bool_array[ i ] = False; return bool_array;
kids-with-the-greatest-number-of-candies
PYTHON DIRECT solution (40ms)
greg_savage
1
70
kids with the greatest number of candies
1,431
0.875
Easy
21,348
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/1853068/Python-(Simple-Approach-Beginner-friendly)
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: output = [] for i in candies: if i+extraCandies >= max(candies): output.append(True) else: output.append(False) return output
kids-with-the-greatest-number-of-candies
Python (Simple Approach, Beginner-friendly)
vishvavariya
1
30
kids with the greatest number of candies
1,431
0.875
Easy
21,349
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/1848317/1-Line-Python-Solution-oror-75-Faster-oror-Memory-less-than-80
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: return [True if candie+extraCandies>=max(candies) else False for candie in candies]
kids-with-the-greatest-number-of-candies
1-Line Python Solution || 75% Faster || Memory less than 80%
Taha-C
1
36
kids with the greatest number of candies
1,431
0.875
Easy
21,350
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/1727574/1431-kids-with-greatest-candies-or-optimising-python-solution
class Solution(object): def kidsWithCandies(self, candies, extraCandies): maxCandy = max(candies) rArray = [] #return_array for i in candies: newAmount = i + extraCandies rArray.append(newAmount == maxCandy or newAmount > maxCandy) return rArray
kids-with-the-greatest-number-of-candies
1431 - kids with greatest candies | optimising python solution
ankit61d
1
67
kids with the greatest number of candies
1,431
0.875
Easy
21,351
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/1727574/1431-kids-with-greatest-candies-or-optimising-python-solution
class Solution(object): def kidsWithCandies(self, candies, extraCandies): maxCandy = max(candies) - extraCandies return [i >= maxCandy for i in candies]
kids-with-the-greatest-number-of-candies
1431 - kids with greatest candies | optimising python solution
ankit61d
1
67
kids with the greatest number of candies
1,431
0.875
Easy
21,352
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/1715113/1431.-Kids-With-the-Greatest-Number-of-Candies
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: lis = [] maxi = max(candies) for x in candies: if x+extraCandies >=maxi: lis.append(True) else: lis.append(False) return lis
kids-with-the-greatest-number-of-candies
1431. Kids With the Greatest Number of Candies
apoorv11jain
1
97
kids with the greatest number of candies
1,431
0.875
Easy
21,353
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/1098692/Python-one-line-%2B95-of-submissions
class Solution(object): def kidsWithCandies(self, candies, extraCandies): """ :type candies: List[int] :type extraCandies: int :rtype: List[bool] """ maxCandy = max(candies) return [True if i+extraCandies >= maxCandy else False for i in candies]
kids-with-the-greatest-number-of-candies
Python one line +95% of submissions
CODPUD
1
105
kids with the greatest number of candies
1,431
0.875
Easy
21,354
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/1003257/python-solution
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: length = len(candies) arr_bool = [] max_candy = max(candies) i = 0 while i < length: if candies[i]+extraCandies >= max_candy: arr_bool.append(True) else: arr_bool.append(False) i += 1 return arr_bool
kids-with-the-greatest-number-of-candies
python solution
EH1
1
292
kids with the greatest number of candies
1,431
0.875
Easy
21,355
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/941125/Python-Simple-Solution
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: r=max(candies)-extraCandies for i in range(len(candies)): candies[i]=candies[i]>=r return candies
kids-with-the-greatest-number-of-candies
Python Simple Solution
lokeshsenthilkumar
1
172
kids with the greatest number of candies
1,431
0.875
Easy
21,356
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/801735/Python-3%3A-Easy-to-read-with-commentary-using-enumerate
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: # Start with determining the max candies in the list max_candies = max(candies) # Enumerate was made for this scenario with both index and value given for index, count in enumerate (candies): # We can save memory space by reusing the same list after we have processed if count + extraCandies >= max_candies: candies[index] = True else: candies[index] = False return candies
kids-with-the-greatest-number-of-candies
Python 3: Easy to read with commentary using enumerate
dentedghost
1
111
kids with the greatest number of candies
1,431
0.875
Easy
21,357
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/739191/Python3-One-Line-Solution%3A-Faster-Than-93-Better-Memory-Usage-Than-72
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: return [n+extraCandies>=max(candies) for n in candies]
kids-with-the-greatest-number-of-candies
[Python3] One Line Solution: Faster Than 93% Better Memory Usage Than 72%
mihirverma
1
159
kids with the greatest number of candies
1,431
0.875
Easy
21,358
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/612647/Runtime%3A-100-or-One-Line-or-Python3
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: return [candy + extraCandies >= max(candies) for candy in candies]
kids-with-the-greatest-number-of-candies
Runtime: 100% | One-Line | Python3
deleted_user
1
128
kids with the greatest number of candies
1,431
0.875
Easy
21,359
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2828628/Easy
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: res=[] n=len(candies) for i in range (0,n): if candies[i]+extraCandies>=max(candies): res.append(True) else: res.append(False) return res
kids-with-the-greatest-number-of-candies
Easy
nishithakonuganti
0
1
kids with the greatest number of candies
1,431
0.875
Easy
21,360
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2806513/Solution-beating-90-of-other-codes
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: a= max(candies) s=[0]*(len(candies)) for i in range(len(candies)): if candies[i]+extraCandies>=a: s[i]=1 return s
kids-with-the-greatest-number-of-candies
Solution beating 90% of other codes
gowdavidwan2003
0
4
kids with the greatest number of candies
1,431
0.875
Easy
21,361
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2800245/Python-solution-using-List-Comprehension.
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: max_val = max(candies) output_array = [(i + extraCandies) >= max_val for i in candies] return output_array
kids-with-the-greatest-number-of-candies
Python solution using List Comprehension.
arvish_29
0
2
kids with the greatest number of candies
1,431
0.875
Easy
21,362
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2787256/simple-python-soln-faster-than-99.46
class Solution: def kidsWithCandies(self, candies, extraCandies): max_candy_count = sorted(candies).pop() return [True if (candy + extraCandies) >= max_candy_count else False for candy in candies]
kids-with-the-greatest-number-of-candies
simple python soln, faster than 99.46%
alamwasim29
0
4
kids with the greatest number of candies
1,431
0.875
Easy
21,363
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2786518/One-liner.-Python
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: return [x + extraCandies >= max(candies) for x in candies]
kids-with-the-greatest-number-of-candies
One liner. Python
nonchalant-enthusiast
0
2
kids with the greatest number of candies
1,431
0.875
Easy
21,364
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2782453/Python-Simple-and-fast-beats-99
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: m = max(candies) result = [] for kid in candies: if kid + extraCandies >= m: greatest = True else: greatest = False result.append(greatest) return result
kids-with-the-greatest-number-of-candies
Python - Simple and fast - beats 99%
jimmyhodl
0
3
kids with the greatest number of candies
1,431
0.875
Easy
21,365
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2780445/Python-simple-O(n)-solution
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: max_num = max(candies) for i in range(len(candies)): candies[i] = candies[i] + extraCandies >= max_num return candies
kids-with-the-greatest-number-of-candies
Python simple O(n) solution
Mark_computer
0
5
kids with the greatest number of candies
1,431
0.875
Easy
21,366
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2763896/python-solution
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: great=max(candies) for i in range(len(candies)): candies[i]=candies[i]+extraCandies >=great return candies
kids-with-the-greatest-number-of-candies
python solution
user7798V
0
7
kids with the greatest number of candies
1,431
0.875
Easy
21,367
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2750409/Python3-My-Satisfying-One-Liner
class Solution: def kidsWithCandies(self, nc: List[int], e: int) -> List[bool]: return [c >= (max(nc)-e) for c in nc]
kids-with-the-greatest-number-of-candies
[Python3] My Satisfying One-Liner
keioon
0
1
kids with the greatest number of candies
1,431
0.875
Easy
21,368
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2681552/Easiest-Solution
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: length = len(candies) Max = max(candies) res = [] for i in range(length): candies[i] += extraCandies for k in range(length): if candies[k] >= Max: res.append(True) else: res.append(False) return res
kids-with-the-greatest-number-of-candies
Easiest Solution
MuazzamAli
0
3
kids with the greatest number of candies
1,431
0.875
Easy
21,369
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2661573/easy-python-soln
class Solution: def kidsWithCandies(self, c: List[int], e: int) -> List[bool]: l=0 for i in c: if i>=l: l=i m=[] for i in c: if i+e>=l: m.append(True) else: m.append(False) return m
kids-with-the-greatest-number-of-candies
easy python soln
AMBER_FATIMA
0
3
kids with the greatest number of candies
1,431
0.875
Easy
21,370
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2656328/Simple-python-code-with-explanation
class Solution: def kidsWithCandies(self, candy, extra): #create an array(res) with all values as True and it's lenght is same as candies res = [True]*len(candy) #iterate over the elements in the array candy for i in range(len(candy)): #if the no. of canides at curr position + extra is greater than or equal to the maximum of candies then continue if (candy[i] + extra) >= max(candy): continue #if not else: #change the value of that position in res as false res[i] = False #return the res list return res
kids-with-the-greatest-number-of-candies
Simple python code with explanation
thomanani
0
18
kids with the greatest number of candies
1,431
0.875
Easy
21,371
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2656328/Simple-python-code-with-explanation
class Solution: def kidsWithCandies(self, candy, extra): #create a variable k and store it's value as max(candy) k = max(candy) #iterate over the elements in list(candy) for i in range(len(candy)): #if the candies at curr index + extra is greater than or equal to max(candy) if (candy[i] + extra) >= k: #then change the curr index value as true candy[i] = True #if not else: #change the curr index value as false candy[i] = False #return the candy list after finishing forloop return candy
kids-with-the-greatest-number-of-candies
Simple python code with explanation
thomanani
0
18
kids with the greatest number of candies
1,431
0.875
Easy
21,372
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2651701/Easy-to-understand-python-3-solution
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: result = [] maxVal = max(candies) for item in candies: if item + extraCandies >= maxVal: result.append(True) else: result.append(False) return result
kids-with-the-greatest-number-of-candies
Easy to understand python 3 solution
kmsanjar007
0
2
kids with the greatest number of candies
1,431
0.875
Easy
21,373
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2582357/Pythonoror1-line-solutionororlist-comprehension
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: return [True if i+extraCandies>=max(candies) else False for i in candies]
kids-with-the-greatest-number-of-candies
Python||1-line solution||list-comprehension
shersam999
0
49
kids with the greatest number of candies
1,431
0.875
Easy
21,374
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2514418/Python-or-With-Explanation-or-faster-than-98.69
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: l=[] a=max(candies) # for i in range(0,len(candies)): if candies[i]+extraCandies>=a: l.append(True) else: l.append(False) return l
kids-with-the-greatest-number-of-candies
Python ✔| With Explanation | faster than 98.69%
omkara18
0
29
kids with the greatest number of candies
1,431
0.875
Easy
21,375
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2300185/Python3-Two-2-liner-solutions
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: max_candies = max(candies) # Using list comprehension return [(candy + extraCandies >= max_candies) for candy in candies] # ------- # # using map and lambda return list(map(lambda candy: candy + extraCandies >= max_candies, candies))
kids-with-the-greatest-number-of-candies
[Python3] Two 2 liner solutions
Gp05
0
15
kids with the greatest number of candies
1,431
0.875
Easy
21,376
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2239216/Python3-easy-solution
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: l=[] for i in candies: if i+extraCandies >= max(candies): l.append(True) else: l.append(False) return (l)
kids-with-the-greatest-number-of-candies
Python3 easy solution
psnakhwa
0
30
kids with the greatest number of candies
1,431
0.875
Easy
21,377
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2189923/Python-solution-multiple-approach-for-beginners-by-beginner.
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: ans = [] max_candies = max(candies) for i in candies: if i + extraCandies >= max_candies: ans.append(True) else: ans.append(False) return ans
kids-with-the-greatest-number-of-candies
Python solution multiple approach for beginners by beginner.
EbrahimMG
0
46
kids with the greatest number of candies
1,431
0.875
Easy
21,378
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2189923/Python-solution-multiple-approach-for-beginners-by-beginner.
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: return [True if candy + extraCandies >= max(candies) else False for candy in candies]
kids-with-the-greatest-number-of-candies
Python solution multiple approach for beginners by beginner.
EbrahimMG
0
46
kids with the greatest number of candies
1,431
0.875
Easy
21,379
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2147899/Python-or-Easy-Solution-or-Clean-Code
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: m=max(candies) l=[] for i in range(len(candies)): s=candies[i]+extraCandies if s>=m: l.append(True) else: l.append(False) return l
kids-with-the-greatest-number-of-candies
Python | Easy Solution | Clean Code
T1n1_B0x1
0
46
kids with the greatest number of candies
1,431
0.875
Easy
21,380
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2119748/Easy-python-code-with-if-else
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: ma = max(candies) result = [] for i in range(len(candies)): if candies[i] + extraCandies >= ma: result.append(True) else: result.append(False) return result
kids-with-the-greatest-number-of-candies
Easy python code with if else
prernaarora221
0
27
kids with the greatest number of candies
1,431
0.875
Easy
21,381
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2060169/Python-or-Neat-and-simple-solution-with-comments
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: ''' Pretty straight forward solution initializing a result list, initially which hold True for each kid. Now we will iterate over each kid position and give them extra candies and check if they have maximum number of candies amoung all, if not toggle to False else just continue. ''' res = [True for _ in range(len(candies))] for i in range(len(candies)): curMax = candies[i] + extraCandies if curMax >= max(candies): continue else: res[i] = False return res
kids-with-the-greatest-number-of-candies
Python | Neat and simple solution with comments
__Asrar
0
58
kids with the greatest number of candies
1,431
0.875
Easy
21,382
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2028410/Python-Clean-and-Concise!
class Solution: def kidsWithCandies(self, candies, extraCandies): maxCandies = max(candies) return [(candy + extraCandies) >= maxCandies for candy in candies]
kids-with-the-greatest-number-of-candies
Python - Clean and Concise!
domthedeveloper
0
89
kids with the greatest number of candies
1,431
0.875
Easy
21,383
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/2015210/Python3-simple-with-python
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: greatest = [] for candie in candies: giving = candie + extraCandies greatest.append(True if giving >= max(candies) else False) return greatest
kids-with-the-greatest-number-of-candies
[Python3] simple with python
Shiyinq
0
56
kids with the greatest number of candies
1,431
0.875
Easy
21,384
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/1925486/Python-Solution
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: boolean = False final_array = [] for i in range(0,len(candies)): candies[i] += extraCandies max_val = max(candies) if candies[i] == max_val: boolean = True final_array.append(boolean) else: boolean = False final_array.append(boolean) candies[i] -= extraCandies return final_array
kids-with-the-greatest-number-of-candies
Python Solution
itsmeparag14
0
46
kids with the greatest number of candies
1,431
0.875
Easy
21,385
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/discuss/1898828/Very-Fast-Python3-Solution-with-Minimum-Storage-Usage-for-Beginners
class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: maxCandy = max(candies) boolResult = [] for candy in candies: if candy + extraCandies >= maxCandy: boolResult.append(True) else: boolResult.append(False) return boolResult
kids-with-the-greatest-number-of-candies
Very Fast Python3 Solution with Minimum Storage Usage for Beginners
Reechychukz
0
33
kids with the greatest number of candies
1,431
0.875
Easy
21,386
https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/discuss/608687/Python3-scan-through-digits
class Solution: def maxDiff(self, num: int) -> int: num = str(num) i = next((i for i in range(len(num)) if num[i] != "9"), -1) #first non-9 digit hi = int(num.replace(num[i], "9")) if num[0] != "1": lo = int(num.replace(num[0], "1")) else: i = next((i for i in range(len(num)) if num[i] not in "01"), -1) lo = int(num.replace(num[i], "0") if i > 0 else num) return hi - lo
max-difference-you-can-get-from-changing-an-integer
[Python3] scan through digits
ye15
4
151
max difference you can get from changing an integer
1,432
0.429
Medium
21,387
https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/discuss/1118317/Python-3-or-20ms-98-faster
class Solution: def maxDiff(self, num: int) -> int: a = b = 0 n = len(str(num)) nums = str(num) if nums[0]!= "1" and nums[0]!= "9": a = int(nums.replace(nums[0],"9")) b = int(nums.replace(nums[0],"1")) return a-b elif nums[0]=="1": a = int(nums.replace("1","9")) b = num for i in range(0,n-1): if nums[i]!="1" and nums[i]!="0": b = int(nums.replace(nums[i],"0")) break return a-b else: a = num b = int(nums.replace("9","1")) for i in range(0,n-1): if nums[i]!=nums[i+1]: a = int(nums.replace(nums[i+1],"9")) break return a-b
max-difference-you-can-get-from-changing-an-integer
Python 3 | 20ms 98% faster
qanghaa
1
91
max difference you can get from changing an integer
1,432
0.429
Medium
21,388
https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/discuss/2490232/Very-simple-Python3-solution-beats-most-submissions-oror-Very-simple
class Solution: def maxDiff(self, num: int) -> int: tmp = num xA, xB = 9, 1 while tmp: digit = tmp % 10 if digit != 9: xA = digit if digit > 1: xB = digit tmp //= 10 yA, yB = 9, 0 if digit == 1 and xB != 1 else 1 digitPlace = 1 a = b = 0 while num: digit = num % 10 a += digitPlace * (digit if digit != xA else yA) b += digitPlace * (digit if digit != xB else yB) num //= 10 digitPlace *= 10 return a - b
max-difference-you-can-get-from-changing-an-integer
✔️ Very simple Python3 solution beats most submissions || Very simple
explusar
0
8
max difference you can get from changing an integer
1,432
0.429
Medium
21,389
https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/discuss/2085232/python-3-oror-greedy-solution-without-strings-oror-O(logn)O(1)
class Solution: def maxDiff(self, num: int) -> int: tmp = num xA, xB = 9, 1 while tmp: digit = tmp % 10 if digit != 9: xA = digit if digit > 1: xB = digit tmp //= 10 yA, yB = 9, 0 if digit == 1 and xB != 1 else 1 digitPlace = 1 a = b = 0 while num: digit = num % 10 a += digitPlace * (digit if digit != xA else yA) b += digitPlace * (digit if digit != xB else yB) num //= 10 digitPlace *= 10 return a - b
max-difference-you-can-get-from-changing-an-integer
python 3 || greedy solution, without strings || O(logn)/O(1)
dereky4
0
56
max difference you can get from changing an integer
1,432
0.429
Medium
21,390
https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/discuss/610030/Python-sol-by-digit-replacement-w-Comment
class Solution: def maxDiff(self, num: int) -> int: num_string = str(num) def apply_transform( src_char: str, dest_char: str, s: str): return int( s.replace( src_char, dest_char ) ) # ----------------------------------------------------------- # digit replacement for maximum number max_num = num for char in num_string: if char < '9': max_num = apply_transform( char, '9', num_string ) break # ----------------------------------------------------------- # digit replacement for minimum number min_num = num if num_string[0] > '1': # leading digit cannot be zero min_num = apply_transform( num_string[0], '1', num_string ) else: for char in num_string[1:]: if char > '1': min_num = apply_transform( char, '0', num_string ) break return max_num - min_num
max-difference-you-can-get-from-changing-an-integer
Python sol by digit replacement [w/ Comment]
brianchiang_tw
0
65
max difference you can get from changing an integer
1,432
0.429
Medium
21,391
https://leetcode.com/problems/check-if-a-string-can-break-another-string/discuss/1415509/ONLY-CODE-N-log-N-sort-and-compare-%3A)-clean-3-liner-and-then-1-liner
class Solution: def checkIfCanBreak(self, s1: str, s2: str) -> bool: return all(a<=b for a,b in zip(min(sorted(s1),sorted(s2)),max(sorted(s1),sorted(s2))))```
check-if-a-string-can-break-another-string
[ONLY CODE] N log N sort and compare :) clean 3 liner and then 1 liner
yozaam
1
60
check if a string can break another string
1,433
0.689
Medium
21,392
https://leetcode.com/problems/check-if-a-string-can-break-another-string/discuss/2785025/Python-sorting-solution
class Solution: def checkIfCanBreak(self, s1: str, s2: str) -> bool: s1 = list(s1) s2 = list(s2) s1.sort() s2.sort() i = 0 while i<len(s1): if s1[i]>=s2[i]: i+=1 else: break if i==len(s1): return True i = 0 while i<len(s2): if s2[i]>=s1[i]: i+=1 else: break if i==len(s2): return True return False
check-if-a-string-can-break-another-string
Python sorting solution
Rajeev_varma008
0
1
check if a string can break another string
1,433
0.689
Medium
21,393
https://leetcode.com/problems/check-if-a-string-can-break-another-string/discuss/2524349/Python-or-Short-and-easy-to-read
class Solution: def checkIfCanBreak(self, s1: str, s2: str) -> bool: s1, s2 = sorted(s1), sorted(s2) first = all([s1[i] >= s2[i] for i,_ in enumerate(s1)]) return first if first else all([s1[i] <= s2[i] for i,_ in enumerate(s1)])
check-if-a-string-can-break-another-string
Python | Short and easy to read
Wartem
0
18
check if a string can break another string
1,433
0.689
Medium
21,394
https://leetcode.com/problems/check-if-a-string-can-break-another-string/discuss/2428327/Python3-Solution-with-using-sorting
class Solution: def checkIfCanBreak(self, s1: str, s2: str) -> bool: s1 = sorted(list(s1)) s2 = sorted(list(s2)) f1, f2 = False, False for i in range(len(s1)): if s1[i] == s2[i]: continue if not f1 and not f2: if s1[i] < s2[i]: f2 = True else: f1 = True if s1[i] < s2[i] and f1 or s1[i] > s2[i] and f2: return False return True
check-if-a-string-can-break-another-string
[Python3] Solution with using sorting
maosipov11
0
12
check if a string can break another string
1,433
0.689
Medium
21,395
https://leetcode.com/problems/check-if-a-string-can-break-another-string/discuss/2085531/python-3-oror-simple-greedy-solution
class Solution: def checkIfCanBreak(self, s1: str, s2: str) -> bool: s1, s2 = sorted(s1), sorted(s2) if s1 < s2: s1, s2 = s2, s1 return all(c1 >= c2 for c1, c2 in zip(s1, s2))
check-if-a-string-can-break-another-string
python 3 || simple greedy solution
dereky4
0
44
check if a string can break another string
1,433
0.689
Medium
21,396
https://leetcode.com/problems/check-if-a-string-can-break-another-string/discuss/1189679/Python3-simple-and-easy-to-understand-solution-using-three-approaches
class Solution: def checkIfCanBreak(self, s1: str, s2: str) -> bool: x = sorted(list(s1)) y = sorted(list(s2)) flag1 = True for i in range(len(x)): if x[i] < y[i]: flag1 = False flag2 = True for i in range(len(x)): if x[i] > y[i]: flag2 = False return flag1 or flag2
check-if-a-string-can-break-another-string
Python3 simple and easy-to-understand solution using three approaches
EklavyaJoshi
0
50
check if a string can break another string
1,433
0.689
Medium
21,397
https://leetcode.com/problems/check-if-a-string-can-break-another-string/discuss/1189679/Python3-simple-and-easy-to-understand-solution-using-three-approaches
class Solution: def checkIfCanBreak(self, s1: str, s2: str) -> bool: x = sorted(list(s1)) y = sorted(list(s2)) l = [] equal = 0 for i in range(len(x)): if x[i] == y[i]: equal += 1 elif x[i] < y[i]: l.append(0) else: l.append(1) return ((len(s1) == l.count(0) + equal) or (len(s1) == l.count(1) + equal))
check-if-a-string-can-break-another-string
Python3 simple and easy-to-understand solution using three approaches
EklavyaJoshi
0
50
check if a string can break another string
1,433
0.689
Medium
21,398
https://leetcode.com/problems/check-if-a-string-can-break-another-string/discuss/1189679/Python3-simple-and-easy-to-understand-solution-using-three-approaches
class Solution: def checkIfCanBreak(self, s1: str, s2: str) -> bool: x = sorted(list(s1)) y = sorted(list(s2)) firstbreak = True secondbreak = True for i in range(len(s1)): if x[i] < y[i]: firstbreak = False if x[i] > y[i]: secondbreak = False return firstbreak or secondbreak
check-if-a-string-can-break-another-string
Python3 simple and easy-to-understand solution using three approaches
EklavyaJoshi
0
50
check if a string can break another string
1,433
0.689
Medium
21,399