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/maximum-number-of-groups-getting-fresh-donuts/discuss/1140716/Python3-Fastest-solution-with-explanation
class Solution: def maxHappyGroups(self, bs: int, gs: List[int]) -> int: c = {i: 0 for i in range(bs)} for g in gs: c[g % bs] += 1 ret = c[0] c[0] = 0 def get_keys(num): keys = [] def rec(stack): if len(stack) == num: if sum(stack) % bs == 0: keys.append(Counter(stack)) else: for i in range(stack[-1] if stack else bs - 1, - 1, - 1): stack.append(i) rec(stack) stack.pop() rec([]) return keys def get_diff_keys(num): keys = [] def rec(stack): if len(stack) == num: if sum(stack) % bs == 0: keys.append(Counter(stack)) else: for i in range(stack[-1] - 1 if stack else bs - 1, - 1, - 1): stack.append(i) rec(stack) stack.pop() rec([]) return keys for tc in range(2, bs): for keys in get_diff_keys(tc): add = min(c[key] // keys[key] for key in keys) if add == 0: continue ret += add for key in keys: c[key] -= add * keys[key] tc = 2 while True: for keys in get_keys(tc): add = min(c[key] // keys[key] for key in keys) if add == 0: continue ret += add for key in keys: c[key] -= add * keys[key] if tc > sum(c.values()): break tc += 1 return ret + bool(sum(c.values())) ```
maximum-number-of-groups-getting-fresh-donuts
[Python3] Fastest solution with explanation
timetoai
0
102
maximum number of groups getting fresh donuts
1,815
0.402
Hard
25,900
https://leetcode.com/problems/truncate-sentence/discuss/1142293/2-lines-of-code-with-100-less-space-used
class Solution: def truncateSentence(self, s: str, k: int) -> str: words = s.split(" ") return " ".join(words[0:k])
truncate-sentence
2 lines of code with 100% less space used
vashisht7
15
1,000
truncate sentence
1,816
0.821
Easy
25,901
https://leetcode.com/problems/truncate-sentence/discuss/1283308/Python-faster-than-95
class Solution: def truncateSentence(self, s: str, k: int) -> str: l = list(s.split(" ")) return (" ".join(l[:k]))
truncate-sentence
Python faster than 95%
aditi_shree
2
307
truncate sentence
1,816
0.821
Easy
25,902
https://leetcode.com/problems/truncate-sentence/discuss/1141804/Python-One-Line
class Solution: def truncateSentence(self, s: str, k: int) -> str: return " ".join(s.split(" ")[:k])
truncate-sentence
Python One Line
mariandanaila01
2
67
truncate sentence
1,816
0.821
Easy
25,903
https://leetcode.com/problems/truncate-sentence/discuss/1141327/Python3-1-line
class Solution: def truncateSentence(self, s: str, k: int) -> str: return " ".join(s.split()[:k])
truncate-sentence
[Python3] 1-line
ye15
2
135
truncate sentence
1,816
0.821
Easy
25,904
https://leetcode.com/problems/truncate-sentence/discuss/2554328/EASY-PYTHON3-SOLUTION-FASTER
class Solution: def truncateSentence(self, s: str, k: int) -> str: res = s.split() return ' '.join(res[:k])
truncate-sentence
✅✔🔥 EASY PYTHON3 SOLUTION 🔥✅✔FASTER
rajukommula
1
24
truncate sentence
1,816
0.821
Easy
25,905
https://leetcode.com/problems/truncate-sentence/discuss/1431216/Python-one-line
class Solution: def truncateSentence(self, s: str, k: int) -> str: return " ".join(s.split(' ')[:k])
truncate-sentence
Python one line
amestri890
1
75
truncate sentence
1,816
0.821
Easy
25,906
https://leetcode.com/problems/truncate-sentence/discuss/1184331/Python3-simple-solution-one-liner
class Solution: def truncateSentence(self, s: str, k: int) -> str: return ' '.join(s.split(' ')[:k])
truncate-sentence
Python3 simple solution one-liner
EklavyaJoshi
1
69
truncate sentence
1,816
0.821
Easy
25,907
https://leetcode.com/problems/truncate-sentence/discuss/1157955/Python-Easy-Solution-with-Comments
class Solution: def truncateSentence(self, s: str, k: int) -> str: x = s.split(' ') #make a list of given words str1 = " " #take an empty string return str1.join((x)[:k]) #join empty string and slice it using k
truncate-sentence
Python Easy Solution with Comments
aishwaryanathanii
1
136
truncate sentence
1,816
0.821
Easy
25,908
https://leetcode.com/problems/truncate-sentence/discuss/2846098/Python3-Solution-with-using-space-counting
class Solution: def truncateSentence(self, s: str, k: int) -> str: idx = 0 while k and idx < len(s): if s[idx] == ' ': k -= 1 idx += 1 return s[:idx - 1] if s[idx - 1] == ' ' else s[:idx]
truncate-sentence
[Python3] Solution with using space counting
maosipov11
0
3
truncate sentence
1,816
0.821
Easy
25,909
https://leetcode.com/problems/truncate-sentence/discuss/2839917/Python-Easy-1-liner-Runtime-34-ms
class Solution: def truncateSentence(self, s: str, k: int) -> str: return ' '.join(s.split(" ")[:k])
truncate-sentence
[Python] Easy 1 liner, Runtime 34 ms
m0nxt3r
0
3
truncate sentence
1,816
0.821
Easy
25,910
https://leetcode.com/problems/truncate-sentence/discuss/2829874/python
class Solution: def truncateSentence(self, s: str, k: int) -> str: n = len(s) for i in range(n): if s[i] == ' ': k -= 1 if k == 0: return s[:i] return s
truncate-sentence
python
xy01
0
1
truncate sentence
1,816
0.821
Easy
25,911
https://leetcode.com/problems/truncate-sentence/discuss/2829578/1-LINE-OF-CODE-oror-Python-3oror-Easy-to-understand
class Solution: def truncateSentence(self, s: str, k: int) -> str: return " ".join(s.split()[:k])
truncate-sentence
1 LINE OF CODE✅ || Python 3🔥|| Easy to understand✌🏼
jhadevansh0809
0
2
truncate sentence
1,816
0.821
Easy
25,912
https://leetcode.com/problems/truncate-sentence/discuss/2826387/Easy-and-Fast-Python-One-Liner-(Beats-99)
class Solution: def truncateSentence(self, s: str, k: int) -> str: return " ".join(s.split()[0: k]) '''
truncate-sentence
Easy and Fast Python One-Liner (Beats 99%)
PranavBhatt
0
4
truncate sentence
1,816
0.821
Easy
25,913
https://leetcode.com/problems/truncate-sentence/discuss/2821204/Simple-Python-Solution-in-just-3-line
class Solution: def truncateSentence(self, s: str, k: int) -> str: arr = s.split() arr[::] = arr[0:k] return ' '.join(arr)
truncate-sentence
Simple Python Solution in just 3 line
ameenusyed09
0
1
truncate sentence
1,816
0.821
Easy
25,914
https://leetcode.com/problems/truncate-sentence/discuss/2787840/Python-one-liner
class Solution: def truncateSentence(self, s: str, k: int) -> str: return ' '.join([word for word in s.split()[:k]])
truncate-sentence
Python one liner
ibozkurt79
0
3
truncate sentence
1,816
0.821
Easy
25,915
https://leetcode.com/problems/truncate-sentence/discuss/2777607/OneLiner-easy-Python-solution.
class Solution: def truncateSentence(self, s: str, k: int) -> str: return " ".join(s.split()[:k])
truncate-sentence
OneLiner easy Python solution.
suyog_097
0
1
truncate sentence
1,816
0.821
Easy
25,916
https://leetcode.com/problems/truncate-sentence/discuss/2752248/Easy-Fast-3-Line-Solution-Python3
class Solution: def truncateSentence(self, s: str, k: int) -> str: a="" s_list =s.split(" ") for i in range(k): a += s_list[i] + " " return a.rstrip()
truncate-sentence
Easy Fast 3 Line Solution Python3
avs-abhishek123
0
2
truncate sentence
1,816
0.821
Easy
25,917
https://leetcode.com/problems/truncate-sentence/discuss/2743997/Python-1-line-code
class Solution: def truncateSentence(self, s: str, k: int) -> str: return' '.join((s.split(' ')[:k]))
truncate-sentence
Python 1 line code
kumar_anand05
0
3
truncate sentence
1,816
0.821
Easy
25,918
https://leetcode.com/problems/truncate-sentence/discuss/2739309/one-line-python-solutionor97-space-complexity
class Solution: def truncateSentence(self, s: str, k: int) -> str: return (' ').join(s.split(' ')[0:k])
truncate-sentence
one line python solution|97% space complexity
vishwas1451
0
3
truncate sentence
1,816
0.821
Easy
25,919
https://leetcode.com/problems/truncate-sentence/discuss/2731764/python-simpler-way
class Solution: def truncateSentence(self, s: str, k: int) -> str: sentence_list = s.split(" ") result = [] for i in range(0, k): result.append(sentence_list[i]) return " ".join(result)
truncate-sentence
python simpler way
arshadali7860
0
3
truncate sentence
1,816
0.821
Easy
25,920
https://leetcode.com/problems/truncate-sentence/discuss/2730453/Python3-Solution-with-explanation
class Solution: def truncateSentence(self, s: str, k: int) -> str: return ' '.join(s.split(' ')[:k])
truncate-sentence
Python3 Solution - with explanation
sipi09
0
4
truncate sentence
1,816
0.821
Easy
25,921
https://leetcode.com/problems/truncate-sentence/discuss/2719449/Python-solutionoror
class Solution: def truncateSentence(self, s: str, k: int) -> str: return(" ".join(s.split(" ")[:k]))
truncate-sentence
Python solution||
sinjan_singh
0
3
truncate sentence
1,816
0.821
Easy
25,922
https://leetcode.com/problems/truncate-sentence/discuss/2700066/Python-3-One-Line-Solution-and-Explanation
class Solution: def truncateSentence(self, s: str, k: int) -> str: return " ".join(s.split(" ")[x] for x in range(k))
truncate-sentence
[Python 3] One-Line Solution and Explanation
keioon
0
4
truncate sentence
1,816
0.821
Easy
25,923
https://leetcode.com/problems/truncate-sentence/discuss/2681776/simple-python-3-solution-oror-83ms
class Solution: def truncateSentence(self, s: str, k: int) -> str: l = [] for i in range(len(s.split())): if i == k: break l.append(s.split()[i]) return ' '.join(l)
truncate-sentence
simple python 3 solution || 83ms
Sumeet07
0
5
truncate sentence
1,816
0.821
Easy
25,924
https://leetcode.com/problems/truncate-sentence/discuss/2613997/O(n)-time-O(1)-space-approach-using-a-loop
class Solution: def truncateSentence(self, s: str, k: int) -> str: for i in range(len(s)): if s[i] == ' ': k -= 1 if k == 0: return s[0:i] return s
truncate-sentence
O(n) time, O(1) space approach using a loop
kcstar
0
5
truncate sentence
1,816
0.821
Easy
25,925
https://leetcode.com/problems/truncate-sentence/discuss/2478740/Very-easy-Python-solution
class Solution: def truncateSentence(self, s: str, k: int) -> str: s_list = s.split(" ") res = [] for i in range(k): res.append(s_list[i]) return " ".join(res)
truncate-sentence
Very easy Python solution
aruj900
0
26
truncate sentence
1,816
0.821
Easy
25,926
https://leetcode.com/problems/truncate-sentence/discuss/2388901/One-line-solution
class Solution: def truncateSentence(self, s: str, k: int) -> str: return " ".join(s.split()[:k])
truncate-sentence
One line solution
samanehghafouri
0
14
truncate sentence
1,816
0.821
Easy
25,927
https://leetcode.com/problems/truncate-sentence/discuss/2224609/1-line-Python-solution
class Solution: def truncateSentence(self, s: str, k: int) -> str: return " ".join(s.split(" ")[:k])
truncate-sentence
1 line Python solution
lyubol
0
43
truncate sentence
1,816
0.821
Easy
25,928
https://leetcode.com/problems/truncate-sentence/discuss/2155191/one-line-solution-Easy-to-understand
class Solution: def truncateSentence(self, s: str, k: int) -> str: return ' '.join(s.split(maxsplit = k)[:k])
truncate-sentence
one line solution, Easy to understand
writemeom
0
26
truncate sentence
1,816
0.821
Easy
25,929
https://leetcode.com/problems/truncate-sentence/discuss/2122612/one-liner-efficient-python-solution-or-96.28-efficient
class Solution: def truncateSentence(self, s: str, k: int) -> str: return " ".join(s.split(" ")[:k])
truncate-sentence
one liner efficient python solution | 96.28% efficient
nikhitamore
0
27
truncate sentence
1,816
0.821
Easy
25,930
https://leetcode.com/problems/truncate-sentence/discuss/2046236/Python3-simple-with-python
class Solution: def truncateSentence(self, s: str, k: int) -> str: return " ".join(s.split(' ')[:k])
truncate-sentence
[Python3] simple with python
Shiyinq
0
42
truncate sentence
1,816
0.821
Easy
25,931
https://leetcode.com/problems/truncate-sentence/discuss/2013859/Python-oneliner
class Solution: def truncateSentence(self, s: str, k: int) -> str: return ' '.join(s.split()[:k])
truncate-sentence
Python oneliner
StikS32
0
30
truncate sentence
1,816
0.821
Easy
25,932
https://leetcode.com/problems/truncate-sentence/discuss/1942304/Python-One-Liner-%2B-Optimization
class Solution: def truncateSentence(self, s, k): return " ".join(s.split()[:k])
truncate-sentence
Python - One-Liner + Optimization
domthedeveloper
0
40
truncate sentence
1,816
0.821
Easy
25,933
https://leetcode.com/problems/truncate-sentence/discuss/1942304/Python-One-Liner-%2B-Optimization
class Solution: def truncateSentence(self, s, k): return " ".join(s.split(maxsplit=k)[:k])
truncate-sentence
Python - One-Liner + Optimization
domthedeveloper
0
40
truncate sentence
1,816
0.821
Easy
25,934
https://leetcode.com/problems/truncate-sentence/discuss/1868092/Python-Easy-and-Fast-Python3
class Solution: def truncateSentence(self, s: str, k: int) -> str: sentence = '' l = s.split() i = 0 while i < k: sentence += l[i] sentence += ' ' i += 1 return sentence[:-1]
truncate-sentence
[Python] Easy and Fast Python3
natscripts
0
48
truncate sentence
1,816
0.821
Easy
25,935
https://leetcode.com/problems/truncate-sentence/discuss/1867153/Python-one-line-solution
class Solution: def truncateSentence(self, s: str, k: int) -> str: return ' '.join(s.split()[0:k])
truncate-sentence
Python one line solution
alishak1999
0
16
truncate sentence
1,816
0.821
Easy
25,936
https://leetcode.com/problems/truncate-sentence/discuss/1859170/Python-dollarolution-(one-line)
class Solution: def truncateSentence(self, s: str, k: int) -> str: return (' '.join(s.split()[:k]))
truncate-sentence
Python $olution (one line)
AakRay
0
17
truncate sentence
1,816
0.821
Easy
25,937
https://leetcode.com/problems/truncate-sentence/discuss/1831370/Python-easy-to-read-and-understand
class Solution: def truncateSentence(self, s: str, k: int) -> str: l = s.split(" ") if k >= len(l): return s ans = '' i = 0 while i < k: ans = ans + l[i] + ' ' i = i+1 return ans[:-1]
truncate-sentence
Python easy to read and understand
sanial2001
0
28
truncate sentence
1,816
0.821
Easy
25,938
https://leetcode.com/problems/truncate-sentence/discuss/1780823/Python-oneline
class Solution: def truncateSentence(self, s: str, k: int) -> str: return " ".join(s.split()[:k])
truncate-sentence
Python oneline
kakchaudhary
0
23
truncate sentence
1,816
0.821
Easy
25,939
https://leetcode.com/problems/truncate-sentence/discuss/1755694/Python-One-Liner%3A-Beats-98
class Solution: def truncateSentence(self, s: str, k: int) -> str: return ' '.join(s.split(' ')[:k])
truncate-sentence
Python One-Liner: Beats 98%
dos_77
0
44
truncate sentence
1,816
0.821
Easy
25,940
https://leetcode.com/problems/truncate-sentence/discuss/1739151/1816.-Truncate-Sentence-solution
class Solution: def truncateSentence(self, s: str, k: int) -> str: sent=s.split() str="" for x in range(0,k): if x>=0 and x<(k-1): str+=(sent[x]+" ") elif x==(k-1): str+=sent[x] return str
truncate-sentence
1816. Truncate Sentence solution
seabreeze
0
42
truncate sentence
1,816
0.821
Easy
25,941
https://leetcode.com/problems/truncate-sentence/discuss/1727006/Python3-or-One-line-solution
class Solution: def truncateSentence(self, s: str, k: int) -> str: return " ".join(s.split()[:k])
truncate-sentence
Python3 | One line solution
khalidhassan3011
0
33
truncate sentence
1,816
0.821
Easy
25,942
https://leetcode.com/problems/truncate-sentence/discuss/1685184/Python-one-line-solution-using-split-and-join-methods
class Solution: def truncateSentence(self, s: str, k: int) -> str: return " ".join(s.split()[:k]) #s.split() method will split each word after space and append it to the empty string using the .join() method where [:k] is same as [0:k] which defines the range of the words which we want to output.
truncate-sentence
Python one line solution using split and join methods
pawelborkar
0
57
truncate sentence
1,816
0.821
Easy
25,943
https://leetcode.com/problems/truncate-sentence/discuss/1678149/easy-simple-solution
class Solution: def truncateSentence(self, s: str, k: int) -> str: answer="" s=s.split() for i in range(k-1): answer+=s[i] answer += " " answer+=s[k-1] return answer
truncate-sentence
easy simple solution
Buyanjargal
0
60
truncate sentence
1,816
0.821
Easy
25,944
https://leetcode.com/problems/truncate-sentence/discuss/1627395/Python3%3A-simple-two-solution%3A-with-split-without-split
class Solution: def truncateSentence(self, s: str, k: int) -> str: #return " ".join(s.split()[0:k]) counter = 0 new_s = '' for c in s: if c == ' ': counter += 1 if counter == k: break new_s += c return new_s
truncate-sentence
Python3: simple two solution: with split, without split
CleverUzbek
0
47
truncate sentence
1,816
0.821
Easy
25,945
https://leetcode.com/problems/truncate-sentence/discuss/1610490/c%2B%2Bpython3-easy-approach
class Solution: def truncateSentence(self, s: str, k: int) -> str: return ' '.join(s.split(' ')[:k])
truncate-sentence
c++/python3 easy approach
qikang1994
0
38
truncate sentence
1,816
0.821
Easy
25,946
https://leetcode.com/problems/truncate-sentence/discuss/1589639/soln-without-using-any-built-in-methods-like-split()-or-python-or-faster-than-95
class Solution: def truncateSentence(self, s: str, k: int) -> str: count = idx = 0 # count refers to the no.of white spaces ans = "" while count != k: # if no.of words > no.of white spaces then count the last index as a white space if idx == len(s)-1 or s[idx] == " ": count += 1 ans += s[idx] idx += 1 return ans[:-1] if idx < len(s) else ans
truncate-sentence
soln without using any built in methods like split() | python | faster than 95%
anandanshul001
0
57
truncate sentence
1,816
0.821
Easy
25,947
https://leetcode.com/problems/truncate-sentence/discuss/1537221/Being-Pythonic%3A-Exploiting-py-features
class Solution: def truncateSentence(self, s: str, k: int) -> str: return ' '.join(s.split()[0:k])
truncate-sentence
Being Pythonic: Exploiting py features
_rust
0
26
truncate sentence
1,816
0.821
Easy
25,948
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/1141356/Python3-hash-map
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: mp = {} for i, t in logs: mp.setdefault(i, set()).add(t) ans = [0]*k for v in mp.values(): if len(v) <= k: ans[len(v)-1] += 1 return ans
finding-the-users-active-minutes
[Python3] hash map
ye15
9
597
finding the users active minutes
1,817
0.807
Medium
25,949
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/1143301/Simple-Python3-solution-with-2-layer-dictionary
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: # use two-layer dict # first layer record user; second layer record the active minutes # 1. Put info. in 2-layer dict O(n) d = {} for (user, active_min) in logs: if not (user in d): d[user] = {} user_log = d[user] user_log[active_min] = True # 2. Extract info. from 2-layer dict (at most O(n)) result = [0] * k for user in d: result[len(d[user])-1] += 1 # len(d[user]) must >= 1 to exist return result
finding-the-users-active-minutes
Simple Python3 solution with 2-layer dictionary
tkuo-tkuo
2
188
finding the users active minutes
1,817
0.807
Medium
25,950
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/1277381/Python3-solution-using-single-dictionary
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: d = {} for i in logs: if i[0] in d: if i[1] not in d[i[0]]: d[i[0]] = d[i[0]] + [i[1]] else: d[i[0]] = [i[1]] res = [0]*k for i in d.values(): res[len(i)-1] += 1 return res
finding-the-users-active-minutes
Python3 solution using single dictionary
EklavyaJoshi
1
142
finding the users active minutes
1,817
0.807
Medium
25,951
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/2837107/golang-python-rust-solution
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: res = [0] * k users = {} for id,time in logs: if id not in users: users[id] = set() users[id].add(time) else: users[id].add(time) for user in users: res[len(users[user])-1]+=1 return res
finding-the-users-active-minutes
golang python rust solution
anshsharma17
0
2
finding the users active minutes
1,817
0.807
Medium
25,952
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/2775142/Python3-Solution-beats-81
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: dct = {} ans = [0] * k for log in logs: if log[0] in dct: dct[log[0]].add(log[1]) else: dct[log[0]] = {log[1]} for i in dct.values(): ans[len(i)-1] = ans[len(i)-1] + 1 return ans
finding-the-users-active-minutes
Python3 Solution, beats 81%
sipi09
0
2
finding the users active minutes
1,817
0.807
Medium
25,953
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/2719369/Python-3-Set-Counter-Counter-w-List-Comprehension
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: c = Counter(Counter([i for i, j in set([(id, tim) for [id, tim] in logs])]).values()) return [c[i+1] for i in range(k)]
finding-the-users-active-minutes
Python 3 Set-Counter-Counter w/ List Comprehension
godshiva
0
4
finding the users active minutes
1,817
0.807
Medium
25,954
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/2700068/MAX-TIME-PYTHON
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: h={} for (i,j) in logs: if i not in h: h[i]=[j] else: if j not in h[i]: h[i].append(j) a=[0]*k for keys in h.values(): a[len(keys)-1]+=1 return a
finding-the-users-active-minutes
MAX TIME PYTHON
2001640100048_2C
0
2
finding the users active minutes
1,817
0.807
Medium
25,955
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/2666049/python3-Faster-than-97
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: users = {} for log in logs: user = log[0] time = log[1] if user in users: users[user].add(time) else: users[user] = set() users[user].add(time) sol = {} for i in users.values(): length = len(i) if length in sol: sol[length] += 1 else: sol[length] = 1 ans = [0]*k for i , j in sol.items(): ans[i-1] = j return ans
finding-the-users-active-minutes
python3 Faster than 97%
Noisy47
0
6
finding the users active minutes
1,817
0.807
Medium
25,956
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/2647969/Can-you-tell-me-why-this-is-slow-Python3-soon
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: res = [] for i in range (0,k): res.append(0) d = {} # id : [minute1, minute2] for pair in logs: id, min = pair[0], pair[1] if id not in d: d[id] = [min] res[0] += 1 else: if min not in d[id]: d[id].append(min) newUam = len(d[id]) res[newUam-2] -= 1 res[newUam-1] += 1 return res
finding-the-users-active-minutes
Can you tell me why this is slow? Python3 soon
egeergull
0
3
finding the users active minutes
1,817
0.807
Medium
25,957
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/2204539/Hash-map-by-Python-easy-to-understand
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: ''' We can use a dict to record the action minutes of every user, the key is the ID, the value are the minutes this user performs actions. Then, use set to remove the same minutes, and count the UAMs. Time complexity: O(n), space comlexity: O(n) ''' counter=collections.defaultdict(list) for i in logs: counter[i[0]].append(i[1]) ans=[0]*k for i in counter.keys(): ans[len(set(counter[i]))-1]+=1 return ans
finding-the-users-active-minutes
Hash map by Python, easy to understand
XRFXRF
0
49
finding the users active minutes
1,817
0.807
Medium
25,958
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/2198998/Faster-than-88-python
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: users = {} ans = [0] * k for el in logs: if el[0] not in users: users[el[0]] = set([el[1]]) else: users[el[0]].update([el[1]]) for user in users.keys(): uam = len(users[user]) ans[uam-1] += 1 return ans
finding-the-users-active-minutes
Faster than 88% python
ketan12sharma
0
38
finding the users active minutes
1,817
0.807
Medium
25,959
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/2123917/python-3-oror-simple-hash-map-and-hash-set-solutionoror-O(n)O(n)
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: userMinutes = collections.defaultdict(set) for user, minute in logs: userMinutes[user].add(minute) answer = [0] * k for uam in map(len, userMinutes.values()): if 1 <= uam <= k: answer[uam - 1] += 1 return answer
finding-the-users-active-minutes
python 3 || simple hash map and hash set solution|| O(n)/O(n)
dereky4
0
84
finding the users active minutes
1,817
0.807
Medium
25,960
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/1729560/Python-Easy-Solution
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: temp=defaultdict(set) res=[0]*k for log in logs: temp[log[0]].add(log[1]) for v in temp.values(): res[len(v)-1]+=1 return res
finding-the-users-active-minutes
Python Easy Solution
shandilayasujay
0
107
finding the users active minutes
1,817
0.807
Medium
25,961
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/1708665/Understandable-code-for-beginners-like-me-in-python-!!
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: answer=[0]*k userIdmap={} for log in logs: userid=log[0] activeminute=log[1] if userid not in userIdmap: userIdmap[userid]=list() userIdmap[userid].append(activeminute) else: if(activeminute not in userIdmap[userid]): userIdmap[userid].append(activeminute) for userid,activeminute in userIdmap.items(): userIdmap[userid]=len(activeminute) for userid,count in userIdmap.items(): answer[count-1]+=1 return answer
finding-the-users-active-minutes
Understandable code for beginners like me in python !!
kabiland
0
62
finding the users active minutes
1,817
0.807
Medium
25,962
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/1673056/Python3-oror-Hashmap-oror-O(n)-time-oror-O(n)-space
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: log_dict = {} for idx, time in logs: log_dict.setdefault(idx,set()).add(time) res = [0] * k for key in log_dict: log_dict[key] = len(log_dict[key]) res[log_dict[key]-1] += 1 return res
finding-the-users-active-minutes
Python3 || Hashmap || O(n) time || O(n) space
s_m_d_29
0
79
finding the users active minutes
1,817
0.807
Medium
25,963
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/1608022/Python3-Hashmap-%2B-hashset-solution
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: user2uams = collections.defaultdict(set) for user_id, minute in logs: user2uams[user_id].add(minute) res = [0] * k for key in user2uams: res[len(user2uams[key]) - 1] += 1 return res
finding-the-users-active-minutes
[Python3] Hashmap + hashset solution
maosipov11
0
67
finding the users active minutes
1,817
0.807
Medium
25,964
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/1371236/Python3-Easy-to-Understand
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: returned_counters = [0 for i in range(0, k)] refer_dict = {} for array in logs: refer_dict[array[0]] = [] for array in logs: if array[1] not in refer_dict[array[0]]: refer_dict[array[0]].append(array[1]) for key, val in refer_dict.items(): uam = len(val) returned_counters[uam - 1] += 1 return returned_counters
finding-the-users-active-minutes
Python3 Easy to Understand
RobertObrochta
0
65
finding the users active minutes
1,817
0.807
Medium
25,965
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/1211433/Simplest-python-3-approach-using-dictionary-(-hash-map-)-or-faster-than-97
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: userDict = dict() """ below we create a dictionary where the user ID is the key and the value is a python set of the minutes the user was active we create a set because we only want unique minutes to be counted """ for log in logs: if(log[0] not in userDict): userDict[log[0]] = set() userDict[log[0]].add(log[1]) """ for the eg; logs = [[0,5],[1,2],[0,2],[0,5],[1,3]], k = 5 userDict will look like { 0: {2, 5}, 1: {2, 3} } which means user-0 was active for minutes 2,5 and so on. Then below we iterate over the dict and count the len of set for each key which gives us the total no of *UNIQUE* minutes the user was active for eg; the user-0 was active for two unique minutes cuz the len of its set is 2 then we increment the corresponding index i in our answer array by 1 which indicates that 1 user was active for i minutes eg; our answer array will be [0,2,0,0,0] which tells us there are 2 users that were active for 2 *unique* minutes """ answer = [0]*k for user in userDict: answer[len(userDict[user])-1] += 1 return answer
finding-the-users-active-minutes
Simplest python 3 approach using dictionary ( hash map ) | faster than 97%
thecoder_elite
0
103
finding the users active minutes
1,817
0.807
Medium
25,966
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/1166855/Python3-Easy-Solution-with-Comments-Used-Set-and-Map
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: ''' 1. Use a dictionary where each key represents a user and the value is the set of minutes when the user was active 2. Initialize the 1-indexed resultant array with k zeros 3. For each logs[i] in list logs -add time_i to the set s_i of key ID_i -update the resultant array's index x where x = no. of elements in s_i - 1, as the array is actually 0 indexed ''' cnt_dict = {} #dictionary of set of unique active minutes for each user res = [0]*k #resultant ouput array, initialized by zeros for log in logs: if log[0] in cnt_dict.keys(): #a key in the dictionary is already present for this user id res[len(cnt_dict[log[0]])-1]-=1 #as the active minute is going to be added to the set, decrease the count of the index (i = no. of elements in set -1) in the array res cnt_dict[log[0]].add(log[1]) #add this time to the set for this user in the dictionary res[len(cnt_dict[log[0]])-1]+=1 #as a new user of this count may be obtained, increase the count of the index (i = no. of elements in set -1) in the array res else: cnt_dict[log[0]]=set() #no key present for this user in the dictionary, initialize empty set cnt_dict[log[0]].add(log[1]) #add this time to the set for this user in the dictionary res[len(cnt_dict[log[0]])-1]+=1 #as a new user of this count may be obtained, increase the count of the index (i = no. of elements in set -1) in the array res return res
finding-the-users-active-minutes
Python3 Easy Solution with Comments, Used Set and Map
bPapan
0
106
finding the users active minutes
1,817
0.807
Medium
25,967
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/1141812/Python-Solution
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: users = {} answer = [0] * k for idd, time in logs: if idd not in users: users[idd] = set() users[idd].add(time) else: users[idd].add(time) for user in users: answer[len(users[user]) - 1] += 1 return answer
finding-the-users-active-minutes
Python Solution
mariandanaila01
0
75
finding the users active minutes
1,817
0.807
Medium
25,968
https://leetcode.com/problems/finding-the-users-active-minutes/discuss/1141458/Simple-Python-3
class Solution: def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]: log = defaultdict(set) for x, y in logs: log[x].add(y) output = [0]*k for i in log.values(): output[len(i)-1] += 1 return output
finding-the-users-active-minutes
Simple, Python 3
VijayantShri
0
44
finding the users active minutes
1,817
0.807
Medium
25,969
https://leetcode.com/problems/minimum-absolute-sum-difference/discuss/1715575/Python-%2B-Fully-Explained-%2B-Best-Solution
class Solution: def minAbsoluteSumDiff(self, nums1: List[int], nums2: List[int]) -> int: n = len(nums1) diff = [] sum = 0 for i in range(n): temp = abs(nums1[i]-nums2[i]) diff.append(temp) sum += temp nums1.sort() best_diff = [] for i in range(n): idx = bisect.bisect_left(nums1, nums2[i]) if idx != 0 and idx != n: best_diff.append( min(abs(nums2[i]-nums1[idx]), abs(nums2[i]-nums1[idx-1]))) elif idx == 0: best_diff.append(abs(nums2[i]-nums1[idx])) else: best_diff.append(abs(nums2[i]-nums1[idx-1])) saved = 0 for i in range(n): saved = max(saved, diff[i]-best_diff[i]) return (sum-saved) % ((10**9)+(7))
minimum-absolute-sum-difference
[Python] + Fully Explained + Best Solution ✔
leet_satyam
10
540
minimum absolute sum difference
1,818
0.302
Medium
25,970
https://leetcode.com/problems/minimum-absolute-sum-difference/discuss/1141447/Python-Binary-Search
class Solution: def minAbsoluteSumDiff(self, nums1: List[int], nums2: List[int]) -> int: nums1, nums2 = zip(*sorted(zip(nums1, nums2))) mad = [abs(nums1[i] - nums2[i]) for i in range(len(nums1))] M = sum(mad) MOD = 10**9 + 7 best = 0 for i in range(len(nums1)): if nums1[i] != nums2[i]: j = bisect.bisect_left(nums1, nums2[i]) if j == len(nums1): best = max(best, mad[i] - abs(nums1[-1] - nums2[i])) elif j == 0: best = max(best, mad[i] - abs(nums1[0] - nums2[i])) else: new = min(abs(nums1[j] - nums2[i]), abs(nums1[j-1] - nums2[i])) best = max(best, mad[i] - new) return (M - best) % MOD
minimum-absolute-sum-difference
[Python] Binary Search
rowe1227
4
384
minimum absolute sum difference
1,818
0.302
Medium
25,971
https://leetcode.com/problems/minimum-absolute-sum-difference/discuss/1141371/Python3-greedy
class Solution: def minAbsoluteSumDiff(self, nums1: List[int], nums2: List[int]) -> int: s1 = sorted(nums1) ans = bnft = 0 for x, y in zip(nums1, nums2): ans += abs(x - y) k = bisect_left(s1, y) if k < len(s1): bnft = max(bnft, abs(x - y) - (s1[k] - y)) # benefit of replacing x to s1[k] if 0 < k: bnft = max(bnft, abs(x - y) - (y - s1[k-1])) # benefit of replacing x to s1[k-1] return (ans - bnft) % 1_000_000_007
minimum-absolute-sum-difference
[Python3] greedy
ye15
3
280
minimum absolute sum difference
1,818
0.302
Medium
25,972
https://leetcode.com/problems/minimum-absolute-sum-difference/discuss/1665756/Python-O(nlogn)-time-O(n)-space-solution-using-sorting-%2B-binary-search
class Solution: def minAbsoluteSumDiff(self, nums1: List[int], nums2: List[int]) -> int: n = len(nums1) total = 0 for i in range(n): total += abs(nums1[i] - nums2[i]) s = sorted(nums1) def minimize(x): left, right = 0, n-1 while left < right: mid = (left + right) // 2 if s[mid] >= x: right = mid else: left = mid + 1 if left == 0: return s[left]-x else: return min(abs(s[left]-x), abs(x-s[left-1])) res = float('inf') for i in range(n): t = total - abs(nums1[i]-nums2[i]) c = minimize(nums2[i]) res = min(res, t + c) return min(res, total) % (10**9 + 7)
minimum-absolute-sum-difference
Python O(nlogn) time, O(n) space solution using sorting + binary search
byuns9334
1
150
minimum absolute sum difference
1,818
0.302
Medium
25,973
https://leetcode.com/problems/minimum-absolute-sum-difference/discuss/2260640/Python3-or-binary-search-or-commented
class Solution: def minAbsoluteSumDiff(self, nums1: List[int], nums2: List[int]) -> int: nums3 = sorted(nums1) n = [-1 for i in range(len(nums1))] # i = index in nums1 | n[i] = index in nums3 | the best diff for every nums1[i] is nums3[n[i]] - nums2[i] pos = -math.inf m_diff, s = 0, 0 for i in range(len(nums2)): m1 = bisect.bisect_left(nums3, nums2[i]) # m1 = upper and m2 = lower if m1 >= len(nums1): # in case m1 is outside the array's range m1 = len(nums1)-1 m2 = m1 - 1 val = abs(nums1[i] - nums2[i]) if abs(nums3[m1] - nums2[i]) < val: val = abs(nums3[m1] - nums2[i]) n[i] = m1 if m2 >= 0: if abs(nums3[m2] - nums2[i]) < val: n[i] = m2 for i in range(len(nums3)): #get the maximum change in difference and the position to change in order to get that currentDiff = abs(nums1[i] - nums2[i]) newDiff = abs(nums3[n[i]] - nums2[i]) if n[i] != -1 and currentDiff > newDiff and currentDiff - newDiff > m_diff: m_diff = currentDiff - newDiff pos = i if pos != -math.inf: nums1[pos] = nums3[n[pos]] # no change to be made if pos == -inf for i in range(len(nums1)): s += abs(nums1[i] - nums2[i]) return s % (10**9 + 7)
minimum-absolute-sum-difference
Python3 | binary search | commented
FlorinnC1
0
174
minimum absolute sum difference
1,818
0.302
Medium
25,974
https://leetcode.com/problems/minimum-absolute-sum-difference/discuss/1351088/Bisect-for-distinct-97-speed
class Solution: def minAbsoluteSumDiff(self, nums1: List[int], nums2: List[int]) -> int: distinct1 = sorted(set(nums1)) len1 = len(distinct1) max_diff = sum_abs_diff = 0 for a, b in zip(nums1, nums2): abs_diff = abs(a - b) sum_abs_diff += abs_diff idx = bisect_left(distinct1, b) if idx < len1: max_diff = max(max_diff, abs_diff - abs(distinct1[idx] - b)) if idx > 0: max_diff = max(max_diff, abs_diff - abs(distinct1[idx - 1] - b)) return (sum_abs_diff - max_diff) % 1_000_000_007
minimum-absolute-sum-difference
Bisect for distinct, 97% speed
EvgenySH
0
167
minimum absolute sum difference
1,818
0.302
Medium
25,975
https://leetcode.com/problems/minimum-absolute-sum-difference/discuss/1142385/python3-Solution-for-ref-with-sort-greedy-algorithm-%2B-binary-search
class Solution: def minAbsoluteSumDiff(self, nums1: List[int], nums2: List[int]) -> int: ## Sort the array to prepare for binary search snums1 = sorted(nums1) ## Binary search solution for finding the closest value based on absolute difference is picked up as block from here (since already exists). ## https://tutorialspoint.dev/algorithm/divide-and-conquer/find-closest-number-array def find_closest_abs_diff(arr, n, target): if (target <= arr[0]): return arr[0] if (target >= arr[n - 1]): return arr[n - 1] i = 0; j = n; mid = 0 while (i < j): mid = (i + j) // 2 if (arr[mid] == target): return arr[mid] if (target < arr[mid]) : if (mid > 0 and target > arr[mid - 1]): return getClosest(arr[mid - 1], arr[mid], target) j = mid else : if (mid < n - 1 and target < arr[mid + 1]): return getClosest(arr[mid], arr[mid + 1], target) i = mid + 1 return arr[mid] def getClosest(val1, val2, target): if (target - val1 >= val2 - target): return val2 else: return val1 ## Find absolute difference values of array's absdiffarr = [abs(nums1[idx] - nums2[idx]) for idx in range(len(nums1))] ## Find the sum of absolute differences, we are trying to minimize this value. absdiff = sum(absdiffarr) output = float('inf') ## go over the arrays and find the value that can be replaced to minimize absolute difference. for idx in range(len(nums1)): change = find_closest_abs_diff(snums1, len(nums1), nums2[idx]) ## remove the current absolute difference at index and check for minimized value, ## if we replace that with the closest value found in the array. output = min(output, absdiff-absdiffarr[idx]+(abs(change - nums2[idx]))) return output % (10**9+7)
minimum-absolute-sum-difference
[python3] Solution for ref with sort, greedy algorithm + binary search
vadhri_venkat
0
90
minimum absolute sum difference
1,818
0.302
Medium
25,976
https://leetcode.com/problems/minimum-absolute-sum-difference/discuss/1141379/Accepted-O(N)-Easy-to-understand-solution-or-Inline-comments-for-explanation
class Solution: def minAbsoluteSumDiff(self, nums1: List[int], nums2: List[int]) -> int: n = len(nums1) MOD = 10**9+7 # Maintain a MAX heap defined as structure - (abs_diffi, nums1[i], nums[j]) heap = [] abs_sum = 0 for i in range(n): abs_diff = abs(nums1[i]-nums2[i]) abs_sum = (abs_sum+abs_diff)%MOD heapq.heappush(heap,(-abs_diff, nums1[i], nums2[i])) # note the -ve sign to make it max_heap print(heap) # Get the element with max absolute difference from heap top = heapq.heappop(heap) abs_sum -= abs(top[0]) # subtract previous abs_diff as we'll update it below k = top[2] # find a new_val closest to k # and min_diff is the new absoulte diff with k min_diff = abs(top[0]) new_val = 0 for num1 in nums1: new_diff = abs(num1-k) if new_diff<min_diff: min_diff = new_diff new_val = num1 # add the new diff to calculate updated sum abs_sum = (abs_sum+min_diff)%MOD return abs_sum
minimum-absolute-sum-difference
[Accepted] O(N) Easy to understand solution | Inline comments for explanation
CaptainX
0
206
minimum absolute sum difference
1,818
0.302
Medium
25,977
https://leetcode.com/problems/number-of-different-subsequences-gcds/discuss/1144445/Python3-enumerate-all-possibilities
class Solution: def countDifferentSubsequenceGCDs(self, nums: List[int]) -> int: nums = set(nums) ans = 0 m = max(nums) for x in range(1, m+1): g = 0 for xx in range(x, m+1, x): if xx in nums: g = gcd(g, xx) if g == x: ans += 1 return ans
number-of-different-subsequences-gcds
[Python3] enumerate all possibilities
ye15
4
234
number of different subsequences gcds
1,819
0.385
Hard
25,978
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/1152412/Python3-line-sweep
class Solution: def arraySign(self, nums: List[int]) -> int: ans = 1 for x in nums: if x == 0: return 0 if x < 0: ans *= -1 return ans
sign-of-the-product-of-an-array
[Python3] line sweep
ye15
58
6,300
sign of the product of an array
1,822
0.66
Easy
25,979
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/1499851/Solution-using-xor-and-only-a-bool-for-memory
class Solution: def arraySign(self, nums: List[int]) -> int: signs = False for x in nums: if x == 0 : return 0 signs = signs ^ (x < 0) if signs : return -1 else: return 1
sign-of-the-product-of-an-array
Solution using xor and only a bool for memory
somedev12
5
221
sign of the product of an array
1,822
0.66
Easy
25,980
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2224875/Python3-simple-one-liner
class Solution: def arraySign(self, nums: List[int]) -> int: def signFun(x): return 1 if x > 0 else -1 if x < 0 else 0 return signFun(math.prod(nums))
sign-of-the-product-of-an-array
📌 Python3 simple one liner
Dark_wolf_jss
3
52
sign of the product of an array
1,822
0.66
Easy
25,981
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/1200880/WEEB-DOES-PYTHON-(BEATS-99.32)
class Solution: def arraySign(self, nums: List[int]) -> int: nums, count = sorted(nums), 0 for i in range(len(nums)): if nums[i] == 0: return 0 if nums[i] > 0: if count % 2 == 0: return 1 else: return -1 count+=1 if count % 2 == 0: # if all are negative return 1 else: return -1
sign-of-the-product-of-an-array
WEEB DOES PYTHON (BEATS 99.32%)
Skywalker5423
2
281
sign of the product of an array
1,822
0.66
Easy
25,982
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2013972/Python-Simple-Solution-or-Time-%3A-O(n)
class Solution: def arraySign(self, nums: List[int]) -> int: negative = 1 for number in nums: if number == 0: return 0 elif number < 0: negative *= -1 return negative
sign-of-the-product-of-an-array
Python Simple Solution | Time : O(n)
Call-Me-AJ
1
70
sign of the product of an array
1,822
0.66
Easy
25,983
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2013972/Python-Simple-Solution-or-Time-%3A-O(n)
class Solution: def arraySign(self, nums: List[int]) -> int: product = 1 for number in nums: product *= number return 0 if product == 0 else -1 if product < 0 else 1
sign-of-the-product-of-an-array
Python Simple Solution | Time : O(n)
Call-Me-AJ
1
70
sign of the product of an array
1,822
0.66
Easy
25,984
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/1851671/Python3-straightforward-solution
class Solution: def arraySign(self, nums: List[int]) -> int: p = math.prod(nums) if p > 0: return 1 elif p < 0: return -1 else: return 0
sign-of-the-product-of-an-array
Python3 straightforward solution
alishak1999
1
44
sign of the product of an array
1,822
0.66
Easy
25,985
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/1851425/2-Lines-Python-Solution-oror-92-Faster-(60ms)-oror-Memory-less-than-65
class Solution: def arraySign(self, nums: List[int]) -> int: p = reduce(mul, nums) return 1 if p>0 else -1 if p<0 else 0
sign-of-the-product-of-an-array
2-Lines Python Solution || 92% Faster (60ms) || Memory less than 65%
Taha-C
1
80
sign of the product of an array
1,822
0.66
Easy
25,986
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/1830272/Python-or-Time%3A-O(n)-or-Space%3A-O(1)
class Solution: def arraySign(self, nums: List[int]) -> int: sign = 1 for n in nums: if n == 0: return 0 elif n < 0: sign = -sign return sign
sign-of-the-product-of-an-array
Python | Time: O(n) | Space: O(1)
nashvenn
1
29
sign of the product of an array
1,822
0.66
Easy
25,987
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/1363381/simple-and-easy-using-python
class Solution: def arraySign(self, nums: List[int]) -> int: t=1 for i in range(len(nums)): t = t* nums[i] if t> 0: return 1 elif t<0: return -1 else: return 0
sign-of-the-product-of-an-array
simple & easy using python
gulsan
1
117
sign of the product of an array
1,822
0.66
Easy
25,988
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/1175539/Sign-of-product-(Fast-and-easy-solution)
class Solution: def arraySign(self, nums: List[int]) -> int: lst = nums prod = 1 for i in lst: prod = prod * i if prod > 0: return 1 elif prod < 0: return -1 elif prod == 0: return 0
sign-of-the-product-of-an-array
Sign of product (Fast and easy solution)
iamvatsalpatel
1
90
sign of the product of an array
1,822
0.66
Easy
25,989
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2793239/python-simplest
class Solution: def arraySign(self, nums: List[int]) -> int: x=1 for i in range(len(nums)): x=x*nums[i] if x==0: return 0 elif x<0: return -1 else: return 1
sign-of-the-product-of-an-array
[python]--simplest
user9516zM
0
3
sign of the product of an array
1,822
0.66
Easy
25,990
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2760779/Python-Solution-All-approach
class Solution: def arraySign(self, nums: List[int]) -> int: count_negative = 0 for ele in nums: if ele<0: count_negative+=1 elif ele==0: return 0 if count_negative&amp;1: return -1 return 1 #ANother approach ''' product=1 for i in nums: if i ==0: return 0 elif i<0: product*=-1 return product # Brute FORCE n=len(nums) prod=1 for i in range (n): prod=prod*nums[i] if prod==0: return 0 elif prod<0: return -1 elif prod>0: return 1''' #Now lets's optimise this code ''' if 0 in nums: return 0 count=0 for i in nums: if i<0: count+=1 if count%2==0: return 1 else: return -1'''
sign-of-the-product-of-an-array
Python Solution All approach
mritunjayyy
0
3
sign of the product of an array
1,822
0.66
Easy
25,991
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2759362/Simple-Python3-Solution-Better-Runtime
class Solution: def arraySign(self, nums: List[int]) -> int: if 0 in nums: return 0 result = 0 for x in nums: if x < 0: result += 1 return 1 if result %2 == 0 else -1
sign-of-the-product-of-an-array
Simple Python3 Solution Better Runtime
vivekrajyaguru
0
1
sign of the product of an array
1,822
0.66
Easy
25,992
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2711251/optimized-solution-python-faster-than-92-submissions
class Solution(object): def arraySign(self, nums): """ :type nums: List[int] :rtype: int """ neg_num=0 for i in nums: if i==0: return 0; elif i<1: neg_num+=1 if neg_num %2 == 0: return 1 return -1 #up vote is appreciated
sign-of-the-product-of-an-array
optimized solution python faster than 92% submissions
ojasgupta25
0
4
sign of the product of an array
1,822
0.66
Easy
25,993
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2702115/Python-or-Less-memory-than-98.3-or-Without-calculating-product
class Solution: def arraySign(self, nums: List[int]) -> int: negative_count = 0 for n in nums: if n == 0: return 0 elif n < 0: negative_count += 1 if negative_count % 2 == 0: return 1 else: return -1
sign-of-the-product-of-an-array
✅ Python | Less memory than 98.3% | Without calculating product
anandanshul001
0
6
sign of the product of an array
1,822
0.66
Easy
25,994
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2692819/Easy-Solution
class Solution: def arraySign(self, nums: List[int]) -> int: res=1 for i in nums: res*=i if(res==0): return 0 elif(res>0): return 1 return -1
sign-of-the-product-of-an-array
Easy Solution
Raghunath_Reddy
0
4
sign of the product of an array
1,822
0.66
Easy
25,995
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2692818/Easy-Solution
class Solution: def arraySign(self, nums: List[int]) -> int: res=1 for i in nums: res*=i if(res==0): return 0 elif(res>0): return 1 return -1
sign-of-the-product-of-an-array
Easy Solution
Raghunath_Reddy
0
2
sign of the product of an array
1,822
0.66
Easy
25,996
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2682891/Python3-or-Easy
class Solution: def arraySign(self, nums: List[int]) -> int: p = 1 # product if 0 in nums: return 0 for i in nums: if i < 0: p *= -1 if p > 0: return 1 else: return -1
sign-of-the-product-of-an-array
Python3 | Easy
AnzheYuan1217
0
17
sign of the product of an array
1,822
0.66
Easy
25,997
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2667803/Python-or-O(n)-solution
class Solution: def arraySign(self, nums: List[int]) -> int: minus, zero = 0, False for num in nums: if num == 0: zero = True break if num < 0: minus += 1 if zero: return 0 if minus % 2: return -1 return 1
sign-of-the-product-of-an-array
Python | O(n) solution
LordVader1
0
9
sign of the product of an array
1,822
0.66
Easy
25,998
https://leetcode.com/problems/sign-of-the-product-of-an-array/discuss/2665144/Python3-Solution.-NO-need-to-calculate-Product
class Solution: def arraySign(self, nums: List[int]) -> int: product = 1 for x in nums: if x == 0: return 0 if x < 0: product *= -1 return product
sign-of-the-product-of-an-array
Python3 Solution. [NO need to calculate Product]
Udaay
0
10
sign of the product of an array
1,822
0.66
Easy
25,999