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/number-of-steps-to-reduce-a-number-to-zero/discuss/2394669/Python-solution-Num-of-steps-to-reduce-a-num-to-zero
class Solution: def numberOfSteps(self, num: int) -> int: count = 0 while(num): if(num%2==0): num = num/2 else: num = num-1 count+=1 return count
number-of-steps-to-reduce-a-number-to-zero
Python solution - Num of steps to reduce a num to zero
gurudadhich76
0
28
number of steps to reduce a number to zero
1,342
0.854
Easy
20,200
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2381047/Time-efficient-Python-using-bit-manipulation
class Solution: def numberOfSteps(self, num: int) -> int: ''' Intuitive Method ''' # res = 0 # while num > 0: # num = num // 2 if num % 2 == 0 else num - 1 # res += 1 # return res ''' Binary Method ''' binary_expression = bin(num)[2:] # to get rid of '0b' return len(binary_expression) + binary_expression.count('1') - 1
number-of-steps-to-reduce-a-number-to-zero
Time-efficient Python using bit manipulation
MajimaAyano
0
73
number of steps to reduce a number to zero
1,342
0.854
Easy
20,201
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2376900/Python-recursive-solution-Fastest
class Solution: def numberOfSteps(self, num: int) -> int: s=0 def ops(n,s): # print(n,s) if n == 0: return s if (n & 1): s += 1 return ops(n-1,s) else: s += 1 return ops(n//2,s) return ops(num,s) ```
number-of-steps-to-reduce-a-number-to-zero
Python recursive solution Fastest
Vigneshbabupj
0
68
number of steps to reduce a number to zero
1,342
0.854
Easy
20,202
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2362339/Python-or-Recursive
class Solution: def recursive_solve(self, num: int, step: int) -> int: if num > 0: return self.recursive_solve(num - 1 if num % 2 else num // 2, step + 1) return step def numberOfSteps(self, num: int) -> int: return self.recursive_solve(num, 0)
number-of-steps-to-reduce-a-number-to-zero
Python | Recursive
c1nn4
0
25
number of steps to reduce a number to zero
1,342
0.854
Easy
20,203
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2354099/Faster-than-80
class Solution: def numberOfSteps(self, num: int) -> int: steps = 0 while num != 0: if num % 2 == 0: num = num/2 else: num -= 1 steps += 1 return steps
number-of-steps-to-reduce-a-number-to-zero
Faster than 80%
Arrstad
0
29
number of steps to reduce a number to zero
1,342
0.854
Easy
20,204
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2294052/Highly-efficient-Python-Solution
class Solution: def numberOfSteps(self, num: int) -> int: steps = 0 while num: if num % 2: num -= 1 steps += 1 else: num /= 2 steps += 1 return steps
number-of-steps-to-reduce-a-number-to-zero
Highly efficient Python Solution
samsub15
0
123
number of steps to reduce a number to zero
1,342
0.854
Easy
20,205
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/2253645/Python-Simplest-Solution-With-Explanation-or-Beg-to-adv-or-Math
class Solution: def numberOfSteps(self, num: int) -> int: count = 0 # taking a couter to count number of steps to reduce it to zero while num>0: # nums should not be less then 0 if num % 2 == 0: # if num is completely divide by zero num = num//2 # to have the quotient count+=1 # as number got reduced we will increase the counter else: num-=1 # and if num is not even then we subtract one out of it. count+=1 # as number got reduced we will increase the counter return count # returning the step occurred o reduce the number.
number-of-steps-to-reduce-a-number-to-zero
Python Simplest Solution With Explanation | Beg to adv | Math
rlakshay14
0
60
number of steps to reduce a number to zero
1,342
0.854
Easy
20,206
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/1816745/Python-Solution-oror-Sliding-Window
class Solution: def numOfSubarrays(self, arr, k, threshold): windowStart = 0 max_avg = 0 avg = 0 c=0 result = [] windowSum = 0 for windowEnd in range(len(arr)): windowSum += arr[windowEnd] if((windowEnd)>=k-1): avg = windowSum//k result.append(avg) windowSum -= arr[windowStart] windowStart += 1 for i in range(len(result)): if(result[i]>=threshold): c=c+1 return c
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
Python Solution || Sliding Window
aashutoshjha21022002
3
281
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,207
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/2797739/Python-oror-91.78Faster-oror-Sliding-Window-oror-O(N)-Solution
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: c,j,n=0,0,len(arr) s=sum(arr[:k]) if s>=k*threshold: c=1 for i in range(k,n): s+=arr[i]-arr[j] if s>=k*threshold: c+=1 j+=1 return c
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
Python || 91.78%Faster || Sliding Window || O(N) Solution
DareDevil_007
2
89
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,208
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/1125577/Python3-sliding-window
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: ans = sm = 0 for i, x in enumerate(arr): sm += x if i >= k: sm -= arr[i-k] if i+1 >= k and sm >= k*threshold: ans += 1 return ans
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
[Python3] sliding window
ye15
2
151
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,209
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/1714628/Python-Simple-Sliding-Window-explained
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: # start with the sum of first k elements summ = sum(arr[:k]) # set out(result) variable to 1 # if the first set of sum is valid # i.e its avg >= threshold out = 1 if summ//k >= threshold else 0 # start the loop from 1, since we've # already taken into account the elements # from i = 0. # go till k elements less than the length # that is the length of our window and since # it has to be inclusive, add 1 to the range for i in range(1, len(arr)-k+1): # remove the last element from the sum # and add the next element (i+k-1) summ -= arr[i-1] summ += arr[i+k-1] # increment counter if avg >= threshold if summ//k >= threshold: out += 1 return out
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
[Python] Simple Sliding Window explained
buccatini
1
136
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,210
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/1617475/easy-python3-solution
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: n=len(arr) res=0 s=sum(arr[:k]) if s/k>=threshold: res+=1 for i in range(1,n-k+1): s-=arr[i-1] s+=arr[i+k-1] if s/k>=threshold: res+=1 return res
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
easy python3 solution
Karna61814
1
59
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,211
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/1468412/Python-Sliding-Window-approach
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: wstart = wsum = count = 0 for wend in range(len(arr)): wsum += arr[wend] if wend >= k: wsum -= arr[wstart] wstart += 1 if (wsum//k) >= threshold and (wend-wstart+1) == k: count += 1 return count
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
Python Sliding Window approach
_r_rahul_
1
142
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,212
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/2799176/Python3-or-Basic-Sliding-Window-Solution
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: i = 0;j = k-1;n = len(arr) ans = 0 windowSum = sum(arr[:k-1]) while(j<n): windowSum+=arr[j] if(windowSum/k>=threshold): ans+=1 windowSum-=arr[i] i+=1;j+=1 return ans
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
Python3 | Basic Sliding Window Solution
ty2134029
0
2
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,213
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/2710093/Clean-Sliding-Window-or-Python
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: n = len(arr) i, j, s, res, count = 0, 0, threshold*k, 0, 0 while j<n: res += arr[j] if j-i+1<k: j += 1 else: if res>=s: count += 1 res -= arr[i] i += 1; j += 1 return count
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
Clean Sliding Window | Python
RajatGanguly
0
9
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,214
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/2653616/simple-and-easy-sliding-window-solution-for-begginers
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: ws=0 we=0 sumi=0 ans=[] c=0 for we in range(len(arr)): sumi+=arr[we] if we>=k-1: ans.append(sumi//k) sumi-=arr[ws] ws+=1 for i in ans: if i>=threshold: c+=1 return c
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
simple and easy sliding window solution for begginers
insane_me12
0
3
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,215
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/2645381/Python3-Solution-oror-O(N)-Time-and-O(1)-Space-Complexity
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: count=0 sum=0 n=len(arr) for i in range(k): sum+=arr[i] if sum>=threshold*k: count+=1 for i in range(k,n): sum=sum+arr[i]-arr[i-k] if sum>=threshold*k: count+=1 return count
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
Python3 Solution || O(N) Time & O(1) Space Complexity
akshatkhanna37
0
3
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,216
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/2635305/Simple-and-easy-solution-using-SLIDING-WINDOW-or-Python
class Solution(object): def numOfSubarrays(self, arr, k, threshold): """ :type arr: List[int] :type k: int :type threshold: int :rtype: int """ count=0 windowSum=0 windowStart = 0 for i in range(len(arr)): windowSum+=arr[i] if i+1>=k: if windowSum/k>=threshold: count+=1 windowSum-=arr[windowStart] windowStart+=1 return count
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
Simple and easy solution using SLIDING WINDOW | Python
msherazedu
0
10
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,217
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/2607918/Python-Sliding-Window-Solution-92
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: left = 0 right = k-1 count = 0 curr_sum = sum(arr[left:right+1]) while righ < len(arr): if curr_sum//k >= threshold: count += 1 curr_sum -= arr[left] left+=1 right+=1 try: curr_sum += arr[right] except: pass return count
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
Python Sliding Window Solution 92%
pandish
0
27
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,218
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/2607571/Straightforward-Python-3-solution
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: res = [] pos, curSum = 0, 0 while pos < k: curSum += arr[pos] pos += 1 res.append(curSum / k) while pos < len(arr): curSum += arr[pos] curSum -= arr[pos-k] res.append(curSum / k) pos += 1 return len( [x for x in res if x >= threshold] )
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
Straightforward Python 3 solution
kodrevol
0
15
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,219
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/2381599/Python-Solution-or-Classic-Sliding-Window-or-Two-Pointers-or-90-Faster
class Solution: def numOfSubarrays(self, nums: List[int], k: int, threshold: int) -> int: currSum = 0 start = 0 end = 0 count = 0 # run right pointer till end element for end in range(len(nums)): # update value to window currSum += nums[end] # check if window size achieved if (end - start + 1) == k: # is average > target val if (currSum // k) >= threshold: count += 1 # slide the window currSum -= nums[start] start += 1 return count
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
Python Solution | Classic Sliding Window | Two Pointers | 90% Faster
Gautam_ProMax
0
20
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,220
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/2110423/Python-3-Interview-Answer-or-Clean-and-Easy-Readability-or-Sliding-Window
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: result, sumWindow = 0, 0 for i, num in enumerate(arr): sumWindow += num if i >= k - 1: if sumWindow / k >= threshold: result += 1 sumWindow -= arr[i - k + 1] return result
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
[Python 3] Interview Answer | Clean & Easy Readability | Sliding Window
Cut
0
36
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,221
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/1957693/python-3-oror-O(n)-faster-than-91oror-python-oror-sliding-window
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: s=sum(arr[:k]) c=0 for i in range(k,len(arr)): avg=s//k if avg>=threshold: c+=1 s=s+arr[i]-arr[i-k] avg=(s+arr[len(arr)-1]-arr[len(arr)-1-k])//k if avg>=threshold: c+=1 return c
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
python 3 || O(n) faster than 91%|| python || sliding window
nileshporwal
0
64
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,222
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/1710387/Easy-Solution-or-Sliding-window-or-Python
class Solution: def numOfSubarrays(self, arr: List[int], k: int, t: int) -> int: add,cnt=0,0 i,j=0,0 target=(k*t) for j in range(len(arr)): add+=arr[j] if j>=k-1: if add >= target: cnt+=1 add-=arr[i] i+=1 return(cnt)
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
Easy Solution | Sliding window | Python
shandilayasujay
0
48
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,223
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/1702174/Python-simple-O(n)-time-O(1)-space-sliding-window-solution
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: n = len(arr) target = k*threshold if n < k: return 0 elif n == k: if sum(arr) >= k*threshold: return 1 else: return 0 res = 0 i = 0 s = sum(arr[:k]) while i < n-k: # i <= n-k-1 if s >= target: res += 1 s -= arr[i] s += arr[i+k] i += 1 if s > target: res += 1
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
Python simple O(n) time, O(1) space sliding window solution
byuns9334
0
78
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,224
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/1304161/Python3-solution-using-sliding-window
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: x = sum(arr[0:k]) i = 1 count = 0 j = k if x/k >= threshold: count += 1 while j < len(arr): x = x-arr[i-1]+arr[j] if x/k >= threshold: count += 1 i += 1 j += 1 return count
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
Python3 solution using sliding window
EklavyaJoshi
0
34
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,225
https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/discuss/1128952/Python-3-Straight-forward-sliding-window
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: window_start, total, res = 0, 0, 0 for i in range(k - 1): total += arr[i] for window_end in range(k - 1, len(arr)): total += arr[window_end] if window_end - window_start + 1 > k: total -= arr[window_start] window_start += 1 if total / k >= threshold: res += 1 return res
number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold
[Python 3] Straight forward sliding window
elliottbarbeau
0
100
number of sub arrays of size k and average greater than or equal to threshold
1,343
0.676
Medium
20,226
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/1911342/Python-one-line-solution-based-on-aptitude-formula
class Solution: def angleClock(self, hour: int, minutes: int) -> float: return min(abs(30*hour-5.5*minutes),360-abs(30*hour-5.5*minutes))
angle-between-hands-of-a-clock
Python one line solution based on aptitude formula
amannarayansingh10
2
49
angle between hands of a clock
1,344
0.634
Medium
20,227
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/1889556/Python-Easiest-to-understand!-Comments-Clear-and-Concise
class Solution: def angleClock(self, h, m): # Convert the hour hand to another minute hand m2 = (h%12 + m/60)*5 # Calculate the difference between the two minute hands diff = abs(m-m2) # Convert the difference to an angle ang = diff*(360/60) # Return the smallest angle return min(360-ang, ang)
angle-between-hands-of-a-clock
Python - Easiest to understand! Comments - Clear and Concise
domthedeveloper
2
81
angle between hands of a clock
1,344
0.634
Medium
20,228
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/2102837/PYTHON-oror-NAIVE-APPROACH-oror-EXPLAINED-WITH-COMMENTS
class Solution: def angleClock(self, hour: int, minutes: int) -> float: # Normalizing minute position in range (1-12) min_clock = minutes/5 # If time is 12.00 returning 0 if minutes == 0 and hour*30 == 360: return 0 # If minutes is 0 then multiply hour by 30 degree as each hour consists of 30 degree elif minutes == 0: only_hour = hour*30 # Check whether it is shorter in opposite direction if only_hour > 180: return 360-(only_hour) return only_hour else: # Finding the degree between minute hand and closest hour of the hour hand time = abs(hour-min_clock)*30 # Finding the difference that needs to added/subtracted diff = 30/(60/minutes) # Subtracting when minute hand is at greater value that hour hand if min_clock > hour: fin_time = time-diff # Adding when minute hand is at lesser value that hour hand else: fin_time = time+diff # Check the shorter direction if fin_time > 180: diff = fin_time-180 return abs(180-diff) else: return abs(fin_time)
angle-between-hands-of-a-clock
PYTHON || NAIVE APPROACH || EXPLAINED WITH COMMENTS
klmsathish
1
41
angle between hands of a clock
1,344
0.634
Medium
20,229
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/2667553/Python-solution
class Solution: def angleClock(self, hour: int, minutes: int) -> float: angle = abs((hour + (minutes / 60)) - (minutes/5)) * 30 if angle > 180: angle = 360 - angle return angle
angle-between-hands-of-a-clock
Python solution
hyroas
0
5
angle between hands of a clock
1,344
0.634
Medium
20,230
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/2155318/Simple-Math-Solution
class Solution: def angleClock(self, hour: int, minutes: int) -> float: minutes_angle = minutes / 60 * 360 hours_angle = hour / 12 * 360 + minutes / 60 * 30 if minutes_angle > hours_angle: angle = minutes_angle - hours_angle return min(angle, 360 - angle) else: angle = hours_angle - minutes_angle return min(angle, 360 - angle)
angle-between-hands-of-a-clock
Simple Math Solution
Vayne1994
0
24
angle between hands of a clock
1,344
0.634
Medium
20,231
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/2097253/Python-One-liner-with-detailed-explanation
class Solution: def angleClock(self, hour: int, minutes: int) -> float: return min(360 - abs(30 * hour - 11/2 * minutes), abs(30 * hour - 11/2 * minutes))
angle-between-hands-of-a-clock
Python One liner with detailed explanation
Coconut0727
0
48
angle between hands of a clock
1,344
0.634
Medium
20,232
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/1979348/Python-easy-to-read-and-understand-or-math
class Solution: def angleClock(self, hour: int, minutes: int) -> float: hr_hand = 30*hour + 0.5*minutes min_hand = 0*hour + 6*minutes res = 0 if min_hand > hr_hand: res = min_hand-hr_hand else: res = hr_hand-min_hand return res if res < 180 else 360-res
angle-between-hands-of-a-clock
Python easy to read and understand | math
sanial2001
0
48
angle between hands of a clock
1,344
0.634
Medium
20,233
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/1593235/Python-Straightforward-Solution-or-Easy-to-Understand
class Solution: def angleClock(self, hr: int, mint: int) -> float: # Formula: angle = (30*Hours) - (11/2 * Minutes) res = abs(30*hr-5.5*mint) if res > 180: return 360-res return res
angle-between-hands-of-a-clock
Python Straightforward Solution | Easy to Understand
leet_satyam
0
54
angle between hands of a clock
1,344
0.634
Medium
20,234
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/1570664/Python3-Solution-with-using-math
class Solution: def angleClock(self, hour: int, minutes: int) -> float: degree_per_minute = 6 degree_per_hour = 30 minute_hand = degree_per_minute * minutes hour_hand = degree_per_hour * (hour % 12) ratio = 0 if minute_hand != 0: ratio = degree_per_hour / (360 / minute_hand) angle = abs(minute_hand - hour_hand - ratio) return angle if angle <= 180.0 else 360.0 - angle # min(angle, 360 - angle)
angle-between-hands-of-a-clock
[Python3] Solution with using math
maosipov11
0
73
angle between hands of a clock
1,344
0.634
Medium
20,235
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/1380248/Python3-solution
class Solution: def angleClock(self, hour: int, minutes: int) -> float: minute_angle = minutes*6 if hour == 12: hour_angle = minutes*0.5 else: hour_angle = hour*30+minutes*0.5 return min(abs(minute_angle - hour_angle),360-abs(minute_angle - hour_angle))
angle-between-hands-of-a-clock
Python3 solution
EklavyaJoshi
0
40
angle between hands of a clock
1,344
0.634
Medium
20,236
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/1297137/Python-solution
class Solution: def angleClock(self, hour: int, minutes: int) -> float: min_degree = minutes / 60 * 360 hour_degree = (hour / 12) * 360 + (minutes / 60) * 30 diff = abs(min_degree-hour_degree) return min(diff, 360-diff)
angle-between-hands-of-a-clock
Python solution
5tigerjelly
0
50
angle between hands of a clock
1,344
0.634
Medium
20,237
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/1052724/Ultra-Simple-CPPJava-Solution-or-0ms-or-Suggestions-or-optimization-are-welcomed-or
class Solution: def angleClock(self, hour: int, minutes: int) -> float: if hour==12: hour=0 mins= 6*minutes hrs=hour*30+int(minutes%60)*0.5 ans=0 if mins>=hrs: ans=mins-hrs if hrs>mins: ans=hrs-mins; if 360-mins+hrs<mins-hrs: ans=360-mins+hrs if 360-hrs+mins<hrs-mins: ans=360-hrs+mins return ans;
angle-between-hands-of-a-clock
Ultra Simple CPP/Java Solution | 0ms | Suggestions or optimization are welcomed |
angiras_rohit
0
56
angle between hands of a clock
1,344
0.634
Medium
20,238
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/738439/Intuitive-approach
class Solution: def angleClock(self, hour: int, minutes: int) -> float: hour = 0 if hour == 12 else hour h_degree = 30 * hour + 30 * (minutes/60) m_degree = 6 * minutes if minutes < 60 else 0 diff_degree = abs(h_degree-m_degree) return min(diff_degree, 360 - diff_degree)
angle-between-hands-of-a-clock
Intuitive approach
puremonkey2001
0
25
angle between hands of a clock
1,344
0.634
Medium
20,239
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/736043/Python3%3A-Easy-To-Understand
class Solution: def angleClock(self, hour: int, minutes: int) -> float: hour_angle = 0.5*(60*hour + minutes) minute_angle = 6*minutes return min(abs(hour_angle-minute_angle), 360-abs(hour_angle-minute_angle))
angle-between-hands-of-a-clock
Python3: Easy To Understand
pranavd2895
0
57
angle between hands of a clock
1,344
0.634
Medium
20,240
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/735352/Python-or-Easy-Solution-for-Beginners-with-Comments-beats-around-80
class Solution(object): def angleClock(self, hour, minutes): # Converting Minutes To Angle Degree #[There are 60min and 360 degree each min counts for six] t_min = 6*minutes # Converting Hours To Angle Degree #[There are 12 hours and 360 degree each hour acccounts for 30 degree] # Partial Angle of Hour is also added depanding upon the minutes t_hour = 30*hour + 30*(minutes/float(60)) # This if Condition is when time is between 12 and 1. #Because degree will exceed more than 360 in that case if t_hour>=360: t_hour = t_hour-360 x = abs(t_hour-t_min) # This if loop is to provide the smallest value from the both the sides. if x>180: return 360-x else: return x
angle-between-hands-of-a-clock
[Python] | Easy Solution for Beginners with Comments beats around 80%
rachitsxn292
0
33
angle between hands of a clock
1,344
0.634
Medium
20,241
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/735352/Python-or-Easy-Solution-for-Beginners-with-Comments-beats-around-80
class Solution(object): def angleClock(self, hour, minutes): t_min = 6*minutes t_hour = 30*hour + 30*(minutes/float(60)) if t_hour>=360: t_hour = t_hour-360 x = abs(t_hour-t_min) if x>180: return 360-x else: return x
angle-between-hands-of-a-clock
[Python] | Easy Solution for Beginners with Comments beats around 80%
rachitsxn292
0
33
angle between hands of a clock
1,344
0.634
Medium
20,242
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/607258/python3-32-ms-easy-newbie-solution
class Solution: def angleClock(self, hour: int, minutes: int) -> float: if hour==12: degh=(12-hour)*30+(minutes*0.5) degm=minutes*6 else: degh=(hour*30)+(minutes*0.5) degm=minutes*6 diff=max(degh,degm)-min(degh,degm) return min(diff,360-diff) ```
angle-between-hands-of-a-clock
python3 32 ms easy newbie solution
Hordlaw
0
34
angle between hands of a clock
1,344
0.634
Medium
20,243
https://leetcode.com/problems/angle-between-hands-of-a-clock/discuss/505123/Python-3-(one-line)-(beats-~97)
class Solution: def angleClock(self, H: int, M: int) -> float: return min(abs(30*H-11*M/2),360-abs(30*H-11*M/2)) - Junaid Mansuri - Chicago, IL
angle-between-hands-of-a-clock
Python 3 (one line) (beats ~97%)
junaidmansuri
0
150
angle between hands of a clock
1,344
0.634
Medium
20,244
https://leetcode.com/problems/jump-game-iv/discuss/1691093/Python3-RECURSIVE-BFS-(_)-Explained
class Solution: def minJumps(self, arr: List[int]) -> int: N, groups = len(arr), defaultdict(list) for i, el in enumerate(arr): groups[el].append(i) vis, vis_groups = set(), set() def bfs(lvl, dist): nextLvl = set() for i in lvl: if i in vis: continue if i == N-1: return dist vis.add(i) if i: nextLvl.add(i-1) if i+1 < N: nextLvl.add(i+1) if not arr[i] in vis_groups: vis_groups.add(arr[i]) nextLvl.update(groups[arr[i]]) return bfs(nextLvl, dist + 1) return bfs(set([0]), 0)
jump-game-iv
✔️ [Python3] RECURSIVE BFS (҂◡_◡) ᕤ, Explained 💥
artod
9
321
jump game iv
1,345
0.44
Hard
20,245
https://leetcode.com/problems/jump-game-iv/discuss/2658643/Python3-Solution-ororFaster-than-56-ororBFSoror-Bahut-TEZ
class Solution: def minJumps(self, arr: List[int]) -> int: h={} for i,e in enumerate(arr): if e not in h: h[e] = [] h[e].append(i) q = [(0,0)] while q: n,d = q.pop(0) if n == len(arr)-1: return d if n+1 == len(arr)-1: return d+1 if n+1 < len(arr) and h.get(arr[n+1]): q.append((n+1,d+1)) if n-1 >= 0 and h.get(arr[n-1]): q.append((n-1,d+1)) for i in h[arr[n]]: if i != n: q.append((i,d+1)) if i == len(arr)-1: return d+1 h[arr[n]] = []
jump-game-iv
Python3 Solution ||Faster than 56% ||BFS|| Bahut TEZ
hoo__mann
4
125
jump game iv
1,345
0.44
Hard
20,246
https://leetcode.com/problems/jump-game-iv/discuss/1775035/Python-easy-to-read-and-understand-or-BFS
class Solution: def minJumps(self, arr: List[int]) -> int: n = len(arr) d = defaultdict(list) for i, val in enumerate(arr): d[val].append(i) visited = [False for _ in range(n)] q = [0] visited[0] = True ans = 0 while q: for i in range(len(q)): ind = q.pop(0) #print(ind) if ind == n-1: return ans if ind + 1 < n and visited[ind+1] == False: visited[ind+1] = True q.append(ind+1) if ind - 1 > 0 and visited[ind-1] == False: visited[ind-1] = True q.append(ind-1) for nei in d[arr[ind]]: if visited[nei] == False: visited[nei] = True q.append(nei) del d[arr[ind]] ans += 1
jump-game-iv
Python easy to read and understand | BFS
sanial2001
1
98
jump game iv
1,345
0.44
Hard
20,247
https://leetcode.com/problems/jump-game-iv/discuss/1692042/Python-Solution-using-Dictionary-and-BFS-!
class Solution: def minJumps(self, arr: List[int]) -> int: n=len(arr) if n<2: return 0 graph={} for i in range(n): if arr[i] not in graph: graph[arr[i]]=[i] else: graph[arr[i]].append(i) current = [0] visited = {0} step = 0 while current: next=[] for node in current: if node == n-1: return step for child in graph[arr[node]]: if child not in visited: visited.add(child) next.append(child) graph[arr[node]].clear() for child in [node-1 , node+1]: if 0<= child <len(arr) and child not in visited: visited.add(child) next.append(child) current = next step = step + 1 return -1 # If It is Usefull to Understand Please UpVote 🙏🙏
jump-game-iv
Python Solution using Dictionary and BFS !
ASHOK_KUMAR_MEGHVANSHI
1
76
jump game iv
1,345
0.44
Hard
20,248
https://leetcode.com/problems/jump-game-iv/discuss/1690774/Python3-or-BFS-or-Memory-limit-solution
class Solution: def minJumps(self, arr: List[int]) -> int: n = len(arr) visited = set() if n <= 1: return 0 same_value = dict() for i, v in enumerate(arr): if v not in same_value.keys(): same_value[v]= [i] else: same_value[v].append(i) stack = list() level = 0 stack.append((0,0)) total = -1 while stack: level,node = stack.pop(0) visited.add(node) if node == n-1: return level else: for a in same_value[arr[node]] : if a != node and a not in visited: stack.append((level+1,a)) same_value[arr[node]].clear() if node+1 < n and node+1 not in visited: stack.append((level+1,node+1)) if node-1 >=0 and node-1 not in visited: stack.append((level+1,node-1)) return level
jump-game-iv
Python3 | BFS | Memory limit solution
letyrodri
1
55
jump game iv
1,345
0.44
Hard
20,249
https://leetcode.com/problems/jump-game-iv/discuss/989599/Python3-bfs-by-level
class Solution: def minJumps(self, arr: List[int]) -> int: loc = defaultdict(list) for i, x in enumerate(arr): loc[x].append(i) ans = 0 seen = {0} queue = deque([0]) while queue: for _ in range(len(queue)): i = queue.popleft() if i+1 == len(arr): return ans for ii in [i-1, i+1] + loc[arr[i]]: if 0 <= ii < len(arr) and ii not in seen: seen.add(ii) queue.append(ii) loc.pop(arr[i]) ans += 1
jump-game-iv
[Python3] bfs by level
ye15
1
108
jump game iv
1,345
0.44
Hard
20,250
https://leetcode.com/problems/jump-game-iv/discuss/2838297/Python-(Simple-BFS)
class Solution: def minJumps(self, arr): n, dict1 = len(arr), defaultdict(list) for i,j in enumerate(arr): dict1[j].append(i) stack, visited, visited_group = [(0,0)], set(), set() visited.add(0) while stack: idx, jump = stack.pop(0) if idx == n-1: return jump if idx+1 < n and idx+1 not in visited: visited.add(idx+1) stack.append((idx+1,jump+1)) if idx-1 >= 0 and idx-1 not in visited: visited.add(idx-1) stack.append((idx-1,jump+1)) if arr[idx] not in visited_group: for j in dict1[arr[idx]]: if j not in visited: visited.add(j) stack.append((j,jump+1)) visited_group.add(arr[idx])
jump-game-iv
Python (Simple BFS)
rnotappl
0
2
jump game iv
1,345
0.44
Hard
20,251
https://leetcode.com/problems/jump-game-iv/discuss/2057794/BFS-Python-Solution
class Solution: def minJumps(self, arr: List[int]) -> int: n=len(arr) ; graph=defaultdict(list) ; layer=[0] ; visited={0} ; step=0 if n<=1: return 0 for i,v in enumerate(arr): graph[v].append(i) while layer: next_layer=[] for node in layer: if node==n-1: return step for child in graph[arr[node]]: if child not in visited: visited.add(child) ; next_layer.append(child) del graph[arr[node]] for child in {node-1,node+1}: if 0<=child<len(arr) and child not in visited: visited.add(child) ; next_layer.append(child) layer=next_layer step+=1
jump-game-iv
BFS Python Solution
Taha-C
0
49
jump game iv
1,345
0.44
Hard
20,252
https://leetcode.com/problems/jump-game-iv/discuss/1692196/Simple-BFS-Solution-or-Challenge-January-Day-15
class Solution: def minJumps(self, arr: List[int]) -> int: N = len(arr) num_dict = defaultdict(list) for i, n in enumerate(arr): num_dict[n].append(i) visited = defaultdict(bool) dist = [math.inf] * N queue = deque([0]) dist[0] = 0 while dist[N - 1] == math.inf: u = queue.popleft() num = arr[u] if u - 1 >= 0 and dist[u - 1] > dist[u] + 1: dist[u - 1] = dist[u] + 1 queue.append(u - 1) if u + 1 < N and dist[u + 1] > dist[u] + 1: dist[u + 1] = dist[u] + 1 queue.append(u + 1) if visited[num]: continue visited[num] = True for v in num_dict[num]: if dist[v] > dist[u] + 1: dist[v] = dist[u] + 1 queue.append(v) return dist[N-1]
jump-game-iv
Simple BFS Solution | Challenge January, Day 15
atiq1589
0
43
jump game iv
1,345
0.44
Hard
20,253
https://leetcode.com/problems/jump-game-iv/discuss/1692061/Python3-Solution
class Solution: def minJumps(self, arr: List[int]) -> int: N, grps = len(arr), defaultdict(list) for i, el in enumerate(arr): grps[el].append(i) vis, vis_grps = set(), set() def bfs(lvl, dist): nextLvl = set() for i in lvl: if i in vis: continue if i == N-1: return dist vis.add(i) if i: nextLvl.add(i-1) if i+1 < N: nextLvl.add(i+1) if not arr[i] in vis_grps: vis_grps.add(arr[i]) nextLvl.update(grps[arr[i]]) return bfs(nextLvl, dist + 1) return bfs(set([0]), 0)
jump-game-iv
Python3 Solution
nomanaasif9
0
46
jump game iv
1,345
0.44
Hard
20,254
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/503507/Python-3-(five-lines)-(beats-100)
class Solution: def checkIfExist(self, A: List[int]) -> bool: if A.count(0) > 1: return True S = set(A) - {0} for a in A: if 2*a in S: return True return False - Junaid Mansuri - Chicago, IL
check-if-n-and-its-double-exist
Python 3 (five lines) (beats 100%)
junaidmansuri
20
4,100
check if n and its double exist
1,346
0.362
Easy
20,255
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2197827/Python3-solution-using-hashMap
class Solution: def checkIfExist(self, arr: List[int]) -> bool: hashMap = {} for i in arr: if(hashMap.get(i+i)): return True if(i%2 == 0 and hashMap.get(i//2)): return True hashMap[i] = True return False
check-if-n-and-its-double-exist
📌 Python3 solution using hashMap
Dark_wolf_jss
6
152
check if n and its double exist
1,346
0.362
Easy
20,256
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2074775/Explanation-oror-(-N-*-LOG-N-)-oror-BINARY-SEARCH
class Solution: def checkIfExist(self, arr: List[int]) -> bool: arr.sort() n=len(arr) for i in range(n): k=arr[i] # binary search for negatives if k<0: lo=0 hi=i while lo<hi: mid=(lo+hi)//2 if arr[mid]==(2*k): return True elif arr[mid]<(2*k): lo=mid+1 else: hi=mid # binary seach for non negatives else: lo=i+1 hi=n while lo<hi: mid=(lo+hi)//2 if arr[mid]==(k*2): return True elif arr[mid]<(k*2): lo=mid+1 else: hi=mid return False
check-if-n-and-its-double-exist
Explanation || ( N * LOG N ) || BINARY SEARCH
karan_8082
5
312
check if n and its double exist
1,346
0.362
Easy
20,257
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2074764/PYTHON-oror-O(-n-logn-)-oror-Binary-Search
class Solution: def checkIfExist(self, arr: List[int]) -> bool: arr.sort() n=len(arr) for i in range(n): k=arr[i] # binary search for negatives if k<0: lo=0 hi=i while lo<hi: mid=(lo+hi)//2 if arr[mid]==(2*k): return True elif arr[mid]<(2*k): lo=mid+1 else: hi=mid # binary seach for non negatives else: lo=i+1 hi=n while lo<hi: mid=(lo+hi)//2 if arr[mid]==(k*2): return True elif arr[mid]<(k*2): lo=mid+1 else: hi=mid return False
check-if-n-and-its-double-exist
PYTHON || O( n logn ) || Binary Search
karan_8082
5
255
check if n and its double exist
1,346
0.362
Easy
20,258
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/503455/JavaPython3-using-hash-set
class Solution: def checkIfExist(self, arr: List[int]) -> bool: seen = set() for x in arr: if 2*x in seen or x/2 in seen: return True seen.add(x) return False
check-if-n-and-its-double-exist
[Java/Python3] using hash set
ye15
4
262
check if n and its double exist
1,346
0.362
Easy
20,259
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2340623/Python-Simple-Python-Solution-Using-Binary-Search
class Solution: def checkIfExist(self, arr: List[int]) -> bool: def BinarySearch(array, target): low = 0 high = len(array) - 1 while low <= high: mid = (low + high ) // 2 if array[mid] == target: return mid elif array[mid] < target: low = mid + 1 elif array[mid] > target: high = mid - 1 return -1 array = sorted(arr) for i in range(len(array)): index = BinarySearch(array, array[i] * 2) if index != -1 and index != i: return True return False
check-if-n-and-its-double-exist
[ Python ] ✅✅ Simple Python Solution Using Binary Search 🔥✌👍
ASHOK_KUMAR_MEGHVANSHI
3
152
check if n and its double exist
1,346
0.362
Easy
20,260
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2597037/PYTHON-Simple-Clean-approach-beat-95-Easy-T%3A-O(N)
class Solution: def checkIfExist(self, arr: List[int]) -> bool: d = {} for val in arr: if d.get(val*2,0) or d.get(val/2,0): return True d[val] = 1 return False
check-if-n-and-its-double-exist
✅ [PYTHON] Simple, Clean approach beat 95% Easy T: O(N)
girraj_14581
2
275
check if n and its double exist
1,346
0.362
Easy
20,261
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2838923/4-LINES-oror-PYTHON-SOLUTION-oror-USING-SETSoror-40-MSororEASY
class Solution: def checkIfExist(self, arr: List[int]) -> bool: if arr.count(0) > 1: return 1 S = set(arr) - {0} for i in arr: if 2*i in S: return 1 return 0
check-if-n-and-its-double-exist
4 LINES || PYTHON SOLUTION || USING SETS|| 40 MS||EASY
thezealott
1
34
check if n and its double exist
1,346
0.362
Easy
20,262
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2818128/python3-beat-85-5-lines-code
class Solution: def checkIfExist(self, arr: List[int]) -> bool: l = len(arr) for i in range(l-1): if arr[i] * 2 in arr[0:i] + arr[i+1:] or arr[i] / 2 in arr[0:i]+ arr[i+1:]: return True return False
check-if-n-and-its-double-exist
python3, beat 85% ; 5 lines code
dongdong2022
1
55
check if n and its double exist
1,346
0.362
Easy
20,263
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2384444/Python-Faster-Solution-using-Set-and-Bitwise-Ops-oror-Documented
class Solution: def checkIfExist(self, arr: List[int]) -> bool: s = set() # lookup table for n in arr: if n << 1 in s or (n &amp; 1 == 0 and (n >> 1) in s): # Means: 2*n in s or ( n%2 == 0 and n//2 in s) return True s.add(n) return False
check-if-n-and-its-double-exist
[Python] Faster Solution using Set & Bitwise Ops || Documented
Buntynara
1
30
check if n and its double exist
1,346
0.362
Easy
20,264
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1947511/Python3-or-simple
class Solution: def checkIfExist(self, arr: List[int]) -> bool: for i, n in enumerate(arr): if 2*n in arr and arr.index(2*n) != i: return True return False
check-if-n-and-its-double-exist
Python3 | simple
user0270as
1
59
check if n and its double exist
1,346
0.362
Easy
20,265
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1810969/3-Lines-Python-Solution-oror-93-Faster-oror-Memory-less-than-76
class Solution: def checkIfExist(self, arr: List[int]) -> bool: for i in range(len(arr)): if (arr[i]*2 in arr[i+1:]) or (arr[i]/2 in arr[i+1:]): return True return False
check-if-n-and-its-double-exist
3-Lines Python Solution || 93% Faster || Memory less than 76%
Taha-C
1
130
check if n and its double exist
1,346
0.362
Easy
20,266
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1547762/PYTHON%3A-Very-easy-and-fast-solution-(99)-using-hashing(dictionary)
class Solution: def checkIfExist(self, arr: List[int]) -> bool: arrDict ={} for num in arr: if num * 2 in arrDict or num/2 in arrDict: return True if num not in arrDict: arrDict[num] = None return False
check-if-n-and-its-double-exist
PYTHON: Very easy and fast solution (99%) using hashing(dictionary)
shubhamrana
1
236
check if n and its double exist
1,346
0.362
Easy
20,267
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1533282/Python-97%2B-Faster-Solution
class Solution: def checkIfExist(self, arr: List[int]) -> bool: for i in range(len(arr)): if arr[i] == False: try: if arr[i+1] == False: return True except: pass if arr[i] + arr[i] in arr and arr[i] != False: return True else: return False
check-if-n-and-its-double-exist
Python 97%+ Faster Solution
aaffriya
1
171
check if n and its double exist
1,346
0.362
Easy
20,268
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1280883/Python-Easy-Solution-90faster
class Solution: def checkIfExist(self, arr: List[int]) -> bool: for num in arr: if num==0 and arr.count(num)>1: return True elif num!=0 and (num/2 in arr or num*2 in arr): return True return False
check-if-n-and-its-double-exist
[Python] Easy Solution 90%faster
arkumari2000
1
188
check if n and its double exist
1,346
0.362
Easy
20,269
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1269876/Python3-simple-solution-using-single-for-loop
class Solution: def checkIfExist(self, arr: List[int]) -> bool: for i,j in enumerate(arr): if i < len(arr)-1 and j*2 in arr[i+1:] or (j%2==0 and j//2 in arr[i+1:]): return True return False
check-if-n-and-its-double-exist
Python3 simple solution using single for loop
EklavyaJoshi
1
127
check if n and its double exist
1,346
0.362
Easy
20,270
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/930522/Python-Solution-HashMap
class Solution: def checkIfExist(self, arr: List[int]) -> bool: hashset = {v:k for k,v in enumerate(arr)} for a in range(len(arr)): if 2*arr[a] in hashset and hashset[2*arr[a]]!=a: return True return False
check-if-n-and-its-double-exist
Python Solution HashMap
bharatgg
1
127
check if n and its double exist
1,346
0.362
Easy
20,271
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2813986/Easy-Python
class Solution: def checkIfExist(self, arr: List[int]) -> bool: a = [] b = [] for i in arr: if i!=0: a.append(2*i) else: b.append(i) for i in a: if i in arr: return True for j in b: if j in arr and len(b)>1: return True break return False
check-if-n-and-its-double-exist
Easy Python
khanismail_1
0
5
check if n and its double exist
1,346
0.362
Easy
20,272
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2809865/simple-python-beats-90
class Solution: def checkIfExist(self, arr: List[int]) -> bool: arr.sort() for i in range(len(arr)): if arr[i]==0: if arr[i+1]==0: return True continue low=0 high=len(arr) while(low<high): mid=(low+high)//2 if arr[i]*2 == arr[mid]: return True elif arr[mid] > arr[i]*2: high = mid else: low = mid+1 return False
check-if-n-and-its-double-exist
simple python beats 90%
sudharsan1000m
0
3
check if n and its double exist
1,346
0.362
Easy
20,273
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2789591/Easy-Python-Solution-Faster-than-99.53
class Solution: def checkIfExist(self, arr: List[int]) -> bool: hash = set() for i in arr: if i*2 in hash or (i/2 in hash): return True else: hash.add(i) return False
check-if-n-and-its-double-exist
Easy Python Solution Faster than 99.53%
Aayush3014
0
13
check if n and its double exist
1,346
0.362
Easy
20,274
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2711064/Easy-solution-oror-5-liner-oror-Beats-98.36
class Solution: def checkIfExist(self, arr: List[int]) -> bool: if arr.count(0) > 1: return True for i in arr: if i*2 in arr and i != 0: return True return False
check-if-n-and-its-double-exist
Easy solution || 5 liner || Beats 98.36%
MockingJay37
0
5
check if n and its double exist
1,346
0.362
Easy
20,275
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2675983/Easy-Python-Solution-or-Loops-or-O(n)
class Solution: def checkIfExist(self, arr: List[int]) -> bool: arr.sort() for i in range(len(arr)): if arr[i] == 0 and arr.count(0) < 2: continue if arr[i] % 2 == 0: if arr[i] // 2 in arr: return True return False
check-if-n-and-its-double-exist
Easy Python Solution | Loops | O(n)
atharva77
0
3
check if n and its double exist
1,346
0.362
Easy
20,276
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2675386/98-Accepted-or-Easy-to-Understand-or-Python
class Solution(object): def checkIfExist(self, arr): hashT = {} for n in arr: if n not in hashT: hashT[n] = 1 else: hashT[n] += 1 for n in arr: if n == 0: if hashT[n] >= 2: return True else: continue if (n * 2) in hashT: return True return False
check-if-n-and-its-double-exist
98% Accepted | Easy to Understand | Python
its_krish_here
0
23
check if n and its double exist
1,346
0.362
Easy
20,277
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2577971/One-pass-over-array
class Solution: def checkIfExist(self, arr: List[int]) -> bool: s = set() for x in arr: if (2*x in s) or (x % 2 == 0 and (x // 2 in s)): return True s.add(x) return False
check-if-n-and-its-double-exist
One pass over array
semochka
0
32
check if n and its double exist
1,346
0.362
Easy
20,278
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2550848/EASY-PYTHON3-SOLUTION
class Solution: def checkIfExist(self, arr: List[int]) -> bool: ans = False for i in range(len(arr)): for j in range(i,len(arr)): if i==j: continue if (arr[i] == arr[j] * 2) or (arr[j] == arr[i] * 2): ans = True break return ans
check-if-n-and-its-double-exist
✅✔🔥 EASY PYTHON3 SOLUTION 🔥✅✔
rajukommula
0
59
check if n and its double exist
1,346
0.362
Easy
20,279
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2502907/PYTHON-SOLUTION-FASTER-THAN-92
class Solution: def checkIfExist(self, arr: list[int]) -> bool: for i in arr: if i == 0 and arr.count(0) == 1: continue elif i == 0 and arr.count(0) >= 2: return True elif i * 2 in arr: return True return False
check-if-n-and-its-double-exist
PYTHON SOLUTION FASTER THAN 92%
gkarthik923
0
27
check if n and its double exist
1,346
0.362
Easy
20,280
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2473821/easy
class Solution: def checkIfExist(self, arr: List[int]) -> bool: for i in range(len(arr)): pr = arr[0:i]+arr[i+1:len(arr)] if arr[i]/2 in pr: return True return False
check-if-n-and-its-double-exist
easy
rohannayar8
0
19
check if n and its double exist
1,346
0.362
Easy
20,281
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2450017/C%2B%2BPython-O(Nlog(N))-best-Optimized-Approach
class Solution: def checkIfExist(self, arr: List[int]) -> bool: def BinarySearch(array, target): low = 0 high = len(array) - 1 while low <= high: mid = (low + high ) // 2 if array[mid] == target: return mid elif array[mid] < target: low = mid + 1 elif array[mid] > target: high = mid - 1 return -1 array = sorted(arr) for i in range(len(array)): index = BinarySearch(array, array[i] * 2) if index != -1 and index != i: return True return False
check-if-n-and-its-double-exist
C++/Python O(Nlog(N)) best Optimized Approach
arpit3043
0
56
check if n and its double exist
1,346
0.362
Easy
20,282
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2418617/simple-python3-solution-with-93-faster
class Solution: def checkIfExist(self, arr: List[int]) -> bool: idx = 0 while idx < len(arr): if arr[idx] * 2 in (arr[:idx]+arr[idx+1:]): return True else: idx += 1 return False # TC = O(n) # SC = O(1)
check-if-n-and-its-double-exist
simple python3 solution with 93% faster
hwf87
0
12
check if n and its double exist
1,346
0.362
Easy
20,283
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2417996/75-ms-faster-than-70.02-of-Python3
class Solution: def checkIfExist(self, arr: List[int]) -> bool: res = [] for num in arr: if num*2 in res or num/2 in res: return True else: res.append(num) return False
check-if-n-and-its-double-exist
75 ms, faster than 70.02% of Python3
Harshi_Tyagi
0
18
check if n and its double exist
1,346
0.362
Easy
20,284
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2122038/Python-or-Binary-Search-or-SHORT-AND-SWEET
class Solution: def checkIfExist(self, nums: list[int]) -> bool: nums.sort() for i in range(len(nums)): target = nums[i] * 2 if nums[i] > 0 else nums[i] / 2 if (j := bisect_right(a=nums, x=target, lo=i + 1) - 1) != i and nums[j] == target: return True return False
check-if-n-and-its-double-exist
🐍 Python | Binary Search | SHORT AND SWEET
miguel_v
0
87
check if n and its double exist
1,346
0.362
Easy
20,285
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/2104267/To-finish-binary-search-study-plan-O(nlogn)-time-andand-O(1)-space
class Solution: def checkIfExist(self, arr: List[int]) -> bool: arr.sort() length=len(arr) for i in range(length): target=arr[i]*2 m,n=0,length-1 while m<=n: mid=(m+n)>>1 if target==arr[mid] and mid!=i: return True elif target>arr[mid]: m=mid+1 else: n=mid-1 return False
check-if-n-and-its-double-exist
To finish binary search study plan, O(nlogn) time && O(1) space
xsank
0
67
check if n and its double exist
1,346
0.362
Easy
20,286
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1995294/Python-3-Solution-using-sorting-and-binary-search
class Solution: def binary_search(self, nums: List[int], target: int) -> bool: low, high = 0, len(nums) - 1 while low <= high: mid = (low + high) // 2 if nums[mid] == target: return True elif nums[mid] > target: high = mid - 1 else: low = mid + 1 return False def checkIfExist(self, arr: List[int]) -> bool: arr.sort() for i in range(len(arr)): if arr[i] >= 0 and self.binary_search(arr[i+1:], arr[i] * 2): return True if arr[i] < 0 and arr[i] % 2 == 0 and self.binary_search(arr[i+1:], arr[i] // 2): return True return False
check-if-n-and-its-double-exist
Python 3 Solution using sorting and binary search
AprDev2011
0
109
check if n and its double exist
1,346
0.362
Easy
20,287
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1928865/Python3-98-faster-with-explanation
class Solution: def checkIfExist(self, arr: List[int]) -> bool: lmap = {} for x in arr: if 2 * x in lmap or x / 2 in lmap: return True if x not in lmap: lmap[x] = 1 return False
check-if-n-and-its-double-exist
Python3, 98% faster with explanation
cvelazquez322
0
94
check if n and its double exist
1,346
0.362
Easy
20,288
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1927359/Python-Clean-and-Simple!-Multiple-Solutions!
class Solution: def checkIfExist(self, arr): return any(i*2 == j for i,j in permutations(arr, 2))
check-if-n-and-its-double-exist
Python - Clean and Simple! Multiple Solutions!
domthedeveloper
0
71
check if n and its double exist
1,346
0.362
Easy
20,289
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1927359/Python-Clean-and-Simple!-Multiple-Solutions!
class Solution: def checkIfExist(self, arr): find = set() for n in arr: if n in find: return True else: if n % 2 == 0: find.add(n//2) find.add(n*2) return False
check-if-n-and-its-double-exist
Python - Clean and Simple! Multiple Solutions!
domthedeveloper
0
71
check if n and its double exist
1,346
0.362
Easy
20,290
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1927359/Python-Clean-and-Simple!-Multiple-Solutions!
class Solution: def checkIfExist(self, arr): seen = set() for n in arr: if n*2 in seen or n % 2 == 0 and n//2 in seen: return True else: seen.add(n) return False
check-if-n-and-its-double-exist
Python - Clean and Simple! Multiple Solutions!
domthedeveloper
0
71
check if n and its double exist
1,346
0.362
Easy
20,291
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1927359/Python-Clean-and-Simple!-Multiple-Solutions!
class Solution: def checkIfExist(self, arr): s1 = set(arr) s2 = {i*2 for i in s1} # handle the 0 edge case if 0 in s1: z = arr.count(0) if z >= 2: return True s1, s2 = s1 - {0}, s2 - {0} return s1 &amp; s2
check-if-n-and-its-double-exist
Python - Clean and Simple! Multiple Solutions!
domthedeveloper
0
71
check if n and its double exist
1,346
0.362
Easy
20,292
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1906259/Python-Solution-or-HashMap-Based-or-Over-95-Faster-or-Clean-Code
class Solution: def checkIfExist(self, arr: List[int]) -> bool: store = defaultdict(int) for idx,ele in enumerate(arr): if store: if ele % 2 == 0 and ele / 2 in store: return True if ele * 2 in store: return True store[ele] = idx return False
check-if-n-and-its-double-exist
Python Solution | HashMap Based | Over 95% Faster | Clean Code
Gautam_ProMax
0
51
check if n and its double exist
1,346
0.362
Easy
20,293
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1824657/Python-using-dict-O(n)
class Solution: def checkIfExist(self, arr: List[int]) -> bool: cnt_dict = dict() for x in arr: cnt_dict[x] = cnt_dict[x] + 1 if x in cnt_dict else 1 for x in arr: if x * 2 in cnt_dict: if x != 0 or cnt_dict[0] >= 2: # edge case for zero return True return False
check-if-n-and-its-double-exist
Python using dict O(n)
nashvenn
0
83
check if n and its double exist
1,346
0.362
Easy
20,294
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1737763/Python-dollarolution
class Solution: def checkIfExist(self, arr: List[int]) -> bool: if arr.count(0) == 2: return True for i in arr: if i == 0: continue if i/2 in arr or i*2 in arr: return True return False
check-if-n-and-its-double-exist
Python $olution
AakRay
0
125
check if n and its double exist
1,346
0.362
Easy
20,295
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1631015/pyhton3-time-O(n)-space-O(n)
class Solution: def checkIfExist(self, arr: List[int]) -> bool: m=defaultdict(int) for num in arr: #if (num%2==0 and (num//2) in m) or num*2 in m or num in m: if num in m: return True if num%2==0: m[num//2]=1 m[num*2]=1 #print(m) return False
check-if-n-and-its-double-exist
pyhton3 time-O(n) space-O(n)
Rohit_Patil
0
138
check if n and its double exist
1,346
0.362
Easy
20,296
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1578796/Python-fast-with-explanation
class Solution: def checkIfExist(self, arr: List[int]) -> bool: if len(arr) < 2: return False ht = {} for i in range(len(arr)): ht[arr[i]] = i for i in range(len(arr)): num = arr[i] if num * 2 in ht and ht[num*2] != i: return True return False
check-if-n-and-its-double-exist
Python fast with explanation
SleeplessChallenger
0
93
check if n and its double exist
1,346
0.362
Easy
20,297
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1376936/Python3oror-optimised-O(n)oror-using-Hashmap-Dictionaryoror-faster-than-90
class Solution: def checkIfExist(self, arr: List[int]) -> bool: #the rqd double can only be an even number. #so for an even number check at di[number//2] di={} for x in arr: di[x]=di.get(x,0)+1 for k,v in di.items(): if k!=0 and k%2==0 and di.get(k//2,0)>0: return True elif k==0 and di[k]>1: return True return False
check-if-n-and-its-double-exist
Python3|| optimised O(n)|| using Hashmap Dictionary|| faster than 90%
ana_2kacer
0
165
check if n and its double exist
1,346
0.362
Easy
20,298
https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/1340234/python3-simple-solution-for-beginners-99-fast
class Solution: def checkIfExist(self, arr: List[int]) -> bool: for i in range(0,len(arr)): if arr[i]==0: for i in range (i+1,len(arr)): if arr[i]==0: return True else: if (2*arr[i]) in arr: return True if arr[i]/2 in arr: return True return False
check-if-n-and-its-double-exist
python3 simple solution for beginners 99% fast
minato_namikaze
0
98
check if n and its double exist
1,346
0.362
Easy
20,299