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/reverse-pairs/discuss/1205560/Python3-summarizing-4-solutions | class Solution:
def reversePairs(self, nums: List[int]) -> int:
ans = 0
seen = []
for x in nums:
k = bisect_right(seen, 2*x)
ans += len(seen) - k
insort(seen, x)
return ans | reverse-pairs | [Python3] summarizing 4 solutions | ye15 | 5 | 223 | reverse pairs | 493 | 0.309 | Hard | 8,600 |
https://leetcode.com/problems/reverse-pairs/discuss/1205560/Python3-summarizing-4-solutions | class Solution:
def reversePairs(self, nums: List[int]) -> int:
def fn(nums, aux, lo, hi):
"""Return number of reverse pairs in nums[lo:hi]."""
if lo + 1 == hi: return 0
mid = lo + hi >> 1
left = fn(aux, nums, lo, mid)
right = fn(aux, nu... | reverse-pairs | [Python3] summarizing 4 solutions | ye15 | 5 | 223 | reverse pairs | 493 | 0.309 | Hard | 8,601 |
https://leetcode.com/problems/reverse-pairs/discuss/2065858/Python-easy-to-read-and-understand-or-mergesort | class Solution:
def merge(self, left, right):
i, j = 0, 0
m, n = len(left), len(right)
while i < m:
while j < n and left[i] > 2*right[j]:
j += 1
self.ans += j
i += 1
i, j = 0, 0
temp = []
while i < m and j <... | reverse-pairs | Python easy to read and understand | mergesort | sanial2001 | 1 | 262 | reverse pairs | 493 | 0.309 | Hard | 8,602 |
https://leetcode.com/problems/reverse-pairs/discuss/2267809/Understanding-for-this-topic-(Merge-sort-method) | class Solution:
def reversePairs(self, arr: List[int]) -> int:
count = 0
if len(arr) > 1:
mid = len(arr)//2
L = arr[:mid]
R = arr[mid:]
count = self.reversePairs(L)
count += self.reversePairs(R)
count += self.merge(L,R,arr... | reverse-pairs | Understanding for this topic (Merge sort method) | ishitab_15 | 0 | 88 | reverse pairs | 493 | 0.309 | Hard | 8,603 |
https://leetcode.com/problems/reverse-pairs/discuss/2016800/Easy-and-Fast-two-Solution-in-Python-T.C-greaterO(nlogn)-and-O(n2) | class Solution:
def reversePairs(self, nums: List[int]) -> int:
t = []
res = 0
for i in range(len(nums)):
j = bisect.bisect_right(t, 2*nums[i])
res += len(t) - j
bisect.insort(t, nums[i])
return res | reverse-pairs | Easy and Fast two Solution in Python T.C->O(nlogn) and O(n^2) | karansinghsnp | 0 | 182 | reverse pairs | 493 | 0.309 | Hard | 8,604 |
https://leetcode.com/problems/reverse-pairs/discuss/2016800/Easy-and-Fast-two-Solution-in-Python-T.C-greaterO(nlogn)-and-O(n2) | class Solution:
def reversePairs(self, nums: List[int]) -> int:
count=0
if len(nums)>1:
# calculate mid
mid=len(nums)//2
# divide the input array in to right and left
left=nums[:mid]
right=nums[mid:]
count+=self.reversePairs(lef... | reverse-pairs | Easy and Fast two Solution in Python T.C->O(nlogn) and O(n^2) | karansinghsnp | 0 | 182 | reverse pairs | 493 | 0.309 | Hard | 8,605 |
https://leetcode.com/problems/reverse-pairs/discuss/1787522/Divide-and-Conquer-with-Two-Pointers | class Solution:
def reversePairs(self, nums: List[int]) -> int:
def dc(nums):
if len(nums) < 2:
return 0
if len(nums) == 2:
return 1 if nums[0] > 2*nums[1] else 0
m = len(nums) // 2
left = nums[:m]
... | reverse-pairs | Divide & Conquer with Two Pointers | hl2425 | 0 | 89 | reverse pairs | 493 | 0.309 | Hard | 8,606 |
https://leetcode.com/problems/reverse-pairs/discuss/1065601/Python-solution-based-on-merge-sort | class Solution(object):
def reversePairs(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
def merge(array,start,mid,end):
leftArray=array[start:mid+1]
rightArray=array[mid+1:end+1]
i,j=0,0
m,n=len(leftArray),len(rightArray... | reverse-pairs | Python solution based on merge sort | SoumithASR | 0 | 154 | reverse pairs | 493 | 0.309 | Hard | 8,607 |
https://leetcode.com/problems/target-sum/discuss/1198338/Python-recursive-solution-with-memoization-(DFS) | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
dic = defaultdict(int)
def dfs(index=0, total=0):
key = (index, total)
if key not in dic:
if index == len(nums):
... | target-sum | Python, recursive solution with memoization (DFS) | swissified | 22 | 1,500 | target sum | 494 | 0.456 | Medium | 8,608 |
https://leetcode.com/problems/target-sum/discuss/1245044/Simple-DP-solution-Variation-of-Knapsack-problem | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
sumOfNums = sum(nums)
n = len(nums)
if target > sumOfNums:
return 0
if (target + sumOfNums) % 2 != 0:
return 0
s1 = (sumOfNums + target) // 2
... | target-sum | Simple DP solution - Variation of Knapsack problem | shubh_027 | 9 | 936 | target sum | 494 | 0.456 | Medium | 8,609 |
https://leetcode.com/problems/target-sum/discuss/1414225/Python-2d-bottom-up-dynamic-programming-solution | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
# initialize dp table
dp = [[0]*(2001) for _ in range(len(nums))]
dp[0][1000+nums[0]] += 1
dp[0][1000-nums[0]] += 1
# state transition function
for i in range(1, len(nums)):
for... | target-sum | Python 2d bottom-up dynamic programming solution | Charlesl0129 | 3 | 393 | target sum | 494 | 0.456 | Medium | 8,610 |
https://leetcode.com/problems/target-sum/discuss/1129274/Python-Simple-Top-Down-DP | class Solution:
def findTargetSumWays(self, nums: List[int], S: int) -> int:
nums = [0]+nums
@lru_cache(None)
def dp(i, total):
if i == 0: return total == 0
return dp(i-1, total - nums[i]) + dp(i-1, total + nums[i])
return dp(len(nums)-1, S) | target-sum | Python Simple Top-Down DP | Black_Pegasus | 3 | 542 | target sum | 494 | 0.456 | Medium | 8,611 |
https://leetcode.com/problems/target-sum/discuss/1865289/Recursive-DFS-with-memorization-Python-code-with-explanation | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
# Global memorization to save time
dic = defaultdict(int)
l = len(nums)
# DFS Tree with memorization
def dfs(i, tol):
# End condtion for recursive function
... | target-sum | Recursive DFS with memorization Python code with explanation | jlu56 | 2 | 158 | target sum | 494 | 0.456 | Medium | 8,612 |
https://leetcode.com/problems/target-sum/discuss/706902/TOP-DOWNPYTHON-3-greater-a-variation-of-Count-number-of-subset-with-given-difference | class Solution:
def findTargetSumWays(self, nums: List[int], S: int) -> int:
if S > sum(nums):
return 0;
if (S+sum(nums))%2!=0 :
return 0;
c=0
for i in nums:
if i==0:
c+=1
s= (S+sum(nums))//2
n=len(... | target-sum | TOP-DOWN[PYTHON 3]--> a variation of Count number of subset with given difference | rl_mayank | 2 | 1,800 | target sum | 494 | 0.456 | Medium | 8,613 |
https://leetcode.com/problems/target-sum/discuss/486955/Python3-1-D-DP | class Solution:
def findTargetSumWays(self, nums: List[int], S: int) -> int:
if not nums: return 0
dp = {}
dp[nums[0]] = 1
dp[-nums[0]] = 1 if nums[0]!= 0 else 2
for i in range(1,len(nums)):
temp = {}
for sum in dp:
temp[sum+nums[i]]=te... | target-sum | Python3 1-D DP | jb07 | 2 | 189 | target sum | 494 | 0.456 | Medium | 8,614 |
https://leetcode.com/problems/target-sum/discuss/2698867/Python-or-DP-or-Top-Down-or-Memoization | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
n = len(nums)
cache = {}
def dp(i, curr):
if (i, curr) in cache:
return cache[(i, curr)]
if i == n:
return int(curr == target)
... | target-sum | Python | DP | Top-Down | Memoization | seangohck | 1 | 345 | target sum | 494 | 0.456 | Medium | 8,615 |
https://leetcode.com/problems/target-sum/discuss/2103751/Why-complex-DP-One-dict-is-all-you-need! | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
mp = {0:1}
for n in nums:
temp = {}
for s in mp.keys():
temp[s+n] = temp[s+n] + mp[s] if s+n in temp else mp[s]
temp[s-n] = temp[s-n] + mp[s] if s-n in temp else ... | target-sum | Why complex DP? One dict is all you need! | EdwardLee_lpz | 1 | 76 | target sum | 494 | 0.456 | Medium | 8,616 |
https://leetcode.com/problems/target-sum/discuss/1605968/Unbounded-knapsack-solution | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
if len(nums) == 1:
if target != nums[0] and target != -nums[0]:
return 0
else: return 1
temp = target + sum(nums)
if temp % 2 != 0: return 0
targetSum = temp // ... | target-sum | Unbounded knapsack solution | vudat1710 | 1 | 203 | target sum | 494 | 0.456 | Medium | 8,617 |
https://leetcode.com/problems/target-sum/discuss/1592713/4-lines-of-code%3A-Easiest-Python-solution-%3A) | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
@lru_cache(None) # try running without it
def dfs(index,total):
if index == len(nums): return 1 if total == target else 0
return dfs(index + 1, total + nums[index]) + dfs(index + 1, to... | target-sum | 4 lines of code: Easiest Python solution :) | iknoor | 1 | 192 | target sum | 494 | 0.456 | Medium | 8,618 |
https://leetcode.com/problems/target-sum/discuss/1405029/Python-Solution-using-Tabulation-Approach | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
tot = sum(nums)
if((tot - target)% 2 != 0 or target > tot):
return 0
s1 = (tot - target)//2
n = len(nums)
dp = [[0 for x in range(s1+1)] for y in range(n+1)]
dp[0][0] = 1
... | target-sum | Python Solution using Tabulation Approach | Ritesh2345 | 1 | 222 | target sum | 494 | 0.456 | Medium | 8,619 |
https://leetcode.com/problems/target-sum/discuss/2823493/Pythonor-DP | class Solution:
def subsets(self, nums, targets):
n = len(nums)
dp = [0 for _ in range(targets+1)]
dp[0] = 1
for i in range(n):
for j in range(targets,-1,-1):
if j >= nums[i]:
dp[j] = dp[j]+dp[j-nums[i]]
... | target-sum | Python| DP | lucy_sea | 0 | 3 | target sum | 494 | 0.456 | Medium | 8,620 |
https://leetcode.com/problems/target-sum/discuss/2819004/Recursion-%2B-Memorization | class Solution:
def __init__(self):
self.res = 0
def findTargetSumWays(self, nums: List[int], target: int) -> int:
"""
[-1, 1]
-1 +
"""
if len(nums) == 0:
return 0
memo = {}
return self.bt(nums, 0, target, memo)
... | target-sum | Recursion + Memorization | lillllllllly | 0 | 2 | target sum | 494 | 0.456 | Medium | 8,621 |
https://leetcode.com/problems/target-sum/discuss/2801446/Python-Super-Easy-DP-Top-Down | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
@functools.lru_cache(None)
def dp( i , r):
if i == len(nums):
if r == target:
return 1
return 0
return dp(i+1, r+nums[i]) + dp(i+1, r-nums[... | target-sum | Python Super Easy DP Top Down | harrychen1995 | 0 | 3 | target sum | 494 | 0.456 | Medium | 8,622 |
https://leetcode.com/problems/target-sum/discuss/2734775/short-python-solution | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
cacheMap = {target: 1}
for n in nums:
m = {}
for target, num in cacheMap.items():
m[target+n] = m.get(target+n, 0) + num
m[target-n] = m.get(target-n, 0) + num
... | target-sum | short python solution | yhu415 | 0 | 3 | target sum | 494 | 0.456 | Medium | 8,623 |
https://leetcode.com/problems/target-sum/discuss/2709281/Python3-2D-and-1D-Dynamic-Programming | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
# X axis - The possible sums [-2 * sum(nums), 2 * sum(nums)] account for 0
# Y axis - All the indices of nums
SUM_NUMS = sum(nums)
# Return Early is not possible to make target as its not within range
... | target-sum | Python3 2D and 1D Dynamic Programming | PartialButton5 | 0 | 7 | target sum | 494 | 0.456 | Medium | 8,624 |
https://leetcode.com/problems/target-sum/discuss/2709281/Python3-2D-and-1D-Dynamic-Programming | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
# X axis - The possible sums [-2 * sum(nums), 2 * sum(nums)] account for 0
# Y axis - All the indices of nums
SUM_NUMS = sum(nums)
# Return Early is not possible to make target as its not within range
... | target-sum | Python3 2D and 1D Dynamic Programming | PartialButton5 | 0 | 7 | target sum | 494 | 0.456 | Medium | 8,625 |
https://leetcode.com/problems/target-sum/discuss/2695092/Python-Solution-using-DP-and-Dictionary-easy-to-understand | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
n = len(nums)
k = nums[0]
d = dict()
# Initialization for the first value k
# Note that d[-k] = 1 will cause error for k = 0
# To explain, if first element is 0, there are 2 ways to get ... | target-sum | Python Solution using DP and Dictionary, easy to understand | lazylzy | 0 | 3 | target sum | 494 | 0.456 | Medium | 8,626 |
https://leetcode.com/problems/target-sum/discuss/2681615/Python-smart-DFS-(beat-90)-no-DP-with-intuition-and-complexity-analysis | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
N = len(nums)
if N == 1:
if abs(nums[0]) == abs(target):
return 2 if target == 0 else 1
else:
return 0
M = N//2
mapper1 = defaultdict(int... | target-sum | Python smart DFS (beat 90%) - no DP - with intuition and complexity analysis | QTP | 0 | 38 | target sum | 494 | 0.456 | Medium | 8,627 |
https://leetcode.com/problems/target-sum/discuss/2665622/Simple-Python-DP-solution-using-0-1-Knapsack-or-efficient-than-94-solutions | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
sumOfNums = sum(nums)
n = len(nums)
if abs(target) > sumOfNums:
return 0
if (target + sumOfNums) % 2 != 0:
return 0
s1 = (sumOfNums + target) // 2
# DP Table
t = [[0] * (s1 + 1) for i in range(n + 1)]
t[0][0] =... | target-sum | Simple Python DP solution using 0 - 1 Knapsack | efficient than 94% solutions | nikhitamore | 0 | 24 | target sum | 494 | 0.456 | Medium | 8,628 |
https://leetcode.com/problems/target-sum/discuss/2661446/Python-DP-Tabulation-Solution | class Solution:
def findWaysOptimize(self, num, tar: int) -> int:
n = len(num)
prev = [0] * (tar+1)
if num[0] == 0:
prev[0] = 2
else:
prev[0] = 1
if num[0] != 0 and num[0] <= tar: # num[0] = 0
prev[num[0]] = 1
for ind in range(1, ... | target-sum | Python - DP - Tabulation Solution | kritikaparmar | 0 | 11 | target sum | 494 | 0.456 | Medium | 8,629 |
https://leetcode.com/problems/target-sum/discuss/2654205/python-easy-oror-DP-oror-memoization | class Solution:
def findTargetSumWays(self, A: List[int], x: int) -> int:
dp = {}
def solve(A,i,x,dp):
# (i,x) as x can repeat but (i,x) pair cannot repeat
if (i,x) in dp:
return dp[(i,x)]
if i==len(A) and x==0:
return 1
... | target-sum | python easy || DP || memoization | Akash_chavan | 0 | 14 | target sum | 494 | 0.456 | Medium | 8,630 |
https://leetcode.com/problems/target-sum/discuss/2491926/Python-DP-Solution-(Memoization) | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
d = {}
def rec(index, sm):
if index == len(nums):
if sm == target:
return 1
else:
return 0
if (index+1, sm+nums[index]) no... | target-sum | Python DP Solution (Memoization) | DietCoke777 | 0 | 44 | target sum | 494 | 0.456 | Medium | 8,631 |
https://leetcode.com/problems/target-sum/discuss/2458806/Python-Simple-Python-Solution-100-Optimal-Solution | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
total = sum(nums)
if (total - target)%2 != 0:
return 0
newT = (total - target)//2
dpdict = {}
def sol(i, T):
if i < 0:
if T == 0:
... | target-sum | [ Python ] ✅ Simple Python Solution ✅✅ ✅100% Optimal Solution | vaibhav0077 | 0 | 101 | target sum | 494 | 0.456 | Medium | 8,632 |
https://leetcode.com/problems/target-sum/discuss/2436675/Python-dp-simple-solution | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
memo = {}
def dp(s,i):
if s==target and i==len(nums):
return 1
elif i>=len(nums):
return 0
elif (s,i) in memo:
return memo[(s,i)]
... | target-sum | Python dp simple solution | Brillianttyagi | 0 | 63 | target sum | 494 | 0.456 | Medium | 8,633 |
https://leetcode.com/problems/target-sum/discuss/2393615/Python-oror-Beats-95-oror-DP-oror-Tabulation-method | class Solution:
def findTargetSumWays(self, arr: List[int], target: int) -> int:
n=len(arr)
total_sum=sum(arr)
s = (target+total_sum)//2
target=abs(target)
tab = [[0 for i in range(int(s + 1))] for y in range(n + 1)]
if (target>total_sum or (total_sum + target)%2 == ... | target-sum | Python || Beats 95% || DP || Tabulation method | deshkarmm | 0 | 89 | target sum | 494 | 0.456 | Medium | 8,634 |
https://leetcode.com/problems/target-sum/discuss/2365209/Python3-Solution-with-using-dp | class Solution:
def __init__(self):
self.res = 0
self.cache = {}
def builder(self, nums, target, cur_idx, cur_val):
if (cur_idx, cur_val) in self.cache:
return self.cache[(cur_idx, cur_val)]
if cur_idx >= len(nums):
if cur_val == target:
... | target-sum | [Python3] Solution with using dp | maosipov11 | 0 | 45 | target sum | 494 | 0.456 | Medium | 8,635 |
https://leetcode.com/problems/target-sum/discuss/2350864/python-easy-concise | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
dp = {}
def backtrack(i, total):
if i == len(nums):
return 1 if total == target else 0
if (i, total) in dp:
return dp[(i, total)]
... | target-sum | python easy concise | soumyadexter7 | 0 | 69 | target sum | 494 | 0.456 | Medium | 8,636 |
https://leetcode.com/problems/target-sum/discuss/2329844/Recursive-5-liner.-81-Faster.-Explained-with-comments | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
@lru_cache(None) # memoization
def dfs(i,tot):
if(i==len(nums)):
return tot==target # return true if target is r... | target-sum | ✅✅✅ Recursive 5 liner. 81% Faster. Explained with comments | arnavjaiswal149 | 0 | 87 | target sum | 494 | 0.456 | Medium | 8,637 |
https://leetcode.com/problems/target-sum/discuss/2319286/Python-easy-to-read-and-understand-or-dynamic-programming | class Solution:
def solve(self, nums, i, sums, target):
if i == len(nums):
if sums == target:
return 1
return 0
x = self.solve(nums, i + 1, sums + nums[i], target)
y = self.solve(nums, i + 1, sums - nums[i], target)
return x + y | target-sum | Python easy to read and understand | dynamic-programming | sanial2001 | 0 | 84 | target sum | 494 | 0.456 | Medium | 8,638 |
https://leetcode.com/problems/target-sum/discuss/2259794/Python-Simple-Python-Solution-orororfaster-than-82.90-orororless-than-80.06-memory-required | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
if (sum(nums) + target) % 2 != 0:
return 0
s1 = (sum(nums) + target)//2
rows = len(nums) + 1
cols = s1 + 1
t = [[0]*cols for i in range(rows)]
try:
for i i... | target-sum | [ Python ] ✅ Simple Python Solution ✅✅ |||faster than 82.90% |||less than 80.06% memory required | vaibhav0077 | 0 | 72 | target sum | 494 | 0.456 | Medium | 8,639 |
https://leetcode.com/problems/target-sum/discuss/2232522/Dynamic-Programming-Approach | class Solution:
def countSubsetSum(self, nums, S):
n = len(nums)
dpMatrix = [[-1]*(S + 1) for _ in range(n + 1)]
for j in range(S + 1):
dpMatrix[0][j] = 0
for i in range(n + 1):
dpMatrix[i][0] = 1
for i in range(1, n... | target-sum | Dynamic Programming Approach | Vaibhav7860 | 0 | 90 | target sum | 494 | 0.456 | Medium | 8,640 |
https://leetcode.com/problems/target-sum/discuss/2184388/0-1-Knapsack-oror-Easy-Python-oror-Tabulation | class Solution:
def findTargetSumWays(self, arr: List[int], target: int) -> int:
# Never saw any thing like that
# Typical dp problem
# Variation of 0-1 Knapsack
if abs(target)>sum(arr):
return 0
N=len(arr)
sum1=(sum(arr)+target)/2
sum2=(... | target-sum | 0-1 Knapsack || Easy Python || Tabulation | Aniket_liar07 | 0 | 90 | target sum | 494 | 0.456 | Medium | 8,641 |
https://leetcode.com/problems/target-sum/discuss/1650275/Easy-python3-solution-using-defaultdict | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
mydict=defaultdict(int)
mydict[target]=1
for val in nums:
temp=defaultdict(int)
for num in mydict:
temp[num+val]+=mydict[num]
... | target-sum | Easy python3 solution using defaultdict | Karna61814 | 0 | 69 | target sum | 494 | 0.456 | Medium | 8,642 |
https://leetcode.com/problems/target-sum/discuss/1650155/Easy-to-understand-python3-dp-solution | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
@lru_cache(None)
def helper(index,target):
if index==len(nums):
if target==0:
return 1
return 0
plus=helper(ind... | target-sum | Easy to understand python3 dp solution | Karna61814 | 0 | 80 | target sum | 494 | 0.456 | Medium | 8,643 |
https://leetcode.com/problems/target-sum/discuss/1633120/python-solution-oror-beginner-friendly-oror-clean | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
dp={} #(index,total_so_far) -> # of ways
def backtrack(index,total):
if index== len(nums):
return 1 if total == target else 0
if (index,total) in dp:
return dp[(... | target-sum | python solution || beginner friendly || clean | minato_namikaze | 0 | 196 | target sum | 494 | 0.456 | Medium | 8,644 |
https://leetcode.com/problems/target-sum/discuss/1591330/Python-using-Dictonary-as-1D-DP-Array | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
dp, zeroCount = {0:1}, sum([1 if n==0 else 0 for n in nums])
for n in nums:
if n==0: continue
tDict = dp.copy()
dp = {}
for k in tDict:
dp[k+n] = (dp[k+n]... | target-sum | Python using Dictonary as 1D DP Array | abrarjahin | 0 | 89 | target sum | 494 | 0.456 | Medium | 8,645 |
https://leetcode.com/problems/target-sum/discuss/1457832/PyPy3-Solution-using-memoization-w-comments | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
"""
Let suppose we can divide the array in to two subsets S1 and S2.
Such that, the difference of those subset is target value.
sum(S1) + sum(S2) = total --- (1)
sum(S1) - sum(S2) =... | target-sum | [Py/Py3] Solution using memoization w/ comments | ssshukla26 | 0 | 216 | target sum | 494 | 0.456 | Medium | 8,646 |
https://leetcode.com/problems/target-sum/discuss/1321549/Python-Top-Down-DP-easy | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
@cache
def helper(idx, curr):
if idx==len(nums) and curr==target:
return 1
elif idx==len(nums):
return 0
else:
ans=0
a... | target-sum | Python Top Down DP, easy | abhinav4202 | 0 | 225 | target sum | 494 | 0.456 | Medium | 8,647 |
https://leetcode.com/problems/target-sum/discuss/861227/Python3-top-down-dp | class Solution:
def findTargetSumWays(self, nums: List[int], S: int) -> int:
@lru_cache(None)
def fn(i, t):
"""Return number of ways to assign symbols to sum nums[i:] to t."""
if i == len(nums): return t == 0
return fn(i+1, t-nums[i]) + fn(i+1, t+nums[i]... | target-sum | [Python3] top-down dp | ye15 | 0 | 99 | target sum | 494 | 0.456 | Medium | 8,648 |
https://leetcode.com/problems/target-sum/discuss/836991/easy-and-simple-python-3-solution-or-recursive-and-memoization | class Solution:
def Util(self, nums, ind, S, cur, l, mem):
if ind == l:
if cur == S:
return 1
return 0
if (ind, cur) in mem:
return mem[(ind, cur)]
ret1 = self.Util(nums, ind + 1, S, cur + nums[ind], l, mem)
ret2 = self.Util(nums, i... | target-sum | easy and simple python 3 solution | recursive and memoization | _YASH_ | 0 | 113 | target sum | 494 | 0.456 | Medium | 8,649 |
https://leetcode.com/problems/target-sum/discuss/479625/~80ms-python3 | class Solution:
def findTargetSumWays(self, nums: List[int], S: int) -> int:
if sum(nums)<S: return 0
if (sum(nums)-S)%2 != 0: return 0
sum_neg = (sum(nums)-S)//2
dic = dict(); dic[0] = 1
for i in nums:
temp = dict()
for mysum in dic:
t... | target-sum | ~80ms python3 | felicia1994 | 0 | 126 | target sum | 494 | 0.456 | Medium | 8,650 |
https://leetcode.com/problems/target-sum/discuss/1646471/Python-DP-solution | class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
def findtarget(nums,currsum, length,target,memo):
if((length,currsum) in memo):
return memo[(length,currsum)]
if(length == len(nums)):
if(currsum == target):
... | target-sum | Python DP solution | mayankpi | -1 | 165 | target sum | 494 | 0.456 | Medium | 8,651 |
https://leetcode.com/problems/teemo-attacking/discuss/1602614/Python-6-lines-O(n)-concise-solution | class Solution(object):
def findPoisonedDuration(self, timeSeries, duration):
repeat = 0
for i in range(len(timeSeries)-1):
diff = timeSeries[i+1] - timeSeries[i]
if diff < duration:
repeat += duration - diff
return len(timeSeries)*duration - repeat | teemo-attacking | Python 6 lines O(n) concise solution | caitlinttl | 8 | 330 | teemo attacking | 495 | 0.57 | Easy | 8,652 |
https://leetcode.com/problems/teemo-attacking/discuss/1414052/Clean-and-Concise-oror-Very-Easy-oror-91-faster | class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
res = 0
for i in range(len(timeSeries)-1):
s = timeSeries[i]+duration-1
if s<timeSeries[i+1]:
res+=s-timeSeries[i]+1
else:
res+=timeSeries[i+1]-timeSeries[i]
... | teemo-attacking | 🐍 Clean & Concise || Very-Easy || 91% faster 📌📌 | abhi9Rai | 2 | 158 | teemo attacking | 495 | 0.57 | Easy | 8,653 |
https://leetcode.com/problems/teemo-attacking/discuss/1267625/Python-3-Easy-Solution-Explained | class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
if duration == 0: return 0
res = 0
for i in range(len(timeSeries)-1):
res += min(timeSeries[i] + duration, timeSeries[i+1]) - timeSeries[i]
res += duration
return res | teemo-attacking | Python 3 Easy Solution Explained | shadow_sm36 | 2 | 180 | teemo attacking | 495 | 0.57 | Easy | 8,654 |
https://leetcode.com/problems/teemo-attacking/discuss/866107/Python-Linear-Time-Solution | class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
if not timeSeries:
return 0
total = 0
for i in range(len(timeSeries) - 1):
total += min(duration, timeSeries[i + 1] - timeSeries[i])
return total + ... | teemo-attacking | [Python] Linear Time Solution | ehdwn1212 | 1 | 31 | teemo attacking | 495 | 0.57 | Easy | 8,655 |
https://leetcode.com/problems/teemo-attacking/discuss/862694/Python3-linear-scan | class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
if not timeSeries: return 0 # edge case (no attack)
ans = 0
for i in range(1, len(timeSeries)):
ans += min(timeSeries[i] - timeSeries[i-1], duration)
return ans + durati... | teemo-attacking | [Python3] linear scan | ye15 | 1 | 47 | teemo attacking | 495 | 0.57 | Easy | 8,656 |
https://leetcode.com/problems/teemo-attacking/discuss/2563114/Python-simple-solution | class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
ans = sec = 0
for i in timeSeries:
ans += i + duration - max(i, sec)
sec = i + duration
return ans | teemo-attacking | Python simple solution | Mark_computer | 0 | 29 | teemo attacking | 495 | 0.57 | Easy | 8,657 |
https://leetcode.com/problems/teemo-attacking/discuss/2257989/Python3-Not-one-liner-but-super-easy-to-understand | class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
n = len(timeSeries)
if n == 0: return 0 # if no timeSeries
result = 0
for i in range(1, n):
# if two timeSeries in the list is more than du... | teemo-attacking | ✅Python3 - Not one liner but super easy to understand | thesauravs | 0 | 26 | teemo attacking | 495 | 0.57 | Easy | 8,658 |
https://leetcode.com/problems/teemo-attacking/discuss/1835519/Python-O(n)-easy-and-explained-with-comments. | class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
i = 0
ans = 0
while i < len(timeSeries)-1:
if (timeSeries[i] + duration) <= timeSeries[i+1]:
ans += duration #If Teemo does not attack again before the... | teemo-attacking | Python O(n) easy and explained with comments. | dc_devesh7 | 0 | 60 | teemo attacking | 495 | 0.57 | Easy | 8,659 |
https://leetcode.com/problems/teemo-attacking/discuss/1803944/5-Lines-Python-Solution-oror-60-Faster-oror-Memory-less-than-80 | class Solution:
def findPoisonedDuration(self, T: List[int], k: int) -> int:
ans = 0
for i in range(len(T)-1):
if T[i+1] < T[i]+k: ans += T[i+1]-T[i]
else: ans += k
return ans + k | teemo-attacking | 5-Lines Python Solution || 60% Faster || Memory less than 80% | Taha-C | 0 | 46 | teemo attacking | 495 | 0.57 | Easy | 8,660 |
https://leetcode.com/problems/teemo-attacking/discuss/1714358/Python-(Better-than-96.44-submissions)-or-O(n)-O(1) | class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
res = 0
max_val = timeSeries[0] + duration - 1
anchor = timeSeries[0]
for i in range(1, len(timeSeries)):
if max_val < timeSeries[i]:
res += max_val - anchor + 1 ... | teemo-attacking | Python (Better than 96.44% submissions) | O(n), O(1) | saurabh717 | 0 | 54 | teemo attacking | 495 | 0.57 | Easy | 8,661 |
https://leetcode.com/problems/teemo-attacking/discuss/1563528/Python-faster-than-99 | class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
last = -duration
res = 0
for time in timeSeries:
if time - last < duration:
res -= last + duration - time
res += duration
last = time
retur... | teemo-attacking | Python faster than 99% | dereky4 | 0 | 153 | teemo attacking | 495 | 0.57 | Easy | 8,662 |
https://leetcode.com/problems/teemo-attacking/discuss/1512476/I'm-returning-len(timeSeries)-when-duration-1.-why-is-this-wrong | class Solution:
def findPoisonedDuration(self, times: List[int], d: int) -> int:
res = 0
prev_interval = [1, 0]
print(len(times))
if d == 1:
return len(times)
for t in times:
new_interval = [t, t+d-1]
if new_interval[... | teemo-attacking | I'm returning len(timeSeries) when duration ==1. why is this wrong? | byuns9334 | 0 | 36 | teemo attacking | 495 | 0.57 | Easy | 8,663 |
https://leetcode.com/problems/teemo-attacking/discuss/1289459/Very-Easy-Way!-O(N) | class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
total = duration
for index in range(1,len(timeSeries)):
total += duration
if timeSeries[index] - timeSeries[index-1] < duration:
total -= duration - (timeSeries[index] - timeSeries[index-1])
return total | teemo-attacking | Very Easy Way! O(N) | yjin232 | 0 | 36 | teemo attacking | 495 | 0.57 | Easy | 8,664 |
https://leetcode.com/problems/teemo-attacking/discuss/1272573/Python3-simple-solution-using-single-for-loop | class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
res = 0
for i in range(len(timeSeries)):
if i == len(timeSeries)-1:
res += duration
else:
res += min(duration, timeSeries[i+1] - timeSeries[i])
... | teemo-attacking | Python3 simple solution using single for loop | EklavyaJoshi | 0 | 43 | teemo attacking | 495 | 0.57 | Easy | 8,665 |
https://leetcode.com/problems/teemo-attacking/discuss/866631/Sweep-Line-python-3 | class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
if not timeSeries:
return 0
n = timeSeries[-1]+duration
count = [0] * (n+2)
for i in timeSeries:
count[i] += 1
count[i+duration] -= 1
for i in rang... | teemo-attacking | Sweep Line-python 3 | zhuzhengyuan824 | 0 | 68 | teemo attacking | 495 | 0.57 | Easy | 8,666 |
https://leetcode.com/problems/teemo-attacking/discuss/866140/Python3-O(n)-with-explanation | class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
if not timeSeries:
return 0
res = 0
for i in range(len(timeSeries) - 1):
if timeSeries[i] + duration - 1 < timeSeries[i + 1]:
res += duration
... | teemo-attacking | Python3 O(n) with explanation | ethuoaiesec | 0 | 34 | teemo attacking | 495 | 0.57 | Easy | 8,667 |
https://leetcode.com/problems/teemo-attacking/discuss/865384/O(n)-simple-solution.-100-faster-in-Python3-submissions. | class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
length = len(timeSeries)
if length == 0:
return 0
final_total = 0
for i in range(length-1):
if timeSeries[i+1] <= timeSeries[i] + duration - 1:
final_t... | teemo-attacking | O(n) simple solution. 100% faster in Python3 submissions. | charizardbhatt | 0 | 36 | teemo attacking | 495 | 0.57 | Easy | 8,668 |
https://leetcode.com/problems/teemo-attacking/discuss/858838/Python-3-or-Two-methods-(Sort-%2B-Sweep-Line-and-Greedy-%2B-1-liner)-or-Explanation | class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
if not timeSeries: return 0
ans = 0
for i in range(len(timeSeries)-1):
ans += min(timeSeries[i + 1] - timeSeries[i], duration) # take the current smallest duration
return ans + du... | teemo-attacking | Python 3 | Two methods (Sort + Sweep Line & Greedy + 1-liner) | Explanation | idontknoooo | 0 | 52 | teemo attacking | 495 | 0.57 | Easy | 8,669 |
https://leetcode.com/problems/teemo-attacking/discuss/858838/Python-3-or-Two-methods-(Sort-%2B-Sweep-Line-and-Greedy-%2B-1-liner)-or-Explanation | class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
return sum(min(timeSeries[i + 1] - timeSeries[i], duration) for i in range(len(timeSeries) - 1)) + duration if timeSeries else 0 | teemo-attacking | Python 3 | Two methods (Sort + Sweep Line & Greedy + 1-liner) | Explanation | idontknoooo | 0 | 52 | teemo attacking | 495 | 0.57 | Easy | 8,670 |
https://leetcode.com/problems/teemo-attacking/discuss/858838/Python-3-or-Two-methods-(Sort-%2B-Sweep-Line-and-Greedy-%2B-1-liner)-or-Explanation | class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
ts = []
for val in timeSeries:
ts.append((val, 1))
ts.append((val+duration, -1))
ans, cur, start = 0, 0, -1
for val, sign in sort... | teemo-attacking | Python 3 | Two methods (Sort + Sweep Line & Greedy + 1-liner) | Explanation | idontknoooo | 0 | 52 | teemo attacking | 495 | 0.57 | Easy | 8,671 |
https://leetcode.com/problems/teemo-attacking/discuss/581232/Python-4-liner | class Solution:
def findPoisonedDuration(self, t: List[int], duration: int) -> int:
if not t:
return 0
n = len(t)
return sum(min(t[t2] - t[t1], duration) for t1, t2 in zip(range(0, n - 1), range(1, n))) + duration | teemo-attacking | Python 4 liner | udayd | 0 | 54 | teemo attacking | 495 | 0.57 | Easy | 8,672 |
https://leetcode.com/problems/next-greater-element-i/discuss/640416/Python-sol-by-monotonic-stack-and-dict-w-Comment | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
# a stack with monotonic decreasing
monotonic_stack = []
# dictionary:
# key: number
# value: next greater number of key
dict_of_greater_number = ... | next-greater-element-i | Python sol by monotonic stack and dict [w/ Comment ] | brianchiang_tw | 14 | 968 | next greater element i | 496 | 0.714 | Easy | 8,673 |
https://leetcode.com/problems/next-greater-element-i/discuss/2080228/Python-Easy-Solution-using-Stack-%2B-Hashmap | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
stack = []
nextGreaterDic = {ch:-1 for ch in nums2}
for i in range(len(nums2)):
while stack and nums2[stack[-1]] < nums2[i]:
nextGreaterDic[nums2[stack.pop()]] ... | next-greater-element-i | [Python] Easy Solution using Stack + Hashmap | samirpaul1 | 4 | 309 | next greater element i | 496 | 0.714 | Easy | 8,674 |
https://leetcode.com/problems/next-greater-element-i/discuss/1884968/Python-Solution-or-O(nums1-%2B-nums2)-or-Monotonic-Stack-(Decreasing)-or-95-Faster | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
ans = defaultdict(lambda: -1)
stack = []
for i in range(len(nums2)):
while stack and stack[-1] < nums2[i]:
ans[stack.pop()] = nums2[i]
stack.append(nums2[i])... | next-greater-element-i | Python Solution | O(nums1 + nums2) | Monotonic Stack (Decreasing) | 95% Faster | Gautam_ProMax | 3 | 264 | next greater element i | 496 | 0.714 | Easy | 8,675 |
https://leetcode.com/problems/next-greater-element-i/discuss/2232038/Python3-solution-using-stack | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
stack = []
nextGreater = {}
i = 0
while i < len(nums2):
while len(stack) and stack[-1] < nums2[i]:
k = stack.pop(-1)
nextGreater[k] = nu... | next-greater-element-i | 📌 Python3 solution using stack | Dark_wolf_jss | 2 | 69 | next greater element i | 496 | 0.714 | Easy | 8,676 |
https://leetcode.com/problems/next-greater-element-i/discuss/2183011/Python-Simple-Python-Solution-By-checking-Next-Element | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
result = []
for i in range(len(nums1)):
start = False
for j in range(len(nums2)):
if nums1[i] == nums2[j]:
start = True
if start == True and nums2[j] > nums1[i]:
result.append(nums2[j])
... | next-greater-element-i | [ Python ] ✅✅ Simple Python Solution By checking Next Element 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 2 | 151 | next greater element i | 496 | 0.714 | Easy | 8,677 |
https://leetcode.com/problems/next-greater-element-i/discuss/2097930/Python3-O(n%2Bm)-MonotonicStack | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
return self.nextGreaterElementWithMonotonic(nums1, nums2)
# optimal solution
# O(n+m)
# runtime: 75ms 55.99%
def nextGreaterElementWithMonotonic(self, num1, num2):
monotonicArray = []
st... | next-greater-element-i | Python3 O(n+m) MonotonicStack; | arshergon | 2 | 140 | next greater element i | 496 | 0.714 | Easy | 8,678 |
https://leetcode.com/problems/next-greater-element-i/discuss/1529058/Python-O(N%2BM)-HashMap-%2B-Stack | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
# build a hashmap for nums1: O(|nums1|)
# Then iterate nums2, maintaining a stack: O(|nums2|)
n1_index = {v: i for i, v in enumerate(nums1)}
ans = [-1] * len(nums1)
stack =... | next-greater-element-i | Python, O(N+M) HashMap + Stack | Glicz | 2 | 105 | next greater element i | 496 | 0.714 | Easy | 8,679 |
https://leetcode.com/problems/next-greater-element-i/discuss/1462848/Brute-Force-oror-Intuition-Explained | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
#nums1 is a subset of nums2
n2 = len(nums2)
arr = []
answer = []
for i in nums1:
for j in range(0, n2):
if i == nums2[j]:
ar... | next-greater-element-i | Brute Force || Intuition Explained | aarushsharmaa | 2 | 171 | next greater element i | 496 | 0.714 | Easy | 8,680 |
https://leetcode.com/problems/next-greater-element-i/discuss/883840/Python3-two-approaches-(forward-and-backward) | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
mp = {} # mapping from element to greater
stack = [] # decreasing mono-stack
for x in reversed(nums2):
while stack and stack[-1] <= x: stack.pop()
if stack: mp[x] = stack[... | next-greater-element-i | [Python3] two approaches (forward & backward) | ye15 | 2 | 183 | next greater element i | 496 | 0.714 | Easy | 8,681 |
https://leetcode.com/problems/next-greater-element-i/discuss/883840/Python3-two-approaches-(forward-and-backward) | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
mp = {} # mapping from element to greater
stack = [] # non-increasing mono-stack
for x in nums2:
while stack and stack[-1] < x: mp[stack.pop()] = x
stack.append(x)
... | next-greater-element-i | [Python3] two approaches (forward & backward) | ye15 | 2 | 183 | next greater element i | 496 | 0.714 | Easy | 8,682 |
https://leetcode.com/problems/next-greater-element-i/discuss/2138568/Python-oror-Easy-Monotonic-stack-with-Hashmap | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
ans = [-1]*len(nums1)
mono = []
ind = defaultdict(int)
for i, v in enumerate(nums1):
ind[v] = i
for i, v in enumerate(nums2):
while mono and nums2[mono[-1]] < v:
val = nums2[mono.pop()]
if val in ind... | next-greater-element-i | Python || Easy Monotonic stack with Hashmap | morpheusdurden | 1 | 96 | next greater element i | 496 | 0.714 | Easy | 8,683 |
https://leetcode.com/problems/next-greater-element-i/discuss/2077454/Python-O(n)-solution-using-stack-and-hashmap | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
stack = []
dic = {k: v for v, k in enumerate(nums2)} # collect index of each element
next_greater = [-1 for _ in range(len(nums2))]
for i, ele in enumerate(nums2):
while stack ... | next-greater-element-i | Python O(n) solution using stack and hashmap | Kennyyhhu | 1 | 101 | next greater element i | 496 | 0.714 | Easy | 8,684 |
https://leetcode.com/problems/next-greater-element-i/discuss/1528824/python-ez-stack-solution | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
stack,cache = [],dict()
for num in nums2[::-1]:
while stack and num > stack[-1]:
stack.pop()
if not stack:
cache[num] = -1
stack.appe... | next-greater-element-i | python ez stack solution | yingziqing123 | 1 | 187 | next greater element i | 496 | 0.714 | Easy | 8,685 |
https://leetcode.com/problems/next-greater-element-i/discuss/1380127/Python-solution-using-map-and-lambda-function | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
mp = {}
for idx, n in enumerate(nums2):
mp[n] = -1
for j in range(idx+1, len(nums2)):
if nums2[j] > n:
mp[n] = nums2[j]
break... | next-greater-element-i | Python solution using map and lambda function | FrostNT1 | 1 | 103 | next greater element i | 496 | 0.714 | Easy | 8,686 |
https://leetcode.com/problems/next-greater-element-i/discuss/1347608/python3-%2B-monotonic-stack-%2B-dictionary-or-time-complexity%3A-O(n) | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
stack = []
m1 = {}
for i in range(len(nums2)):
if len(stack) == 0 or nums2[i] <= stack[-1]:
stack.append(nums2[i])
else:
while len(stack) > 0... | next-greater-element-i | python3 + monotonic stack + dictionary | time complexity: O(n) | Francis98Liu | 1 | 67 | next greater element i | 496 | 0.714 | Easy | 8,687 |
https://leetcode.com/problems/next-greater-element-i/discuss/1297071/python-solution-using-stack-with-comments | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
stack = []
s = {}
for num in nums2:
# if there are number bigger, pop stack and add to map
while stack and stack[-1] < num:
last = stack.pop()
... | next-greater-element-i | python solution using stack with comments | 5tigerjelly | 1 | 160 | next greater element i | 496 | 0.714 | Easy | 8,688 |
https://leetcode.com/problems/next-greater-element-i/discuss/1144019/Python-O(n)-solution-Stack-and-Dictionary | class Solution:
def nextGreaterElement(self, A: List[int], B: List[int]) -> List[int]:
result = {}
stack = []
for val in B:
while stack and val > stack[-1]:
result[stack.pop()] = val
stack.append(val)
... | next-greater-element-i | Python - O(n) solution - Stack and Dictionary | piyushagg19 | 1 | 164 | next greater element i | 496 | 0.714 | Easy | 8,689 |
https://leetcode.com/problems/next-greater-element-i/discuss/1012850/SImple-solution-using-indexing | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
'''
The idea is to find the index corresponding to which numbers is both the lists match, then compare the elements to the right of that index
with the element from first list and if a greater element is foun... | next-greater-element-i | SImple solution using indexing | thisisakshat | 1 | 123 | next greater element i | 496 | 0.714 | Easy | 8,690 |
https://leetcode.com/problems/next-greater-element-i/discuss/252828/Python-3-too-many-loops | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
res = []
for i in range(len(nums1)):
res.append(-1)
for j in range(len(nums1)):
for i in range (len(nums2)):
if(nums1[j] == nums2[i]):
k ... | next-greater-element-i | Python 3 - too many loops | m_hdurina | 1 | 237 | next greater element i | 496 | 0.714 | Easy | 8,691 |
https://leetcode.com/problems/next-greater-element-i/discuss/2847246/Python-Monotonous-Stack-and-Hashmap-Solution | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
nextLargest = dict()
stack = deque()
for num in nums2:
while(stack and stack[-1] < num):
pop = stack.pop()
nextLargest[pop] = num
stack.appen... | next-greater-element-i | Python [Monotonous Stack and Hashmap Solution] | jamesg6198 | 0 | 1 | next greater element i | 496 | 0.714 | Easy | 8,692 |
https://leetcode.com/problems/next-greater-element-i/discuss/2845903/Python-Decreasing-Monotonic-Stack | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
res = [-1]*len(nums1)
hmap = {}
for i,n in enumerate(nums1): hmap[n] = i
# decreasing monotonic stack
stack = []
for i in nums2:
while stack and stack[-1] < i:
... | next-greater-element-i | [Python] Decreasing Monotonic Stack | keshan-spec | 0 | 3 | next greater element i | 496 | 0.714 | Easy | 8,693 |
https://leetcode.com/problems/next-greater-element-i/discuss/2829934/Easy-solution-using-ONLY-Loop-and-Boolean | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
tn2 = []
ans = []
checked = False
for num in nums1:
iin2 = nums2.index(num)
tn2 = nums2[iin2:]
for i in tn2:
if num < i:
... | next-greater-element-i | Easy solution using ONLY Loop and Boolean | bladdeee | 0 | 1 | next greater element i | 496 | 0.714 | Easy | 8,694 |
https://leetcode.com/problems/next-greater-element-i/discuss/2818965/Python-version-of-solution-3 | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
# Stack and hashmap for problem processing
stack = []
next_greater_hashmap = dict()
# put in initial result
stack.append(nums2[0])
# loop indices in nums2 as it is the ma... | next-greater-element-i | Python version of solution 3 | laichbr | 0 | 2 | next greater element i | 496 | 0.714 | Easy | 8,695 |
https://leetcode.com/problems/next-greater-element-i/discuss/2812037/Loop-Solution | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
c = []
for i in range(len(nums1)):
c.append(-1)
for j in range(len(nums1)):
for i in range (len(nums2)):
if(nums1[j] == nums2[i]):
k = i
... | next-greater-element-i | Loop Solution | salonipatadia | 0 | 6 | next greater element i | 496 | 0.714 | Easy | 8,696 |
https://leetcode.com/problems/next-greater-element-i/discuss/2809845/98.56-Faster-Solution-or-Easy-to-Understand-or-Python | class Solution(object):
def nextGreaterElement(self, nums1, nums2):
hashT = {}
stack = []
for i in range(len(nums2) - 1, -1, -1):
while stack and stack[-1] < nums2[i]:
stack.pop()
if not stack:
hashT[nums2[i]] = -1
else:
... | next-greater-element-i | 98.56% Faster Solution | Easy to Understand | Python | its_krish_here | 0 | 11 | next greater element i | 496 | 0.714 | Easy | 8,697 |
https://leetcode.com/problems/next-greater-element-i/discuss/2807752/Next-Greater-Element-I | class Solution:
def nextGreaterElement(self, n1: List[int], n2: List[int]) -> List[int]:
an={}
a=[]
for j in n2:
while a and j>a[-1]:
k=a.pop()
an[k]=j
a.append(j)
for i in a:
an[i]=-1
return [an[i] for i in ... | next-greater-element-i | Next Greater Element I | Ayush_22 | 0 | 6 | next greater element i | 496 | 0.714 | Easy | 8,698 |
https://leetcode.com/problems/next-greater-element-i/discuss/2799778/Simple-brute-force-in-Python | class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
"""
Solution #1: Brute force.
Iterate over nums1 (cur_num1)
- iterate over nums2 (cur_num2)
- - if cur_num1 and cur_num2 equals
--- iterate over last part of nums2: [cur_num... | next-greater-element-i | Simple brute force in Python | denisOgr | 0 | 7 | next greater element i | 496 | 0.714 | Easy | 8,699 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.