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/count-elements-with-strictly-smaller-and-greater-elements/discuss/1711401/Python-O(N)-no-need-to-complicate-simple-things
class Solution: def countElements(self, nums: List[int]) -> int: min_, max_ = min(nums), max(nums) return sum(min_ < n < max_ for n in nums)
count-elements-with-strictly-smaller-and-greater-elements
Python, O(N), no need to complicate simple things
blue_sky5
1
15
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,800
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/1711231/Python3-check-numbers
class Solution: def countElements(self, nums: List[int]) -> int: mn, mx = min(nums), max(nums) return sum(mn < x < mx for x in nums)
count-elements-with-strictly-smaller-and-greater-elements
[Python3] check numbers
ye15
1
22
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,801
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/2823763/Python-3-Solution
class Solution: def countElements(self, nums: List[int]) -> int: if min(nums)==max(nums): return 0 return len(nums) - nums.count(min(nums)) - nums.count(max(nums))
count-elements-with-strictly-smaller-and-greater-elements
Python 3 Solution
mati44
0
1
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,802
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/2779262/Python-3-solution-using-dictionary
class Solution: def countElements(self, nums: List[int]) -> int: counts = Counter(nums) del counts[max(nums)] del counts[min(nums)] return sum(counts.values())
count-elements-with-strictly-smaller-and-greater-elements
Python 3 solution using dictionary
bettend
0
2
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,803
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/2715953/PYTHON-100-EASY-TO-UNDERSTANDSIMPLECLEAN
class Solution: def countElements(self, nums: List[int]) -> int: smallest = nums[0] largest = nums[0] count = 0 for i in nums: if i < smallest: smallest = i elif i > largest: largest = i for j in nums: if j != smallest and j != largest: count += 1 return count
count-elements-with-strictly-smaller-and-greater-elements
🔥PYTHON 100% EASY TO UNDERSTAND/SIMPLE/CLEAN🔥
YuviGill
0
2
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,804
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/2704504/Python-Simple-Python-Solution-Using-Sorting
class Solution: def countElements(self, nums: List[int]) -> int: nums = sorted(nums) result = 0 for index in range(1,len(nums)-1): min_num = min(nums[:index]) max_num = max(nums[index + 1:]) if nums[index] > min_num and nums[index] < max_num: result = result + 1 return result
count-elements-with-strictly-smaller-and-greater-elements
[ Python ] ✅✅ Simple Python Solution Using Sorting 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
0
4
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,805
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/2362473/Count-Elements-With-Strictly-Smaller-and-Greater-Elements
class Solution: def countElements(self, nums: List[int]) -> int: n= len(nums) if n ==1: return 0 for i in range(n): currentMinIndex= i for j in range(i+1,n): if nums[currentMinIndex]>nums[j]: currentMinIndex= j x = nums[currentMinIndex] nums[currentMinIndex]=nums[i] nums[i]= x if nums[0] == nums[-1]: return 0 l = 0 while(l<n-1): if(nums[l]!=nums[l+1]): break l+=1 r = n-1 while(r>0): if(nums[r]!=nums[r-1]) : break r-=1 return (r)-(l+1)
count-elements-with-strictly-smaller-and-greater-elements
Count Elements With Strictly Smaller and Greater Elements
dhananjayaduttmishra
0
7
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,806
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/2235702/Python-simple-oneliner
class Solution: def countElements(self, nums: List[int]) -> int: return len([x for x in nums if x != min(nums) and x != max(nums)])
count-elements-with-strictly-smaller-and-greater-elements
Python simple oneliner
StikS32
0
28
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,807
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/2177626/faster-than-94.75-oror-Memory-Usage%3A-less-than-96.69
class Solution: def countElements(self, nums: List[int]) -> int: m, M = min(nums), max(nums) return sum(1 for i in nums if m<i<M)
count-elements-with-strictly-smaller-and-greater-elements
faster than 94.75% || Memory Usage: less than 96.69%
writemeom
0
32
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,808
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/2161424/Python-or-easy-solution-with-explanation
class Solution: def countElements(self, nums: List[int]) -> int: ''' Sort the list which will take O(nlogn) time and then take out the min and max elements and for each list elements which whether it lies between (minEle,maxEle) exclusive. Total TC: O(nlogn) + O(n) + 1 = O(nlogn). ''' nums.sort() minEle = nums[0] maxEle = nums[-1] count = 0 for i in range(len(nums)): if minEle < nums[i] < maxEle: count += 1 return count
count-elements-with-strictly-smaller-and-greater-elements
Python | easy solution with explanation
__Asrar
0
49
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,809
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/2068558/Python-3-simple-solution-O(n)-Time-O(1)-Space
class Solution: def countElements(self, nums: List[int]) -> int: n = len(nums) max_num = max(nums) min_num = min(nums) count = 0 for num in nums: if num > min_num and num < max_num: count+=1 return count
count-elements-with-strictly-smaller-and-greater-elements
Python 3 simple solution O(n) Time O(1) Space
emerald19
0
36
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,810
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/2028457/Python-One-Liner!
class Solution: def countElements(self, nums): return len([x for x in nums if min(nums)<x<max(nums)])
count-elements-with-strictly-smaller-and-greater-elements
Python - One-Liner!
domthedeveloper
0
46
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,811
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/1941003/Python-dollarolution-(using-min-and-max)
class Solution: def countElements(self, nums: List[int]) -> int: s, g = min(nums), max(nums) count = 0 for i in nums: if i in range(s+1,g): count += 1 return count
count-elements-with-strictly-smaller-and-greater-elements
Python $olution (using min & max)
AakRay
0
30
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,812
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/1926063/count-elements-with-strictly-smaller-and-greater
class Solution: def countElements(self, nums: List[int]) -> int: c=0 for i in nums: if i>min(nums) and i<max(nums): c+=1 return c
count-elements-with-strictly-smaller-and-greater-elements
count elements with strictly smaller and greater
anil5829354
0
20
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,813
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/1873333/Simple-and-Easy-to-Understand-Solution
class Solution: def countElements(self, nums: List[int]) -> int: nums.sort() ans=0 for i in range(1,len(nums)): minimum=min(nums[:i]) #Find minimum element left of currrent element maximum=max(nums[i:]) #Find maximum element right of current element if nums[i]>minimum and nums[i]<maximum: ans+=1 return ans
count-elements-with-strictly-smaller-and-greater-elements
Simple and Easy-to-Understand Solution
imjenit
0
25
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,814
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/1827801/python-simple-solution
class Solution: def countElements(self, nums: List[int]) -> int: max_el = max(nums) min_el = min(nums) return sum(x not in (max_el, min_el) for x in nums)
count-elements-with-strictly-smaller-and-greater-elements
python simple solution
abkc1221
0
26
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,815
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/1739792/python-simple-solution
class Solution: def countElements(self, nums: List[int]) -> int: if(len(set(nums))<3): return 0 nums.sort() i=0 while(i<len(nums)-1): if(nums[i]!=nums[i+1]): j=len(nums)-1 while(j>0): if(nums[j]!=nums[j-1]): return j-i-1 j=j-1 i=i+1
count-elements-with-strictly-smaller-and-greater-elements
python simple solution
Rajashekar_Booreddy
0
45
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,816
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/1720909/One-pass-91-speed
class Solution: def countElements(self, nums: List[int]) -> int: min_n, max_n = inf, -inf min_count = max_count = 0 for n in nums: if n < min_n: min_n = n min_count = 1 elif n == min_n: min_count += 1 if n > max_n: max_n = n max_count = 1 elif n == max_n: max_count += 1 return 0 if min_n == max_n else len(nums) - min_count - max_count
count-elements-with-strictly-smaller-and-greater-elements
One pass, 91% speed
EvgenySH
0
31
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,817
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/1720828/Python3-accepted-solution
class Solution: def countElements(self, nums: List[int]) -> int: num = sorted(list(set(nums)))[1:-1] count = 0 for i in range(len(num)): count += nums.count(num[i]) return count
count-elements-with-strictly-smaller-and-greater-elements
Python3 accepted solution
sreeleetcode19
0
25
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,818
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/1720754/Python-easy-O(n)-time-O(1)-space-solution
class Solution: def countElements(self, nums: List[int]) -> int: minv, maxv = min(nums), max(nums) if minv != maxv: return len(nums) - nums.count(minv) - nums.count(maxv) else: return 0
count-elements-with-strictly-smaller-and-greater-elements
Python easy O(n) time, O(1) space solution
byuns9334
0
19
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,819
https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements/discuss/1711646/Python-sort-the-array
class Solution: def countElements(self, nums: List[int]) -> int: if len(nums) < 2: return 0 # sort the num nums.sort() i = 1 j = len(nums)-2 while i < len(nums) and nums[i] == nums[i-1]: i+=1 while j >= 0 and nums[j] == nums[j+1]: j-=1 return max(0,j-i+1)
count-elements-with-strictly-smaller-and-greater-elements
Python - sort the array
AsifIqbal1997
0
10
count elements with strictly smaller and greater elements
2,148
0.6
Easy
29,820
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/1711329/Two-Pointers
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: return [i for t in zip([p for p in nums if p > 0], [n for n in nums if n < 0]) for i in t]
rearrange-array-elements-by-sign
Two Pointers
votrubac
20
5,200
rearrange array elements by sign
2,149
0.811
Medium
29,821
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/1711302/Python-Simple-Solution
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: ans, positives, negatives = [], [], [] for val in nums: if val >= 0: positives.append(val) else: negatives.append(val) for i in range(len(positives)): ans.append(positives[i]) ans.append(negatives[i]) return ans
rearrange-array-elements-by-sign
Python Simple Solution
anCoderr
3
216
rearrange array elements by sign
2,149
0.811
Medium
29,822
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2346819/Python-Easy-To-Understand-Solution-oror-Brute-force
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: neg = [] pos = [] ans = [] for i in nums: if i<0: neg.append(i) else: pos.append(i) for j in range(len(nums)//2): ans.append(pos[j]) ans.append(neg[j]) return ans
rearrange-array-elements-by-sign
Python Easy To Understand Solution || Brute force
Shivam_Raj_Sharma
2
46
rearrange array elements by sign
2,149
0.811
Medium
29,823
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/1991002/Python3-Runtime%3A-1756ms-64.56-memory%3A-45.2mb-54.13
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: if not nums: return nums newArray = [0] * len(nums) i, j = 0, 1 for num in nums: if num > 0: newArray[i] = num i += 2 else: newArray[j] = num j += 2 return newArray
rearrange-array-elements-by-sign
Python3 Runtime: 1756ms 64.56% memory: 45.2mb 54.13%
arshergon
2
93
rearrange array elements by sign
2,149
0.811
Medium
29,824
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2513838/Python-or-4-Lines-or-Easy-to-read
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: positives = [num for num in nums if num > 0] negatives = [num for num in nums if num < 0] res = zip(positives, negatives) return chain(*res)
rearrange-array-elements-by-sign
Python | 4 Lines | Easy to read
Wartem
1
64
rearrange array elements by sign
2,149
0.811
Medium
29,825
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2128075/Pyhton3-faster-than-82.69-easily-explained.
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: # first we store all positive/negative occurrences in positive &amp; negative lists by list comprehension. # then since the given nums is of even length and there are equal number of positive and negative # integers, we can just run a loop for the length of positive numbers or the length of negative numbers # and add ith index value of Positive and as well as Negative number to nums2 at once # then return nums2 positive = [num for num in nums if num > 0] negative = [num for num in nums if num < 0] nums2 = [] for i in range(len(positive)): nums2.append(positive[i]) nums2.append(negative[i]) return nums2
rearrange-array-elements-by-sign
Pyhton3; faster than 82.69%, easily explained.
shubhamdraj
1
70
rearrange array elements by sign
2,149
0.811
Medium
29,826
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/1742119/Faster-than-96.22-python-submissions.
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: pos = [] neg = [] ans = [] #dividing the nums to postive array and negative array for i in nums: if i > 0: pos.append(i) if i < 0: neg.append(i) a = len(pos) #alternately adding positive and negative numbers from pos and neg for i in range(a): ans.append(pos[i]) ans.append(neg[i]) #returning the final answer return ans
rearrange-array-elements-by-sign
Faster than 96.22% python submissions.
ebrahim007
1
77
rearrange array elements by sign
2,149
0.811
Medium
29,827
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2835856/Python-solution
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: pos=[] neg=[] for i in range(len(nums)): if nums[i]>=0: pos.append(nums[i]) else: neg.append(nums[i]) res=[]; for i in range(len(pos)): res.append(pos[i]) res.append(neg[i]) return res
rearrange-array-elements-by-sign
Python solution
cjatherton19
0
1
rearrange array elements by sign
2,149
0.811
Medium
29,828
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2777440/Simple-Python-Solution
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: i, j = 0, 1 res = [0] * len(nums) for item in nums: if item > 0: res[i] = item i += 2 elif item < 0: res[j] = item j += 2 return res
rearrange-array-elements-by-sign
Simple Python Solution
arefinsalman86
0
2
rearrange array elements by sign
2,149
0.811
Medium
29,829
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2746969/Python3-Solution-faster-than-91.23
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: p, n = [],[] for num in nums: p.append(num) if num > 0 else n.append(num) ans = [] for i in range(len(nums)//2): ans.append(p[i]) ans.append(n[i]) return ans
rearrange-array-elements-by-sign
Python3 Solution faster than 91.23%
sipi09
0
2
rearrange array elements by sign
2,149
0.811
Medium
29,830
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2746109/rearrange-array-elements-by-sign
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: l=[0]*len(nums) p=0 n=1 for i in nums: if i>0: l[p]=i p+=2 else: l[n]=i n+=2 return l #approach1 #store all positive in pos_arr and negatives in neg_arr then as they are in pairs so traversing through len of any of pos or neg array size and keep on appending it in new arraylist # pos_arr = [num for num in nums if num > 0] # [3, 1, 2] # neg_arr = [num for num in nums if num < 0] # [-2, -5, -4] # output = list() # for i in range(len(pos_arr)): # output.append(pos_arr[i]) # output.append(neg_arr[i]) # return output # [3,-2,1,-5,2,-4]
rearrange-array-elements-by-sign
rearrange array elements by sign
shivansh2001sri
0
4
rearrange array elements by sign
2,149
0.811
Medium
29,831
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2734009/simple-python-solution-in-O(n)-time-and-space-complexity.
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: #odd indexes will be 1,3,5,7...... #even indexes will be 0,2,4,6..... n = len(nums) ans = [0]*n odd_index = 1 even_index = 0 for i in nums: if(i<0): ans[odd_index] = i odd_index +=2 else: ans[even_index] =i even_index+=2 return ans
rearrange-array-elements-by-sign
simple python solution in O(n) time and space complexity.
aryankiran928
0
3
rearrange array elements by sign
2,149
0.811
Medium
29,832
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2702323/Python3-Simple-Solution
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: neg = [] pos = [] for num in nums: if num < 0: neg.append(num) else: pos.append(num) i = 0 res = [] while i < len(pos): res.append(pos[i]) res.append(neg[i]) i += 1 return res
rearrange-array-elements-by-sign
Python3 Simple Solution
mediocre-coder
0
8
rearrange array elements by sign
2,149
0.811
Medium
29,833
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2699878/python-easy
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: p=0 n=1 a=[0]*len(nums) for i in nums: if i>0: a[p]=i p+=2 if i<0: a[n]=i n+=2 return a
rearrange-array-elements-by-sign
python easy
2001640100048_2C
0
2
rearrange array elements by sign
2,149
0.811
Medium
29,834
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2668717/Python-Solution-oror-Two-pointer-approach-oror-beats-95
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: positive=[] negative=[] for i in range(len(nums)): if nums[i]>=0: positive.append(nums[i]) else: negative.append(nums[i]) p=len(positive)-1 n=len(negative)-1 for i in range(len(nums)-1,-1,-1): if i%2==0: nums[i]=positive[p] p-=1 else: nums[i]=negative[n] n-=1 return nums
rearrange-array-elements-by-sign
Python Solution || Two-pointer approach || beats 95%
utsa_gupta
0
15
rearrange array elements by sign
2,149
0.811
Medium
29,835
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2659981/SIMPLEST-APPROACH-for-beginners-in-Python
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: q1=[] #positive q2=[] #negative for i in nums: if i>0: q1+=[i] else: q2+=[i] res_nums=[] for i in range(0,int(len(nums)/2)): res_nums+=[q1[i]] res_nums+=[q2[i]] print(res_nums) return res_nums
rearrange-array-elements-by-sign
SIMPLEST APPROACH for beginners in Python
kushagrathisside
0
2
rearrange array elements by sign
2,149
0.811
Medium
29,836
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2630884/SIMPLE-PYTHON3-SOLUTION-Explained-in-a-easy-way-of-approach-irrespective-of-time-and-space
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: arr_pos = [] arr_neg = [] for i in nums: # Dividing and adding positive and negitive elements to two seperate arrays if i<0:arr_neg.append(i) else:arr_pos.append(i) new_arr = [] # Joining positive(FIRST) and negitive(SECOND) values to a new_arr for j in range(len(nums)//2): new_arr.append(arr_pos[j]) new_arr.append(arr_neg[j]) return new_arr
rearrange-array-elements-by-sign
✅✔ SIMPLE PYTHON3 SOLUTION ✅✔ Explained in a easy way of approach irrespective of time and space
rajukommula
0
6
rearrange array elements by sign
2,149
0.811
Medium
29,837
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2600638/Rearrange-Array-Elements-by-Sign-oror-Easy-Solution
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: a = 0 b = 1 new_arr = [] for i in range(len(nums)): if nums[i] > 0: new_arr.insert(a,nums[i]) a += 2 else: new_arr.insert(b,nums[i]) b += 2 return new_arr
rearrange-array-elements-by-sign
Rearrange Array Elements by Sign || Easy Solution
PravinBorate
0
15
rearrange array elements by sign
2,149
0.811
Medium
29,838
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2539739/Python-or-Looping-or-O(n)-Time-or-O(n)-Space
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: n = len(nums) ans = [] even, odd = 0, 0 for i in range(n): if i%2==0: while even<n: if nums[even]>0: break even+=1 ans.append(nums[even]) even+=1 else: while odd<n: if nums[odd]<0: break odd+=1 ans.append(nums[odd]) odd+=1 return ans
rearrange-array-elements-by-sign
Python | Looping | O(n) Time | O(n) Space
coolakash10
0
16
rearrange array elements by sign
2,149
0.811
Medium
29,839
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2513551/Easy-Python-solution-using-Arrays
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: pos=[] neg=[] ans=[] for i in nums: if i<0: neg.append(i) else: pos.append(i) for j in range(0,len(pos)): ans.append(pos[j]) ans.append(neg[j]) return ans
rearrange-array-elements-by-sign
Easy Python solution using Arrays
keertika27
0
31
rearrange array elements by sign
2,149
0.811
Medium
29,840
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2499397/2149.Rearrange-Array-Elements-by-Sign
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: ans=[] p=[] n=[] for i in nums: if i>0: p.append(i) else: n.append(i) for i in range(len(p)): ans.append(p[i]) ans.append(n[i]) return ans``
rearrange-array-elements-by-sign
2149.Rearrange Array Elements by Sign
shagun_pandey
0
18
rearrange array elements by sign
2,149
0.811
Medium
29,841
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2410913/Simple-python-3-code-with-explanation
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: #Create a new list to store the positive elements pos = [] #Create a new list to store the negative elements neg = [] #Create a new list to store the result res = [] #iterate over the elements in nums for i in nums: #if the value is greater than 0 if i > 0: # add that element in positive list-->pos pos.append(i) else: #if not add that element in negative list--> neg neg.append(i) #iterate over the len of nums because length of nums and length of res are same for j in range(len(nums)): #if j is less than the length of pos-->list if j < len(pos): #then add jth element of pos in res res.append(pos[j]) #if j is less than the length of neg-->list if j < len(neg): #then add jth element of neg in res res.append(neg[j]) #after adding all elements return the res list return res
rearrange-array-elements-by-sign
Simple python 3 code with explanation
thomanani
0
20
rearrange array elements by sign
2,149
0.811
Medium
29,842
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2327380/Python-or-Easy-and-Simple-solution-or-two-pointer-or-O(n)
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: pos, neg = 0, 1 ans = [None]*len(nums) for n in nums: if n > 0: ans[pos] = n pos += 2 else: ans[neg] = n neg += 2 return ans
rearrange-array-elements-by-sign
Python | Easy & Simple solution | two pointer | O(n)
desalichka
0
63
rearrange array elements by sign
2,149
0.811
Medium
29,843
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2298770/Python3-easy-to-understand
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: p=[] n=[] for i in range(len(nums)): if nums[i]>0: p.append(nums[i]) else: n.append(nums[i]) ans=[] for i in range(len(nums)//2): ans.append(p[i]) ans.append(n[i]) return ans
rearrange-array-elements-by-sign
Python3 easy to understand
morrismoppp
0
18
rearrange array elements by sign
2,149
0.811
Medium
29,844
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2284561/Python3-oror-Easy-to-Understand-oror-Two-Arrays-oror-O(n)
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: # Take two arrays, positive and negative positive, negative = [], [] # Add positive numbers in positive array and negative numbers in negative array for i in nums: if i < 0: negative.append(i) else: positive.append(i) # Add alternate numbers in the nums array using for loop incremented by 2 for i in range(0, len(nums) - 1, 2): nums[i] = positive[i // 2] nums[i + 1] = negative[i // 2] return nums
rearrange-array-elements-by-sign
Python3 || Easy to Understand || Two Arrays || O(n)
sanjaycp
0
18
rearrange array elements by sign
2,149
0.811
Medium
29,845
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2217823/Python-solution-for-beginners-by-beginner.
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: pos = [] neg = [] for i in nums: if i > 0 : pos.append(i) if i < 0: neg.append(i) ans = [] for i in range(len(nums)//2): ans.append(pos[i]) ans.append(neg[i]) return ans
rearrange-array-elements-by-sign
Python solution for beginners by beginner.
EbrahimMG
0
33
rearrange array elements by sign
2,149
0.811
Medium
29,846
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2157621/Python-Simple-Easy-to-Understand-Solution-with-O(n)%3ATime-Complexity
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: p=[] n=[] for i in nums: if i<0: n.append(i) else: p.append(i) return [j for i in list(zip(p,n)) for j in i]
rearrange-array-elements-by-sign
Python Simple Easy to Understand Solution with O(n):Time Complexity
Kunalbmd
0
43
rearrange array elements by sign
2,149
0.811
Medium
29,847
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/2122151/Python3-Solution-with-using-two-pointers
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: pptr, nptr = 0, 0 res = [] while pptr < len(nums) or nptr < len(nums): while pptr < len(nums) and nums[pptr] < 0: pptr += 1 if pptr < len(nums): res.append(nums[pptr]) while nptr < len(nums) and nums[nptr] > 0: nptr += 1 if nptr < len(nums): res.append(nums[nptr]) pptr += 1 nptr += 1 return res
rearrange-array-elements-by-sign
[Python3] Solution with using two-pointers
maosipov11
0
38
rearrange array elements by sign
2,149
0.811
Medium
29,848
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/1867117/Python-solution-good-for-beginners
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: pos = [x for x in nums if x > 0] neg = [y for y in nums if y < 0] res = [] for i in range(len(pos)): res.append(pos[i]) res.append(neg[i]) return res
rearrange-array-elements-by-sign
Python solution good for beginners
alishak1999
0
51
rearrange array elements by sign
2,149
0.811
Medium
29,849
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/1790035/Python3-first-draft-ugly-and-slow.
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: def getPositives(): res = [] for num in nums: if num > 0: res.append(num) return res def getNegatives(): res = [] for num in nums: if num < 0: res.append(num) return res def go(pos,neg): res = [] p = 0 n = 0 sign = "p" done = 0 while done < 1: if sign == "p" and p < len(pos): res.append(pos[p]) sign = "n" p += 1 elif sign == "n" and n < len(neg): res.append(neg[n]) sign = "p" n += 1 else: done = 1 return res pos = getPositives() neg = getNegatives() answer = go(pos,neg) return answer
rearrange-array-elements-by-sign
Python3 first draft, ugly and slow.
user5571e
0
40
rearrange array elements by sign
2,149
0.811
Medium
29,850
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/1749151/Python3-RunTime%3A-2603ms-27.69-Memory%3A-44.7mb-80.88
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: l1, l2 = list(), list() res = [0] * len(nums) for i in nums: if i > 0: l1.append(i) else: l2.append(i) neg_next_prt = 1 pos_next_prt = 0 for i in range(len(l1)): # l1.insert(next_prt, l2[i]) #it will failed for gigantic inputs # next_prt += 2 # thats why I created a new list[0] to add them up. res[pos_next_prt] = l1[i] res[neg_next_prt] = l2[i] pos_next_prt += 2 neg_next_prt += 2 return res
rearrange-array-elements-by-sign
Python3 RunTime: 2603ms 27.69% Memory: 44.7mb 80.88%
arshergon
0
42
rearrange array elements by sign
2,149
0.811
Medium
29,851
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/1735612/2250-ms-faster-than-57.98-of-Python3-or-Simple-python-solution
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: arr = [0] * len(nums) x,y = 0,1 for i in range(len(nums)): if nums[i] > 0: arr[x] = nums[i] x += 2 else: arr[y] = nums[i] y += 2 return (arr)
rearrange-array-elements-by-sign
2250 ms, faster than 57.98% of Python3 | Simple python solution
Coding_Tan3
0
29
rearrange array elements by sign
2,149
0.811
Medium
29,852
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/1729436/O(n)-space-and-time-easy-solution
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: new_nums,odd,even=[None]*len(nums),1,0 for num in nums: if num>=0: new_nums[even]=num even+=2 else: new_nums[odd]=num odd+=2 return new_nums ``` Alternate Solution: ``` class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: positive_nums,negative_nums=[],[] i,j=0,0 for num in nums: if num>=0: positive_nums.append(num) else: negative_nums.append(num) while j < len(nums): nums[j], nums[j+1]=positive_nums[i], negative_nums[i] i+=1 j+=2 return nums
rearrange-array-elements-by-sign
O(n) space and time easy solution
shandilayasujay
0
67
rearrange array elements by sign
2,149
0.811
Medium
29,853
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/1724954/Python-3-easy-two-pointer-solution
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: res = [0] * len(nums) pos_i, neg_i = 0, 1 for num in nums: if num > 0: res[pos_i] = num pos_i += 2 else: res[neg_i] = num neg_i += 2 return res
rearrange-array-elements-by-sign
Python 3 easy two-pointer solution
dereky4
0
88
rearrange array elements by sign
2,149
0.811
Medium
29,854
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/1720603/Python3-accepted-solution
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: pos = [] neg = [] ans = [] for i in range(len(nums)): if(nums[i]<0): neg.append(nums[i]) else: pos.append(nums[i]) for i in range(len(pos)): ans.append(pos[i]) ans.append(neg[i]) return ans
rearrange-array-elements-by-sign
Python3 accepted solution
sreeleetcode19
0
33
rearrange array elements by sign
2,149
0.811
Medium
29,855
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/1720376/Python-O(N)-Easy-Solution
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: pos = [] neg = [] for num in nums: if num > 0: pos.append(num) else: neg.append(num) for i in range(0,len(nums),2): nums[i] = pos[i//2] nums[i+1] = neg[i//2] return nums
rearrange-array-elements-by-sign
Python O(N) Easy Solution
sarahwu0823
0
43
rearrange array elements by sign
2,149
0.811
Medium
29,856
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/1716672/Python-4-lines-(list-slicing)
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: pos = [n for n in nums if n > 0] neg = [n for n in nums if n < 0] nums[::2], nums[1::2] = pos, neg return nums
rearrange-array-elements-by-sign
Python 4 lines (list slicing)
SmittyWerbenjagermanjensen
0
43
rearrange array elements by sign
2,149
0.811
Medium
29,857
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/1711664/Easy-python-solution
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: n,p = [], [] for item in nums: if item > 0 : p.append(item) else: n.append(item) res = [] for i in range(0,len(p)): res.append(p[i]) res.append(n[i]) return res ```
rearrange-array-elements-by-sign
Easy python solution
shakilbabu
0
23
rearrange array elements by sign
2,149
0.811
Medium
29,858
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/1711643/Splitting-array
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: # maintain array for both positive and negative numbers p = [x for x in nums if x>0] n = [x for x in nums if x<0] ans = [] # add first postive and then negative element in final ans for a,b in zip(p,n): ans.append(a) ans.append(b) return ans
rearrange-array-elements-by-sign
Splitting array
AsifIqbal1997
0
12
rearrange array elements by sign
2,149
0.811
Medium
29,859
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/1711246/Python3-simulation
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: pos, neg = [], [] for x in nums: if x > 0: pos.append(x) else: neg.append(x) ans = [] for p, n in zip(pos, neg): ans.extend([p, n]) return ans
rearrange-array-elements-by-sign
[Python3] simulation
ye15
0
33
rearrange array elements by sign
2,149
0.811
Medium
29,860
https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/1711484/Easy-Python-Solution
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: a=[0]*len(nums) p=0 n=1 for i in nums: if i<0: a[n]=i n+=2 else: a[p]=i p+=2 return a
rearrange-array-elements-by-sign
Easy Python Solution
Sneh17029
-1
50
rearrange array elements by sign
2,149
0.811
Medium
29,861
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/1711316/Counter
class Solution: def findLonely(self, nums: List[int]) -> List[int]: m = Counter(nums) return [n for n in nums if m[n] == 1 and m[n - 1] + m[n + 1] == 0]
find-all-lonely-numbers-in-the-array
Counter
votrubac
43
3,400
find all lonely numbers in the array
2,150
0.608
Medium
29,862
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/1830420/Python-Dictionary
class Solution: def findLonely(self, nums: List[int]) -> List[int]: dict1=dict() l=[] for i in nums: if(i in dict1.keys()): dict1[i]=-1 else: dict1[i]=1 dict1[i-1]=-1 dict1[i+1]=-1 for i in nums: if(dict1[i]==1): l.append(i) return l
find-all-lonely-numbers-in-the-array
Python Dictionary
vedank98
2
76
find all lonely numbers in the array
2,150
0.608
Medium
29,863
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/1716024/C%2B%2B-and-Python-or-Easy-to-understand-or-O(n)-or-Simple-Clean-Explained
class Solution: def findLonely(self, nums: List[int]) -> List[int]: freq = dict() result = [] for n in nums: freq[n] = freq.get(n, 0) + 1 for n in nums: if freq.get(n - 1, 0) == 0 and freq.get(n, 0) == 1 and freq.get(n + 1, 0) == 0: result.append(n) return result
find-all-lonely-numbers-in-the-array
C++ & Python | Easy to understand | O(n) | Simple, Clean, Explained
indujashankar
2
175
find all lonely numbers in the array
2,150
0.608
Medium
29,864
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/1711278/Python-Solution-Using-Sorting-and-Counter
class Solution: def findLonely(self, nums: List[int]) -> List[int]: ans = [] freq_count = Counter(nums) nums.sort() n = len(nums) for i in range(n): val = nums[i] if freq_count[val] == 1 and (i == 0 or nums[i-1] != val-1) and (i == n-1 or nums[i+1] != val+1): ans.append(val) return ans
find-all-lonely-numbers-in-the-array
Python Solution Using Sorting & Counter
anCoderr
2
135
find all lonely numbers in the array
2,150
0.608
Medium
29,865
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/1724491/Python-Easy-to-undestand-hash-map
class Solution: def findLonely(self, nums: List[int]) -> List[int]: if not nums: return [] count = {} for num in nums: if num in count: count[num] += 1 else: count[num] = 1 result = [] for num in nums: if not count.get(num-1) and not count.get(num+1) and count.get(num, 0) < 2: result.append(num) return result
find-all-lonely-numbers-in-the-array
[Python] Easy to undestand hash map
dlog
1
112
find all lonely numbers in the array
2,150
0.608
Medium
29,866
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/2837347/Python3-easy-to-understand
class Solution: def findLonely(self, nums: List[int]) -> List[int]: res = [] d = Counter(nums) for num in nums: if d[num] == 1 and (num - 1) not in d and (num + 1) not in d: res.append(num) return res
find-all-lonely-numbers-in-the-array
Python3 - easy to understand
mediocre-coder
0
2
find all lonely numbers in the array
2,150
0.608
Medium
29,867
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/2799779/Python-O(n)-solution-Accepted
class Solution: def findLonely(self, nums: List[int]) -> List[int]: ans = [] cnt = collections.Counter(nums) for i in range(len(nums)): if cnt[nums[i] + 1] > 0 or cnt[nums[i] - 1] > 0: continue if cnt[nums[i]] == 1: ans.append(nums[i]) return ans
find-all-lonely-numbers-in-the-array
Python O(n) solution [Accepted]
lllchak
0
2
find all lonely numbers in the array
2,150
0.608
Medium
29,868
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/2730179/Python3-Solution-with-using-counting
class Solution: def findLonely(self, nums: List[int]) -> List[int]: c = collections.Counter(nums) res = [] for key in c: if c[key] == 1 and c[key - 1] == 0 and c[key + 1] == 0: res.append(key) return res
find-all-lonely-numbers-in-the-array
[Python3] Solution with using counting
maosipov11
0
3
find all lonely numbers in the array
2,150
0.608
Medium
29,869
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/2669794/Easy-Python-Solution-Using-Dictionary
class Solution: def findLonely(self, arr: List[int]) -> List[int]: dic={} lst=[] for i in arr: if i not in dic: dic[i]=1 else: dic[i]+=1 for k,v in dic.items(): if v==1 and k-1 not in dic and k+1 not in dic: lst.append(k) return lst
find-all-lonely-numbers-in-the-array
Easy Python Solution Using Dictionary
ankitr8055
0
3
find all lonely numbers in the array
2,150
0.608
Medium
29,870
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/2528015/easy-python-solution
class Solution: def findLonely(self, nums: List[int]) -> List[int]: if len(nums) == 1 : return nums else : output = [] nums.sort() for i in range(len(nums)) : if i == len(nums) - 1 : if nums[i] != nums[i-1] : if nums[i] - nums[i-1] != 1 : output.append(nums[i]) elif (nums[i] != nums[i-1]) and (nums[i] != nums[i+1]) : if ((nums[i] - nums[i-1]) != 1) and (nums[i+1] - nums[i] != 1) : output.append(nums[i]) return output
find-all-lonely-numbers-in-the-array
easy python solution
sghorai
0
40
find all lonely numbers in the array
2,150
0.608
Medium
29,871
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/2514016/Short-python-solution
class Solution: def findLonely(self, nums: List[int]) -> List[int]: c = Counter(nums) res = [] for k, v in c.items(): if v == 1 and not c[k-1] and not c[k+1]: res.append(k) return res
find-all-lonely-numbers-in-the-array
Short python solution
yhc22593
0
30
find all lonely numbers in the array
2,150
0.608
Medium
29,872
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/2163720/Python-or-Easy-and-clean-solution-using-hashmap-with-comments
class Solution: def findLonely(self, nums: List[int]) -> List[int]: d = {} res = [] # creating a hash table num as key and it's frequency as value for n in nums: d[n] = 1 + d.get(n,0) # now checking for any num if its occurence is not more than one and (num-1) or (num+1) do not exist for i in nums: if d[i] == 1 and (i-1) not in d and (i+1) not in d: res.append(i) return res
find-all-lonely-numbers-in-the-array
Python | Easy and clean solution using hashmap with comments
__Asrar
0
54
find all lonely numbers in the array
2,150
0.608
Medium
29,873
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/2088780/Python-2-Lines-or-HASHMAP
class Solution: def findLonely(self, nums: List[int]) -> List[int]: hm = Counter(nums) return {i for i in nums if (not i-1 in hm) and (not i+1 in hm) and not (hm[i]) > 1}
find-all-lonely-numbers-in-the-array
Python 2 Lines | HASHMAP
Nk0311
0
27
find all lonely numbers in the array
2,150
0.608
Medium
29,874
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/1727846/Does-anyone-know-why-this-is-getting-runtime-error
class Solution: def findLonely(self, nums: List[int]) -> List[int]: return [digit for digit in nums if not(digit - 1 in nums or digit + 1 in nums) and nums.count(digit) == 1]
find-all-lonely-numbers-in-the-array
Does anyone know why this is getting runtime error
0xa48rx394r83e9
0
14
find all lonely numbers in the array
2,150
0.608
Medium
29,875
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/1720666/Python3-accepted-solution
class Solution: def findLonely(self, nums: List[int]) -> List[int]: from collections import defaultdict ans = [] collect = defaultdict(int) for i in range(len(nums)): collect[nums[i]] += 1 for i in range(len(nums)): if(nums[i]==0 or (nums[i]>0 and collect[nums[i]-1]==0)): if(collect[nums[i]]==1 and collect[nums[i]+1]==0): ans.append(nums[i]) return ans
find-all-lonely-numbers-in-the-array
Python3 accepted solution
sreeleetcode19
0
25
find all lonely numbers in the array
2,150
0.608
Medium
29,876
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/1717005/Python-or-Time-Complexity%3A-O(N*logN)-Space-Complexity%3A-O(1)-or-beats-99-in-memory-usage
class Solution: def findLonely(self, nums: List[int]) -> List[int]: answer = [] nums.sort() n = len(nums) if len(nums) == 1: return nums for i in range(n): if i == 0: if nums[i] != nums[i+1] and nums[i] != nums[i+1] -1: answer.append(nums[i]) elif i == n-1: if nums[i] != nums[i-1] and nums[i] != nums[i-1] +1: answer.append(nums[i]) elif 0 < i < n-1 and nums[i] == nums[i-1]: continue else: if nums[i] != nums[i+1] -1 and nums[i] != nums[i-1] +1 and nums[i] != nums[i-1] and nums[i] != nums[i+1]: answer.append(nums[i]) return answer
find-all-lonely-numbers-in-the-array
Python | Time Complexity: O(N*logN) Space Complexity: O(1) | beats 99% in memory usage
Mikey98
0
31
find all lonely numbers in the array
2,150
0.608
Medium
29,877
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/1711637/Python-using-dict-O(n)-time
class Solution: def findLonely(self, nums: List[int]) -> List[int]: # dict to store all the element m = defaultdict(int) for x in nums: m[x] += 1 ans = [] for k, v in m.items(): # check if its lonely if v == 1 and k-1 not in m and k+1 not in m: ans.append(k) return ans
find-all-lonely-numbers-in-the-array
Python using dict O(n) time
AsifIqbal1997
0
12
find all lonely numbers in the array
2,150
0.608
Medium
29,878
https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/1833214/Python-easy-to-read-and-understand-or-hashmap
class Solution: def findLonely(self, nums: List[int]) -> List[int]: d = {} for num in nums: d[num] = d.get(num, 0) + 1 ans = [] for num in nums: if d[num] == 1 and (num-1) not in d and (num+1) not in d: ans.append(num) return ans
find-all-lonely-numbers-in-the-array
Python easy to read and understand | hashmap
sanial2001
-1
32
find all lonely numbers in the array
2,150
0.608
Medium
29,879
https://leetcode.com/problems/maximum-good-people-based-on-statements/discuss/1711677/Python-3-or-Itertools-and-Bitmask-or-Explanation
class Solution: def maximumGood(self, statements: List[List[int]]) -> int: ans, n = 0, len(statements) for person in itertools.product([0, 1], repeat=n): # use itertools to create a list only contains 0 or 1 valid = True # initially, we think the `person` list is valid for i in range(n): if not person[i]: continue # only `good` person's statement can lead to a contradiction, we don't care what `bad` person says for j in range(n): if statements[i][j] == 2: continue # ignore is no statement was made if statements[i][j] != person[j]: # if there is a contradiction, then valid = False valid = False break # optimization: break the loop when not valid if not valid: # optimization: break the loop when not valid break if valid: ans = max(ans, sum(person)) # count sum only when valid == True return ans
maximum-good-people-based-on-statements
Python 3 | Itertools & Bitmask | Explanation
idontknoooo
2
126
maximum good people based on statements
2,151
0.486
Hard
29,880
https://leetcode.com/problems/maximum-good-people-based-on-statements/discuss/1711677/Python-3-or-Itertools-and-Bitmask-or-Explanation
class Solution: def maximumGood(self, statements: List[List[int]]) -> int: ans, n = 0, len(statements) for mask in range(1 << n): # 2**n possibilities valid = True for i in range(n): if not (mask >> i) &amp; 1: continue # check if the `i`th person is a `good` person for j in range(n): if statements[i][j] == 2: continue elif statements[i][j] != (mask >> j) &amp; 1:# check if `i`th person's statement about `j` matches what `mask` says valid = False break # optimization: break the loop when not valid if not valid: break # optimization: break the loop when not valid if valid: ans = max(ans, bin(mask).count('1')) # count `1` in mask, take the largest return ans
maximum-good-people-based-on-statements
Python 3 | Itertools & Bitmask | Explanation
idontknoooo
2
126
maximum good people based on statements
2,151
0.486
Hard
29,881
https://leetcode.com/problems/maximum-good-people-based-on-statements/discuss/1726746/Backtracking-explanation
class Solution: def maximumGood(self, statements: List[List[int]]) -> int: n = len(statements) self.ans = 0 # good candidate set for O(1) lookup good = set() def backtracking(idx, count): if idx == n: # found a potential solution self.ans = max(self.ans, count) return # assume idx is good good.add(idx) for i in range(idx): # check for any contradictions with previous assumptions if (statements[idx][i] == 1 and i not in good) or (statements[idx][i] == 0 and i in good) or (i in good and statements[i][idx] == 0): break else: # if no contradiction found then we can assume idx is good and continue search backtracking(idx + 1, count + 1) good.remove(idx) # assume idx is bad for i in range(idx): # check for any contradictions with previous candidates thought to be good if (i in good and statements[i][idx] == 1): # contradiction so prune current branch return backtracking(idx + 1, count) backtracking(0, 0) return self.ans
maximum-good-people-based-on-statements
Backtracking explanation
captainspongebob1
1
155
maximum good people based on statements
2,151
0.486
Hard
29,882
https://leetcode.com/problems/maximum-good-people-based-on-statements/discuss/1714340/Python3-check-all-candidates
class Solution: def maximumGood(self, statements: List[List[int]]) -> int: n = len(statements) ans = 0 for k in range(n, -1, -1): for good in combinations(list(range(n)), k): cand = True for i in good: if cand: for j in range(n): if i != j and (statements[i][j] == 0 and j in good or statements[i][j] == 1 and j not in good): cand = False break if cand: return k
maximum-good-people-based-on-statements
[Python3] check all candidates
ye15
1
58
maximum good people based on statements
2,151
0.486
Hard
29,883
https://leetcode.com/problems/maximum-good-people-based-on-statements/discuss/1713827/Python-O(N-*-(2-N))-Bitmask-pre-processing-and-bit-manipulation-beats-100
class Solution: def maximumGood(self, statements: List[List[int]]) -> int: res, n, good, bad = 0, len(statements), defaultdict(int), defaultdict(int) for i in range(n): for j in range(n): if statements[i][j] == 1: good[i] = good[i] | (1 << j) elif statements[i][j] == 0: bad[i] = bad[i] | (1 << j) for mask in range(1 << n): good_count = 0 for a in range(n): if (1 << a) &amp; mask: good_count += 1 if not(mask &amp; good[a] == good[a] and mask &amp; bad[a] ^ bad[a] == bad[a]): break else: res = max(res, good_count) return res
maximum-good-people-based-on-statements
Python O(N * (2 ^ N)) Bitmask, pre-processing and bit manipulation, beats 100%
WRWRW
0
32
maximum good people based on statements
2,151
0.486
Hard
29,884
https://leetcode.com/problems/maximum-good-people-based-on-statements/discuss/1712051/Another-brute-force-python-solution
class Solution: def maximumGood(self, s: List[List[int]]) -> int: """ brute force: try if all N people are good, if not, try N-1 etc. """ N = len(s) gooddict = defaultdict(set) baddict = defaultdict(set) for r in range(N): for c in range(N): if s[r][c]==1: gooddict[r].add(c) elif s[r][c] == 0: baddict[r].add(c) def valid(good): bad = set(x for x in range(N) if x not in good) for g in good: for gg in gooddict[g]: if gg not in good: return False for gb in baddict[g]: if gb not in bad: return False return True for t in range(len(s),0,-1): choices = set(itertools.combinations(range(N), t)) for c in choices: if valid(c): return t return 0
maximum-good-people-based-on-statements
Another brute-force python solution
202022021
0
32
maximum good people based on statements
2,151
0.486
Hard
29,885
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/2531609/Python-solution-simple-faster-than-80-less-memory-than-99
class Solution: def findFinalValue(self, nums: List[int], original: int) -> int: while original in nums: original *= 2 return original
keep-multiplying-found-values-by-two
Python solution simple faster than 80% less memory than 99%
Theo704
2
62
keep multiplying found values by two
2,154
0.731
Easy
29,886
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/2479533/Python-Solution-Easy
class Solution: def findFinalValue(self, n: List[int], o: int) -> int: n = sorted(n) for i in range(len(n)) : if o == n[i]: o *= 2 return o
keep-multiplying-found-values-by-two
Python Solution Easy
SouravSingh49
1
68
keep multiplying found values by two
2,154
0.731
Easy
29,887
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/2479533/Python-Solution-Easy
class Solution: def findFinalValue(self, n: List[int], o: int) -> int: while o in n: o *= 2 return o
keep-multiplying-found-values-by-two
Python Solution Easy
SouravSingh49
1
68
keep multiplying found values by two
2,154
0.731
Easy
29,888
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1846100/Easy-oror-Faster-92oror-Less-memory-usage-than-92
class Solution: def findFinalValue(self, nums: List[int], original: int) -> int: while original in nums: original *= 2 return original
keep-multiplying-found-values-by-two
✅ Easy || Faster 92%|| Less memory usage than 92%
fazliddindehkanoff
1
111
keep multiplying found values by two
2,154
0.731
Easy
29,889
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1769116/Using-set-O(n)-solution
class Solution(object): def findFinalValue(self, nums, original): s = set(nums) while original in s: original *=2 return original
keep-multiplying-found-values-by-two
Using set O(n) solution
user4578H
1
156
keep multiplying found values by two
2,154
0.731
Easy
29,890
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1744758/Efficient-python-solution-with-explanation
class Solution: def findFinalValue(self, nums: List[int], original: int) -> int: flag = True while flag: for i in range(len(nums)): if nums[i] == original: original *= 2 break else: return original
keep-multiplying-found-values-by-two
Efficient python solution with explanation
dporwal985
1
38
keep multiplying found values by two
2,154
0.731
Easy
29,891
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/1732701/1-liner-Python-JavaScript
class Solution: def findFinalValue(self, nums: List[int], og: int) -> int: return og if og not in nums else self.findFinalValue(nums, 2 * og)
keep-multiplying-found-values-by-two
1 liner Python / JavaScript
SmittyWerbenjagermanjensen
1
129
keep multiplying found values by two
2,154
0.731
Easy
29,892
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/2831143/Two-line-python-solution
class Solution: def findFinalValue(self, nums: List[int], original: int) -> int: if original in nums: return self.findFinalValue(nums,original * 2) return original
keep-multiplying-found-values-by-two
Two line python solution
lornedot
0
2
keep multiplying found values by two
2,154
0.731
Easy
29,893
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/2815673/Python-or-Easy-or-Beginner-Friendly
class Solution: def findFinalValue(self, nums: List[int], original: int) -> int: while original in nums: original *= 2 return original
keep-multiplying-found-values-by-two
Python | Easy | Beginner Friendly
pawangupta
0
1
keep multiplying found values by two
2,154
0.731
Easy
29,894
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/2743217/easiest-way
class Solution: def findFinalValue(self, nums: List[int], original: int) -> int: while original in nums: original=original*2 else: return original
keep-multiplying-found-values-by-two
easiest way
sindhu_300
0
3
keep multiplying found values by two
2,154
0.731
Easy
29,895
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/2640392/Super-easy-python-solution
class Solution: def findFinalValue(self, nums: List[int], original: int) -> int: nums.sort() for n in nums: if original == n : original *= 2 return original
keep-multiplying-found-values-by-two
Super easy python solution
pandish
0
9
keep multiplying found values by two
2,154
0.731
Easy
29,896
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/2609571/Python-Solution-with-a-Set-and-While-Loop
class Solution: def findFinalValue(self, nums: List[int], original: int) -> int: numSet = set(nums) while original in numSet: original *= 2 return original
keep-multiplying-found-values-by-two
Python Solution with a Set and While Loop
kcstar
0
19
keep multiplying found values by two
2,154
0.731
Easy
29,897
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/2537643/python-solution-O(n)-using-set
class Solution: def findFinalValue(self, nums: List[int], original: int) -> int: set_nums = set(nums) for num in nums: if original not in set_nums: return original original = original * 2 return original
keep-multiplying-found-values-by-two
python solution O(n) using set
samanehghafouri
0
10
keep multiplying found values by two
2,154
0.731
Easy
29,898
https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/2537623/python-solution-97.68-faster!-using-binary-search
class Solution: def binary_search(self, nums: List[int], target, low, high) -> int: if high >= low: mid = (low + high) // 2 if nums[mid] == target: return True elif nums[mid] > target: return self.binary_search(nums, target, low, mid - 1) else: return self.binary_search(nums, target, mid + 1, high) else: return False def findFinalValue(self, nums: List[int], original: int) -> int: nums.sort() for _ in nums: if original > nums[-1]: return original else: if self.binary_search(nums, original, 0, len(nums)) is False: return original else: original = original * 2 return original
keep-multiplying-found-values-by-two
python solution 97.68% faster! using binary search
samanehghafouri
0
14
keep multiplying found values by two
2,154
0.731
Easy
29,899