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/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2848431/Easiest-solution-in-Python
class Solution: def subtractProductAndSum(self, n: int) -> int: su=0 pro=1 while(n>0): a=n%10 n=int(n/10) su=su+a pro=pro*a return pro-su
subtract-the-product-and-sum-of-digits-of-an-integer
Easiest solution in Python
ArijitDeveloper
0
1
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,000
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2847130/python-solution-time-O(logn)-space-O(1)
class Solution: def subtractProductAndSum(self, n: int) -> int: sum_val=0 pro_val=1 while(n!=0): num=n%10 sum_val+=num pro_val*=num n//=10 return pro_val-sum_val
subtract-the-product-and-sum-of-digits-of-an-integer
python solution time O(logn) space O(1)
sintin1310
0
1
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,001
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2846338/97-Python-(list-comprehension-%2B-reduce)
class Solution: def subtractProductAndSum(self, n: int) -> int: S = [int(x) for x in str(n)] return reduce(lambda a, b: a * b, S) - reduce(lambda a, b: a + b, S)
subtract-the-product-and-sum-of-digits-of-an-integer
97% Python (list comprehension + reduce) ✔
amarildoaliaj
0
1
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,002
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2843572/Python-Solution-faster-than-90-of-solutions
class Solution: def subtractProductAndSum(self, n: int): product = 1 total = 0 arr = [] arr = [int(i) for i in str(n)] for i in range(len(arr)): total += arr[i] product = product * arr[i] return product - total
subtract-the-product-and-sum-of-digits-of-an-integer
Python Solution - faster than 90% of solutions
heli_kolambekar
0
1
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,003
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2837492/Python3-Straightforward-solution
class Solution: def subtractProductAndSum(self, n: int) -> int: m = [int(x) for x in str(n)] j = 1 for i in m: j *= i return j - sum(m)
subtract-the-product-and-sum-of-digits-of-an-integer
[Python3] Straightforward solution
Peanut_in_Motion
0
1
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,004
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2836681/The-simple-and-understandable-way
class Solution: def subtractProductAndSum(self, n: int) -> int: m = 1 a = 0 for i in str(n): m *= int(i) a += int(i) return m-a
subtract-the-product-and-sum-of-digits-of-an-integer
The simple and understandable way
Abraha111
0
1
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,005
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2801526/1281.-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer
class Solution: def subtractProductAndSum(self, n: int) -> int: l = [int(x) for x in str(n)] mul = 1 for _ in l: mul *= _ sum = 0 for _ in l: sum += _ return (mul - sum)
subtract-the-product-and-sum-of-digits-of-an-integer
1281. Subtract the Product and Sum of Digits of an Integer
jmukesh99
0
2
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,006
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2789719/Subtract-the-Product-and-sum-in-python-very-easy-solution
class Solution: def subtractProductAndSum(self, n: int) -> int: sub = 0 mul = 1 while n > 0: r = n % 10 sub += r mul *= r n = n // 10 return mul - sub
subtract-the-product-and-sum-of-digits-of-an-integer
Subtract the Product and sum in python very easy solution
jashii96
0
2
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,007
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2789718/Subtract-the-Product-and-sum-in-python-very-easy-solution
class Solution: def subtractProductAndSum(self, n: int) -> int: sub = 0 mul = 1 while n > 0: r = n % 10 sub += r mul *= r n = n // 10 return mul - sub
subtract-the-product-and-sum-of-digits-of-an-integer
Subtract the Product and sum in python very easy solution
jashii96
0
0
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,008
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2780115/Python-Easy-Solution-(Simple-Approach)
class Solution: def subtractProductAndSum(self, n: int) -> int: sum = 0 prod = 1 while n > 0 : rem = n % 10 sum = sum + rem prod = prod * rem n = n//10 return (prod - sum)
subtract-the-product-and-sum-of-digits-of-an-integer
Python Easy Solution (Simple Approach)
Asmogaur
0
4
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,009
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2774195/python-easy-solution
class Solution: def subtractProductAndSum(self, n: int) -> int: s=0 p=1 while n: r=n%10 s=s+r p=p*r n=n//10 return p-s
subtract-the-product-and-sum-of-digits-of-an-integer
python easy solution
dummu_chandini
0
2
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,010
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2757002/Python3-26ms-13.8MB-Solution
class Solution: def subtractProductAndSum(self, n: int) -> int: str_n = str(n) product = 1 sum_n = 0 for char in str_n: product = product * int(char) sum_n = sum_n + int(char) return product - sum_n
subtract-the-product-and-sum-of-digits-of-an-integer
Python3 - 26ms 13.8MB Solution
NikhileshNanduri
0
2
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,011
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2752773/Simple-python-code-with-explanation
class Solution: def subtractProductAndSum(self, n): #cat(sum) is a variable to store the sum of digits in integer cat = 0 #rat(product) is a variable to store the product of digits in integer rat = 1 #while n is not 0 this while loop will work while n != 0: #add the last digit to cat cat += (n%10) #multiply the last digit with rat rat *= (n%10) #remove the last digit n = n//10 #return the difference of rat(product) - cat(sum) return (rat - cat)
subtract-the-product-and-sum-of-digits-of-an-integer
Simple python code with explanation
thomanani
0
2
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,012
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2719521/simple-python-solution-%3A-)
class Solution: def subtractProductAndSum(self, n: int) -> int: a = str(n) d = [] for i in a: d.append(int(i)) z = 1 for i in d: z = z * i return z - sum(d)
subtract-the-product-and-sum-of-digits-of-an-integer
simple python solution :-)
ft3793
0
4
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,013
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2714615/Python-3-or-O(n)-or-Two-Approaches-or-One-Liner
class Solution: def subtractProductAndSum(self, n: int) -> int: nums = [] while(n>0): #This loop will continue looping until all the digits in n are removed nums.append(n % 10) # This appends the last digit of n n //= 10 # This removes the last digit of n return prod(nums)-sum(nums)
subtract-the-product-and-sum-of-digits-of-an-integer
✔️Python 3 | 🕝 O(n) | Two Approaches | One-Liner
keioon
0
4
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,014
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2693581/Python-solution-faster-than-92-and-less-memory-than-95-or-Without-converting-to-string
class Solution: def subtractProductAndSum(self, n: int) -> int: product = 1 sum = 0 while n > 0: digit = n % 10 product = product * digit sum = sum + digit n = n // 10 return product - sum
subtract-the-product-and-sum-of-digits-of-an-integer
🐍 Python solution faster than 92% & less memory than 95% | Without converting to string
anandanshul001
0
4
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,015
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2687773/Python%2BNumPy
class Solution: def subtractProductAndSum(self, n: int) -> int: from numpy import prod,sum N=[int(x) for x in str(n)] return prod(N)-sum(N)
subtract-the-product-and-sum-of-digits-of-an-integer
Python+NumPy
Leox2022
0
1
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,016
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2684120/Python-solution
class Solution: def subtractProductAndSum(self, n: int) -> int: product = 1 sum_digits = 0 for num in str(n): product *= int(num) sum_digits += int(num) return product - sum_digits
subtract-the-product-and-sum-of-digits-of-an-integer
Python solution
samanehghafouri
0
16
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,017
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2678670/Easy-solution-or-faster-than-99.7
class Solution: def subtractProductAndSum(self, n: int) -> int: def prod(x): p = 1 while x>0: p *= x%10 x//=10 return p def add(x): s = 0 while x>0: s += x%10 x//=10 return s return prod(n)-add(n)
subtract-the-product-and-sum-of-digits-of-an-integer
Easy solution | faster than 99.7%
MockingJay37
0
20
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,018
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2668005/Easy-python-solution
class Solution: def subtractProductAndSum(self, n: int) -> int: a=1 b=0 res = [int(a) for a in str(n)] for i in res: a*=i b+=i return a-b
subtract-the-product-and-sum-of-digits-of-an-integer
Easy python solution
Navaneeth7
0
3
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,019
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2664645/Python3-Solutions
class Solution: def subtractProductAndSum(self, n: int) -> int: p = 1 # product s = 0 # sum for i in str(n): p *= int(i) s += int(i) return p - s
subtract-the-product-and-sum-of-digits-of-an-integer
Python3 Solutions
AnzheYuan1217
0
20
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,020
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2664645/Python3-Solutions
class Solution: def subtractProductAndSum(self, n: int) -> int: p = 1 s = 0 for i in range(len(str(n))): digit = n % 10 n //= 10 p *= digit s += digit return p - s
subtract-the-product-and-sum-of-digits-of-an-integer
Python3 Solutions
AnzheYuan1217
0
20
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,021
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2664645/Python3-Solutions
class Solution: def subtractProductAndSum(self, n: int) -> int: p = 1 s = 0 while n != 0: digit = n % 10 n //= 10 p *= digit s += digit return p - s
subtract-the-product-and-sum-of-digits-of-an-integer
Python3 Solutions
AnzheYuan1217
0
20
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,022
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2664645/Python3-Solutions
class Solution: def subtractProductAndSum(self, n: int) -> int: p = 1 s = 0 length = len(str(n)) - 1 for i in range(len(str(n))): digit = n // (10 ** length) n = n % (10 ** length) length -= 1 p *= digit s += digit return p - s
subtract-the-product-and-sum-of-digits-of-an-integer
Python3 Solutions
AnzheYuan1217
0
20
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,023
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2658825/Python-or-Easy-solution
class Solution: def subtractProductAndSum(self, n: int) -> int: s, p = 0, 1 while n: mod = n % 10 s += mod p *= mod n //= 10 return p - s
subtract-the-product-and-sum-of-digits-of-an-integer
Python | Easy solution
LordVader1
0
24
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,024
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2645586/easy-approach!
class Solution: def subtractProductAndSum(self, n: int) -> int: sum,pro = 0,1 for i in str(n): sum += int(i) ; pro *= int(i) return pro - sum
subtract-the-product-and-sum-of-digits-of-an-integer
easy approach!
sanjeevpathak
0
2
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,025
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2622443/Python-Simple-Solution
class Solution: def subtractProductAndSum(self, n: int) -> int: digit_prod = 1 digit_sum = 0 for i in range(len(str(n))): digit_prod *= int(str(n)[i]) digit_sum += int(str(n)[i]) return digit_prod - digit_sum
subtract-the-product-and-sum-of-digits-of-an-integer
Python Simple Solution
sharondev
0
23
subtract the product and sum of digits of an integer
1,281
0.867
Easy
19,026
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/712693/Python-O(n)-Easy-to-Understand
class Solution: # Time: O(n) # Space: O(n) def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: res, dic = [], {} for idx, group in enumerate(groupSizes): if group not in dic: dic[group] = [idx] else: dic[group].append(idx) if len(dic[group]) == group: res.append(dic[group]) del dic[group] return res
group-the-people-given-the-group-size-they-belong-to
Python O(n) Easy to Understand
whissely
5
214
group the people given the group size they belong to
1,282
0.857
Medium
19,027
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/1942719/Python-Hashmap-Simple-Approach
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: # Step 1 : Categorise using hashmap h = {} for index,value in enumerate(groupSizes): h[value] = h.get(value,[]) + [index] ans = [] # Step 2 : Prepare the groups for size in h.keys(): num_grps = len(h[size])//size # // means integer division for x in range(num_grps): temp = [] for j in range(size): temp.append(h[size].pop(0)) ans.append(temp) # Return the answer return ans
group-the-people-given-the-group-size-they-belong-to
Python Hashmap Simple Approach
BeardedOwl1357
1
40
group the people given the group size they belong to
1,282
0.857
Medium
19,028
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/1777376/Python-3-HashMap-solution
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: res = [] groups = collections.defaultdict(list) for i, size in enumerate(groupSizes): groups[size].append(i) if len(groups[size]) == size: res.append(groups[size]) groups[size] = [] return res
group-the-people-given-the-group-size-they-belong-to
Python 3, HashMap solution
dereky4
1
149
group the people given the group size they belong to
1,282
0.857
Medium
19,029
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/1479892/Python3-solution-or-faster-than-76-or-Comments
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: d = {} for i in range(len(groupSizes)): # creating a dictionary to group all persons that have the same groupSize if groupSizes[i] not in d: d[groupSizes[i]] = [] d[groupSizes[i]].append(i) else: d[groupSizes[i]].append(i) res = [] k = 0 # index of the list we return for i, ele in enumerate(d): # we iterate through the dictionary and every list count = 0 res.append([]) for j in range(len(d[ele])): res[k].append(d[ele][j]) count += 1 # we count how many persons we add -> if there are more persons than the number of groupSize we append another empty list if count == ele: if j < len(d[ele])-1: res.append([]) k += 1 count = 0 k += 1 return res
group-the-people-given-the-group-size-they-belong-to
Python3 solution | faster than 76% | Comments
FlorinnC1
1
117
group the people given the group size they belong to
1,282
0.857
Medium
19,030
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/446452/Python3-group-ids-according-to-size
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: ids = dict() #mapping from size to ids for i, size in enumerate(groupSizes): ids.setdefault(size, []).append(i) ans = [] for size, ids in ids.items(): ans.extend([ids[i:i+size] for i in range(0, len(ids), size)]) #split list into list of list return ans
group-the-people-given-the-group-size-they-belong-to
[Python3] group ids according to size
ye15
1
55
group the people given the group size they belong to
1,282
0.857
Medium
19,031
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/2831803/Python-Solution-Hashmap-approach-oror-Explained
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: d={} for i in range(len(groupSizes)): #putting array of indeces into the values #of keys (which are the same numbers present in groupSizes) #d={key:[values]} if groupSizes[i] in d: d[groupSizes[i]].append(i) else: d[groupSizes[i]]=[i] ans=[] for i in d.keys(): j=0 l=[] while j<len(d[i]): #from every key storing indices array as value l.append(d[i][j]) #make subarrays same to the size of i if len(l)==i: #if one subarray reach to the length of i #and still numbers remaining in d[i] then make another subarray ans.append(l) #answer array l=[] j+=1 #traversing in d[i] values if len(l)!=0: #if values finished in d[i] then l will be having few values so append to ans ans.append(l) return ans
group-the-people-given-the-group-size-they-belong-to
Python Solution - Hashmap approach || Explained✔
T1n1_B0x1
0
3
group the people given the group size they belong to
1,282
0.857
Medium
19,032
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/2825501/Python-3-HashTable-O(n)
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: lookup = defaultdict(list) result = [] # Build lookup dictionary key: group size, value: list of ids that are in that group size for id, size in enumerate(groupSizes): lookup[size].append(id) # Check for too many ids for group size and break up into groups of 'key' size for key, value in lookup.items(): left = 0 right = key while right <= len(value): result.append(value[left:right]) left += key right += key return result
group-the-people-given-the-group-size-they-belong-to
Python 3 HashTable O(n)
bettend
0
2
group the people given the group size they belong to
1,282
0.857
Medium
19,033
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/2771714/python-solution-Group-the-People-Given-the-Group-Size-They-Belong
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: counter = {} main_list = [] for i in range(len(groupSizes)): if groupSizes[i] not in counter: counter[groupSizes[i]] = [i] else: counter[groupSizes[i]].append(i) if len(counter[groupSizes[i]]) == groupSizes[i]: main_list.append(counter[groupSizes[i]]) counter[groupSizes[i]] = [] return (main_list)
group-the-people-given-the-group-size-they-belong-to
python solution Group the People Given the Group Size They Belong
sarthakchawande14
0
3
group the people given the group size they belong to
1,282
0.857
Medium
19,034
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/2702608/Simple-and-Easy-to-Understand-or-Beginner's-Friendly-or-Python
class Solution(object): def groupThePeople(self, g): hashT = {} ans = [] for i in range(len(g)): if g[i] not in hashT: hashT[g[i]] = [i] else: if len(hashT[g[i]]) == g[i]: ans.append(hashT[g[i]]) hashT[g[i]] = [i] else: hashT[g[i]].append(i) for n in hashT: ans.append(hashT[n]) return ans
group-the-people-given-the-group-size-they-belong-to
Simple and Easy to Understand | Beginner's Friendly | Python
its_krish_here
0
14
group the people given the group size they belong to
1,282
0.857
Medium
19,035
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/2695449/Python3-Simple-Solution
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: d = defaultdict(list) for i, size in enumerate(groupSizes): d[size].append(i) res = [] for k, v in d.items(): for i in range(0, len(v) - k + 1, k): res.append(d[k][i: i + k]) return res
group-the-people-given-the-group-size-they-belong-to
Python3 Simple Solution
mediocre-coder
0
6
group the people given the group size they belong to
1,282
0.857
Medium
19,036
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/2478766/A-straight-forward-Python-Solution
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: lookup_table = {} output = [] for person, groupSize in enumerate(groupSizes): if groupSize in lookup_table: lookup_table[groupSize].append(person) continue lookup_table[groupSize] = [person] for key in lookup_table.keys(): if key == len(lookup_table[key]): output.append(lookup_table[key]) continue group_stub = [0] * key for idx in range(len(lookup_table[key]) // key): output.append(lookup_table[key][idx * key:(idx*key) + key]) return output
group-the-people-given-the-group-size-they-belong-to
A straight forward Python Solution
cengleby86
0
28
group the people given the group size they belong to
1,282
0.857
Medium
19,037
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/2475318/Easiest-Solution-using-Hash-Map
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: #Step 1: Store people who have group with the same size group_size_people = {} for i, size in enumerate(groupSizes): if size not in group_size_people: group_size_people[size]=[i] else: group_size_people[size].append(i) #Step 2: Put them in to group res = [] for size, people in group_size_people.items(): if size < len(people): for i in range(len(people)): if (i+1)%size ==0: res.append(people[i+1-size:i+1]) else: res.append(people) return res
group-the-people-given-the-group-size-they-belong-to
Easiest Solution using Hash Map
williamhuybui
0
10
group the people given the group size they belong to
1,282
0.857
Medium
19,038
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/2473110/Python-Solution-or-defaultdict-or-easy-understanding
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: ans = [] d = defaultdict(list) for i, g in enumerate(groupSizes): d[g].append(i) if len(d[g]) == g: ans.append(d[g]) d[g] = [] return ans
group-the-people-given-the-group-size-they-belong-to
Python Solution | defaultdict | easy understanding
imneeraj_kumar
0
33
group the people given the group size they belong to
1,282
0.857
Medium
19,039
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/2412445/Hashtable-Python3-Statistic
class Solution: def groupThePeople(self, g: List[int]) -> List[List[int]]: N = len(g) count = {} res = [] for i in range(N): if g[i] in count: l = count[g[i]] if len(l) < g[i] - 1: count[g[i]] = count[g[i]] + [i] else: res.append(count[g[i]] + [i]) count.pop(g[i]) else: if g[i] > 1: count[g[i]] = [i] else: res.append([i]) return res
group-the-people-given-the-group-size-they-belong-to
Hashtable Python3 Statistic
jdai1234
0
25
group the people given the group size they belong to
1,282
0.857
Medium
19,040
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/2275059/No-additional-collections-just-sorting-with-indices
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: indexed = sorted([(i, v) for i, v in enumerate(groupSizes)], key=lambda t: t[1]) answer = [] i = 0 while i < len(groupSizes): answer.append([v[0] for v in indexed[i: i + indexed[i][1]]]) i += indexed[i][1] return answer
group-the-people-given-the-group-size-they-belong-to
No additional collections, just sorting with indices
amaargiru
0
25
group the people given the group size they belong to
1,282
0.857
Medium
19,041
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/2238575/Python3-intuitive-solution-w-zipping
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: pos = [i for i in range(len(groupSizes))] zipped = zip(pos, groupSizes) A = sorted(zipped, key = lambda x:x[1]) i, ans = 0, [] while i < len(groupSizes): chunk = A[i][1] tmp = [] for j in range(i, i+ chunk): tmp.append(A[j][0]) i += chunk ans.append(tmp) return(ans)
group-the-people-given-the-group-size-they-belong-to
Python3 intuitive solution w/ zipping
TheKivs
0
35
group the people given the group size they belong to
1,282
0.857
Medium
19,042
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/2153851/Python-Solution-Using-Dictionaries-(BASIC)-O(n2)%3ATime-Complexity
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: d={} for i in set(groupSizes): d[i]=[] for i in range(len(groupSizes)): d[groupSizes[i]]=d.get(groupSizes[i])+[i] res=[] for i in list(d.keys()): for j in range(0,len(d[i]),i): res.append(d[i][j:j+i]) return res
group-the-people-given-the-group-size-they-belong-to
Python Solution Using Dictionaries (BASIC) O(n^2):Time Complexity
Kunalbmd
0
28
group the people given the group size they belong to
1,282
0.857
Medium
19,043
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/2120706/Python-Dictionary-Implementation
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: mapp = {} for i in range(len(groupSizes)): if groupSizes[i] in mapp: mapp[groupSizes[i]].append(i) else: mapp[groupSizes[i]] = [i] ans = [] for key,value in mapp.items(): temp = [] j =0 for i in range(len(value)): temp.append(value[i]) j+=1 if j == key: ans.append(temp) temp = [] j = 0 return ans
group-the-people-given-the-group-size-they-belong-to
Python Dictionary Implementation
somendrashekhar2199
0
59
group the people given the group size they belong to
1,282
0.857
Medium
19,044
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/1832191/Personal-python-solution
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: # define dict to sort groups buckets = {} #define ans list to return later ans = [] #iterate through list with enumerate for index,val in enumerate(groupSizes): #if val in buckets append index of person to group bucket if val in buckets: temp = buckets[val] temp.append(index) buckets[val] = temp else: #if val not in buckets add val as with list of index of first person in group buckets[val] = [index] #iterate through dict keys (groups) for val in buckets: #define pointers to slice the groups for ans pointer1 = 0 pointer2 = val #define list of curr group indexes to be sliced bucket_list = buckets[val] #iterate through list with pointers and slice it into groups until out of vals while pointer2 <= len(bucket_list): group_slice = bucket_list[pointer1:pointer2] ans.append(group_slice) pointer1 += val pointer2 += val return(ans)
group-the-people-given-the-group-size-they-belong-to
Personal python solution
LegendaryBeagle
0
46
group the people given the group size they belong to
1,282
0.857
Medium
19,045
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/1832191/Personal-python-solution
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: buckets = {} ans = [] for index,val in enumerate(groupSizes): if val in buckets: temp = buckets[val] temp.append(index) buckets[val] = temp else: buckets[val] = [index] pointer1 = 0 for val in buckets: pointer1 = 0 pointer2 = val bucket_list = buckets[val] while pointer2 <= len(bucket_list): group_slice = bucket_list[pointer1:pointer2] ans.append(group_slice) pointer1 += val pointer2 += val return(ans)
group-the-people-given-the-group-size-they-belong-to
Personal python solution
LegendaryBeagle
0
46
group the people given the group size they belong to
1,282
0.857
Medium
19,046
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/1546657/Python3-Solution-with-using-map
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: d = {} res = [] for idx, gs in enumerate(groupSizes): if gs not in d: d[gs] = [] d[gs].append(idx) if len(d[gs]) == gs: res.append(d[gs]) d[gs] = [] return res
group-the-people-given-the-group-size-they-belong-to
[Python3] Solution with using map
maosipov11
0
49
group the people given the group size they belong to
1,282
0.857
Medium
19,047
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/1425156/Python-3
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: dictn = {} for i, v in enumerate(groupSizes): dictn.setdefault(v, []).append(i) res = [] for x, y in dictn.items(): p = x j = 0 if(len(y) < x): res.append(y) else: for _ in range(0, int(len(y)/x)): res.append(y[j:p]) j = p p += x res.sort(key=len) return res
group-the-people-given-the-group-size-they-belong-to
Python 3
amestri890
0
59
group the people given the group size they belong to
1,282
0.857
Medium
19,048
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/1416688/Python3-simple-solution-99.41-faster
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: dict = {} outList = [] for i in range(0,len(groupSizes)): if groupSizes[i] not in dict: dict[groupSizes[i]] = [i] else: dict[groupSizes[i]] += [i] for k,v in dict.items(): for i in range(0,len(v),k): outList.append(v[i:i+k]) return outList
group-the-people-given-the-group-size-they-belong-to
Python3 simple solution 99.41% faster
sunnysharma03
0
99
group the people given the group size they belong to
1,282
0.857
Medium
19,049
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/1297655/python-easy-solution-beats-~91
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: h={} for person,size in enumerate(groupSizes): if size not in h: h[size]=[person] else: h[size].append(person) ans=[] for i in h: if len(h[i])!=i: for end in range(0,len(h[i]),i): ans.append(h[i][end:end+i]) else: ans.append(h[i]) return (ans)
group-the-people-given-the-group-size-they-belong-to
python easy solution beats ~91%
prakharjagnani
0
73
group the people given the group size they belong to
1,282
0.857
Medium
19,050
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/1273671/Simple-and-Clean-Python-solution
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: dic=defaultdict(list) #Store in a dictionary[i] the list of indices of input array groupSizes that belong to a group size i for index,ele in enumerate(groupSizes): dic[ele].append(index) answer=[] for key,value in dic.items(): for idx in range(0,len(value),key): answer.append(value[idx:idx + key]) return answer
group-the-people-given-the-group-size-they-belong-to
Simple and Clean Python solution
jaipoo
0
150
group the people given the group size they belong to
1,282
0.857
Medium
19,051
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/1265774/Python-O(N)-solution
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: groups = {} ans = [] for i, size in enumerate(groupSizes): if not groups.get(size, None): groups[size] = [] groups[size].append(i) # O(1) if len(groups[size]) == size: ans.append(groups[size]) groups[size] = [] return ans
group-the-people-given-the-group-size-they-belong-to
Python O(N) solution
user5573CJ
0
62
group the people given the group size they belong to
1,282
0.857
Medium
19,052
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/1239434/Python-3-Solution-using-dictionary
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: d = {} final = [] # Create a dic with number as key and its positions as value for i, x in enumerate(groupSizes): if x not in d: d[x] = [i] else: d[x].append(i) tmp = dict(sorted(d.items())) for x,y in tmp.items(): if len(y) == x: final.append(y) else: for i in range(0,len(y),x): final.append(y[i:i+x]) return(final)
group-the-people-given-the-group-size-they-belong-to
[Python 3] Solution using dictionary
SushilG96
0
43
group the people given the group size they belong to
1,282
0.857
Medium
19,053
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/1102734/WEEB-EXPLAINS-SIMPLE-PYTHON-CODE
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: adjList = defaultdict(list) for idx, value in enumerate(groupSizes): adjList[value].append(idx) # construct adjacency list result = [] for i in adjList: level = [] # for each group we create a new level for j in adjList[i]: # for the people in that particular group level += [j] # add to group if len(level) == i: # if group is full result.append(level) # put in result level = [] # clean the group cuz if not then it will overflow return result
group-the-people-given-the-group-size-they-belong-to
WEEB EXPLAINS SIMPLE PYTHON CODE
Skywalker5423
0
103
group the people given the group size they belong to
1,282
0.857
Medium
19,054
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/1078169/Python-Queue
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: graph = collections.defaultdict(collections.deque) for idx, i in enumerate(groupSizes): graph[i].append(idx) result = [] for key, val in graph.items(): while val: tmp = [] for i in range(key): tmp += [val.popleft()] result.append(tmp) return result
group-the-people-given-the-group-size-they-belong-to
Python Queue
dev-josh
0
83
group the people given the group size they belong to
1,282
0.857
Medium
19,055
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/996513/Simple-solution-in-python3-handling-case-of-groupSizei-1-seperately-using-hashmap
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: hashmap = dict() for i in range(len(groupSizes)): if groupSizes[i] != 1: if groupSizes[i] not in hashmap: hashmap[groupSizes[i]] = [i] else: hashmap[groupSizes[i]].append(i) ans = [] for k in hashmap: v = hashmap[k] number_groups = len(v) // k start = 0 for i in range(number_groups): ans.append(v[start: start + k]) start = start + k for i in range(len(groupSizes)): if groupSizes[i] == 1: ans.append([i]) return ans
group-the-people-given-the-group-size-they-belong-to
Simple solution in python3 handling case of groupSize[i] == 1 seperately using hashmap
amoghrajesh1999
0
53
group the people given the group size they belong to
1,282
0.857
Medium
19,056
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/765755/Simple-python-solution-using-dictionary-and-list.
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: sol = {} for i in range(len(groupSizes)): if groupSizes[i] not in sol: sol[groupSizes[i]] = [i] else: sol[groupSizes[i]].append(i) res = [] for key in sol: val = sol[key] temp = [] for i in range(len(val)): temp.append(val[i]) if (i + 1) % key == 0: res.append(temp) temp = [] return res
group-the-people-given-the-group-size-they-belong-to
Simple python solution using dictionary and list.
C3Wizard
0
57
group the people given the group size they belong to
1,282
0.857
Medium
19,057
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/755013/Simple-Python3-List-Solution-Beats-92
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: # Maximum possible number of groups is the same as number of people groups = [[] for _ in range(len(groupSizes)+1)] # Add invididuals to groups without subdivisions for i in range(len(groupSizes)): groups[groupSizes[i]].append(i) res = [] # Split the groups into subgroups if necessary for i in range(len(groups)): if len(groups[i]) == 0: continue ind = 0 for p in range(0, len(groups[i]), i): res.append(groups[i][i*ind:i*(ind+1)]) ind += 1 return res
group-the-people-given-the-group-size-they-belong-to
Simple Python3 List Solution Beats 92%
jacksilver
0
122
group the people given the group size they belong to
1,282
0.857
Medium
19,058
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/639225/Python-easy-to-understand-two-step-solution-using-list
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: groups_of_same_size = [[i for i, e in enumerate(groupSizes) if e == s] for s in set(groupSizes)] result = [] for groups in groups_of_same_size: size = groupSizes[groups[0]] count = len(groups) // size result.extend(groups[i*size:(i+1)*size] for i in range(count)) return result
group-the-people-given-the-group-size-they-belong-to
Python easy to understand two-step solution using list
usualwitch
0
92
group the people given the group size they belong to
1,282
0.857
Medium
19,059
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/550393/Noob-Python3-Answer-(First-Attempt)-Improvement-Suggestions-Welcome
class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: #We are wanting to return a list of groups, where each group is a list that contains IDs of persons. A person's #ID is equal to their index in the groupSizes list (which is the input parameter to this function). groupsOfPeople = list() #Dictionary to track the occurrences for each group size groupSizeToOccurrences = dict() #Count the occurrences and populate the dictionary for i in range(len(groupSizes)): groupSize = groupSizes[i] personId = i if groupSize in groupSizeToOccurrences: groupSizeToOccurrences[groupSize] += 1 else: groupSizeToOccurrences[groupSize] = 1 #Make the groups for groupSize in groupSizeToOccurrences: groupSizeOccurrences = groupSizeToOccurrences[groupSize] numGroupsToCreate = groupSizeOccurrences//groupSize groupsList = self.getListOfGroups(groupSize, numGroupsToCreate, groupSizes) groupsOfPeople += groupsList return groupsOfPeople def getListOfGroups(self, groupSize: int, numGroupsToCreate: int, groupSizes: List[int]) -> List[List[int]]: listOfGroups = self.createEmptyListOfGroups(numGroupsToCreate) for i in range(len(groupSizes)): #Only look at the groupSize that matches the input parameter to this function if(groupSizes[i] == groupSize): #Get the index (personId) and put it into a list personId = i nextGroup = self.getNextGroup(listOfGroups, groupSize) nextGroup.append(personId) return listOfGroups def getNextGroup(self, listOfGroups: List[List[int]], groupSize): #Get the next group that has not yet reached it's expected size and return it for i in range(len(listOfGroups)): hasReachedGroupSize = (len(listOfGroups[i]) == groupSize) if not hasReachedGroupSize: return listOfGroups[i] def createEmptyListOfGroups(self, numGroupsToCreate: int): emptyGroups = list() for i in range(numGroupsToCreate): emptyGroups.append(list()) return emptyGroups
group-the-people-given-the-group-size-they-belong-to
Noob Python3 Answer (First Attempt) - Improvement Suggestions Welcome
jleikam
0
42
group the people given the group size they belong to
1,282
0.857
Medium
19,060
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/discuss/446439/Python-3-(three-lines)-(beats-100)
class Solution: def groupThePeople(self, G: List[int]) -> List[List[int]]: S, D = set(G), collections.defaultdict(list) for i, g in enumerate(G): D[g].append(i) return [D[i][i*j:i*(j+1)] for i in S for j in range(G.count(i)//i)] - Junaid Mansuri - Chicago, IL
group-the-people-given-the-group-size-they-belong-to
Python 3 (three lines) (beats 100%)
junaidmansuri
0
286
group the people given the group size they belong to
1,282
0.857
Medium
19,061
https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/discuss/863333/Python3-Binary-search-with-explanation
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: left, right = 1, max(nums) while left + 1 < right: mid = (left + right) // 2 div_sum = self.get_sum(mid, nums) if div_sum > threshold: left = mid else: right = mid div_sum = self.get_sum(left, nums) if div_sum <= threshold: return left return right def get_sum(self, divisor, nums): res = 0 for n in nums: tmp = n // divisor if tmp * divisor < n: tmp += 1 res += tmp return res
find-the-smallest-divisor-given-a-threshold
Python3 Binary search with explanation
ethuoaiesec
3
313
find the smallest divisor given a threshold
1,283
0.554
Medium
19,062
https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/discuss/840531/Python-3-or-Binary-Search-or-Explanations
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: def ok(mid): ans = 0 for num in nums: ans += math.ceil(num / mid) if ans > threshold: return False return True l, r = 1, int(1e6) while l <= r: mid = (l+r) // 2 if ok(mid): r = mid - 1 else: l = mid + 1 return l
find-the-smallest-divisor-given-a-threshold
Python 3 | Binary Search | Explanations
idontknoooo
3
668
find the smallest divisor given a threshold
1,283
0.554
Medium
19,063
https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/discuss/2761376/Python-Binary-Serach
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: left, right = 1, max(nums) while left < right: mid = (left + right )//2 count = 0 for num in nums: count += math.ceil(num/mid) if count > threshold: left = mid + 1 else: right = mid return left
find-the-smallest-divisor-given-a-threshold
Python Binary Serach
vijay_2022
0
2
find the smallest divisor given a threshold
1,283
0.554
Medium
19,064
https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/discuss/2597711/Python3-or-Solved-Using-Binary-Search-on-Divisor-Range-of-Values
class Solution: #Let n = len(nums)! #Time-Complexity: O(log(max(nums)) * n) #Space-Complexity: O(1) def smallestDivisor(self, nums: List[int], threshold: int) -> int: #Approach: Define the search space for divisors, ranging from 1 to #maximum element in input array nums! #Perform binary search on such search space and update minimal divisor #s.t. it meets the condition as we go! #Once binary search stops, the answer variable should store value #which is the minimal divisor to not exceed threshold! #define a helper function to find sum of elements after dividing each #and every element in nums by divisor! def helper(divisor): nonlocal nums accumulator = 0 for e in nums: divided = e / divisor #if it's integer, leave it! if(divided.is_integer()): accumulator += int(divided) continue else: divided = int(divided) #round up divided += 1 accumulator += divided return accumulator L, H = 1, max(nums) ans = None while L <= H: cur = (L + H) // 2 total_sum = helper(cur) #check against the threshold! if(total_sum <= threshold): ans = cur H = cur - 1 continue else: L = cur + 1 continue return ans
find-the-smallest-divisor-given-a-threshold
Python3 | Solved Using Binary Search on Divisor Range of Values
JOON1234
0
7
find the smallest divisor given a threshold
1,283
0.554
Medium
19,065
https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/discuss/2398462/Python-Binary-Search
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: high = max(nums) low = 1 while (low < high): mid = (low + high)//2 if self.helper(nums, mid, threshold): high = mid else: low = mid + 1 return low def helper(self, nums, thres, threshold): divSum = 0 for i in range(len(nums)): divSum += math.ceil(nums[i]/thres) return (divSum <= threshold)
find-the-smallest-divisor-given-a-threshold
Python Binary Search
logeshsrinivasans
0
31
find the smallest divisor given a threshold
1,283
0.554
Medium
19,066
https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/discuss/2381064/python3-binary-search-solution
class Solution: # O(nlogn) time, # O(1) space, # Approach: binary search, def smallestDivisor(self, nums: List[int], threshold: int) -> int: def evalFunction(div): tot = 0 for num in nums: qutnt = num//div tot +=qutnt if num % div != 0: tot +=1 return tot def binarySearch(start, end): mid = (start + end) // 2 qutnt_sum = evalFunction(mid) if qutnt_sum <= threshold and (end-start < 2): return mid elif start >= end-1: return end elif qutnt_sum <= threshold: return binarySearch(start, mid) else: return binarySearch(mid, end) return binarySearch(1, max(nums))
find-the-smallest-divisor-given-a-threshold
python3 binary search solution
destifo
0
11
find the smallest divisor given a threshold
1,283
0.554
Medium
19,067
https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/discuss/2094606/Python-Solution-neat-and-clean-easy-to-understand
class Solution: def helper(self,nums,m): Sum = 0 for n in nums: Sum += math.ceil(n/m) return Sum def smallestDivisor(self, nums: List[int], threshold: int) -> int: l,r = 1, max(nums) while l < r: mid = (l+r)//2 Sum = self.helper(nums,mid) if Sum > threshold: l = mid + 1 else: r = mid return r
find-the-smallest-divisor-given-a-threshold
Python Solution neat and clean easy to understand
__Asrar
0
78
find the smallest divisor given a threshold
1,283
0.554
Medium
19,068
https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/discuss/1933364/6-Lines-Python-Solution-oror-90-Faster-oror-Memory-less-than-80
class Solution: def smallestDivisor(self, nums: List[int], t: int) -> int: lo=1 ; hi=max(nums) while lo<=hi: mid=(lo+hi)//2 if sum(ceil(num/mid) for num in nums)<=t: hi=mid-1 else: lo=mid+1 return lo
find-the-smallest-divisor-given-a-threshold
6-Lines Python Solution || 90% Faster || Memory less than 80%
Taha-C
0
62
find the smallest divisor given a threshold
1,283
0.554
Medium
19,069
https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/discuss/1915781/Python-or-Binary-Search-or-Easy
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: # print(((1-1)//5)+1) def helper(x): ans = 0 for i in range(len(nums)): ans+=(((nums[i]-1)//x)+1) return ans<=threshold low = 1 high = max(nums) while low<high: mid = low+(high-low)//2 if helper(mid): high = mid else: low=mid+1 return low
find-the-smallest-divisor-given-a-threshold
Python | Binary Search | Easy
Brillianttyagi
0
36
find the smallest divisor given a threshold
1,283
0.554
Medium
19,070
https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/discuss/1078615/Python3-binary-search-beats-99
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: compute_sum = lambda x : sum([ceil(n / x) for n in nums]) real_sum = sum(nums) if real_sum < threshold: return 1 if threshold == len(nums): return max(nums) left = ceil(real_sum / threshold) right = ceil(real_sum / (threshold - len(nums))) if compute_sum(left) <= threshold: return left while left < right - 1: mid = (right - left) // 2 + left if compute_sum(mid) > threshold: left = mid else: right = mid return right
find-the-smallest-divisor-given-a-threshold
Python3 binary search beats 99%
fengzhixiwu
0
95
find the smallest divisor given a threshold
1,283
0.554
Medium
19,071
https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/discuss/718528/Python3-bisect_left-Find-the-Smallest-Divisor-Given-a-Threshold
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: class Wrapper: def __getitem__(self, x): return sum(-n//x for n in nums) return bisect.bisect_left(Wrapper(), -threshold, 1, max(nums))
find-the-smallest-divisor-given-a-threshold
Python3 bisect_left - Find the Smallest Divisor Given a Threshold
r0bertz
0
113
find the smallest divisor given a threshold
1,283
0.554
Medium
19,072
https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/discuss/446492/Python3-Binary-search
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: # True if sum of numbers divided by d <= threshold fn = lambda d: sum(ceil(x/d) for x in nums) <= threshold # "first true" binary search lo, hi = 1, max(nums) while lo < hi: mid = lo + hi >> 1 if fn(mid): hi = mid else: lo = mid + 1 return lo
find-the-smallest-divisor-given-a-threshold
[Python3] Binary search
ye15
0
93
find the smallest divisor given a threshold
1,283
0.554
Medium
19,073
https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/discuss/446492/Python3-Binary-search
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: lo, hi = 1, 10**6 while lo < hi: mid = lo + hi >> 1 if sum(ceil(x/mid) for x in nums) <= threshold: hi = mid else: lo = mid + 1 return lo
find-the-smallest-divisor-given-a-threshold
[Python3] Binary search
ye15
0
93
find the smallest divisor given a threshold
1,283
0.554
Medium
19,074
https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/discuss/446396/Python-3-(beats-100)-(Optimized-w-Proof)-(Bisection-Search)-(six-lines)
class Solution: def smallestDivisor(self, N: List[int], t: int) -> int: N.sort(); S, L, ceil = sum(N), len(N), math.ceil; a, b = ceil(S/t), min(ceil(S/(t - L)),N[-1]) while a < b: m = (a+b)//2 if sum(ceil(n/m) for n in N) > t: a = m + 1 else: b = m return a
find-the-smallest-divisor-given-a-threshold
Python 3 (beats 100%) (Optimized w/ Proof) (Bisection Search) (six lines)
junaidmansuri
0
300
find the smallest divisor given a threshold
1,283
0.554
Medium
19,075
https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/discuss/446396/Python-3-(beats-100)-(Optimized-w-Proof)-(Bisection-Search)-(six-lines)
class Solution: def smallestDivisor(self, N: List[int], t: int) -> int: N.sort(); a, b, ceil = 1, N[-1], math.ceil while a < b: m = (a+b)//2 if sum(ceil(n/m) for n in N) > t: a = m + 1 else: b = m return a
find-the-smallest-divisor-given-a-threshold
Python 3 (beats 100%) (Optimized w/ Proof) (Bisection Search) (six lines)
junaidmansuri
0
300
find the smallest divisor given a threshold
1,283
0.554
Medium
19,076
https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/discuss/446552/Python-3-(ten-lines)-(Check-All-Permutations)
class Solution: def minFlips(self, G: List[List[int]]) -> int: M, N = len(G), len(G[0]) P = [(i,j) for i,j in itertools.product(range(M),range(N))] for n in range(M*N+1): for p in itertools.permutations(P,n): H = list(map(list,G)) for (x,y) in p: for (i,j) in (x,y-1),(x,y),(x,y+1),(x-1,y),(x+1,y): if 0 <= i < M and 0 <= j < N: H[i][j] = 1 - H[i][j] if max(max(H)) == 0: return n return -1 - Junaid Mansuri - Chicago, IL
minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix
Python 3 (ten lines) (Check All Permutations)
junaidmansuri
3
441
minimum number of flips to convert binary matrix to zero matrix
1,284
0.72
Hard
19,077
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/452166/Python-3-(four-different-one-line-solutions)-(beats-100)
class Solution: def findSpecialInteger(self, A: List[int]) -> int: return collections.Counter(A).most_common(1)[0][0] from statistics import mode class Solution: def findSpecialInteger(self, A: List[int]) -> int: return mode(A) class Solution: def findSpecialInteger(self, A: List[int]) -> int: return max(set(A), key = A.count) class Solution: def findSpecialInteger(self, A: List[int]) -> int: return (lambda C: max(C.keys(), key = lambda x: C[x]))(collections.Counter(A)) - Junaid Mansuri - Chicago, IL
element-appearing-more-than-25-in-sorted-array
Python 3 (four different one-line solutions) (beats 100%)
junaidmansuri
18
1,800
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,078
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/2056417/Python-easy-to-understand-solution
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: per = len(arr)//4 for i in arr: occ = arr.count(i) if occ > per: return i
element-appearing-more-than-25-in-sorted-array
Python easy to understand solution
Shivam_Raj_Sharma
3
121
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,079
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/1103979/Python3-simple-solution-using-four-approaches
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: n = len(arr) for i in arr: if arr.count(i) > n/4: return i
element-appearing-more-than-25-in-sorted-array
Python3 simple solution using four approaches
EklavyaJoshi
2
122
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,080
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/1103979/Python3-simple-solution-using-four-approaches
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: return sorted(arr, key = lambda x: arr.count(x), reverse = True)[0]
element-appearing-more-than-25-in-sorted-array
Python3 simple solution using four approaches
EklavyaJoshi
2
122
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,081
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/1103979/Python3-simple-solution-using-four-approaches
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: a = 0 count = 0 n = len(arr) for i in arr: if i == a: count += 1 else: a = i count = 1 if count > n/4: return a
element-appearing-more-than-25-in-sorted-array
Python3 simple solution using four approaches
EklavyaJoshi
2
122
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,082
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/1332139/WEEB-DOES-PYTHON-(ONE-LINER)
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: return Counter(arr).most_common(1)[0][0]
element-appearing-more-than-25-in-sorted-array
WEEB DOES PYTHON (ONE-LINER)
Skywalker5423
1
102
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,083
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/451307/Python3-one-pass
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: for i in range(len(arr)): if arr[i] == arr[i+len(arr)//4]: return arr[i]
element-appearing-more-than-25-in-sorted-array
[Python3] one pass
ye15
1
71
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,084
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/2781682/python-easy-(2-lines)
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: a =len(arr) * (25/100); for i in arr : if arr.count(i) > a: return i
element-appearing-more-than-25-in-sorted-array
python easy (2 lines)
seifsoliman
0
2
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,085
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/2687124/Python3-Solution-oror-O(N)-Time-and-O(1)-Space-Complexity
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: n=len(arr) element=arr[0] count=1 for i in arr[1:]: if count>n//4: break if element==i: count+=1 else: element=i count=1 return element
element-appearing-more-than-25-in-sorted-array
Python3 Solution || O(N) Time & O(1) Space Complexity
akshatkhanna37
0
3
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,086
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/2391977/Very-simple
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: l=len(arr) c=(l//4)+1 d={} for i in arr: if i in d: d[i]+=1 else: d[i]=1 if d[i]>=c: return i
element-appearing-more-than-25-in-sorted-array
Very simple
sunakshi132
0
26
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,087
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/2308037/Using-HashMap-or-faster-from-67
class Solution(object): def findSpecialInteger(self, arr): """ :type arr: List[int] :rtype: int """ n=len(arr) record={} pcnt=n/4 for i in arr: try: record[i]+=1 if record[i]>pcnt: return i except: record[i]=1 return record.keys()[0]
element-appearing-more-than-25-in-sorted-array
Using HashMap | faster from 67
abdulhaseeb13
0
25
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,088
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/2298962/Two-Python-1-line-solutions
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: return collections.Counter(arr).most_common(1)[0][0]
element-appearing-more-than-25-in-sorted-array
Two Python 1 line solutions
zip_demons
0
18
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,089
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/2047203/Simple-python-solution
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: return max([x for x in arr if arr.count(x) > len(arr)//4])
element-appearing-more-than-25-in-sorted-array
Simple python solution
StikS32
0
38
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,090
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/1921797/Python-two-beginner-friendly-methods
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: return Counter(arr).most_common()[0][0]
element-appearing-more-than-25-in-sorted-array
Python two beginner friendly methods
alishak1999
0
50
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,091
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/1921797/Python-two-beginner-friendly-methods
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: for i in set(arr): if arr.count(i) > (0.25 * len(arr)): return i
element-appearing-more-than-25-in-sorted-array
Python two beginner friendly methods
alishak1999
0
50
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,092
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/1853075/Python-(Simple-Approach-and-Beginner-friendly)
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: dict = {} number = int(0.25*len(arr)) for i in arr: if i not in dict: dict[i] = 1 else: dict[i]+=1 for i,n in dict.items(): if n>number: return i
element-appearing-more-than-25-in-sorted-array
Python (Simple Approach and Beginner-friendly)
vishvavariya
0
26
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,093
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/1848778/1-Line-Python-Solution-oror-90-Faster-oror-Memory-less-than-40
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: return Counter(arr).most_common(1)[0][0]
element-appearing-more-than-25-in-sorted-array
1-Line Python Solution || 90% Faster || Memory less than 40%
Taha-C
0
45
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,094
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/1848778/1-Line-Python-Solution-oror-90-Faster-oror-Memory-less-than-40
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: for a in set(arr): if arr.count(a)>len(arr)//4: return a
element-appearing-more-than-25-in-sorted-array
1-Line Python Solution || 90% Faster || Memory less than 40%
Taha-C
0
45
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,095
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/1311342/pyhton-3-%3A-super-simple-solution-using-dict
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: dic = {} for i in arr : if i in dic : dic[i] += 1 else : dic[i] = 1 return max(dic,key=dic.get)
element-appearing-more-than-25-in-sorted-array
pyhton 3 : super simple solution using dict
rohitkhairnar
0
75
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,096
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/924684/Python%3A-O(N)-Time-%2B-O(1)-Space-(No-CounterZip)
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: # Setting equal to the first element # since length of the array can be 1 maxInt = arr[0] maxCount = 1 currInt = arr[0] currCount = 1 # O(N) linear scan for i in range(1, len(arr)): # print (f"arr[i]: {arr[i]}, currInt: {currInt}, currCount: {currCount}, maxInt: {maxInt}, maxCount: {maxCount}") if arr[i] == currInt: currCount += 1 else: if currCount > maxCount: maxCount = currCount maxInt = currInt else: # refreshing out curr values and setting them # equal to the new value we just encountered currCount = 1 currInt = arr[i] # For the edge case where the solution # happens to be towards the end if currCount > maxCount: return currInt else: return maxInt
element-appearing-more-than-25-in-sorted-array
Python: O(N) Time + O(1) Space (No Counter/Zip)
JuanTheDoggo
0
62
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,097
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/501594/Runtime%3A-88-ms-faster-than-92.56-of-Python3-online-submissions
class Solution: def findSpecialInteger(self, arr: List[int]) -> int: larr = len(arr) if larr == 0: return None v = len(arr)*.25 prev = None ct = 0 for e in arr: if e == prev: ct += 1 else: ct = 1 prev = e if ct > v: return e return None
element-appearing-more-than-25-in-sorted-array
Runtime: 88 ms, faster than 92.56% of Python3 online submissions
jcravener
0
123
element appearing more than 25 percent in sorted array
1,287
0.595
Easy
19,098
https://leetcode.com/problems/remove-covered-intervals/discuss/1784520/Python3-SORTING-Explained
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: res, longest = len(intervals), 0 srtd = sorted(intervals, key = lambda i: (i[0], -i[1])) for _, end in srtd: if end <= longest: res -= 1 else: longest = end return res
remove-covered-intervals
✔️ [Python3] SORTING 👀, Explained
artod
46
1,600
remove covered intervals
1,288
0.572
Medium
19,099