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/sign-of-the-product-of-an-array/discuss/2657341/Easy-Python-Solution
class Solution: def arraySign(self, nums: List[int]) -> int: return 1 if math.prod(nums) > 0 else (-1 if math.prod(nums) < 0 else 0)
sign-of-the-product-of-an-array
🐍Easy Python Solution ✔⭐
kanvi26
0
1
sign of the product of an array
1,822
0.66
Easy
26,000
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2604867/Python-One-liner-without-if-else-Statement
class Solution: def arraySign(self, nums: List[int]) -> int: return (sum(i < 0 for i in nums) % 2 * (-2) + 1) * (0 not in nums)
sign-of-the-product-of-an-array
[Python] One-liner without if-else Statement
andy2167565
0
24
sign of the product of an array
1,822
0.66
Easy
26,001
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2449062/Python3-Brute-Force-Easy-Self-Explanatory-Solution
class Solution: def arraySign(self, nums: List[int]) -> int: product = 1 # Initializing product variable (any number multiplied with 1 will be same number) for i in nums: product = product * i # Storing total product in the product variable if product > 0: # Positive return 1 if product < 0: # Negative return -1 if product == 0: # Equal to zero return 0
sign-of-the-product-of-an-array
Python3 Brute-Force Easy Self Explanatory Solution
MusfiqDehan
0
18
sign of the product of an array
1,822
0.66
Easy
26,002
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2426897/Sign-of-the-Product-of-an-Array-or-Python-Step-by-Step-Solution-or
class Solution: def arraySign(self, nums: List[int]) -> int: for i in range(len(nums)): # iterating through loop and setting each element 0,1 or -1 if nums[i] >= 1: nums[i] = 1 elif nums[i] < 0: nums[i] = -1 else: nums[i] = 0 n = 1 if 0 in nums: return 0 else: for e in nums: n *= e #now we have array containg only 1 and -1. so Multiplying them. if n >= 1: return 1 else: return -1
sign-of-the-product-of-an-array
Sign of the Product of an Array | Python Step by Step Solution |
mayankus
0
10
sign of the product of an array
1,822
0.66
Easy
26,003
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2416386/98-faster-than-other
class Solution: def arraySign(self, nums: List[int]) -> int: product = 1 for i in range(len(nums)): product = product * nums[i] if product > 0 : return 1 elif product < 0 : return -1 return 0
sign-of-the-product-of-an-array
98% faster than other
Akash2907
0
28
sign of the product of an array
1,822
0.66
Easy
26,004
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2384838/EASY-oror-95-Faster-oror-python3
class Solution: def arraySign(self, nums: List[int]) -> int: if 0 in nums: return 0 else: c=0 for i in nums: if i<0: c+=1 if c%2==0: return 1 else: return -1
sign-of-the-product-of-an-array
EASY || 95% Faster || python3
keertika27
0
27
sign of the product of an array
1,822
0.66
Easy
26,005
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2238128/Python3.-negative-numbers-counter.-faster-than-99.77-and-memory-better-than-76
class Solution: def arraySign(self, nums: List[int]) -> int: res = 0 for num in nums: if num==0: return 0 elif num<0: res += 1 if res%2>0: return -1 else: return 1
sign-of-the-product-of-an-array
Python3. negative numbers counter. faster than 99.77% and memory better than 76%
devmich
0
37
sign of the product of an array
1,822
0.66
Easy
26,006
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2177002/Python-Simple-Python-Solution
class Solution: def arraySign(self, nums: List[int]) -> int: result = 1 for num in nums: result = result * num if result == 0: return 0 elif result < 0: return -1 else: return 1
sign-of-the-product-of-an-array
[ Python ] ✅✅ Simple Python Solution 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
0
76
sign of the product of an array
1,822
0.66
Easy
26,007
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2159994/Python3-34ms-solution-99
class Solution(object): def arraySign(self, nums): """ :type nums: List[int] :rtype: int """ negs = 0 #for each value in nums, asses its sign for value in nums: #track number of negatives we hit if value <0: negs += 1 #if at any point we hit a 0 we can just return 0 elif value ==0: return 0 #if we have an even number of negative values 0, 2, 4, etc, they multiply to positive values if negs %2 ==0: return 1 #multiply an odd number of negatives will result in a negative number else: return -1 ```
sign-of-the-product-of-an-array
Python3 34ms solution 99%
mm91
0
40
sign of the product of an array
1,822
0.66
Easy
26,008
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2105198/Best-and-very-easy-python-solution
class Solution: def arraySign(self, nums: List[int]) -> int: prod = 1 for i in nums: prod = prod*i if(prod<0): return -1 elif(prod>0): return 1 else: return 0
sign-of-the-product-of-an-array
Best and very easy python solution
yashkumarjha
0
55
sign of the product of an array
1,822
0.66
Easy
26,009
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2099246/PYTHON-or-Simple-python-solution
class Solution: def arraySign(self, nums: List[int]) -> int: res = 1 for i in nums: res *= i if res < 0: return -1 elif res == 0: return 0 else: return 1
sign-of-the-product-of-an-array
PYTHON | Simple python solution
shreeruparel
0
53
sign of the product of an array
1,822
0.66
Easy
26,010
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2073477/Easy-to-understand-python-solution-with-complexities
class Solution: def arraySign(self, nums: List[int]) -> int: ans = 1 for i in nums: if i == 0: ans = 0 break elif i > 0: ans = ans * 1 elif i < 0 : ans = ans * (-1) return ans # time O(N) # space O (1)
sign-of-the-product-of-an-array
Easy to understand python solution with complexities
mananiac
0
36
sign of the product of an array
1,822
0.66
Easy
26,011
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2049345/Beats-80-Python-Simple-Solution
class Solution: def arraySign(self, nums: List[int]) -> int: if 0 in nums: return 0 return 1 if len([x for x in nums if x < 0]) % 2 == 0 else -1
sign-of-the-product-of-an-array
Beats 80% - Python Simple Solution
7yler
0
47
sign of the product of an array
1,822
0.66
Easy
26,012
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2045117/Python-solution
class Solution: def arraySign(self, nums: List[int]) -> int: from functools import reduce from operator import mul if reduce(mul, nums) > 0: return 1 elif reduce(mul, nums) < 0: return -1 else: return 0
sign-of-the-product-of-an-array
Python solution
StikS32
0
41
sign of the product of an array
1,822
0.66
Easy
26,013
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/1889907/simple-Python-Solution-or-Clean-and-Easy-to-Understand
class Solution: def arraySign(self, nums: List[int]) -> int: total = nums[0] for i in nums[1:]: total *= i return self.signFunc(total) def signFunc(self, n: int) ->int: if n > 0: return 1 elif n < 0: return -1 return 0
sign-of-the-product-of-an-array
simple Python Solution | Clean and Easy to Understand
parthpatel9414
0
66
sign of the product of an array
1,822
0.66
Easy
26,014
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/1888159/Python-clean-and-simple!-Literal-approach
class Solution: def arraySign(self, nums): # There is a function signFunc(x) that returns 1 if x is positive, -1 if x is negative, 0 if x is equal to 0. def signFunc(x): return 1 if x > 0 else -1 if x < 0 else 0 # Let product be the product of all values in the array nums. product = prod(nums) # Return signFunc(product). return signFunc(product)
sign-of-the-product-of-an-array
Python - clean and simple! Literal approach
domthedeveloper
0
53
sign of the product of an array
1,822
0.66
Easy
26,015
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/1863692/Python-Solution
class Solution: def arraySign(self, nums: List[int]) -> int: ans = 1 for i in range(len(nums)): if nums[i] == 0: return 0 else: if nums[i] < 0: ans *= -1 else: ans *= 1 return ans
sign-of-the-product-of-an-array
Python Solution
DietCoke777
0
44
sign of the product of an array
1,822
0.66
Easy
26,016
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/1859178/Python-dollarolution
class Solution: def arraySign(self, nums: List[int]) -> int: if 0 in nums: return 0 count = 1 for i in nums: if i < 0: count *= -1 return count
sign-of-the-product-of-an-array
Python $olution
AakRay
0
37
sign of the product of an array
1,822
0.66
Easy
26,017
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/1152420/Python3-simulation
class Solution: def findTheWinner(self, n: int, k: int) -> int: nums = list(range(n)) i = 0 while len(nums) > 1: i = (i + k-1) % len(nums) nums.pop(i) return nums[0] + 1
find-the-winner-of-the-circular-game
[Python3] simulation
ye15
16
1,900
find the winner of the circular game
1,823
0.779
Medium
26,018
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/1152420/Python3-simulation
class Solution: def findTheWinner(self, n: int, k: int) -> int: ans = 0 for x in range(2, n+1): ans = (ans + k) % x return ans + 1
find-the-winner-of-the-circular-game
[Python3] simulation
ye15
16
1,900
find the winner of the circular game
1,823
0.779
Medium
26,019
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/1181024/Python3-simple-solution-beats-90-users
class Solution: def findTheWinner(self, n: int, k: int) -> int: l = list(range(1,n+1)) i = 0 while len(l) > 1: i = (i + k - 1)%len(l) l.pop(i) return l[0]
find-the-winner-of-the-circular-game
Python3 simple solution beats 90% users
EklavyaJoshi
3
206
find the winner of the circular game
1,823
0.779
Medium
26,020
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/2770302/Simple-Approach%3A-BF
class Solution: def findTheWinner(self, n: int, k: int) -> int: l = list(range(n)) s = 0 while len(l) > 1: i = (k%len(l) + s)%len(l) l = l[: i] + l[i+1: ] s = i-1 return l[0] or n
find-the-winner-of-the-circular-game
Simple Approach: BF
Mencibi
1
106
find the winner of the circular game
1,823
0.779
Medium
26,021
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/1993022/Python-oror-6-line-using-SET
class Solution: def findTheWinner(self, n: int, k: int) -> int: friends = sorted(set(range(1, n + 1))) idx = 0 while len(friends) > 1: idx = (idx + k - 1) % len(friends) friends.remove(friends[idx]) return friends[0]
find-the-winner-of-the-circular-game
Python || 6-line using SET
gulugulugulugulu
1
106
find the winner of the circular game
1,823
0.779
Medium
26,022
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/1726293/Python-Solution-Using-Recursion
class Solution: def findTheWinner(self, n: int, k: int) -> int: def solve(n, k, s): if len(n)==1: return n[-1] n.pop(s) l = len(n) s = (s+k-1)%l return solve(n,k,s) l = list(range(1, n+1)) x = (0+k-1)%n ans = solve(l, k, x) return ans
find-the-winner-of-the-circular-game
Python Solution Using Recursion
ayush_kushwaha
1
107
find the winner of the circular game
1,823
0.779
Medium
26,023
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/1280511/python3-simple-iterative-solution-88-faster
class Solution: def findTheWinner(self, n: int, k: int) -> int: temp,curr=[x for x in range(1, n+1)], 0 while len(temp)>1: curr=(curr+k-1)%len(temp) temp.pop(curr) if len(temp)==curr: curr=0 return temp[0]
find-the-winner-of-the-circular-game
[python3] simple, iterative solution, 88% faster
alter_mage
1
116
find the winner of the circular game
1,823
0.779
Medium
26,024
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/1153739/Python3-Stimulation-Easy
class Solution: def findTheWinner(self, n: int, k: int) -> int: arr = [i+1 for i in range(n)] prev = k-1 # k-1 steps from 1 for i in range(n-1): del arr[prev] # delete #print(arr) prev = (prev+k-1)%len(arr) # move k-1 more steps, mod because circular return arr[0]
find-the-winner-of-the-circular-game
Python3 Stimulation Easy
gautamsw51
1
97
find the winner of the circular game
1,823
0.779
Medium
26,025
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/1152779/Josephus-problem-oror-No-RECURSION-oror-Python
class Solution: def findTheWinner(self, n: int, k: int) -> int: a=[] for i in range(1,n+1): a.append(i) i = 0 while len(a) != 1: i = (i + k-1) % len(a) a.remove(a[i]) return a[0]
find-the-winner-of-the-circular-game
Josephus problem || No RECURSION || Python
suvoo
1
194
find the winner of the circular game
1,823
0.779
Medium
26,026
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/2847011/Python-code-with-Intuition-or-O(n)-or-O(1)
class Solution: def findTheWinner(self, n: int, k: int) -> int: return self.helper(n,k)+1 def helper(self, n:int, k:int)-> int: if(n==1): return 0 prevWinner = self.helper(n-1, k) return (prevWinner + k) % n
find-the-winner-of-the-circular-game
Python code with Intuition | O(n) | O(1)
samart3010
0
5
find the winner of the circular game
1,823
0.779
Medium
26,027
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/2781788/Faster-than-100
class Solution: def solve(self,n,k): if n == 1: return 0 return (self.solve(n-1,k)+k )%n def findTheWinner(self, n: int, k: int) -> int: return self.solve(n,k)+1
find-the-winner-of-the-circular-game
Faster than 100%
sanskar_
0
17
find the winner of the circular game
1,823
0.779
Medium
26,028
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/2757338/Python3-Solution-with-using-queue
class Solution: def findTheWinner(self, n: int, k: int) -> int: q = collections.deque([i for i in range(1, n + 1)]) while len(q) > 1: for i in range(k - 1): val = q.popleft() q.append(val) q.popleft() return q.popleft()
find-the-winner-of-the-circular-game
[Python3] Solution with using queue
maosipov11
0
7
find the winner of the circular game
1,823
0.779
Medium
26,029
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/2714886/Python-O(n)-and-O(n*k)-Solutions
class Solution: def findTheWinner(self, n: int, k: int) -> int: # O(n), O(1) frd = 1 for i in range(2,n+1): frd = (frd+k-1) % i+1 return frd # Using queue O(n*k), O(n) q = deque(range(1,n+1)) while len(q) > 1: # append friends in clockwise dir for _ in range(k-1): q.append(q.popleft()) q.popleft() return q[0]
find-the-winner-of-the-circular-game
Python O(n) and O(n*k) Solutions
oluomo_lade
0
14
find the winner of the circular game
1,823
0.779
Medium
26,030
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/2705458/python-solution
class Solution: def findTheWinner(self, n: int, k: int) -> int: ls = [i for i in range(1,n+1)] s=0 while len(ls) != 1: rem = (s + k-1) % len(ls) ls.pop(rem) s = rem return ls[0]
find-the-winner-of-the-circular-game
python solution
anshsharma17
0
10
find the winner of the circular game
1,823
0.779
Medium
26,031
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/2683297/Python-Solution-Find-the-Winner-of-the-Circular-Game-using-queue
class Solution: def findTheWinner(self, n: int, k: int) -> int: q = [i+1 for i in range(n)] while len(q)>1: for i in range(k-1): t = q.pop(0) q.append(t) q.pop(0) return q.pop(0)
find-the-winner-of-the-circular-game
Python Solution Find the Winner of the Circular Game using queue
sarthakchawande14
0
4
find the winner of the circular game
1,823
0.779
Medium
26,032
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/2667978/Python-oror-Easy-Solution-oror-Recursion
class Solution: def findTheWinner(self, n: int, k: int) -> int: def get_indx(cur_indx, steps, lnth): new_indx = cur_indx + steps - 1 if new_indx >= lnth: new_indx = new_indx%lnth return new_indx def get_ans(arr, indx): lnth = len(arr) if lnth == 1: return arr[0] else: arr.pop(indx) return get_ans(arr, get_indx(indx, k, lnth - 1)) return get_ans([i for i in range(1, n+1)], get_indx(0, k, n))
find-the-winner-of-the-circular-game
Python || Easy Solution || Recursion
Rahul_Kantwa
0
7
find the winner of the circular game
1,823
0.779
Medium
26,033
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/2631466/Pyhton-basic-approach
class Solution: def findTheWinner(self, n: int, k: int) -> int: arr=list(range(1,n+1)) while len(arr) != 1: d=(k-1)%len(arr) arr.pop(d) ''' [1, 2, 3, 4, 5]Pop - 2 at d= 1 [3, 4, 5, 1] Pop -4 at d=1 [5, 1, 3] Pop - 1 at d=1 [3, 5] 5 Pop -5 at d=1 ''' arr=arr[d:]+arr[:d] return arr[0]
find-the-winner-of-the-circular-game
Pyhton basic approach
beingab329
0
20
find the winner of the circular game
1,823
0.779
Medium
26,034
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/2425593/Python3-or-Simple-Queue-Solution
class Solution: #Time-Complexity: O(n + (n-1) * (k-1)) -> O(n + nk) -> O(n) #Space-Complexity: O(n) def findTheWinner(self, n: int, k: int) -> int: #approach: Initialize a queue DS that stores the players labels from i = 1 to n! #Reading elements from left to right would correspond to going in clockwise direction! #For n-1 times, we can pop from front and push back to our queue since those players #will be counted but won't be ousted from the game! #For the kth player though, the kth player will be eliminated by popping and not pushing it back! #Game's turns will continue as long as there is at least 2 players. We can check for this #by checking length of queue after every round! #Rem. player in queue is winner! q = collections.deque() #initialize queue! for i in range(1, n+1): q.append(i) #run a while loop as long as game can continue! while(len(q) >= 2): #for k-1 first players starting from the player at front of queue including, skip over them! for i in range(k-1): cur = q.popleft() q.append(cur) #then, remove the kth player from the game! q.popleft() #once we break out of while loop return the sole rem player in queue! winner = q.popleft() return winner
find-the-winner-of-the-circular-game
Python3 | Simple Queue Solution
JOON1234
0
22
find the winner of the circular game
1,823
0.779
Medium
26,035
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/2412167/Python-O(N)
class Solution: def findTheWinner(self, n: int, k: int) -> int: if n == 1: return 1 # start from the last round -> choose the winner in [1, 2] (2 player) if k % 2 == 0: start = 1 else: start = 2 # current_index = (prev_round_index - k) % (size of pre_round player) # prev_round_idx = (current_index + k) % (size of pre_round player) # end at first round (there are n players) # travel from 3 players, because we know the winner in 2 players round for number_of_player in range(3, n+1): start += k # get the previous round index number start = start % number_of_player if start == 0: start = number_of_player return start
find-the-winner-of-the-circular-game
Python O(N)
TerryHung
0
43
find the winner of the circular game
1,823
0.779
Medium
26,036
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/2229400/Python-4-lines-simulation.-Time%3A-O(N2).-Space%3A-O(N)
class Solution: def findTheWinner(self, n: int, k: int) -> int: idx, nums = 0, list(range(1, n+1)) while len(nums) > 1: del nums[idx := (idx + k - 1) % len(nums)] return nums[0]
find-the-winner-of-the-circular-game
Python, 4 lines, simulation. Time: O(N^2). Space: O(N)
blue_sky5
0
21
find the winner of the circular game
1,823
0.779
Medium
26,037
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/1988841/Python3-Solution-by-Simulation
class Solution: def findTheWinner(self, n: int, k: int) -> int: friends = [i for i in range(1, n + 1)] p = 0 while len(friends) > 1: p = (p + k - 1) % len(friends) friends.pop(p) return friends[0]
find-the-winner-of-the-circular-game
[Python3] Solution by Simulation
terrencetang
0
50
find the winner of the circular game
1,823
0.779
Medium
26,038
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/1933431/Easy-understanding-Queue-in-Python
class Solution: def findTheWinner(self, n: int, k: int) -> int: queue = deque(list(range(1, n+1))) while len(queue) > 1: for i in range(k): tmp = queue.popleft() if i != k-1: queue.append(tmp) return queue.popleft()
find-the-winner-of-the-circular-game
Easy understanding Queue in Python
echonesis
0
51
find the winner of the circular game
1,823
0.779
Medium
26,039
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/1645559/PYTHON-Simulation-Solution-Easy-to-understand
class Solution: def findTheWinner(self, n: int, k: int) -> int: arr = [i for i in range(1, n+1)] i = 0 count = 1 while len(arr) > 1: if count == k: arr.pop(i) count = 1 else: count += 1 i += 1 i = i % len(arr) return arr[0]
find-the-winner-of-the-circular-game
[PYTHON] Simulation Solution - Easy to understand
satyu
0
138
find the winner of the circular game
1,823
0.779
Medium
26,040
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/1397757/Python3-Solution
class Solution: def findTheWinner(self, n: int, k: int) -> int: circular_list = [n for n in range(1, n + 1)] i = 0 iteration = n - 1 while iteration > 0: for x in range(1, k + 1): if i > len(circular_list) - 1: i = i - len(circular_list) i += 1 iteration -= 1 circular_list.remove(circular_list[i - 1]) i -= 1 return circular_list[0]
find-the-winner-of-the-circular-game
Python3 Solution
RobertObrochta
0
93
find the winner of the circular game
1,823
0.779
Medium
26,041
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/1166045/Python3-Simple-Solution-with-Explanation-and-Comments-Beats-96-in-Running-Time
class Solution: def findTheWinner(self, n: int, k: int) -> int: ''' 1. Make a list of players from 1 to n inclusive 2. Until n-1 players have been eliminated, starting from a specific index, find the index of the player to be eliminated in each round. The index of the player to be eliminated in round i = (starting index of round i) + k -1 3. To consider wrapping around, take [(starting index of round i) + k -1]%(number of players in round i) 4. The starting index for round (i+1) = the index of the player to be eliminated in round i, as considering a list, if one item is removed from the list, then the length of the list decreases by 1. ''' players = [i for i in range(1,n+1)] #make a list of players from 1 to n inclusive start=0 #this is the starting index from where k positions are checked while len(players)>1: #the final player in the list is the winner, so check until n-1 players have been eliminated start = (start+k-1)%len(players) #for wrapping around, use the mod operator to get the correct index of the player to be eliminated del players[start] #eliminate the player at that index return players[0] #the only player remaining in the list is the winner
find-the-winner-of-the-circular-game
Python3 Simple Solution with Explanation and Comments, Beats 96% in Running Time
bPapan
0
107
find the winner of the circular game
1,823
0.779
Medium
26,042
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/1152565/Python-Simple-Solution-or-O(n)-or-100-Faster
class Solution: def findTheWinner(self, n: int, k: int) -> int: if k==1: return n lst = list(range(1, n+1)) p = n ptr = -1 while p!=1: x = ptr+k ptr = x if x < p else x%p lst.pop(ptr) ptr -= 1 p -= 1 return lst[0]
find-the-winner-of-the-circular-game
Python Simple Solution | O(n) | 100% Faster
VijayantShri
0
94
find the winner of the circular game
1,823
0.779
Medium
26,043
https://leetcode.com/problems/find-the-winner-of-the-circular-game/discuss/1152479/Python-DLL-Solution
class Solution: def findTheWinner(self, n: int, k: int) -> int: class Node: def __init__(self,val): self.prev = None self.next = None self.val = val head = Node(1) temp = head i = 2 while i<n: nnode = Node(i) temp.next = nnode nnode.prev = temp temp = temp.next i += 1 lastnode = Node(n) temp.next = lastnode lastnode.prev = temp lastnode.next = head head.prev = lastnode ##game start,prevnode = head,head.prev while start: if start == start.next: #case to check if this node is the winner return start.val i = 1 while i<k: #move k number of steps prevnode = start start = start.next i += 1 #delete this node p = start.next prevnode.next = p p.prev = prevnode start = p
find-the-winner-of-the-circular-game
Python DLL Solution
SaSha59
0
95
find the winner of the circular game
1,823
0.779
Medium
26,044
https://leetcode.com/problems/minimum-sideway-jumps/discuss/1480131/Python-3-or-DP-or-Explanation
class Solution: def minSideJumps(self, obstacles: List[int]) -> int: n = len(obstacles) dp = [[sys.maxsize] * n for _ in range(3)] dp[0][0]= 1 dp[1][0]= 0 dp[2][0]= 1 for i in range(1, n): dp[0][i] = dp[0][i-1] if obstacles[i] != 1 else sys.maxsize dp[1][i] = dp[1][i-1] if obstacles[i] != 2 else sys.maxsize dp[2][i] = dp[2][i-1] if obstacles[i] != 3 else sys.maxsize if obstacles[i] != 1: for j in [1, 2]: dp[0][i] = min(dp[0][i], dp[j][i] + 1 if obstacles[i] != j+1 else sys.maxsize) if obstacles[i] != 2: for j in [0, 2]: dp[1][i] = min(dp[1][i], dp[j][i] + 1 if obstacles[i] != j+1 else sys.maxsize) if obstacles[i] != 3: for j in [0, 1]: dp[2][i] = min(dp[2][i], dp[j][i] + 1 if obstacles[i] != j+1 else sys.maxsize) return min(dp[0][-1], dp[1][-1], dp[2][-1])
minimum-sideway-jumps
Python 3 | DP | Explanation
idontknoooo
4
539
minimum sideway jumps
1,824
0.495
Medium
26,045
https://leetcode.com/problems/minimum-sideway-jumps/discuss/1480131/Python-3-or-DP-or-Explanation
class Solution: def minSideJumps(self, obstacles: List[int]) -> int: n = len(obstacles) a, b, c = 1, 0, 1 for i in range(1, n): a = a if obstacles[i] != 1 else sys.maxsize b = b if obstacles[i] != 2 else sys.maxsize c = c if obstacles[i] != 3 else sys.maxsize if obstacles[i] != 1: a = min(a, b + 1 if obstacles[i] != 2 else sys.maxsize, c + 1 if obstacles[i] != 3 else sys.maxsize) if obstacles[i] != 2: b = min(b, a + 1 if obstacles[i] != 1 else sys.maxsize, c + 1 if obstacles[i] != 3 else sys.maxsize) if obstacles[i] != 3: c = min(c, a + 1 if obstacles[i] != 1 else sys.maxsize, b + 1 if obstacles[i] != 2 else sys.maxsize) return min(a, b, c)
minimum-sideway-jumps
Python 3 | DP | Explanation
idontknoooo
4
539
minimum sideway jumps
1,824
0.495
Medium
26,046
https://leetcode.com/problems/minimum-sideway-jumps/discuss/1152546/Python3-dp
class Solution: def minSideJumps(self, obstacles: List[int]) -> int: ans = [0]*3 for i in reversed(range(len(obstacles) - 1)): tmp = [inf]*3 for k in range(3): if obstacles[i]-1 != k: tmp[k] = ans[k] if obstacles[i]-1 != (k+1)%3: tmp[k] = min(tmp[k], 1 + ans[(k+1)%3]) if obstacles[i]-1 != (k+2)%3: tmp[k] = min(tmp[k], 1 + ans[(k+2)%3]) ans = tmp return ans[1]
minimum-sideway-jumps
[Python3] dp
ye15
2
137
minimum sideway jumps
1,824
0.495
Medium
26,047
https://leetcode.com/problems/minimum-sideway-jumps/discuss/1238483/Python-Recursive-greater-Bottom-Up
class Solution: def minSideJumps(self, obstacles: List[int]) -> int: @functools.lru_cache(None) def recurse(idx, lane): if idx >= len(obstacles): return 0 if obstacles[idx] == lane: return float('inf') return min([ (1 if l != lane else 0) + recurse(idx+1, l) for l in range(1, 4) if l != obstacles[idx] ]) return recurse(0, 2)
minimum-sideway-jumps
Python Recursive -> Bottom Up
dev-josh
1
194
minimum sideway jumps
1,824
0.495
Medium
26,048
https://leetcode.com/problems/minimum-sideway-jumps/discuss/1238483/Python-Recursive-greater-Bottom-Up
class Solution: def minSideJumps(self, A: List[int]) -> int: # Because we also "look ahead", we want to shorten the DP array N = len(A) - 1 dp = [ [float('inf')] * 3 for _ in range(N) ] # Initial state dp[0] = [1, 0, 1] for i in range(1, N): for j in range(3): """ This line here is the tricky one. Think about this: if we can jump to a space but the immediate next space is a rock, can will we succeed? NO. We don't! So we must consider this """ if A[i] == j+1 or A[i+1] == j+1: dp[i][j] = float('inf') else: """ Other people use the modulo "%" to be a bit more clean, but to me, this is the easiest to read :) """ dp[i][j] = min([ dp[i-1][0] + (1 if j != 0 else 0), dp[i-1][1] + (1 if j != 1 else 0), dp[i-1][2] + (1 if j != 2 else 0), ]) return min(dp[-1])
minimum-sideway-jumps
Python Recursive -> Bottom Up
dev-josh
1
194
minimum sideway jumps
1,824
0.495
Medium
26,049
https://leetcode.com/problems/minimum-sideway-jumps/discuss/1238483/Python-Recursive-greater-Bottom-Up
class Solution: def minSideJumps(self, A: List[int]) -> int: # 1 N = len(A) - 1 dp = [1, 0, 1] # 2 for i in range(1, N): for j in range(3): # 3 if j+1 == A[i]: dp[j] = float('inf') else: dp[j] = min( dp[0] + (1 if j != 0 else 0) + (float('inf') if A[i] == 1 else 0), dp[1] + (1 if j != 1 else 0) + (float('inf') if A[i] == 2 else 0), dp[2] + (1 if j != 2 else 0) + (float('inf') if A[i] == 3 else 0), ) # 4 return min(dp)
minimum-sideway-jumps
Python Recursive -> Bottom Up
dev-josh
1
194
minimum sideway jumps
1,824
0.495
Medium
26,050
https://leetcode.com/problems/minimum-sideway-jumps/discuss/1161090/Python-Solution-Minimum-Sideway-Jumps
class Solution: def minSideJumps(self, obstacles: List[int]) -> int: n = len(obstacles) inf = sys.maxsize lane1,lane2,lane3 = 1,0,1 for indx in range(1,n): lane1_yes,lane2_yes,lane3_yes = True,True,True if obstacles[indx] == 1: lane1 = inf lane1_yes = False elif obstacles[indx] == 2: lane2 = inf lane2_yes = False elif obstacles[indx] == 3: lane3 = inf lane3_yes = False if lane1_yes: lane1 = min(lane1,min(lane2+1,lane3+1)) if lane2_yes: lane2 = min(lane2,min(lane1+1,lane3+1)) if lane3_yes: lane3 = min(lane3,min(lane1+1,lane2+1)) return min(lane3,min(lane1,lane2))
minimum-sideway-jumps
Python Solution-Minimum Sideway Jumps
SaSha59
1
188
minimum sideway jumps
1,824
0.495
Medium
26,051
https://leetcode.com/problems/minimum-sideway-jumps/discuss/1153029/100-faster-0(n)-bottom-up-dp-solution
class Solution: def minSideJumps(self, obstacles: List[int]) -> int: n = len(obstacles)-1 mem = [[-1]*(4) for i in range(n+1)] def jumps(pos, lane): # if we reached the last position return 0 if pos == n: return 0 # first we check in memoization table, if already present return that if mem[pos][lane]!=-1: return mem[pos][lane] #if either we dont have any obstacle in next pos or the obstacle in not in our lane, then # recur for the next position but in the same lane if obstacles[pos+1] == 0 or obstacles[pos+1]!=lane: mem[pos][lane] = jumps(pos+1, lane) return mem[pos][lane] # if obstacle is in our lane then we need to jump to sideways if obstacles[pos+1] == lane: first, second = sys.maxsize, sys.maxsize # we can check for all the lanes, if currently in lane 1, then check for lane 2 and 3 if lane == 1: #if we have a obstacle in 2, then we cant jump there. #else we can jump so add 1 to the ans and recur for next position if obstacles[pos]!=2: first = 1+ jumps(pos+1, 2) if obstacles[pos]!=3: second =1+ jumps(pos+1, 3) if lane == 2: if obstacles[pos]!=1: first = 1+ jumps(pos+1, 1) if obstacles[pos]!=3: second = 1+ jumps(pos+1, 3) if lane == 3: if obstacles[pos]!=2: first = 1+ jumps(pos+1, 2) if obstacles[pos]!=1: second = 1+ jumps(pos+1, 1) #after calculation of first and second path, take the minimum mem[pos][lane] = min(first, second) return mem[pos][lane] return jumps(0, 2)
minimum-sideway-jumps
100% faster, 0(n) bottom up dp solution
deleted_user
1
222
minimum sideway jumps
1,824
0.495
Medium
26,052
https://leetcode.com/problems/minimum-sideway-jumps/discuss/1152560/4-lines-Python-O(n)-Dynamic-Programming-with-detailed-explanation-and-readable-code
class Solution: def minSideJumps(self, obstacles: list[int]) -> int: n = len(obstacles) &nbsp; &nbsp; &nbsp; &nbsp;dp = [[float("inf")] * 4 for _ in range(n - 1)] + [[0] * 4] for i in range(n - 2, -1, -1): for l in {1, 2, 3} - {obstacles[i]}: if obstacles[i + 1] != l: dp[i][l] = dp[i + 1][l] else: for nl in {1, 2, 3} - {obstacles[i], l}: dp[i][l] = min(dp[i][l], 1 + dp[i + 1][nl]) return dp[0][2]
minimum-sideway-jumps
4 lines Python O(n) Dynamic Programming with detailed explanation and readable code
guangxu-li
1
178
minimum sideway jumps
1,824
0.495
Medium
26,053
https://leetcode.com/problems/minimum-sideway-jumps/discuss/1152560/4-lines-Python-O(n)-Dynamic-Programming-with-detailed-explanation-and-readable-code
class Solution: def minSideJumps(self, obs: list[int]) -> int: dp = [0] * 4 for i in range(len(obs) - 2, -1, -1): # for l in {1, 2, 3} - {obs[i], obs[i + 1]}: # if obs[i + 1] == l: # dp[l] = 1 + min(dp[nl] for nl in {1, 2, 3} - {obs[i], l}) dp[obs[i + 1]] = 1 + min(dp[nl] for nl in {1, 2, 3} - {obs[i], obs[i + 1]}) return dp[2]
minimum-sideway-jumps
4 lines Python O(n) Dynamic Programming with detailed explanation and readable code
guangxu-li
1
178
minimum sideway jumps
1,824
0.495
Medium
26,054
https://leetcode.com/problems/minimum-sideway-jumps/discuss/1352763/One-pass-update-min-jumps-per-lane-89-speed
class Solution: def minSideJumps(self, obstacles: List[int]) -> int: lanes = [1, 0, 1] for obstacle_id in obstacles: if obstacle_id == 0: lanes[0] = min(lanes[0], lanes[1] + 1, lanes[2] + 1) lanes[1] = min(lanes[1], lanes[0] + 1, lanes[2] + 1) lanes[2] = min(lanes[2], lanes[0] + 1, lanes[1] + 1) elif obstacle_id == 1: lanes[0] = inf lanes[1] = min(lanes[1], lanes[2] + 1) lanes[2] = min(lanes[1] + 1, lanes[2]) elif obstacle_id == 2: lanes[1] = inf lanes[0] = min(lanes[0], lanes[2] + 1) lanes[2] = min(lanes[2], lanes[0] + 1) elif obstacle_id == 3: lanes[2] = inf lanes[0] = min(lanes[0], lanes[1] + 1) lanes[1] = min(lanes[1], lanes[0] + 1) return min(lanes)
minimum-sideway-jumps
One pass, update min jumps per lane, 89% speed
EvgenySH
0
108
minimum sideway jumps
1,824
0.495
Medium
26,055
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1178397/Python3-simple-solution-beats-90-users
class Solution: def minOperations(self, nums: List[int]) -> int: count = 0 for i in range(1,len(nums)): if nums[i] <= nums[i-1]: x = nums[i] nums[i] += (nums[i-1] - nums[i]) + 1 count += nums[i] - x return count
minimum-operations-to-make-the-array-increasing
Python3 simple solution beats 90% users
EklavyaJoshi
8
764
minimum operations to make the array increasing
1,827
0.782
Easy
26,056
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1609290/WEEB-DOES-PYTHON
class Solution: def minOperations(self, nums: List[int]) -> int: count = 0 for i in range(1,len(nums)): if nums[i] <= nums[i-1]: initial = nums[i] nums[i] = nums[i-1] + 1 count += nums[i] - initial return count
minimum-operations-to-make-the-array-increasing
WEEB DOES PYTHON
Skywalker5423
2
95
minimum operations to make the array increasing
1,827
0.782
Easy
26,057
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/2152188/Python-or-Easy-and-Understanding-Solution
class Solution: def minOperations(self, nums: List[int]) -> int: n=len(nums) if(n==1): return 0 ans=0 for i in range(1,n): if(nums[i]<=nums[i-1]): ans+=nums[i-1]-nums[i]+1 nums[i]=nums[i-1]+1 return ans
minimum-operations-to-make-the-array-increasing
Python | Easy & Understanding Solution
backpropagator
1
108
minimum operations to make the array increasing
1,827
0.782
Easy
26,058
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1753941/Simple-Python-Solution
class Solution: def minOperations(self, nums: List[int]) -> int: s = 0 for i in range(1,len(nums)): s += max(0,nums[i-1]-nums[i]+1) nums[i] = max(nums[i-1]+1, nums[i]) return s
minimum-operations-to-make-the-array-increasing
Simple Python Solution
MengyingLin
1
65
minimum operations to make the array increasing
1,827
0.782
Easy
26,059
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1326644/Simple-Python-Solution
class Solution: def minOperations(self, nums) -> int: count=0 value=0 for i in range(len(nums)-1): if nums[i+1] <= nums[i]: value = (nums[i]+1) -nums[i+1] nums[i+1] +=value count+=value return count
minimum-operations-to-make-the-array-increasing
Simple Python Solution
sangam92
1
168
minimum operations to make the array increasing
1,827
0.782
Easy
26,060
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1171530/Python-Easy-solution-faster-than-92
class Solution: def minOperations(self, nums: List[int]) -> int: n=0 for i in range(0,len(nums)-1): if (nums[i]>=nums[i+1]): n+=1+(nums[i]-nums[i+1]) nums[i+1]=nums[i]+1 return n
minimum-operations-to-make-the-array-increasing
Python Easy solution faster than 92%
kashish_ag
1
102
minimum operations to make the array increasing
1,827
0.782
Easy
26,061
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1163946/Intuitive-approach-by-loop
class Solution: def minOperations(self, nums: List[int]) -> int: ans = 0 for i in range(1, len(nums)): if nums[i] <= nums[i-1]: ans += abs(nums[i-1]-nums[i]) + 1 nums[i] = nums[i-1] + 1 return ans
minimum-operations-to-make-the-array-increasing
Intuitive approach by loop
puremonkey2001
1
37
minimum operations to make the array increasing
1,827
0.782
Easy
26,062
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1163137/Python3-sweep
class Solution: def minOperations(self, nums: List[int]) -> int: ans = 0 for i in range(1, len(nums)): if nums[i-1] >= nums[i]: ans += 1 + nums[i-1] - nums[i] nums[i] = 1 + nums[i-1] return ans
minimum-operations-to-make-the-array-increasing
[Python3] sweep
ye15
1
70
minimum operations to make the array increasing
1,827
0.782
Easy
26,063
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/2846523/Easy-to-Understand.
class Solution: def minOperations(self, nums: List[int]) -> int: res=0 first,second = 0,1 total = len(nums) while total>1: if nums[second] >= nums[first]+1: total-=1 else: total-=1 need = nums[first] - nums[second] +1 res+=need nums[second] +=need first+=1 second+=1 return res
minimum-operations-to-make-the-array-increasing
Easy to Understand.
Warrior-Quant
0
1
minimum operations to make the array increasing
1,827
0.782
Easy
26,064
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/2814506/python-or-easy-or-solution
class Solution: def minOperations(self, nums: List[int]) -> int: cn=0 l=[] n=len(nums) l.append(nums[0]) for i in range (1,(len(nums))): if nums[i]<=l[i-1]: l.insert(i,(l[i-1]+1)) cn+=(l[i-1]+1)-nums[i] else: l.append(nums[i]) print(l) return cn
minimum-operations-to-make-the-array-increasing
python | easy | solution
tush18
0
1
minimum operations to make the array increasing
1,827
0.782
Easy
26,065
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/2803779/Python-Solution
class Solution: def minOperations(self, nums: List[int]) -> int: if(len(nums)==1): return 0 o=0 for i in range(1,len(nums)): if(nums[i]<=nums[i-1]): o=nums[i-1]-nums[i]+1+o nums[i]=nums[i]+nums[i-1]-nums[i]+1 return o
minimum-operations-to-make-the-array-increasing
Python Solution
CEOSRICHARAN
0
3
minimum operations to make the array increasing
1,827
0.782
Easy
26,066
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/2797459/Python-Simple-greedy-solution
class Solution: def minOperations(self, nums: list[int]) -> int: ans, prev = 0, nums[0] for i in nums[1:]: if prev >= i: ans += prev - i + 1 prev += 1 else: prev = i return ans
minimum-operations-to-make-the-array-increasing
[Python] Simple greedy solution
Mark_computer
0
1
minimum operations to make the array increasing
1,827
0.782
Easy
26,067
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/2547646/EASY-PYTHON3-SOLUTION-98faster
class Solution: def minOperations(self, nums: List[int]) -> int: if len(nums) == 1: return 0 count = 0 for i in range(1, len(nums)): if nums[i] <= nums[i-1]: temp = nums[i] nums[i] = max(nums[i-1]+1, nums[i]) count += nums[i] - temp return count
minimum-operations-to-make-the-array-increasing
✅✔🔥 EASY PYTHON3 SOLUTION 🔥✅✔98%faster
rajukommula
0
33
minimum operations to make the array increasing
1,827
0.782
Easy
26,068
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/2489257/Very-simple-python-solution
class Solution: def minOperations(self, nums: List[int]) -> int: count = 0 for i in range(1,len(nums)): if nums[i-1] > nums[i]: count += nums[i-1] - nums[i] + 1 nums[i] = nums[i-1] + 1 elif nums[i-1] == nums[i]: count += 1 nums[i] = nums[i-1] + 1 else: continue return count
minimum-operations-to-make-the-array-increasing
Very simple python solution
aruj900
0
25
minimum operations to make the array increasing
1,827
0.782
Easy
26,069
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/2454847/Simple-Python-Solution
class Solution: def minOperations(self, nums: List[int]) -> int: ans=0 for i in range(1,len(nums)): if nums[i-1]>=nums[i]: ans+=abs(nums[i-1]-nums[i])+1 nums[i]=nums[i-1]+1 return ans
minimum-operations-to-make-the-array-increasing
Simple Python Solution
_anuraag_
0
17
minimum operations to make the array increasing
1,827
0.782
Easy
26,070
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/2421719/Python-easy-solution-81.14-Faster
class Solution: def minOperations(self, nums: List[int]) -> int: count = 0 for num in range(len(nums) - 1): if nums[num] >= nums[num + 1]: diff = abs(nums[num] - nums[num + 1]) count += diff + 1 nums[num + 1] = nums[num + 1] + diff + 1 return count
minimum-operations-to-make-the-array-increasing
Python easy solution 81.14% Faster
samanehghafouri
0
20
minimum operations to make the array increasing
1,827
0.782
Easy
26,071
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/2367124/Minimum-Operations-to-Make-the-Array-Increasing
class Solution: def minOperations(self, nums: List[int]) -> int: count = 0 for i in range(1,len(nums)): if nums[i]<=nums[i-1]: x = nums[i-1]-nums[i]+1 count+=x nums[i]+=x return count
minimum-operations-to-make-the-array-increasing
Minimum Operations to Make the Array Increasing
dhananjayaduttmishra
0
15
minimum operations to make the array increasing
1,827
0.782
Easy
26,072
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/2298987/Minimum-Operations-to-Make-the-Array-Increasing-With-Python3
class Solution: def minOperations(self, nums: List[int]) -> int: total = 0 for i in range(len(nums)-1): # 0 1 2 3 if nums[i] >= nums[i+1]: total += nums[i] - nums[i+1] + 1 nums[i+1] = nums[i] + 1 return total
minimum-operations-to-make-the-array-increasing
Minimum Operations to Make the Array Increasing With Python3
ibrahimbayburtlu5
0
19
minimum operations to make the array increasing
1,827
0.782
Easy
26,073
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/2055667/Python3-Easy-Solution
class Solution: def minOperations(self, nums: List[int]) -> int: if len(nums) == 1: return 0 output = 0 for i in range(1, len(nums)): if nums[i] <= nums[i-1]: output += nums[i-1] - nums[i] + 1 nums[i] = nums[i-1]+1 return output
minimum-operations-to-make-the-array-increasing
Python3 Easy Solution
PythonerAlex
0
56
minimum operations to make the array increasing
1,827
0.782
Easy
26,074
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1989991/Simple-Python-Solution
class Solution: def minOperations(self, nums: List[int]) -> int: if len(nums)==0 or len(nums)==1: return 0 ans=0 prev=nums[0] for i in range(1, len(nums)): if nums[i]>prev: prev=nums[i] else: ans+=prev+1 - nums[i] prev=prev+1 return ans
minimum-operations-to-make-the-array-increasing
Simple Python Solution
Siddharth_singh
0
42
minimum operations to make the array increasing
1,827
0.782
Easy
26,075
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1966066/Python-3-Solution-O(n)-Time-Complexity-Greedy-Alforithm-Explained-with-comments
class Solution: def minOperations(self, nums: List[int]) -> int: operations = 0 for i in range(len(nums) - 1): if nums[i + 1] <= nums[i]: # Checking if the next element is smaller or equal diff = nums[i] - nums[i + 1] # If the above codition is true, I take the difference between current and the next element. nums[i + 1] += (diff + 1) """Then I add difference incremented by one to the next element and also to the operations variable.""" operations += (diff + 1) return operations
minimum-operations-to-make-the-array-increasing
Python 3 Solution, O(n) Time Complexity, Greedy Alforithm, Explained with comments
AprDev2011
0
32
minimum operations to make the array increasing
1,827
0.782
Easy
26,076
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1884208/Python-solution-faster-than-85
class Solution: def minOperations(self, nums: List[int]) -> int: op_count = 0 for i in range(0, len(nums)-1): if nums[i+1] <= nums[i]: op_count += (nums[i] + 1) - nums[i+1] nums[i+1] = nums[i] + 1 return op_count
minimum-operations-to-make-the-array-increasing
Python solution faster than 85%
alishak1999
0
67
minimum operations to make the array increasing
1,827
0.782
Easy
26,077
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1859188/Python-dollarolution
class Solution: def minOperations(self, nums: List[int]) -> int: count = 0 for i in range(1,len(nums)): if nums[i] < nums[i-1]+1: x = nums[i-1] - nums[i] + 1 count += x nums[i] += x return count
minimum-operations-to-make-the-array-increasing
Python $olution
AakRay
0
33
minimum operations to make the array increasing
1,827
0.782
Easy
26,078
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1821440/5-Lines-Python-Solution-oror-70-Faster-oror-Memory-less-than-97
class Solution: def minOperations(self, nums: List[int]) -> int: sm=sum(nums) if len(nums)==1: return 0 for i in range(len(nums)-1): if nums[i+1]<=nums[i]: nums[i+1]=nums[i]+1 return sum(nums)-sm
minimum-operations-to-make-the-array-increasing
5-Lines Python Solution || 70% Faster || Memory less than 97%
Taha-C
0
40
minimum operations to make the array increasing
1,827
0.782
Easy
26,079
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1808555/Easy-and-Simple-Python-solution
class Solution: def minOperations(self, nums: List[int]) -> int: if len(nums)==1: return 0 c=0 for i in range(1,len(nums)): if nums[i]<=nums[i-1]: k=nums[i] #store the original value of nums[i] nums[i]=nums[i-1]+1 # print(nums) c+=nums[i]-k #new value - original value return c
minimum-operations-to-make-the-array-increasing
Easy and Simple Python solution
adityabaner
0
32
minimum operations to make the array increasing
1,827
0.782
Easy
26,080
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1799735/Python-O(n)-Solution
class Solution: def minOperations(self, nums: List[int]) -> int: if len(nums) == 1: return 0 result = 0 for i in range(len(nums) - 1): if nums[i] < nums[i + 1]: pass else: numOps = nums[i] - nums[i + 1] + 1 result += numOps nums[i + 1] += numOps return result
minimum-operations-to-make-the-array-increasing
Python O(n) Solution
White_Frost1984
0
25
minimum operations to make the array increasing
1,827
0.782
Easy
26,081
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1733814/Easy-Python-Solution-with-memory-usage-less-than-100-other-submissions.
class Solution: def minOperations(self, nums: List[int]) -> int: operations = 0 for i in range(len(nums)-1): if nums[i] == nums[i+1]: nums[i+1] += 1 operations += 1 elif nums[i] > nums[i+1]: difference = nums[i] - nums[i+1] + 1 nums[i+1] += difference operations += difference else: continue return operations
minimum-operations-to-make-the-array-increasing
Easy Python Solution with memory usage less than 100% other submissions.
babuchakjethiya
0
71
minimum operations to make the array increasing
1,827
0.782
Easy
26,082
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1682966/O(N)-python
class Solution: def minOperations(self, nums: List[int]) -> int: if len(nums) == 1: return 0 prev = nums[0] increment = 0 for i in range(1, len(nums)): if nums[i] - prev <= 0: increment = increment + prev - nums[i] + 1 prev = prev + 1 else: prev = nums[i] return increment
minimum-operations-to-make-the-array-increasing
O(N) python
snagsbybalin
0
42
minimum operations to make the array increasing
1,827
0.782
Easy
26,083
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1642959/99.9-Simple-Python-Solution
class Solution: def minOperations(self, nums: List[int]) -> int: result, level = 0, 0 for num in nums: if num <= level: level += 1 result += level - num else: level = num return result
minimum-operations-to-make-the-array-increasing
99.9% Simple Python Solution
migash
0
115
minimum operations to make the array increasing
1,827
0.782
Easy
26,084
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1580905/python-soln-based-on-hint-provided-by-leetcode
class Solution: def minOperations(self, nums: List[int]) -> int: count = 0 copy = nums[:] for idx in range(1,len(nums)): nums[idx] = max(nums[idx - 1] + 1, nums[idx]) count += abs(nums[idx] - copy[idx]) return count
minimum-operations-to-make-the-array-increasing
python soln based on hint provided by leetcode
anandanshul001
0
48
minimum operations to make the array increasing
1,827
0.782
Easy
26,085
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1554346/Python3-Simple-Solution-but-not-fast
class Solution: def minOperations(self, nums: List[int]) -> int: sum_nums = sum(nums) for idx in range(1, len(nums)): nums[idx] = max(nums[idx], nums[idx-1] + 1) return sum(nums) - sum_nums
minimum-operations-to-make-the-array-increasing
[Python3] Simple Solution but not fast
JingMing_Chen
0
48
minimum operations to make the array increasing
1,827
0.782
Easy
26,086
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1306299/Python3-Simple-code-faster-than-89-submissions
class Solution: def minOperations(self, nums: list) -> int: operations = 0 prev = None for num in nums: if not prev: prev = num continue if prev >= num: diff = prev - num new_diff = diff+1 operations+=new_diff prev = new_diff + num else: prev = num return operations
minimum-operations-to-make-the-array-increasing
[Python3] Simple code, faster than 89% submissions
GauravKK08
0
53
minimum operations to make the array increasing
1,827
0.782
Easy
26,087
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1300790/Easy-Python-Solution(95.52)
class Solution: def minOperations(self, nums: List[int]) -> int: c=0 s=0 for i in range(1,len(nums)): if(nums[i-1]>=nums[i]): c=nums[i-1]-nums[i]+1 s+=c nums[i]+=c return s
minimum-operations-to-make-the-array-increasing
Easy Python Solution(95.52%)
Sneh17029
0
147
minimum operations to make the array increasing
1,827
0.782
Easy
26,088
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1274508/Easy-Python-Solution
class Solution: def minOperations(self, nums: List[int]) -> int: n=len(nums) if n==1: return 0 c=0 for i in range(n-1): if nums[i]>=nums[i+1]: c=c+nums[i]-nums[i+1]+1 nums[i+1]=nums[i]+1 return c
minimum-operations-to-make-the-array-increasing
Easy Python Solution
sakshigoel123
0
63
minimum operations to make the array increasing
1,827
0.782
Easy
26,089
https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/discuss/1170201/C%2B%2B-and-Python
class Solution: def minOperations(self, nums: List[int]) -> int: minimumOperations = 0 for i in range(1, len(nums)): # Note that for loop starts with index 1 (we skip the index 0) if nums[i] <= nums[i-1]: oldNumber = nums[i] nums[i] = nums[i-1] + 1 # update the current number to new number in the input list newNumber = nums[i] minimumOperations += (newNumber - oldNumber) return minimumOperations
minimum-operations-to-make-the-array-increasing
C++ and Python
vetikke
0
52
minimum operations to make the array increasing
1,827
0.782
Easy
26,090
https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/discuss/1163133/Python-One-liner
class Solution: def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]: return [sum(math.sqrt((x0-x1)**2 + (y0-y1)**2) <= r for x1, y1 in points) for x0, y0, r in queries]
queries-on-number-of-points-inside-a-circle
Python One-liner
Black_Pegasus
21
4,000
queries on number of points inside a circle
1,828
0.864
Medium
26,091
https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/discuss/1167044/Python-3-512-ms-faster-than-100-using-complex-numbers
class Solution: def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]: points = list(map(complex, *zip(*points))) queries = ((complex(x, y), r) for x, y, r in queries) return [sum(abs(p - q) <= r for p in points) for q, r in queries]
queries-on-number-of-points-inside-a-circle
[Python 3] 512 ms, faster than 100% using complex numbers
kingjonathan310
15
1,400
queries on number of points inside a circle
1,828
0.864
Medium
26,092
https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/discuss/2484361/Simple-python-code-with-explanation
class Solution: def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]: #create a empty list k k = [] #create a variable and initialise it to 0 count = 0 #iterate over the elements in the queries for i in queries: #in each sublist 1st element is x co-ordinate x1 = i[0] #in each sublist 2nd element is y co-ordinate y1 = i[1] #in each sublist 3rd element is radius r = i[2] #iterate ovet the lists in points for j in points: #in each sublist 1st element is x co-ordinate x2 = j[0] #in each sublist 2nd element is y co-ordinate y2 = j[1] #if the distance between those two co-ordinates is less than or equal to radius if (((x2 - x1)**2 + (y2 - y1)**2)**0.5) <= r : #then the point lies inside the cirle or on the circle #then count how many points are inside the circle or on the circle by incrementing count variable count = count + 1 #after finishing one co-ordinate in points then add the count value in list k k.append(count) #then rearrange the value of count to 0 before starting next circle count = 0 #after finishing all circles return the list k return k
queries-on-number-of-points-inside-a-circle
Simple python code with explanation
thomanani
3
138
queries on number of points inside a circle
1,828
0.864
Medium
26,093
https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/discuss/1493474/Python-Solution-Naive-and-Efficient-methods
class Solution: def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]: count = [[] for each in range(len(queries))] for i in range(len(queries)): for j in range(len(points)): x = queries[i][0] y = queries[i][1] r = queries[i][2] c = pow(x-points[j][0],2) + pow(y-points[j][1],2) - pow(r,2) if c <= 0: count[i].append(1) for i in range(len(count)): count[i] = sum(count[i]) return count
queries-on-number-of-points-inside-a-circle
Python Solution - Naive and Efficient methods
deleted_user
3
265
queries on number of points inside a circle
1,828
0.864
Medium
26,094
https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/discuss/1493474/Python-Solution-Naive-and-Efficient-methods
class Solution: def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]: res = [] for x,y,r in queries: count = 0 for a,b in points: if (x-a)*(x-a) + (y-b)*(y-b) <= r*r: count += 1 res.append(count) return res
queries-on-number-of-points-inside-a-circle
Python Solution - Naive and Efficient methods
deleted_user
3
265
queries on number of points inside a circle
1,828
0.864
Medium
26,095
https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/discuss/1163186/Easy-to-understand-Python-solution
class Solution: def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]: answer = [0 for i in range(len(queries))] for i, query in enumerate(queries): for point in points: if (((point[0] - query[0])*(point[0] - query[0])) + ((point[1] - query[1])*(point[1] - query[1]))) <= query[2]*query[2]: answer[i] += 1 return answer
queries-on-number-of-points-inside-a-circle
Easy to understand Python solution
donchenkonadia
2
205
queries on number of points inside a circle
1,828
0.864
Medium
26,096
https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/discuss/1618006/python3-faster-than-97
class Solution: def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]: points = [complex(x,y) for x,y in points] queries = [(complex(x, y), r) for x, y, r in queries] return [sum(abs(p - q) <= r for p in points) for q, r in queries]
queries-on-number-of-points-inside-a-circle
python3 faster than 97%
fatmakahveci
1
203
queries on number of points inside a circle
1,828
0.864
Medium
26,097
https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/discuss/1606303/Python-Easy-Solution-or-Math-Formula
class Solution: def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]: circle = [] for x2, y2, radius in queries: count = 0 for x1, y1 in points: dis = ((x2-x1)**2+(y2-y1)**2)**0.5 # Use the Distance Formula... if dis <= radius: count += 1 circle.append(count) return circle
queries-on-number-of-points-inside-a-circle
Python Easy Solution | Math Formula ✔
leet_satyam
1
127
queries on number of points inside a circle
1,828
0.864
Medium
26,098
https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/discuss/1383495/Python-with-comments
class Solution: def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]: ans = [] for circle in queries: radius = circle[-1] count = 0 # Calculate distance of each point to circle's origin # If distance <= radius, point is in circle for p in points: distance = self.calcDistance(p[0], p[1], circle[0], circle[1]) if distance <= radius: count += 1 ans.append(count) return ans def calcDistance(self, pX, pY, cX, cY): # Euclidean distance formula root = pow(pX-cX, 2) + pow(pY-cY, 2) return math.sqrt(root)
queries-on-number-of-points-inside-a-circle
Python with comments
SmittyWerbenjagermanjensen
1
151
queries on number of points inside a circle
1,828
0.864
Medium
26,099