post_href
stringlengths 57
213
| python_solutions
stringlengths 71
22.3k
| slug
stringlengths 3
77
| post_title
stringlengths 1
100
| user
stringlengths 3
29
| upvotes
int64 -20
1.2k
| views
int64 0
60.9k
| problem_title
stringlengths 3
77
| number
int64 1
2.48k
| acceptance
float64 0.14
0.91
| difficulty
stringclasses 3
values | __index_level_0__
int64 0
34k
|
|---|---|---|---|---|---|---|---|---|---|---|---|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/1906996/Python3-with-helper
|
class Solution:
def helper(self, nums: List[int]) -> List[int]:
dp = []
i = 0
while i < len(nums) - 1:
dp.append((nums[i] + nums[i + 1]) % 10)
i += 1
return dp
def triangularSum(self, nums: List[int]) -> int:
while len(nums) > 1:
nums = self.helper(nums)
return nums[0]
|
find-triangular-sum-of-an-array
|
Python3 with helper
|
ElizaZoldyck
| 5
| 375
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,800
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2073909/Python-Solution-Easy-Solution-W-No-Extra-Memory
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
for i in range(len(nums) - 1, -1, -1):
for j in range(i):
nums[j] = (nums[j] + nums[j + 1]) % 10
return nums[0]
|
find-triangular-sum-of-an-array
|
Python Solution - Easy Solution W/ No Extra Memory
|
alexerling
| 3
| 282
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,801
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2394567/python-easily-understandable-recursion
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]
newNums = []
for i in range(len(nums)-1):
k = (nums[i] + nums[i+1]) % 10
newNums.append(k)
return self.triangularSum(newNums)
|
find-triangular-sum-of-an-array
|
python easily understandable, recursion
|
user7981Bv
| 1
| 113
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,802
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2328239/Python3-DP-to-Inplace-O(n2)-to-O(1)-Runtime%3A-2255ms-84.49-oror-Memory%3A-14mb-80.16
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
return self.solOne(nums)
return self.solTwo(nums)
# Runtime: 2255ms 84.49% || Memory: 14mb 80.16%
# O(n^2) || O(1)
def solOne(self, nums):
if len(nums) == 1:
return nums[0]
for i in reversed(range(len(nums))):
for x in range(i):
nums[x] = (nums[x] + nums[x + 1]) % 10
return nums[0]
# Runtime: 2809ms 77.12% || Memory: 18.9mb 12.99%
def solTwo(self, nums):
if len(nums) == 1:
return nums[0]
return helper(len(nums) - 1, nums)
def helper(size, array):
dp = [0] * (size + 1)
if size == 0:
return array[0]
for i in range(1, size+1):
dp[i-1] = (array[i-1] + array[i]) % 10
return helper(size - 1, dp)
|
find-triangular-sum-of-an-array
|
Python3 DP to Inplace O(n^2) to O(1) Runtime: 2255ms 84.49% || Memory: 14mb 80.16%
|
arshergon
| 1
| 167
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,803
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2842240/Simple-python-solution
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
while len(nums)>1:
for i in range(len(nums)-1):
nums[i] = ((nums[i]+nums[i+1])%10)
nums.pop()
return nums[0]
|
find-triangular-sum-of-an-array
|
Simple python solution
|
Rajeev_varma008
| 0
| 2
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,804
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2824424/Python-or-Easy-Solution-or-Loops
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
while len(nums) != 1:
ans = []
# print(nums)
for i in range(len(nums)-1):
ans.append((nums[i]+nums[i+1])%10)
nums = ans
return nums[0]
|
find-triangular-sum-of-an-array
|
Python | Easy Solution | Loops
|
atharva77
| 0
| 1
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,805
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2811462/Python-or-Easy-approach-or-O(n*n)
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
for j in range(len(nums)-1):
for i in range(len(nums)-1):
nums[i] = nums[i] + nums[i+1]
nums[i] = nums[i]%10
return nums[0]
|
find-triangular-sum-of-an-array
|
Python | Easy approach | O(n*n)
|
bhuvneshwar906
| 0
| 2
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,806
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2811218/Simple-Logic-in-Python
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
l = []
if len(nums) == 1:
return nums[0]
else:
for i in range(len(nums) - 1):
l.append((nums[i] + nums[i + 1]) % 10)
if len(l) == 1:
return l[0]
else:
while True:
L = []
for j in range(len(l) - 1):
L.append((l[j] + l[j + 1]) % 10)
l = L
if len(L) <= 1:
return L[0]
|
find-triangular-sum-of-an-array
|
Simple Logic in Python
|
Himakar_C
| 0
| 2
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,807
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2804904/Python-Solution-EASY-TO-UNDERSTAND
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
n=len(nums)
while n!=1:
newnums=[]
for i in range(n-1):
newnums.append((nums[i] + nums[i+1]) % 10)
nums=newnums
n=len(nums)
return sum(nums)
|
find-triangular-sum-of-an-array
|
Python Solution - EASY TO UNDERSTAND
|
T1n1_B0x1
| 0
| 2
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,808
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2776751/Python-easy-to-understand-solution
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
while (len(nums)>1):
for i in range((len(nums))-1):
nums[i]=(nums[i]+nums[i+1])%10
nums.pop(len(nums)-1)
return nums[0]
|
find-triangular-sum-of-an-array
|
Python easy to understand solution
|
divyanshuan
| 0
| 9
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,809
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2776477/python3-slow-solution
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
for j in range(len(nums), 0, -1):
for i in range(1, j):
nums[i - 1] += nums[i]
nums[i - 1] %= 10
return nums[0]
|
find-triangular-sum-of-an-array
|
python3 slow solution
|
rupamkarmakarcr7
| 0
| 3
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,810
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2764402/easy-python-solutionoror-easy-to-understand
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
while len(nums)>1:
for i in reversed(range(1,len(nums))):
nums[i] = (nums[i]+nums[i-1])%10
nums = nums[1:]
return nums[0]
|
find-triangular-sum-of-an-array
|
✅✅easy python solution|| easy to understand
|
chessman_1
| 0
| 6
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,811
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2710651/Easy-Python-Solution
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
l=[]
while len(nums)>1:
for i in range(len(nums)-1):
l.append((nums[i]+nums[i+1])%10)
nums=l
l=[]
return nums[0]
|
find-triangular-sum-of-an-array
|
Easy Python Solution
|
abhint1
| 0
| 11
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,812
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2709346/Brute-force-approach
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
n = len(nums)
while n > 1:
ls = []
for i in range(1,n):
ls.append((nums[i] + nums[i-1])%10)
n-=1
nums=ls
return nums[0]
|
find-triangular-sum-of-an-array
|
Brute force approach
|
anshsharma17
| 0
| 2
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,813
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2683796/Find-Triangular-Sum-of-an-Array
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
n = len(nums)
while n > 1:
for i in range(len(nums) - 1):
nums[i] = (nums[i] + nums[i + 1]) % 10
nums.pop()
n -= 1
return nums[-1]
|
find-triangular-sum-of-an-array
|
Find Triangular Sum of an Array
|
Muggles102
| 0
| 4
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,814
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2681002/easy-python
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
m = len(nums) - 1
mCk = 1
result = 0
for k, num in enumerate(nums):
result = (result + mCk * num) % 10
# print("r",result)
mCk *= m - k
# print("mc",mCk)
mCk //= k + 1
# print("mc",mCk)
return (result)
|
find-triangular-sum-of-an-array
|
easy python
|
vivekraj185
| 0
| 28
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,815
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2641439/Python-solution-with-one-question!-HELP!-Time%3AO(N)-Space%3AO(1)
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
m = len(nums)-1
res = 0
m_fac = 1
i = 0
while i<(m+1)//2:
res += m_fac*(nums[i]+nums[m-i])
m_fac*=(m-i)
m_fac=m_fac//(i+1)
# m_fac=m_fac/(i+1)
i += 1
if m%2==0:
res += m_fac*(nums[i])
return int(res%10)
|
find-triangular-sum-of-an-array
|
Python solution with one question! HELP! Time:O(N) Space:O(1)
|
CgriefTesla
| 0
| 12
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,816
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2625747/Simple-Python-Solution-with-comments
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
n=len(nums)
# base conditions
if n==0:
return 0
if n<=2:
return sum(nums)%10
# we will iterate over nums list
# keep updating the sum in the same list
# pop from the end (to reduce the list just like a triangle)
while len(nums)>1:
temp=nums[-1]
for j in range(len(nums)-2, -1, -1):
a=nums[j]
nums[j]=(nums[j]+temp)%10 #element constraint: 0<=nums[i]<=9
temp=a
nums.pop()
# print(nums)
return nums[0]
|
find-triangular-sum-of-an-array
|
Simple Python Solution with comments
|
Siddharth_singh
| 0
| 35
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,817
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2614049/Python3-Brute-Force
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
while len(nums) > 1:
for index in range(len(nums) -1):
nums[index] = (nums[index] + nums[index + 1]) % 10
nums.pop()
return nums[0]
|
find-triangular-sum-of-an-array
|
[Python3] Brute Force
|
peatear-anthony
| 0
| 30
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,818
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2595990/Python-easy-solution
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
tmp = []
while(len(nums)>1):
tmp = [0]*(len(nums)-1)
for i in range(len(tmp)):
tmp[i] = nums[i]+nums[i+1]
if(tmp[i]>=10):
tmp[i] -= 10
nums = tmp.copy()
return nums[0]
|
find-triangular-sum-of-an-array
|
Python easy solution
|
Jack_Chang
| 0
| 67
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,819
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2569085/Python-both-recursive-and-iterative-straight-forward-solutions
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]
ans = []
for i in range(len(nums) - 1):
ans.append((nums[i] + nums[i + 1]) % 10)
nums = ans
return self.triangularSum(nums)
|
find-triangular-sum-of-an-array
|
Python both recursive and iterative straight-forward solutions
|
Mark_computer
| 0
| 51
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,820
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2569085/Python-both-recursive-and-iterative-straight-forward-solutions
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
while len(nums) > 1:
ans = []
for i in range(len(nums) - 1):
ans.append((nums[i] + nums[i + 1]) % 10)
nums = ans
return nums[0]
|
find-triangular-sum-of-an-array
|
Python both recursive and iterative straight-forward solutions
|
Mark_computer
| 0
| 51
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,821
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2543984/Short-and-sweet-python-inline-solution
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
right_border = len(nums)
while right_border > 1:
for i in range(right_border - 1):
nums[i] = (nums[i] + nums[i+1]) % 10
right_border -= 1
return nums[0]
|
find-triangular-sum-of-an-array
|
Short and sweet python inline solution
|
jestpunk
| 0
| 27
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,822
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2506613/Python-Binomial-Coefficients-with-Math-Explained-O(n)-Time
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
coef = 1
sum = nums[0]
n = len(nums) - 1
for k in range(1, len(nums)):
coef *= (n - k + 1)
coef //= k # integer division to avoid floating point
sum += coef * nums[k]
sum %= 10
return sum
|
find-triangular-sum-of-an-array
|
Python Binomial Coefficients with Math Explained - O(n) Time
|
npavlosky
| 0
| 95
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,823
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2498613/Python-simple-and-optmized
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
n = len(nums) # to find the lenght of list nums
if n == 1: # if list contains only one element
return nums[-1] #then return that element
for i in range(n-1): # n-1 steps to solve the problem
for j in range(n-1-i): # after every step you want 1 less result, traverse only till n-1-i
nums[j] = (nums[j] + nums[j+1])%10
return nums[0]
|
find-triangular-sum-of-an-array
|
Python simple and optmized
|
prime_utkarsh
| 0
| 82
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,824
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2489915/Simple-python-code-with-explanation
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
n = len(nums) # to find the lenght of list nums
if n == 1: # if list contains only one element
return nums[-1] #then return that element
for i in range(n-1): #for every element iterate over sub list
for i in range(n-1): #for every sub list
nums[i] = (nums[i] + nums[i+1])%10 #do sum of curr indexval and next index val and modulo divide by 10
return nums[0] #return final element in the nums
|
find-triangular-sum-of-an-array
|
Simple python code with explanation
|
thomanani
| 0
| 56
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,825
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2244074/Python3-Pascal's-Triangle
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
comb = [1]
for i in range(len(nums)-1):
comb.append(comb[-1]*(len(nums)-1-i)//(i+1))
return sum(x*y for x, y in zip(nums, comb)) % 10
|
find-triangular-sum-of-an-array
|
[Python3] Pascal's Triangle
|
ye15
| 0
| 53
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,826
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2181963/Python-simple-recursive-solution
|
class Solution:
def triangularSum(self, n: List[int]) -> int:
def ts(nums: List[int]) -> int:
ln = len(nums)
if ln == 1:
return nums[0]
else:
arr = []
for i in range(1,ln):
arr.append((nums[i-1]+nums[i])%10)
return ts(arr)
return ts(n)
|
find-triangular-sum-of-an-array
|
Python simple recursive solution
|
StikS32
| 0
| 64
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,827
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2174611/Naive-Approach-or-Easy-to-understand
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
while len(nums) != 1:
temp_arr = []
for i in range(len(nums)-1):
add = (nums[i]+nums[i+1])%10
temp_arr.append(add)
nums = temp_arr
return nums[0]
|
find-triangular-sum-of-an-array
|
Naive Approach | Easy to understand
|
yash921
| 0
| 35
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,828
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/2162656/Easiest-Approach
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
length = len(nums)
for i in range(length-1,-1,-1):
for j in range(i):
nums[j] = (nums[j] + nums[j+1])%10
return nums[0]
|
find-triangular-sum-of-an-array
|
Easiest Approach
|
Vaibhav7860
| 0
| 41
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,829
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/1942289/5-Lines-Python-Solution-oror-92-Faster-oror-Memory-less-than-97
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
while len(nums)>1:
newNums=[]
for i in range(len(nums)-1): newNums.append((nums[i]+nums[i+1])%10)
nums=newNums
return nums[0]
|
find-triangular-sum-of-an-array
|
5-Lines Python Solution || 92% Faster || Memory less than 97%
|
Taha-C
| 0
| 81
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,830
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/1942289/5-Lines-Python-Solution-oror-92-Faster-oror-Memory-less-than-97
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
n=len(nums)
while n>1:
for i in range(n-1): nums[i]=(nums[i]+nums[i+1])%10
n-=1
return nums[0]
|
find-triangular-sum-of-an-array
|
5-Lines Python Solution || 92% Faster || Memory less than 97%
|
Taha-C
| 0
| 81
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,831
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/1926382/6-line-Python-code-or-O(n2)-time-O(1)-space-or-Easy-to-Understand
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
n = len(nums)
while(n > 1):
for i in range(n-1):
nums[i] = (nums[i]+nums[i+1])%10
n -= 1
return nums[0]
|
find-triangular-sum-of-an-array
|
6 line Python code | O(n^2) time, O(1) space | Easy to Understand
|
shiva1gandluri
| 0
| 59
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,832
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/1926061/Python3-simple-solution
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
while len(nums) > 1:
ans = []
for i in range(len(nums)-1):
ans.append((nums[i] + nums[i+1])%10)
nums = ans
return nums[0]
|
find-triangular-sum-of-an-array
|
Python3 simple solution
|
EklavyaJoshi
| 0
| 29
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,833
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/1915303/python-3-easy-and-fast-solutionoror-O(n2)-time-O(1)-space
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
while len(nums)>1:
ls=[]
for i in range(len(nums)-1):
ls.append((nums[i]+nums[i+1])%10)
nums=ls
return nums[0]
|
find-triangular-sum-of-an-array
|
python 3 easy and fast solution|| O(n^2) time , O(1) space
|
nileshporwal
| 0
| 59
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,834
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/1912913/Two-loops-one-array-100-speed
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
for n in range(len(nums) - 1, 0, -1):
for i in range(n):
nums[i] = (nums[i] + nums[i + 1]) % 10
return nums[0]
|
find-triangular-sum-of-an-array
|
Two loops, one array, 100% speed
|
EvgenySH
| 0
| 43
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,835
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/1912371/Python-easy-solution-with-memory-less-than-90
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
n = len(nums)
for i in range(n - 1, 0, -1):
newnums = [0] * i
for j in range(0, i):
newnums[j] = (nums[j] + nums[j+1]) % 10
nums = newnums
return nums[0]
|
find-triangular-sum-of-an-array
|
Python easy solution with memory less than 90%
|
alishak1999
| 0
| 27
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,836
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/1912339/Simple-and-Easy-To-Understand-Python-Solution
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
from collections import deque
dq = deque(nums)
while len(dq) > 1:
cur = len(dq)
while cur > 1:
a = dq.popleft()
b = dq.popleft()
c = (a + b) % 10
dq.appendleft(b)
dq.append(c)
cur -= 1
dq.popleft()
return dq.popleft()
|
find-triangular-sum-of-an-array
|
Simple and Easy To Understand Python Solution
|
danielkua
| 0
| 12
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,837
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/1911677/python-3-oror-simple-brute-force
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
while len(nums) > 1:
nums = [(nums[i] + nums[i+1]) % 10 for i in range(len(nums) - 1)]
return nums[0]
|
find-triangular-sum-of-an-array
|
python 3 || simple brute force
|
dereky4
| 0
| 21
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,838
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/1907687/Python-3-or-In-Place-Compression-or-Explanation
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
n = len(nums)
while n > 1:
prev = nums[n-1]
for right in range(n-1, 0, -1):
prev, nums[right-1] = nums[right-1], (prev + nums[right-1]) % 10
n -= 1
return nums[0]
|
find-triangular-sum-of-an-array
|
Python 3 | In-Place Compression | Explanation
|
idontknoooo
| 0
| 52
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,839
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/1907529/Simple-python-with-comments
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
def doMath(nums):
pointer_one = 0
pointer_two = 1
new_nums = []
# while loop while the length of nums is > 1
while pointer_two < len(nums):
# take the neighbors and add them then
num_to_add = (nums[pointer_one] + nums[pointer_two]) % 10
new_nums.append(num_to_add)
pointer_one = pointer_one + 1
pointer_two = pointer_two + 1
if len(new_nums) > 1:
return doMath(new_nums)
else:
return new_nums
if len(nums) > 1:
answer = doMath(nums)
else:
return nums[0]
return answer[0]
|
find-triangular-sum-of-an-array
|
Simple python with comments
|
arizala13
| 0
| 41
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,840
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/1907170/Simple-Python-3-solution
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
while(len(nums)>1):
newarray=[]
for i in range(len(nums)-1):
newarray.append((nums[i]+nums[i+1])%10)
nums=newarray
return nums[0]
|
find-triangular-sum-of-an-array
|
Simple Python 3 solution
|
user8795ay
| 0
| 17
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,841
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/1907050/Python3-or-Simple-Approach
|
class Solution:
def triangularSum(self, nums: List[int]) -> int:
temp = []
while len(nums) > 1:
for i in range(len(nums) - 1):
temp.append((nums[i] + nums[i + 1])%10)
nums = temp
temp = []
return nums[0]
|
find-triangular-sum-of-an-array
|
Python3 | Simple Approach
|
goyaljatin9856
| 0
| 24
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,842
|
https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/1908063/Python3-elegant
|
class Solution:
def triangular_sum(self, nums: List[int]) -> int:
nums1 = nums
while len(nums1) > 1:
nums2 = []
for i in range(len(nums1) - 1):
nums2 += [(nums1[i] + nums1[i + 1]) % 10]
nums1 = nums2
return nums1[0]
|
find-triangular-sum-of-an-array
|
Python3 elegant
|
Tallicia
| -1
| 26
|
find triangular sum of an array
| 2,221
| 0.79
|
Medium
| 30,843
|
https://leetcode.com/problems/number-of-ways-to-select-buildings/discuss/1979756/python-3-oror-short-and-simple-oror-O(n)O(1)
|
class Solution:
def numberOfWays(self, s: str) -> int:
zeros = s.count('0')
ones = len(s) - zeros
zeroPrefix = onePrefix = res = 0
for c in s:
if c == '0':
res += onePrefix * (ones - onePrefix)
zeroPrefix += 1
else:
res += zeroPrefix * (zeros - zeroPrefix)
onePrefix += 1
return res
|
number-of-ways-to-select-buildings
|
python 3 || short and simple || O(n)/O(1)
|
dereky4
| 5
| 304
|
number of ways to select buildings
| 2,222
| 0.512
|
Medium
| 30,844
|
https://leetcode.com/problems/number-of-ways-to-select-buildings/discuss/1907871/Python-3-or-Prefix-and-Suffix-sum-Easy-to-understand-or-Explanation
|
class Solution:
def numberOfWays(self, s: str) -> int:
prefix = []
one = zero = 0
for c in s: # find number of 0 or 1 before index `i`
prefix.append([zero, one])
if c == '1':
one += 1
else:
zero += 1
suffix = []
one = zero = 0
for c in s[::-1]: # find number of 0 or 1 after index `i`
suffix.append([zero, one])
if c == '1':
one += 1
else:
zero += 1
suffix = suffix[::-1] # reverse since we trace from right to left
ans = 0
for i, c in enumerate(s): # for c=='1' number of combination is prefix[i][0] * suffix[i][0] ([0 before index `i`] * [0 after index `i`])
if c == '1':
ans += prefix[i][0] * suffix[i][0]
else:
ans += prefix[i][1] * suffix[i][1]
return ans
|
number-of-ways-to-select-buildings
|
Python 3 | Prefix & Suffix sum, Easy to understand | Explanation
|
idontknoooo
| 2
| 153
|
number of ways to select buildings
| 2,222
| 0.512
|
Medium
| 30,845
|
https://leetcode.com/problems/number-of-ways-to-select-buildings/discuss/2753063/5-line-Python-Solution-or-Easy-%2B-Fast
|
class Solution:
def numberOfWays(self, s: str) -> int:
x0,x1,x01,x10,ans = 0,0,0,0,0
for i in s:
if i=="1": x1+=1;x01+=x0;ans+=x10
else: x0+=1;x10+=x1;ans+=x01
return ans
|
number-of-ways-to-select-buildings
|
5 line Python Solution | Easy + Fast
|
RajatGanguly
| 1
| 63
|
number of ways to select buildings
| 2,222
| 0.512
|
Medium
| 30,846
|
https://leetcode.com/problems/number-of-ways-to-select-buildings/discuss/2781109/Python-Easy-Solution-without-DP
|
class Solution:
def numberOfWays(self, s: str) -> int:
prefix = {"0": 0, "1": 0}
suffix = {"0": 0, "1": 0}
for i in s:
suffix[i] += 1
res = 0
for i in s:
complement = "0" if i == "1" else "1"
suffix[i] -= 1
res += suffix[complement] * prefix[complement]
prefix[i] += 1
return res
|
number-of-ways-to-select-buildings
|
[Python] Easy Solution without DP
|
rubenciranni
| 0
| 5
|
number of ways to select buildings
| 2,222
| 0.512
|
Medium
| 30,847
|
https://leetcode.com/problems/number-of-ways-to-select-buildings/discuss/2248166/Python3-dp
|
class Solution:
def numberOfWays(self, s: str) -> int:
ans = n0 = n1 = n01 = n10 = 0
for ch in s:
if ch == '0':
ans += n01
n10 += n1
n0 += 1
else:
ans += n10
n01 += n0
n1 += 1
return ans
|
number-of-ways-to-select-buildings
|
[Python3] dp
|
ye15
| 0
| 71
|
number of ways to select buildings
| 2,222
| 0.512
|
Medium
| 30,848
|
https://leetcode.com/problems/number-of-ways-to-select-buildings/discuss/1916316/Linear-solution-95-speed
|
class Solution:
def numberOfWays(self, s: str) -> int:
len_s = len(s)
if len_s < 3:
return 0
total_zeros, total_ones = s.count("0"), s.count("1")
count = zeros = ones = 0
if s[0] == "0":
zeros = 1
else:
ones = 1
for i in range(1, len_s - 1):
if s[i] == "1":
count += zeros * (total_zeros - zeros)
ones += 1
else:
count += ones * (total_ones - ones)
zeros += 1
return count
|
number-of-ways-to-select-buildings
|
Linear solution, 95% speed
|
EvgenySH
| 0
| 67
|
number of ways to select buildings
| 2,222
| 0.512
|
Medium
| 30,849
|
https://leetcode.com/problems/number-of-ways-to-select-buildings/discuss/1911159/Python3-Simple-Solution
|
class Solution:
def numberOfWays(self, s: str) -> int:
zero,cur_zero,one,cur_one,res = 0,0,0,0,0
for i in s:
zero += i == '0'
one += i == '1'
for i in s:
if i == '0':
res += (cur_one*(one-cur_one))
cur_zero += 1
else:
res += (cur_zero*(zero-cur_zero))
cur_one += 1
return res
|
number-of-ways-to-select-buildings
|
[Python3] Simple Solution
|
abhijeetmallick29
| 0
| 18
|
number of ways to select buildings
| 2,222
| 0.512
|
Medium
| 30,850
|
https://leetcode.com/problems/number-of-ways-to-select-buildings/discuss/1908015/Python3
|
class Solution:
def numberOfWays(self, s: str) -> int:
def find_subsequence_count(S, T):
m = len(T)
n = len(S)
if m > n:
return 0
mat = [[0 for _ in range(n + 1)]
for __ in range(m + 1)]
for i in range(1, m + 1):
mat[i][0] = 0
for j in range(n + 1):
mat[0][j] = 1
for i in range(1, m + 1):
for j in range(1, n + 1):
if T[i - 1] != S[j - 1]:
mat[i][j] = mat[i][j - 1]
else:
mat[i][j] = (mat[i][j - 1] +
mat[i - 1][j - 1])
return mat[m][n]
x = find_subsequence_count(s, '101')
y = find_subsequence_count(s, '010')
return x + y
|
number-of-ways-to-select-buildings
|
Python3
|
Tallicia
| 0
| 17
|
number of ways to select buildings
| 2,222
| 0.512
|
Medium
| 30,851
|
https://leetcode.com/problems/sum-of-scores-of-built-strings/discuss/2256814/Python3-rolling-hash-and-z-algorithm
|
class Solution:
def sumScores(self, s: str) -> int:
mod = 119_218_851_371
hs = 0
vals = [0]
for i, ch in enumerate(s):
hs = (hs * 26 + ord(ch) - 97) % mod
vals.append(hs)
p26 = [1]
for _ in range(len(s)): p26.append(p26[-1] * 26 % mod)
ans = 0
for i in range(len(s)):
if s[0] == s[i]:
lo, hi = i, len(s)
while lo < hi:
mid = lo + hi + 1 >> 1
hs = (vals[mid] - vals[i]*p26[mid-i]) % mod
if hs == vals[mid-i]: lo = mid
else: hi = mid - 1
ans += lo - i
return ans
|
sum-of-scores-of-built-strings
|
[Python3] rolling hash & z-algorithm
|
ye15
| 1
| 82
|
sum of scores of built strings
| 2,223
| 0.37
|
Hard
| 30,852
|
https://leetcode.com/problems/sum-of-scores-of-built-strings/discuss/2256814/Python3-rolling-hash-and-z-algorithm
|
class Solution:
def sumScores(self, s: str) -> int:
ans = [0] * len(s)
lo = hi = ii = 0
for i in range(1, len(s)):
if i <= hi: ii = i - lo
if i + ans[ii] <= hi: ans[i] = ans[ii]
else:
lo, hi = i, max(hi, i)
while hi < len(s) and s[hi] == s[hi - lo]: hi += 1
ans[i] = hi - lo
hi -= 1
return sum(ans) + len(s)
|
sum-of-scores-of-built-strings
|
[Python3] rolling hash & z-algorithm
|
ye15
| 1
| 82
|
sum of scores of built strings
| 2,223
| 0.37
|
Hard
| 30,853
|
https://leetcode.com/problems/sum-of-scores-of-built-strings/discuss/2776139/Python-solution-or-KMP-prefix-or-Z-function
|
class Solution:
def sumScores(self, s: str) -> int:
n = len(s)
dp, p = [1]*n, [0]*n
for i in range(1, n):
j = p[i-1]
while(j and s[i]!=s[j]):
j = p[j-1]
if s[i]==s[j]:
dp[i] += dp[j]
j += 1
p[i] = j
return sum(dp)
|
sum-of-scores-of-built-strings
|
Python solution | KMP-prefix | Z-function
|
vincent_du
| 0
| 7
|
sum of scores of built strings
| 2,223
| 0.37
|
Hard
| 30,854
|
https://leetcode.com/problems/sum-of-scores-of-built-strings/discuss/2776139/Python-solution-or-KMP-prefix-or-Z-function
|
class Solution:
def sumScores(self, s: str) -> int:
n, l, r = len(s), 0, 0
z = [0] * n
for i in range(1, n):
if i <= r:
z[i] = min(r - i + 1, z[i - l])
while i + z[i] < n and s[z[i]] == s[i + z[i]]:
z[i] += 1
if i + z[i] - 1 > r:
l, r = i, i + z[i] - 1
return sum(z)+len(s)
|
sum-of-scores-of-built-strings
|
Python solution | KMP-prefix | Z-function
|
vincent_du
| 0
| 7
|
sum of scores of built strings
| 2,223
| 0.37
|
Hard
| 30,855
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/1908786/Easy-Python-Solution-or-Convert-time-to-minutes
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
current_time = 60 * int(current[0:2]) + int(current[3:5]) # Current time in minutes
target_time = 60 * int(correct[0:2]) + int(correct[3:5]) # Target time in minutes
diff = target_time - current_time # Difference b/w current and target times in minutes
count = 0 # Required number of operations
# Use GREEDY APPROACH to calculate number of operations
for i in [60, 15, 5, 1]:
count += diff // i # add number of operations needed with i to count
diff %= i # Diff becomes modulo of diff with i
return count
|
minimum-number-of-operations-to-convert-time
|
⭐Easy Python Solution | Convert time to minutes
|
anCoderr
| 30
| 1,200
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,856
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/1908924/Python-O(1)-Time
|
class Solution:
def convertTime(self, s: str, c: str) -> int:
dif=(int(c[:2])*60+int(c[3:]))-(int(s[:2])*60+int(s[3:]))
count=0
print(dif)
arr=[60,15,5,1]
for x in arr:
count+=dif//x
dif=dif%x
return count
|
minimum-number-of-operations-to-convert-time
|
Python O(1) Time
|
amlanbtp
| 2
| 52
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,857
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/1908893/Simple-Elegant-Solution-With-Explaination
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
def get_time(t):
hh, mm = t.split(':')
return int(hh) * 60 + int(mm)
current, correct = get_time(current), get_time(correct)
operations = 0
diff = correct - current
for mins in [60, 15, 5, 1]:
quotient, remainder = divmod(diff, mins)
operations += quotient
diff = remainder
return operations
|
minimum-number-of-operations-to-convert-time
|
Simple Elegant Solution With Explaination
|
mrunankmistry52
| 2
| 55
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,858
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/1961744/Python3-division-and-modulo
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
fn = lambda x, y: 60*x + y
m0 = fn(*map(int, current.split(':')))
m1 = fn(*map(int, correct.split(':')))
ans = 0
diff = m1 - m0
for x in 60, 15, 5, 1:
ans += diff // x
diff %= x
return ans
|
minimum-number-of-operations-to-convert-time
|
[Python3] division & modulo
|
ye15
| 1
| 47
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,859
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/1913661/Python-Simple-Solution-or-Easy-to-understand-or-Begginners
|
class Solution(object):
def convertTime(self, current, correct):
"""
:type current: str
:type correct: str
:rtype: int
"""
l = current.split(":")
m = correct.split(":")
c = 0
c+=int(m[0])-int(l[0])
x = int(m[1])-int(l[1])
if int(m[1])<int(l[1]):
c-=1
x = int(m[1])
x+=60-int(l[1])
while x>0:
if x>=15:
c+=x//15
x=x%15
elif x>=5:
c+=x//5
x = x%5
else:
c+=x
x=0
return c
|
minimum-number-of-operations-to-convert-time
|
Python Simple Solution | Easy to understand | Begginners
|
AkashHooda
| 1
| 38
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,860
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/2649434/Python3-Fun-With-Lambdas
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
x = lambda a, b: int(a) * 60 + int(b)
m = reduce(sub, map(lambda m: reduce(x, m.split(":")), [correct, current]))
u = [60, 15, 5, 1]
c = 0
for i in u:
c += m // i
m %= i
return c
|
minimum-number-of-operations-to-convert-time
|
Python3 Fun With Lambdas
|
godshiva
| 0
| 1
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,861
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/2395347/Python-greedy-faster-than-98
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
minutes1=int(current[:2])*60 + int(current[3:])
minutes2=int(correct[:2])*60 + int(correct[3:])
minutes=abs(minutes1-minutes2)
c60= minutes//60
minutes-=c60*60
c15= minutes//15
minutes-=c15*15
c5=minutes//5
minutes-=c5*5
return c60+c15+c5+minutes
|
minimum-number-of-operations-to-convert-time
|
Python greedy faster than 98%
|
sunakshi132
| 0
| 80
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,862
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/2256723/Python-Solution
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
diff = (int(correct[0:2]) * 60 + int(correct[-2:])) - (int(current[0:2]) * 60 + int(current[-2:]))
result = 0
for t in 60, 15, 5, 1:
q, diff = divmod(diff, t)
result += q
return result
|
minimum-number-of-operations-to-convert-time
|
Python Solution
|
hgalytoby
| 0
| 46
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,863
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/2143999/Python3-simple-solution
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
current = current.split(':')
correct = correct.split(':')
current = int(current[0])*60 + int(current[1])
correct = int(correct[0])*60 + int(correct[1])
if current > correct:
correct += 1440
elif current == correct:
return 0
x = correct - current
count = 0
count += x // 60
x = x % 60
count += x // 15
x = x % 15
count += x // 5
x = x % 5
count += x // 1
return count
|
minimum-number-of-operations-to-convert-time
|
Python3 simple solution
|
EklavyaJoshi
| 0
| 56
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,864
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/2120149/Python-oror-Easy-Approach
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
current_list = current.split(":")
current_minute = 60 * int(current_list[0]) + int(current_list[1])
correct_list = correct.split(":")
correct_minute = 60 * int(correct_list[0]) + int(correct_list[1])
time_oper = correct_minute - current_minute
n = 1
if time_oper < 5:
n = time_oper
if 5 < time_oper < 15:
num1 = int(time_oper / 5)
r1 = time_oper - num1 * 5
n = r1 + num1
if 15 < time_oper < 60:
num2= int(time_oper / 15)
r2 = time_oper - num2 * 15
num1 = int(r2 / 5)
r1 = r2 - num1 * 5
n = r1 + num1 + num2
if time_oper > 60:
num3 = int(time_oper / 60)
r3 = time_oper - num3 * 60
num2= int(r3 / 15)
r2 = r3 - num2 * 15
num1 = int(r2 / 5)
r1 = r2 - num1 * 5
n = r1 + num1 + num2 + num3
return n
|
minimum-number-of-operations-to-convert-time
|
✅Python || Easy Approach
|
chuhonghao01
| 0
| 50
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,865
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/2088623/Python-or-Easy-to-understand
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
current = current.split(':')
correct = correct.split(':')
for i in range(len(current)):
current[i] = int(current[i])
correct[i] = int(correct[i])
remaining = ((correct[0] * 60) + correct[1]) - ((current[0] * 60) + current[1])
res = 0
while remaining != 0:
if remaining >= 60:
res += 1
remaining -= 60
elif remaining >= 15:
res += 1
remaining -= 15
elif remaining >= 5:
res += 1
remaining -= 5
elif remaining >= 1:
res += 1
remaining -= 1
return res
|
minimum-number-of-operations-to-convert-time
|
Python | Easy to understand
|
shreeruparel
| 0
| 60
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,866
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/2078911/Python-90ms-solution
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
ans = 0
diff = (int(correct.split(':')[0])*60+int(correct.split(':')[1]))-(int(current.split(':')[0])*60+int(current.split(':')[1]))
while diff != 0:
if diff//60 != 0:
ans += diff//60
diff %= 60
continue
if diff//15 != 0:
ans += diff//15
diff %= 15
continue
if diff//5 != 0:
ans += diff//5
diff %= 5
continue
ans += diff
diff = 0
return ans
|
minimum-number-of-operations-to-convert-time
|
Python 90%ms solution
|
StikS32
| 0
| 46
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,867
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/1959317/Python-dollarolution-(99.81-Faster)
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
h, m = int(correct[0:2]) - int(current[0:2]), int(correct[3:5]) - int(current[3:5])
if m < 0:
m = m+60
h -= 1
h += m // 15
m = m % 15
h += m // 5
m = m % 5
h += m // 1
return h
|
minimum-number-of-operations-to-convert-time
|
Python $olution (99.81% Faster)
|
AakRay
| 0
| 69
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,868
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/1920733/Python3-or-Intuitive-approach
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
hr_curr, min_curr = current.split(":")
hr_corr, min_corr = correct.split(":")
hr_diff = int(hr_corr) - int(hr_curr)
min_diff = int(min_corr) - int(min_curr)
min_diff += hr_diff *60
ans = 0
while min_diff >= 60:
min_diff -= 60
ans += 1
while min_diff >= 15:
min_diff -= 15
ans += 1
while min_diff >= 5:
min_diff -= 5
ans += 1
while min_diff >= 1:
min_diff -= 1
ans += 1
return ans
|
minimum-number-of-operations-to-convert-time
|
Python3 | Intuitive approach
|
user0270as
| 0
| 28
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,869
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/1920229/Python-Case-by-case-solution
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
if current == correct:
return 0
count_h = 0
curr_h = int(current[:2])
target_h = int(correct[:2])
while curr_h < target_h:
curr_h += 1
count_h += 1
count_m = 0
curr_m = int(current[3:])
target_m = int(correct[3:])
if target_m == curr_m:
return count_h
if target_m < curr_m:
count_h -= 1
target_m += 60
while curr_m < target_m:
t = target_m - curr_m
if t >= 15:
curr_m += 15
elif t >= 5:
curr_m += 5
else:
curr_m += 1
count_m += 1
return count_h + count_m
|
minimum-number-of-operations-to-convert-time
|
[Python] Case by case solution
|
casshsu
| 0
| 24
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,870
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/1911649/python-3-oror-simple-solution
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
currH, currM = map(int, current.split(':'))
corrH, corrM = map(int, correct.split(':'))
diff = 60*(corrH - currH) + corrM - currM
res = 0
for m in (60, 15, 5, 1):
x, diff = divmod(diff, m)
res += x
return res
|
minimum-number-of-operations-to-convert-time
|
python 3 || simple solution
|
dereky4
| 0
| 28
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,871
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/1910709/Python-convert-to-minutes
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
current = 60 * int(current[:2]) + int(current[-2:])
correct = 60 * int(correct[:2]) + int(correct[-2:])
diff = correct - current
if diff < 0:
diff += 24 * 60
num_operations = 0
for step in [60, 15, 5, 1]:
num_operations += diff // step
diff = diff % step
return num_operations
|
minimum-number-of-operations-to-convert-time
|
Python, convert to minutes
|
blue_sky5
| 0
| 19
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,872
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/1909971/EZ-oror-O(1)-TC-oror-Beats-~83
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
curr_split=current.split(":")
corr_split=correct.split(":")
h, m = 0, 0
h=int(corr_split[0])-int(curr_split[0])
if int(curr_split[1])<=int(corr_split[1]):
m=int(corr_split[1])-int(curr_split[1])
else:
m=60-int(curr_split[1])+int(corr_split[1])
if h: h-=1 # If hour difference is atleast 1 then reduce it by 1 else leave it as it is
d=[15,5,1] # Always take the max number first to reduce minuteDifference to 0 in minimum moves
i, c = 0, 0
for i in range(3):
if m>=d[i]:
c+=m//d[i]
m%=d[i]
return c+h
|
minimum-number-of-operations-to-convert-time
|
EZ || O(1) TC || Beats ~83%
|
ashu_py22
| 0
| 15
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,873
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/1909232/Python-Greedy-Solution
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
def to_mins(t):
hh, mm = t.split(":")
return 60*int(hh)+int(mm)
s, t = to_mins(current), to_mins(correct)
d = t-s # difference
cnt = 0
for i in [60,15,5,1]:
x = d // i
d %= i
cnt += x
return cnt
|
minimum-number-of-operations-to-convert-time
|
[Python] Greedy Solution
|
nightybear
| 0
| 21
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,874
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/1909114/Convert-to-Minutes
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
cur_h, cur_m = current.split(":")
cor_h, cor_m = correct.split(":")
# Get the differences in terms of minute
diff_time = (int(cor_h) * 60 + int(cor_m)) - (int(cur_h) * 60 + int(cur_m))
res = 0
# decrease as much as possible
while diff_time:
if diff_time - 60 >= 0:
diff_time = diff_time - 60
elif diff_time -15 >= 0:
diff_time = diff_time - 15
elif diff_time - 5 >= 0:
diff_time = diff_time - 5
else:
diff_time = diff_time - 1
res += 1
return res
|
minimum-number-of-operations-to-convert-time
|
Convert to Minutes
|
M-Phuykong
| 0
| 10
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,875
|
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/1908897/Convert-To-Minutes
|
class Solution:
def convertTime(self, current: str, correct: str) -> int:
count = 0
hour_start = int(current.split(":")[0])
hour_end = int(correct.split(":")[0])
minute_start = int(current.split(":")[1])
minute_end = int(correct.split(":")[1])
total_start = hour_start * 60 + minute_start
total_end = hour_end * 60 + minute_end
print(total_start,total_end)
while total_start < total_end:
if abs(total_start - total_end) >= 60 :
total_start += 60
elif abs(total_start - total_end) >= 15:
total_start += 15
elif abs(total_start - total_end) >= 5 :
total_start += 5
elif abs(total_start - total_end) >= 1 :
total_start += 1
count += 1
return count
|
minimum-number-of-operations-to-convert-time
|
Convert To Minutes
|
abhijeetmallick29
| 0
| 5
|
minimum number of operations to convert time
| 2,224
| 0.655
|
Easy
| 30,876
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/1908760/Python-Solution-with-Hashmap
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
winners, losers, table = [], [], {}
for winner, loser in matches:
# map[key] = map.get(key, 0) + change . This format ensures that KEY NOT FOUND error is always prevented.
# map.get(key, 0) returns map[key] if key exists and 0 if it does not.
table[winner] = table.get(winner, 0) # Winner
table[loser] = table.get(loser, 0) + 1
for k, v in table.items(): # Player k with losses v
if v == 0:
winners.append(k) # If player k has no loss ie v == 0
if v == 1:
losers.append(k) # If player k has one loss ie v == 1
return [sorted(winners), sorted(losers)] # Problem asked to return sorted arrays.
|
find-players-with-zero-or-one-losses
|
⭐Python Solution with Hashmap
|
anCoderr
| 22
| 1,200
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,877
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/1929484/5-Lines-Python-Solution-oror-99-Faster-oror-Memory-less-than-85
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
winners=[x for (x,y) in matches]
losers=[y for (x,y) in matches]
perfect_winners=list(set(winners)-set(losers))
C=Counter(losers) ; one_lost=[loser for loser in C if C[loser]==1]
return [sorted(perfect_winners), sorted(one_lost)]
|
find-players-with-zero-or-one-losses
|
5-Lines Python Solution || 99% Faster || Memory less than 85%
|
Taha-C
| 1
| 85
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,878
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/2842661/Python-or-Easy-Solution-or-Hashmap-or-Loops-or-Sets
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
win = []
loss = []
hashmap = {}
occur = set()
for values in matches:
wins = values[0]
losss = values[1]
if wins not in hashmap:
hashmap[wins] = 1
if losss not in hashmap:
hashmap[losss] = 0
if wins in hashmap and hashmap[wins] == 0 and hashmap[wins] != -1 and wins in occur:
hashmap[wins] = 0
if losss in hashmap and hashmap[losss] == 0 and losss in occur:
hashmap[losss] = -1
if losss in hashmap and hashmap[losss] == 1 and hashmap[losss] != -1 and losss in occur:
hashmap[losss] = 0
if wins in hashmap and hashmap[wins] == 1 and hashmap[wins] != -1 and wins in occur:
hashmap[wins] = 1
occur.add(wins)
occur.add(losss)
for i in hashmap:
if hashmap[i] == 0:
loss.append(i)
if hashmap[i] == 1:
win.append(i)
win.sort()
loss.sort()
return [win, loss]
|
find-players-with-zero-or-one-losses
|
Python | Easy Solution | Hashmap | Loops | Sets
|
atharva77
| 0
| 4
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,879
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/2820914/O(n)-Python3-with-Dict
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
counts = defaultdict(int)
for winner, loser in matches:
counts[winner] += 0
counts[loser] += 1
return [sorted(key for (key, value) in counts.items() if value == 0), sorted(key for (key, value) in counts.items() if value == 1)]
|
find-players-with-zero-or-one-losses
|
O(n) Python3 with Dict
|
haly-leshchuk
| 0
| 4
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,880
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/2802531/Log-games-played-and-games-won-with-hash-maps
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
# Consider two HashMaps g is #games and w is #wins
# if g[k] - w[k] =0. Then all games are won
# if g[k] - w[k] =1. Then all games are won except 1 game (ie. exactly 1 fail)
from collections import defaultdict
g,w = defaultdict(int), defaultdict(int)
allTeams =set()
for aGame in matches:
winner,looser = aGame[0], aGame[1]
g[winner] +=1
g[looser] +=1
w[winner] +=1
allTeams.add(winner)
allTeams.add(looser)
allTeams = sorted(allTeams)
allwin,onefail = [],[]
for aTeam in allTeams:
if g[aTeam] - w[aTeam] ==0:
allwin.append(aTeam)
if g[aTeam] - w[aTeam] ==1:
onefail.append(aTeam)
return [allwin,onefail]
|
find-players-with-zero-or-one-losses
|
Log games played and games won with hash-maps
|
user5580aS
| 0
| 1
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,881
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/2780126/Simple-Python-Solution-Using-2-Maps
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
answer = []
win_map, lose_map = {}, {}
for match in matches:
# add to win_map
if match[0] not in win_map:
win_map[match[0]] = 0
win_map[match[0]] += 1
# add to lose
if match[1] not in lose_map:
lose_map[match[1]] = 0
lose_map[match[1]] += 1
players_that_lost_zero_matches = []
players_that_lost_one_match = []
for player in win_map:
if player not in lose_map:
players_that_lost_zero_matches.append(player)
players_that_lost_zero_matches.sort()
for player in lose_map:
if lose_map[player] == 1:
players_that_lost_one_match.append(player)
players_that_lost_one_match.sort()
answer.append(players_that_lost_zero_matches)
answer.append(players_that_lost_one_match)
return answer
|
find-players-with-zero-or-one-losses
|
Simple Python Solution Using 2 Maps
|
devashishnyati
| 0
| 2
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,882
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/2698498/Python-hash-map-(dictionary)-O(n)
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
# create two empty dicts
players = {}
# we will now iterate over the matches
for match in matches:
winner = match[0]
loser = match[1]
if winner not in players:
# this means it's a new player
# we'll create a player card for him
# with 1 wins and 0 losses
players[winner] = [1, 0]
# this means the winner is not a new player
# then we don't really need to count it as we don't care about wins
# should make our code a bit faster
# players[winner][0] += 1
if loser not in players:
players[loser] = [0, 1]
continue
players[loser][1] += 1
# now we have a dict, players, that has counts of all the players
# we can iterate of that dict
no_losses = []
one_loss = []
for player, values in players.items():
if values[1] == 0:
no_losses.append(player)
if values[1] == 1:
one_loss.append(player)
return [sorted(no_losses), sorted(one_loss)]
|
find-players-with-zero-or-one-losses
|
Python, hash map (dictionary), O(n)
|
gal1993
| 0
| 3
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,883
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/2593038/Python-easy-solution
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
w=set()
l={}
for i in matches:
if i[1] in l:
l[i[1]] +=1
else:
l[i[1]] = 1
w.add(i[0])
w1 = [i for i in w if i not in l.keys()]
l1 = []
for i,j in l.items():
if j==1:
l1.append(i)
return [sorted(w1),sorted(l1)]
|
find-players-with-zero-or-one-losses
|
Python easy solution
|
anshsharma17
| 0
| 7
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,884
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/2403111/easy-python-solution
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
winner, loser = [], []
player_dict = {}
for match in matches :
if match[0] not in player_dict.keys() :
player_dict[match[0]] = {0 : 0, 1 : 0}
player_dict[match[0]][0] = 1
else :
player_dict[match[0]][0] += 1
if match[1] not in player_dict.keys() :
player_dict[match[1]] = {0 : 0, 1 : 1}
player_dict[match[1]][1] = 1
else :
player_dict[match[1]][1] += 1
for key in player_dict.keys() :
if player_dict[key][1] == 0 :
winner.append(key)
elif player_dict[key][1] == 1 :
loser.append(key)
winner.sort()
loser.sort()
return [winner, loser]
|
find-players-with-zero-or-one-losses
|
easy python solution
|
sghorai
| 0
| 29
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,885
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/2393487/Python3-Solution-with-using-hashmap
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
absolute = set()
other = collections.defaultdict(int)
for w, l in matches:
if other[w] == 0:
absolute.add(w)
if l in absolute:
absolute.remove(l)
other[l] += 1
return [sorted(list(absolute)), sorted([key for key in other if other[key] == 1])]
|
find-players-with-zero-or-one-losses
|
[Python3] Solution with using hashmap
|
maosipov11
| 0
| 13
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,886
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/2099134/Python-modular-solution-with-graph
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
graph = self.build_graph(matches)
answer = []
zero_lost = sorted(self.find_n_lost(graph, 0))
one_lost = sorted(self.find_n_lost(graph, 1))
answer.append(zero_lost)
answer.append(one_lost)
return answer
def build_graph(self, matches):
graph = {}
for winner, loser in matches:
if winner not in graph:
graph[winner] = []
if loser not in graph:
graph[loser] = []
graph[loser].append(winner)
return graph
def find_n_lost(self, graph, n):
ans = []
for loser, win_list in graph.items():
if len(win_list) == n:
ans.append(loser)
return ans
|
find-players-with-zero-or-one-losses
|
Python modular solution with graph
|
Valens992
| 0
| 23
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,887
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/1961759/Python3-freq-table
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
freq = Counter()
for x, y in matches:
if x not in freq: freq[x] = 0
freq[y] += 1
return [sorted(k for k, v in freq.items() if v == 0), sorted(k for k, v in freq.items() if v == 1)]
|
find-players-with-zero-or-one-losses
|
[Python3] freq table
|
ye15
| 0
| 21
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,888
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/1941877/Easy-Python-solution-with-comments
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
# creating dictionary for winners and losers
mp_winners={}
mp_losers={}
# if player is a winner insert it into mp_winners
for i in range(len(matches)):
mp_winners[matches[i][0]]=1
# if player has lost and a match and is a part of mp_winners then remove that player
# if the player is a loser then add the count of the matches it has lost in mp_losers
for i in range(len(matches)):
if matches[i][1] in mp_winners:
del mp_winners[matches[i][1]]
if matches[i][1] in mp_losers:
mp_losers[matches[i][1]]+=1
else:
mp_losers[matches[i][1]]=1
# win will store players who have never lost and lost_1 will store players who have lost 1 match
win=[]
lost_1=[]
for i in mp_winners:
win.append(i)
for i in mp_losers:
if mp_losers[i]==1:
lost_1.append(i)
win.sort()
lost_1.sort()
return [win,lost_1]
|
find-players-with-zero-or-one-losses
|
Easy Python solution with comments
|
aparnajha17
| 0
| 57
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,889
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/1926960/Python-straightforward
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
lost = set(b for _, b in matches)
a0 = set(a for a, _ in matches if a not in lost)
oneLoss = defaultdict(int)
for a, b in matches:
oneLoss[b] += 1
a1 = set(k for k, v in oneLoss.items() if v == 1)
return [sorted(a0), sorted(a1)]
|
find-players-with-zero-or-one-losses
|
Python straightforward
|
SmittyWerbenjagermanjensen
| 0
| 30
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,890
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/1913715/Python-or-Dictionary-or-Simple-solution-or-Easy-to-Understand
|
class Solution(object):
def findWinners(self, matches):
"""
:type matches: List[List[int]]
:rtype: List[List[int]]
"""
d = {}
for i in range(len(matches)):
if matches[i][0] not in d:
d[matches[i][0]] = [1,0]
else:
d[matches[i][0]][0]+=1
if matches[i][1] not in d:
d[matches[i][1]] = [0,1]
else:
d[matches[i][1]][1]+=1
l,m = [],[]
for i in d:
if d[i][1]==0:
l.append(i)
elif d[i][1]==1:
m.append(i)
l.sort()
m.sort()
return [l,m]
|
find-players-with-zero-or-one-losses
|
Python | Dictionary | Simple solution | Easy to Understand
|
AkashHooda
| 0
| 18
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,891
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/1911259/Python-dictionary
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
d=defaultdict(int)
a1=[]
a2=[]
for i in range(len(matches)):
if matches[i][0] not in d:
d[matches[i][0]] = 0
if matches[i][1] not in d:
d[matches[i][1]] = -1
else:
d[matches[i][1]] -= 1
for player, winlose in d.items():
if winlose==0:
a1.append(player)
elif winlose ==-1:
a2.append(player)
a1.sort()
a2.sort()
return [a1,a2]
|
find-players-with-zero-or-one-losses
|
Python dictionary
|
hugsypenguin
| 0
| 11
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,892
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/1910007/Ez-oror-O(-n-log-n)-TC
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
dw=defaultdict(int) # Player's win count
dl=defaultdict(int) # Player's loss count
wli=[] # To store the players with atleast one win and 0 loss
lli=[] # To store the players with one loss
for i in matches:
dw[i[0]]+=1
dl[i[1]]+=1
for i in dw:
if dw[i]>0 and dl[i]==0: # To check if the player has won atleast one match and lost none
wli.append(i)
for i in dl:
if dl[i]==1: # To check if the player has lost only 1 match
lli.append(i)
if wli: wli.sort()
if lli: lli.sort()
return[wli,lli]
|
find-players-with-zero-or-one-losses
|
Ez || O( n log n) TC
|
ashu_py22
| 0
| 9
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,893
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/1909721/Python-Count-the-indegree-of-nodes
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
indegree = collections.defaultdict(int)
nodeset = set()
# Find all nodes and calculate the indegree of each node
for v1, v2 in matches:
nodeset.add(v1)
nodeset.add(v2)
indegree[v2] += 1
# Generate result by finding the 0-indegree and 1-indegree nodes
res = [[] for _ in range(2)]
for v in sorted(list(nodeset)):
if v in indegree:
if indegree[v] == 1:
res[1].append(v)
else:
res[0].append(v)
return res
|
find-players-with-zero-or-one-losses
|
[Python] Count the indegree of nodes
|
ianlai
| 0
| 18
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,894
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/1909214/Python-Easy-Solution-with-HashMap
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
players = set()
lose = collections.defaultdict(list)
for idx, (w, l) in enumerate(matches):
players.add(w)
players.add(l)
lose[l].append(idx)
lose_one = []
not_lose = []
for p in players:
if p not in lose:
not_lose.append(p)
continue
if len(lose[p]) == 1:
lose_one.append(p)
return [sorted(not_lose), sorted(lose_one)]
|
find-players-with-zero-or-one-losses
|
[Python] Easy Solution with HashMap
|
nightybear
| 0
| 11
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,895
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/1909001/Simple-Python-Soln-With-Explaination
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
c = Counter()
for winner, loser in matches:
c[loser] += 1
c[winner] += 0 # We do this to insert winner in c.
ans = [[], []]
for player, lost in c.items():
if lost < 2:
ans[lost].append(player)
ans[0].sort()
ans[1].sort()
return ans
|
find-players-with-zero-or-one-losses
|
Simple Python Soln With Explaination
|
mrunankmistry52
| 0
| 8
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,896
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/1908991/Simple-Python-3-or-Hashmap-or-Explanation-added
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
dic={} #losers dictionary
for i in range(len(matches)):
dic[matches[i][1]]=0
#Store number of matches lost
for i in range(len(matches)):
dic[matches[i][1]]+=1
temp1=[] #list of all players that have not lost any matches
temp2=[] #list of all players that have lost exactly one match
for ele in dic:
if dic[ele]==1:
temp2.append(ele)
for i in range(len(matches)):
if matches[i][0] not in dic: #check if winner not present in loser dictionary
temp1.append(matches[i][0])
dic[matches[i][0]]=0 #add winner to dictionary to avoid duplicates
return [sorted(temp1),sorted(temp2)]
|
find-players-with-zero-or-one-losses
|
Simple Python 3 | Hashmap | Explanation added
|
user8795ay
| 0
| 15
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,897
|
https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/1908792/Python-or-Easy-Dict
|
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
lose_dict = defaultdict(list)
players = set()
answer = [[],[]]
for winner, loser in matches:
lose_dict[loser].append(winner)
players.add(winner)
players.add(loser)
# find the list of all players that have not lost any matches
for player in players:
if player not in lose_dict:
answer[0].append(player)
# answer[1] is a list of all players that have lost exactly one match
for player in lose_dict.keys():
if len(lose_dict[player]) == 1:
answer[1].append(player)
answer[0].sort()
answer[1].sort()
return answer
|
find-players-with-zero-or-one-losses
|
Python | Easy Dict
|
Mikey98
| 0
| 24
|
find players with zero or one losses
| 2,225
| 0.686
|
Medium
| 30,898
|
https://leetcode.com/problems/maximum-candies-allocated-to-k-children/discuss/1912213/Easy-To-Understand-Python-Solution-(Binary-Search)
|
class Solution:
def maximumCandies(self, candies, k):
n = len(candies)
left = 1 # the least number of candy in each stack we can give to each student is one
right = max(candies) # the max number of candy in each stack that we can give to each student is the maximum number in the candies array
ans = 0 # ans here is used to store the maximum amount in each stack that we can give to each children.
# If we don't have enough to distribute, we will return 0 at the end so we initialize it to be 0 now.
while left <= right: # binary search
numberOfPiles = 0
mid = (left) + (right - left) // 2 # the number of candies we require to form a stack
for i in range(n): # loop through the array to find the numbers of stack we can form
numberOfPiles += candies[i] // mid # we add to the numberOfPiles whenever we find that this current stack (candies[i]) can be split into mid (the number of candies we require to form a stack)
if numberOfPiles >= k: # if our number of piles is greater or equal than the students we have, so we have enough to distribute
ans = max(ans, mid) # we first store the max no. of candies in each stack that we can give to each student
left = mid + 1 # we will try to increase the number of candies in each stack that we can give to each student
else:
right = mid - 1 # we will try to reduce the number of candies in each stack that we can give to each student
return ans
|
maximum-candies-allocated-to-k-children
|
Easy To Understand Python Solution (Binary Search)
|
danielkua
| 2
| 87
|
maximum candies allocated to k children
| 2,226
| 0.361
|
Medium
| 30,899
|
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.