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/minimum-swaps-to-group-all-1s-together-ii/discuss/1677016/Python-3-Sliding-window-to-solution
|
class Solution:
def minSwaps(self, nums: List[int]) -> int:
one_len = sum(nums)
left, right = 0,one_len #window width
loop_num = nums + nums
ans = float('inf')
len_sum = sum(nums[left:right])
while right <=len(loop_num):
ans = min(one_len - len_sum,ans)
if right != len(loop_num):
len_sum += loop_num[right]
len_sum -= loop_num[left]
right += 1
left += 1
return ans
|
minimum-swaps-to-group-all-1s-together-ii
|
Python 3 Sliding window to solution
|
AndrewHou
| 0
| 60
|
minimum swaps to group all 1s together ii
| 2,134
| 0.507
|
Medium
| 29,600
|
https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together-ii/discuss/1676997/Clear-Typical-Python-3-Sliding-window-with-Explanation
|
class Solution:
def minSwaps(self, nums: List[int]) -> int:
cntone = 0
# First, compute how many 1s here
for n in nums:
if n == 1:
cntone += 1
l, res = 0, float('inf')
# Doing this for circular array
nums.extend(nums)
need = {0:0, 1:0}
# Start sliding window
for r, val in enumerate(nums):
need[val] += 1
# our window should group all 1 together
if need[0] + need[1] == cntone:
res = min(res, need[0])
need[nums[l]] -= 1
l += 1
return res if res != float('inf') else 0
|
minimum-swaps-to-group-all-1s-together-ii
|
Clear, Typical Python 3 Sliding window with Explanation
|
Swimmingatease
| 0
| 58
|
minimum swaps to group all 1s together ii
| 2,134
| 0.507
|
Medium
| 29,601
|
https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/discuss/1676852/Python3-bitmask
|
class Solution:
def wordCount(self, startWords: List[str], targetWords: List[str]) -> int:
seen = set()
for word in startWords:
m = 0
for ch in word: m ^= 1 << ord(ch)-97
seen.add(m)
ans = 0
for word in targetWords:
m = 0
for ch in word: m ^= 1 << ord(ch)-97
for ch in word:
if m ^ (1 << ord(ch)-97) in seen:
ans += 1
break
return ans
|
count-words-obtained-after-adding-a-letter
|
[Python3] bitmask
|
ye15
| 84
| 5,900
|
count words obtained after adding a letter
| 2,135
| 0.428
|
Medium
| 29,602
|
https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/discuss/1677044/Help-with-the-testcase
|
class Solution:
def wordCount(self, startWords: List[str], targetWords: List[str]) -> int:
sw = {}
for ss in startWords:
sw[ss] = Counter(ss)
S = set()
for target in targetWords:
counter_t = Counter(target)
for ss in sw:
if len(target) == len(ss) + 1 and len(counter_t.keys()) == len(sw[ss].keys()) + 1:
s = sum([val for key, val in (sw[ss] & counter_t).items()])
if s == len(ss):
S.add(target)
break
return len(S)
|
count-words-obtained-after-adding-a-letter
|
Help with the testcase
|
ernieyang09
| 10
| 412
|
count words obtained after adding a letter
| 2,135
| 0.428
|
Medium
| 29,603
|
https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/discuss/2083464/Clean-Python-Using-Set-No-Sorting-Binary-Char-Representation
|
class Solution:
def wordCount(self, startWords: List[str], targetWords: List[str]) -> int:
# represent chars in word with binary encoding "ca" = (1,0,1,...,0)
def createWordTuple(word):
ans = [0]*26
for c in word:
ans[ord(c) - ord('a')] = 1
return tuple(ans)
# create set with binary encoded words
words = set()
for word in startWords:
words.add(createWordTuple(word))
# for each targetWord remove one char and look in the set whether
# the reduced binary encoded character string is there
ans = 0
for word in targetWords:
for i in range(len(word)):
cutWord = word[:i] + word[i+1:]
if createWordTuple(cutWord) in words:
ans += 1
break
return ans
|
count-words-obtained-after-adding-a-letter
|
Clean Python, Using Set, No Sorting, Binary Char Representation
|
boris17
| 4
| 237
|
count words obtained after adding a letter
| 2,135
| 0.428
|
Medium
| 29,604
|
https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/discuss/1695229/Simple-String-Sorting-and-Hash-Map
|
class Solution:
def wordCount(self, s: List[str], t: List[str]) -> int:
d={}
for i in s:
x = "".join(sorted(i))
d[x] = 1
ans=0
for i in t:
i = "".join(sorted(i))
for j in range(len(i)):
x = i[:j]+i[j+1:]
if x in d:
ans+=1
break
return ans
|
count-words-obtained-after-adding-a-letter
|
Simple String Sorting & Hash-Map
|
gamitejpratapsingh998
| 2
| 328
|
count words obtained after adding a letter
| 2,135
| 0.428
|
Medium
| 29,605
|
https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/discuss/1679513/Python-bitwise-2-liner
|
class Solution:
def wordCount(self, startWords: List[str], targetWords: List[str]) -> int:
startWords = set([reduce(lambda res, c: res ^ (1 << (ord(c) - ord('a'))), word, 0) for word in startWords])
return len(list(filter(lambda tup: any(tup[0] ^ (1 << (ord(c) - ord('a'))) in startWords for c in tup[1]), list(zip([reduce(lambda res, c: res ^ (1 << (ord(c) - ord('a'))), word, 0) for word in targetWords], targetWords)))))
|
count-words-obtained-after-adding-a-letter
|
Python bitwise 2 liner
|
davidc360
| 2
| 78
|
count words obtained after adding a letter
| 2,135
| 0.428
|
Medium
| 29,606
|
https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/discuss/2703906/Python-or-Hashing-or-Bit-signature-for-anagrams
|
class Solution:
def wordCount(self, start_words: List[str], target_words: List[str]) -> int:
def signature(word):
sign = 0
for ch in word:
sign ^= 1 << (ord(ch) - 97)
return sign
start_signatures = set([signature(w) for w in start_words])
start_lengths = set([len(w) for w in start_words])
ans = 0
for word in target_words:
if (len(word) - 1) not in start_lengths:
continue
sign = signature(word)
for ch in word:
p = 1 << (ord(ch) - 97)
sign ^= p # remove character
if sign in start_signatures:
ans += 1
break
sign ^= p # add removed character back
return ans
|
count-words-obtained-after-adding-a-letter
|
Python | Hashing | Bit signature for anagrams
|
on_danse_encore_on_rit_encore
| 0
| 5
|
count words obtained after adding a letter
| 2,135
| 0.428
|
Medium
| 29,607
|
https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/discuss/2268679/Suggestions-on-optimization
|
class Solution:
def wordCount(self, startWords: List[str], targetWords: List[str]) -> int:
start_len={}
for word in startWords:
if len(word) in start_len:
start_len[len(word)].append(word)
else:
start_len[len(word)]=[word]
res=0
for word in targetWords:
if not start_len.get(len(word)-1,0):
continue
for starter in start_len[len(word)-1]:
if len(set(word)-set(starter))==1:
res+=1
break
return res
|
count-words-obtained-after-adding-a-letter
|
Suggestions on optimization
|
mayankbhola1
| 0
| 43
|
count words obtained after adding a letter
| 2,135
| 0.428
|
Medium
| 29,608
|
https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/discuss/1692730/Python-using-sorted-strings-as-hash
|
class Solution:
def wordCount(self, startWords: List[str], targetWords: List[str]) -> int:
sws = set()
for sw in startWords:
for c in set(string.ascii_lowercase) - set(sw):
sws.add("".join(sorted(sw + c)))
return sum("".join(sorted(tw)) in sws for tw in targetWords)
|
count-words-obtained-after-adding-a-letter
|
Python, using sorted strings as hash
|
blue_sky5
| 0
| 147
|
count words obtained after adding a letter
| 2,135
| 0.428
|
Medium
| 29,609
|
https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/discuss/1687280/Set-of-sorted-words-98-speed
|
class Solution:
def wordCount(self, startWords: List[str], targetWords: List[str]) -> int:
start_set = {"".join(sorted(w)) for w in startWords}
count = 0
for word in targetWords:
if len(word) > 1:
w = "".join(sorted(word))
for i in range(len(word)):
if f"{w[:i]}{w[i + 1:]}" in start_set:
count += 1
break
return count
|
count-words-obtained-after-adding-a-letter
|
Set of sorted words, 98% speed
|
EvgenySH
| 0
| 205
|
count words obtained after adding a letter
| 2,135
| 0.428
|
Medium
| 29,610
|
https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/discuss/1677128/Python-Solution-(Using-sets)
|
class Solution:
def wordCount(self, startWords: List[str], targetWords: List[str]) -> int:
hmap = collections.defaultdict(set)
for word in startWords:
s = frozenset(word)
hmap[len(word)].add(s)
ans = 0
for word in targetWords:
s = set()
for i in range(0,len(word)):
w = word[:i]+word[i+1:]
s.add(frozenset(w))
intn = hmap[len(word)-1].intersection(s)
if len(intn)>0:
ans+=1
return ans
|
count-words-obtained-after-adding-a-letter
|
Python Solution (Using sets)
|
benslie
| 0
| 125
|
count words obtained after adding a letter
| 2,135
| 0.428
|
Medium
| 29,611
|
https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/discuss/1677063/Python-or-Simple-or-Intuitive-or-Set
|
class Solution:
def wordCount(self, startWords: List[str], targetWords: List[str]) -> int:
# store the startWords in a dictionary of set based on its length. We can store it in a plain set as well as retreival is faster in both cases
dic = defaultdict(set)
for word in startWords:
dic[len(word)].add(''.join(sorted(word)))
print(dic)
ans=0
# for every sorted target word, remove each character and check if its present in source.
# NOTE: Its enough for a target word to present in 1 source word after removing character, hence break the loop for a particular if match found once
for word in targetWords:
trgt = sorted(word)
for i in range(len(word)):
if "".join(trgt[:i]+trgt[i+1:]) in dic[len(word)-1]:
ans+=1
break
return ans
|
count-words-obtained-after-adding-a-letter
|
Python | Simple | Intuitive | Set
|
vishyarjun1991
| 0
| 106
|
count words obtained after adding a letter
| 2,135
| 0.428
|
Medium
| 29,612
|
https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/discuss/1676956/Python-Bitwise-explanation
|
class Solution:
def wordCount(self, startWords: List[str], targetWords: List[str]) -> int:
# turns word into binary string
# i.e., 'ab' -> b11
# 'ac' -> b101
# 'ad' -> b1001
# a-z -> b11111111111111111111111111
def bin_word(word):
num = 0
for c in word:
cn = ord(c) - ord('a')
mask = 1 << cn
num ^= mask
return num
# take out a letter from a binary string
def bin_take_out(word, letter):
cn = ord(letter) - ord('a')
mask = 1 << cn
return word ^ mask
starts = set([bin_word(w) for w in startWords])
count = 0
for t in targetWords:
t_b = bin_word(t)
for c in t:
# try taking out each letter in t
# and see if the resulting binary
# in starts
if bin_take_out(t_b, c) in starts:
count += 1
break
return count
|
count-words-obtained-after-adding-a-letter
|
Python Bitwise explanation
|
davidc360
| 0
| 72
|
count words obtained after adding a letter
| 2,135
| 0.428
|
Medium
| 29,613
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/1676837/Grow-then-plant
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
res = 0
for grow, plant in sorted(zip(growTime, plantTime)):
res = max(res, grow) + plant
return res
|
earliest-possible-day-of-full-bloom
|
Grow then plant
|
votrubac
| 203
| 6,900
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,614
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/1676955/Python-greedy-(faster-than-100.00-Runtime%3A-1808-ms)
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
data = list(zip(plantTime, growTime))
data.sort(key=lambda x: -x[1]) #sort by grow time in descending order
res = 0
start_time = 0
for plant, grow in data:
start_time += plant
res = max(res, start_time + grow)
return res
|
earliest-possible-day-of-full-bloom
|
Python greedy (faster than 100.00%, Runtime: 1808 ms)
|
kryuki
| 13
| 721
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,615
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2754281/FASTEST-GREEDY-SOLUTION-(7-LINES)
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
comb=[(plantTime[i],growTime[i]) for i in range(len(plantTime))]
mx,passed_days=0,0
comb.sort(key=lambda x:(-x[1],x[0]))
for i in range(len(plantTime)):
mx=max(mx,(passed_days+comb[i][0]+comb[i][1]))
passed_days+=comb[i][0]
return mx
|
earliest-possible-day-of-full-bloom
|
FASTEST GREEDY SOLUTION (7 LINES)
|
beneath_ocean
| 5
| 383
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,616
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2335471/Python3-or-Sort-by-grow-time
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
plant_finish = grow_finish = 0
for grow, plant in sorted(zip(growTime, plantTime), reverse=True):
plant_finish += plant
grow_finish = max(grow_finish, plant_finish + grow)
return grow_finish
|
earliest-possible-day-of-full-bloom
|
Python3 | Sort by grow time
|
snorkin
| 4
| 99
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,617
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/1676860/Python3-greedy
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
ans = prefix = 0
for p, g in sorted(zip(plantTime, growTime), key=lambda x: x[1], reverse=True):
prefix += p
ans = max(ans, prefix + g)
return ans
|
earliest-possible-day-of-full-bloom
|
[Python3] greedy
|
ye15
| 4
| 275
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,618
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2757075/Python3-OneLine-Solution
|
class Solution:
def earliestFullBloom(self, pts: List[int], gts: List[int]) -> int:
return max((ptt := ptt + pt if 'ptt' in locals() else pt) + gt for gt, pt in reversed(sorted(zip(gts, pts))))
|
earliest-possible-day-of-full-bloom
|
Python3 OneLine Solution
|
malegkin
| 2
| 17
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,619
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2756770/Python-real-world-simple-explanation
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
prevPlant, ans = 0, 0
for grow, plant in sorted(zip(growTime, plantTime), reverse=True):
ans = max(ans, (grow + plant + prevPlant))
prevPlant += plant
return ans
|
earliest-possible-day-of-full-bloom
|
🌷 Python real world simple explanation
|
sezanhaque
| 2
| 33
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,620
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2013877/Its-a-video-game-problem-(python3)
|
class Solution:
def earliestFullBloom(self, B: List[int], C: List[int]) -> int:
A=[[i,j] for i ,j in zip(B,C)]
A=sorted(A,key=lambda x:x[1],reverse=True)
ans=-1
date=-1
for i , j in A:
date+=i
ans=max(ans,date+j+1)
return ans
|
earliest-possible-day-of-full-bloom
|
Its a video game problem (python3)
|
amit_upadhyay
| 1
| 67
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,621
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/1986581/Strict-Proof-Detailed-Explanation-Python-Sorting-O(nlogn)
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
opt = 0
ctp = 0
for tg, tp in sorted([(growTime[i],plantTime[i]) for i in range(len(plantTime))], reverse=True):
ctp += tp
opt = max(opt, ctp+tg)
return opt
|
earliest-possible-day-of-full-bloom
|
Strict Proof; Detailed Explanation; Python Sorting O(nlogn)
|
YCMTrivial
| 1
| 106
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,622
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2757397/earliest-possible-day-of-full-bloom
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
p=[]
n=len(plantTime)
for i in range(n):
p.append([plantTime[i],growTime[i]])
p=sorted(p, key = lambda x: x[1],reverse=True)
print(p)
for i in range(1,n):
p[i][0]=p[i-1][0]+p[i][0]
maxx=0
for i in range(n):
maxx=max(maxx,p[i][0]+p[i][1])
return maxx
|
earliest-possible-day-of-full-bloom
|
earliest-possible-day-of-full-bloom
|
meenu155
| 0
| 4
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,623
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2756734/Easliest-possible-day
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
ziped = sorted(zip(growTime, plantTime), reverse = True)
time = 0
max1 = 0
for i, j in ziped:
print(time)
time += j
max1 = max(max1, time + i)
print(max1)
return max1
|
earliest-possible-day-of-full-bloom
|
Easliest possible day
|
Vedant-G
| 0
| 6
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,624
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2756673/Reverse-sort-with-python
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
# sort the seed using the growTime backwards
# get the sum of the maximun time getting until the seed 'i'
latest_seed_bloom = 0
total_plant_time= 0
for growt, plantt in sorted(zip(growTime, plantTime), reverse=True):
time_to_bloom_seed_i = plantt + growt
day_bloom_seed_i = total_plant_time + time_to_bloom_seed_i
total_plant_time += plantt
if day_bloom_seed_i > latest_seed_bloom:
latest_seed_bloom = day_bloom_seed_i
return latest_seed_bloom
|
earliest-possible-day-of-full-bloom
|
Reverse sort with python
|
DavidCastillo
| 0
| 7
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,625
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2756271/Python-oror-Easy-Solution-oror-O(nlog(n))
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
ans = sum(plantTime)
lst = []
for i in range(len(plantTime)):
lst.append([plantTime[i], growTime[i]])
lst.sort(key = lambda x : (-x[1], -x[0]))
total = ans
while len(lst):
p = lst.pop()
ans = max(total + p[1], ans)
total -= p[0]
return ans
|
earliest-possible-day-of-full-bloom
|
Python || Easy Solution || O(nlog(n))
|
Rahul_Kantwa
| 0
| 5
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,626
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2756126/Python-Solution
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
times = [(plantTime[i], growTime[i]) for i in range(len(plantTime))]
times = sorted(times, key=lambda x: x[1], reverse=True)
time = 0
mx = 0
for plantTime, growTime in times:
time += plantTime
mx = max(mx, time + growTime)
return mx
|
earliest-possible-day-of-full-bloom
|
Python Solution
|
ziqinyeow
| 0
| 7
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,627
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2755760/Python-or-Optimized-Solution-or-Greedy-Approach
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
plant_time = 0
total_time = 0
# list of indices in descending order according to growTime list..
indices = sorted( range(len(plantTime)), key=lambda i: -growTime[i] )
# iterate though every seeds..
for i in indices:
# increment plant_time linearly when plant different seed,
# bcuz every day can plant only one seed..
plant_time += plantTime[i]
# evaluate total_time..
total_time = max(total_time, plant_time + growTime[i])
return total_time
|
earliest-possible-day-of-full-bloom
|
Python | Optimized Solution | Greedy Approach
|
quarnstric_
| 0
| 2
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,628
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2755446/Python-solution-faster-than-100-users
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
ans = 0 # Ex: plantTime = [1,2,3,2]
# growTime = [2,1,2,1]
for g,p in sorted(zip(growTime,plantTime)): # p g ans
ans = g + p if g >= ans else ans + p # ––– ––– ––––
# 1 2 3
return ans # 2 2 5
# 3 1 6
# 2 3 9 <-- answer
|
earliest-possible-day-of-full-bloom
|
Python solution faster than 100% users
|
mritunjayyy
| 0
| 7
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,629
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2755420/Python-(Faster-than-98)-or-Greedy-solution-O(nlogn)
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
time = 0
currentTime = 0
plant_grow = sorted(zip(growTime, plantTime), reverse = True)
for grow, plant in plant_grow:
time = max(time, currentTime + plant + grow)
currentTime += plant
return time
|
earliest-possible-day-of-full-bloom
|
Python (Faster than 98%) | Greedy solution O(nlogn)
|
KevinJM17
| 0
| 4
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,630
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2755141/Simple-Python3-Solution-oror-Sorting
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
pairs = [[p, g] for p, g in zip(plantTime, growTime)]
pairs = sorted(pairs, key=lambda x: x[1], reverse=True)
ans, days = 0, 0
for pair in pairs:
days += pair[0]
ans = max(ans, days + pair[1])
return ans
|
earliest-possible-day-of-full-bloom
|
Simple Python3 Solution || Sorting
|
joshua_mur
| 0
| 4
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,631
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2755127/Python-Simple-Python-Solution-Using-Sorting-and-Greedy-Approach
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
length = len(plantTime)
pots = []
for index in range(length):
plant = plantTime[index]
grow = growTime[index]
pots.append([plant, grow])
pots = sorted(pots, key = lambda x:x[1], reverse = True)
current_day = 0
result = 0
for pot in pots:
current_plant , current_grow = pot
current_day = current_day + current_plant
result = max(result, current_day + current_grow)
return result
|
earliest-possible-day-of-full-bloom
|
[ Python ] ✅✅ Simple Python Solution Using Sorting and Greedy Approach 🥳✌👍
|
ASHOK_KUMAR_MEGHVANSHI
| 0
| 11
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,632
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2754999/Python-simple-solution-O(N)-100
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
growix_reverse_sorted= map(lambda x: x[0],sorted(enumerate(growTime), key=lambda x: x[1],reverse=True))
plantSum=0
earliest_blooming=0
for i in growix_reverse_sorted:
earliest_blooming = max(earliest_blooming, plantSum + plantTime[i] + growTime[i] )
plantSum += plantTime[i]
return earliest_blooming
```
|
earliest-possible-day-of-full-bloom
|
Python simple solution O(N) 100%
|
user3904Q
| 0
| 8
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,633
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2754935/Easy-fully-explained-python-solution-using-sorting.
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
day, res = 0, 0
flowers = [(plant, grow) for plant, grow in zip(plantTime, growTime)]
# Sorting flowers on grow time in descending order
flowers.sort(key=lambda x: x[1], reverse=True)
for plant, grow in flowers:
day += plant
res = max(res, day + grow)
return res
|
earliest-possible-day-of-full-bloom
|
Easy fully explained python solution using sorting.
|
varun_date
| 0
| 7
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,634
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2754865/Faster-than-90-Easy-and-short-Solution
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
plant_grow = []
for i, j in zip(plantTime, growTime):
plant_grow.append((i, j))
#Sort by descending grow time, because we can plant others while its growing
plant_grow.sort(key = lambda plant_grow : plant_grow[1] , reverse = True)
#Getting result in timeline as shown in examples
temp = 0
res = 0
for i in plant_grow:
temp += i[0]
res = max(res, temp + i[1])
return res
|
earliest-possible-day-of-full-bloom
|
Faster than 90%, Easy and short Solution
|
user6770yv
| 0
| 6
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,635
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2754764/Python3-Simple-Solution-with-Explanation
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
# We sort grow time in descending order
# because we want to utilize the time efficiently
# Imagine you want to start from the lesser grow time or larger one
# If lesser one, we cannot use the time efficiently
# If larger one, when the other plants are growing,
# we can plant the other plants
pairs = [[p, g] for p, g in zip(plantTime, growTime)]
pairs.sort(key=lambda x: -x[1])
# curr_day is to record the planting day
# since we take care of other plant in planting day
# the planting day will largely affect the final result
# so curr_day += planting days
curr_day = 0
# ans is to update the maximum day we will use
# for all the plants to bloom
# ans = max(ans, curr_day + grow time) because
# grow time is not as important as plant time
# it might or might not affect the final result
# depending on is it use more days or not
ans = 0
for p, g in pairs:
curr_day += p
ans = max(ans, curr_day+g)
return ans
|
earliest-possible-day-of-full-bloom
|
Python3 Simple Solution with Explanation
|
rjnkokre
| 0
| 6
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,636
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2754753/Python-98-faster-explained-with-comments
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
#By intuition, it seems that planting plants with larger growTime will minimize the total time to bloom
timeTuple = [(x,y) for x,y in zip(plantTime, growTime)]
# Sort in order of growTime in Desc order and plantTime in Asc order
timeTuple.sort(key = lambda x: -x[1])
#keep track of plant time and last growTime
plantTimeDays = 0
# keep calculating maxGrowTime to factor in corner case where growTime of one of the plant is huge
maxGrowTime = 0
for plant,grow in timeTuple:
plantTimeDays += plant
maxGrowTime = max(maxGrowTime, plantTimeDays + grow )
plantTimeDays += timeTuple[-1][1]
return max(plantTimeDays, maxGrowTime)
|
earliest-possible-day-of-full-bloom
|
Python 98% faster, explained with comments
|
pradyumna04
| 0
| 7
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,637
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2754586/More-explanation-on-which-flower-to-plant-first
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
n = len(plantTime)
flower = []
for i in range(n):
flower.append((plantTime[i], growTime[i]))
flower.sort(reverse = True, key = lambda x: x[1])
res = 0
sum_plant = 0
for i in range(n):
sum_plant += flower[i][0]
res = max(res, sum_plant + flower[i][1])
return res
|
earliest-possible-day-of-full-bloom
|
More explanation on which flower to plant first
|
lazylzy
| 0
| 4
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,638
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2754502/StraightForward-answer
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
l=[]
for i in range(len(plantTime)):
l.append([growTime[i],plantTime[i]])
p=sorted(l,key=lambda x: x[0],reverse=True)
maxi=0;tp=0
for i in p:
tp+=i[1]
maxi=max(maxi,tp+i[0])
return maxi
|
earliest-possible-day-of-full-bloom
|
StraightForward answer
|
hemanth_12
| 0
| 8
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,639
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2754406/Python-short-and-easy-understanding-solution
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
times = [(plantTime[i],growTime[i]) for i in range(len(plantTime))]
times = sorted(times, key = lambda item: -item[1])
pdays = 0
bday = 0
for t1,t2 in times:
pdays+= t1
bday = max(bday,pdays+t2)
return bday
|
earliest-possible-day-of-full-bloom
|
Python short and easy understanding solution
|
harsh30199
| 0
| 9
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,640
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2754207/python-or-simple-or-easy
|
class Solution:
def earliestFullBloom(self, plantTime: list[int], growTime: list[int]) -> int:
a=[]
l=len(growTime)
for i in range(0,l):
a.append([plantTime[i],growTime[i]])
a.sort(key=lambda x:x[1],reverse=True)
m=0
def do(i,st):
if i==l:
return
st+=a[i][0]
nonlocal m
m=max(m,st+a[i][1])
do(i+1,st)
do(0,0)
return m
|
earliest-possible-day-of-full-bloom
|
python | simple | easy
|
VignaTejReddy
| 0
| 8
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,641
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2754158/Python3-Sorting-Visual-Explanation
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
plantTime,growTime = zip(*sorted(
zip(plantTime,growTime),
key = lambda x: x[1],
reverse = True
))
pt,gt = 0,0
n = len(growTime)
for i in range(n):
p,g = plantTime[i],growTime[i]
pt += p
gt = max(gt,pt+g)
return max(gt,pt)
|
earliest-possible-day-of-full-bloom
|
Python3 Sorting Visual Explanation
|
user9611y
| 0
| 8
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,642
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2754156/Python3-Sorting-sortVisual-Explanation
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
plantTime,growTime = zip(*sorted(
zip(plantTime,growTime),
key = lambda x: x[1],
reverse = True
))
pt,gt = 0,0
n = len(growTime)
for i in range(n):
p,g = plantTime[i],growTime[i]
pt += p
gt = max(gt,pt+g)
return max(gt,pt)
|
earliest-possible-day-of-full-bloom
|
Python3 Sorting sortVisual Explanation
|
user9611y
| 0
| 3
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,643
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2754116/Python-Solution-in-3-lines
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
ans = 0
for g,p in sorted(zip(growTime,plantTime)):
ans = g + p if g >= ans else ans + p
return ans
|
earliest-possible-day-of-full-bloom
|
Python Solution in 3 lines
|
dnvavinash
| 0
| 13
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,644
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2736697/Python3-Simple-Greedy
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
# sort by decreasing grow time, allows maximum growing/planting overlap
plants = sorted(zip(plantTime, growTime), key=lambda tup: tup[1], reverse=True)
res = 0
total = 0 # cumulative plant time
for p, g in plants:
total += p # add plant time to total plant time
res = max(res, total+g) # bloom time for this plant
return res
|
earliest-possible-day-of-full-bloom
|
Python3 Simple Greedy
|
jonathanbrophy47
| 0
| 13
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,645
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/2567108/Python3-or-Largest-Growing-Time-plant-First
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
time=list(tuple(zip(plantTime,growTime)))
time.sort(key=lambda x:x[1],reverse=True)
adv,ans=0,0
for start,end in time:
start+=adv
end+=start
adv=start
ans=max(ans,end)
return ans
|
earliest-possible-day-of-full-bloom
|
[Python3] | Largest Growing Time plant First
|
swapnilsingh421
| 0
| 25
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,646
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/1716343/Sort-tuples-89-speed
|
class Solution:
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
plants = sorted((g, -p) for g, p in zip(growTime, plantTime))
rem_plant_time = sum(plantTime)
ans = -inf
for g, p in plants:
ans = max(ans, g + rem_plant_time)
rem_plant_time += p
return ans
|
earliest-possible-day-of-full-bloom
|
Sort tuples, 89% speed
|
EvgenySH
| 0
| 35
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,647
|
https://leetcode.com/problems/earliest-possible-day-of-full-bloom/discuss/1681925/Python-Simple-Solution-Greedy-Easy-to-Understand
|
class Solution:
def earliestFullBloom(self, plantTime, growTime):
PLANT, GROW = 0,1
#create array of (plant, grow) tuples
times = list(zip(plantTime, growTime))
#sort by grow time
times.sort(key=lambda i:i[GROW], reverse=True)
prev_plant = prev_bloom = 0
for time in times:
cur_bloom = time[PLANT]+time[GROW]
#get the time of the last bloom
prev_bloom = max(prev_bloom, prev_plant+cur_bloom)
prev_plant += time[PLANT]
return prev_bloom
|
earliest-possible-day-of-full-bloom
|
Python Simple Solution Greedy Easy to Understand
|
matthewlkey
| 0
| 63
|
earliest possible day of full bloom
| 2,136
| 0.749
|
Hard
| 29,648
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1694807/Python-O(n)-solution
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
length = len(s)
res=[]
for i in range(0,length,k):
if i+k>length:
break
res.append(s[i:i+k])
mod =length%k
if mod!= 0:
fill_str = fill *(k-mod)
add_str = s[i:]+fill_str
res.append(add_str)
return res
|
divide-a-string-into-groups-of-size-k
|
Python O(n) solution
|
SamyakKrSahoo
| 2
| 100
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,649
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1692914/Python3-simulation
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
ans = []
for i in range(0, len(s), k):
ss = s[i:i+k]
ans.append(ss + (k-len(ss))*fill)
return ans
|
divide-a-string-into-groups-of-size-k
|
[Python3] simulation
|
ye15
| 2
| 88
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,650
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/2760583/Easy-Python-Solution-or-Faster-than-84
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
ans = []
no_of_groups = math.ceil(len(s)/k)
start = 0
while no_of_groups>0:
if s[start:start+k]:
ans.append(s[start:start+k])
else:
ans.append(s[start:])
start+=k
no_of_groups-=1
while len(ans[-1])!=k:
ans[-1]+=fill
return ans
|
divide-a-string-into-groups-of-size-k
|
Easy Python Solution | Faster than 84%
|
aniketbhamani
| 1
| 32
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,651
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/2843543/PYTHON-Simple-or-With-Comments
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
while len(s)%k != 0:
s += fill
# checking until the string is dividable into k groups
# if not, it will add the filler until it is
c = []
for x in range(0, len(s), k): # starts at 0, goes to len(s), iteration value is k
c.append(s[x:x+k]) # appends everything from x->x+k (iteration value)
return c # return final list
|
divide-a-string-into-groups-of-size-k
|
[PYTHON] Simple | With Comments
|
omkarxpatel
| 0
| 4
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,652
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/2835884/Simple-python-solution
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
groups = len(s)//k
remaining = len(s)%k
res = []
for i in range(groups):
res.append(s[i*k:i*k+k])
if remaining > 0:
res.append(s[len(s)-remaining:]+fill*(k-remaining))
return res
|
divide-a-string-into-groups-of-size-k
|
Simple python solution
|
aruj900
| 0
| 2
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,653
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/2815302/Easy-To-Read-Python-Solution-or-Faster-than-94
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
end = []
for x in range(0,len(s),k):
end.append(s[x:x+k])
diff = k-len(end[len(end)-1])
if diff != 0:
end[len(end)-1] += (diff*fill)
return end
|
divide-a-string-into-groups-of-size-k
|
Easy To Read Python Solution | Faster than 94%
|
felipe-l
| 0
| 2
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,654
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/2777075/O(n)-Python3
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
ans=[]
st=""
c=0
for i in range(len(s)):
st+=s[i]
c+=1
if c==k:
ans.append(st)
st=""
c=0
if c!=0:
ans.append(st+((k-len(st))*fill))
return ans
|
divide-a-string-into-groups-of-size-k
|
O(n) Python3
|
Xhubham
| 0
| 4
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,655
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/2747667/Python-or-Division-Modulus-Slices
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
n = len(s)
ans = []
for i in range(n // k):
l = i*k
r = l + k
ans.append(s[l:r])
r = n % k
if r > 0:
ans.append(s[-r:n] + fill*(k - r))
return ans
|
divide-a-string-into-groups-of-size-k
|
Python | Division, Modulus, Slices
|
on_danse_encore_on_rit_encore
| 0
| 6
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,656
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/2384947/Python3-Easy-and-faster-than-90
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
ans=[]
l=len(s)
for i in range(0,l,k):
ans.append(s[i:i+k])
if len(ans[-1]) < k:
ans[-1] = ans[-1] + (k-len(ans[-1]))*fill #concatnate fill to the last word if needed
return ans
|
divide-a-string-into-groups-of-size-k
|
[Python3] Easy and faster than 90%
|
sunakshi132
| 0
| 40
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,657
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/2310959/Easy-python-solution
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
if len(s) < k :
return [s + (k - len(s))*fill]
else :
counter = 0
output, substr = [], ""
for i in range(len(s)) :
substr += s[i]
counter += 1
if counter == k :
counter = 0
output.append(substr)
substr = ""
if output[-1] != s[-k:] :
output.append(substr + fill*(k- len(substr)))
return output
else :
return output
|
divide-a-string-into-groups-of-size-k
|
Easy python solution
|
sghorai
| 0
| 23
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,658
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/2143916/Python3-simple-solution
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
ans = []
z = ''
i = 0
while i < len(s):
if len(z) < k:
z += s[i]
elif len(z) == k:
ans.append(z)
z = s[i]
i += 1
if len(z) == k:
ans.append(z)
else:
ans.append(z + (k - len(z))*fill)
return ans
|
divide-a-string-into-groups-of-size-k
|
Python3 simple solution
|
EklavyaJoshi
| 0
| 41
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,659
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/2080246/Python-Solutions
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
return list(map(lambda i: s[i:i + k] + (k - len(s[i:i + k])) * fill, range(0, len(s), k)))
|
divide-a-string-into-groups-of-size-k
|
Python Solutions
|
hgalytoby
| 0
| 76
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,660
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/2080246/Python-Solutions
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
result = []
for i in range(0, len(s), k):
words = s[i:i + k]
result.append(words + (k - len(words)) * fill)
return result
|
divide-a-string-into-groups-of-size-k
|
Python Solutions
|
hgalytoby
| 0
| 76
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,661
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1980730/Simple-and-Easy-Python-Code
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
if len(s)%k!=0:
x=len(s)%k
print(x)
for i in range(0,k-x):
s+=fill
result=[]
for i in range(0,len(s),k):
result.append(s[i:i+k])
return result
|
divide-a-string-into-groups-of-size-k
|
Simple and Easy Python Code
|
sangam92
| 0
| 30
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,662
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1979889/Python-Solution
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> list[str]:
l = []
i = 0
r = len(s)
while i < r:
m = ""
for j in range(k):
if i + j < r:
m += (s[i + j])
else:
m += fill
i += k
l.append(m)
return l
|
divide-a-string-into-groups-of-size-k
|
Python Solution
|
EddieAtari
| 0
| 28
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,663
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1940956/Python-dollarolution
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
i, v = 0, []
while i < len(s):
if i+k < len(s):
v.append(s[i:i+k])
else:
v.append((s[i:] + fill *(k - len(s)%k))[:k])
i += k
return v
|
divide-a-string-into-groups-of-size-k
|
Python $olution
|
AakRay
| 0
| 36
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,664
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1893333/Python-easy-solution-with-memory-less-than-81
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
res = []
for i in range(0, len(s), k):
res.append(s[i:i+k])
if len(res[-1]) < k:
res[-1] += fill * (k - len(res[-1]))
return res
|
divide-a-string-into-groups-of-size-k
|
Python easy solution with memory less than 81%
|
alishak1999
| 0
| 44
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,665
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1824224/python-easy
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
n = len(s)
fill_count = k - n%k
res = []
for i in range(0, n, k):
res.append(s[i:i+k])
if len(res[-1]) < k:
res[-1] += (fill*fill_count) # concat filler character
return res
|
divide-a-string-into-groups-of-size-k
|
python easy
|
abkc1221
| 0
| 41
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,666
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1798226/2-Lines-Python-Solution-oror-90-Faster-(32ms)-oror-Memory-less-than-97
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
ans = [s[i:i+k] for i in range(0,len(s),k)]
return ans[:-1] + [ans[-1] + fill*(k-len(ans[-1]))]
|
divide-a-string-into-groups-of-size-k
|
2-Lines Python Solution || 90% Faster (32ms) || Memory less than 97%
|
Taha-C
| 0
| 31
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,667
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1778119/Python3-faster-than-96
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
res = [s[i:i+k] for i in range(0, len(s), k)]
if len(res[-1]) < k:
res[-1] = res[-1] + fill * (k - len(res[-1]))
return res
|
divide-a-string-into-groups-of-size-k
|
Python3 faster than 96%
|
rrrares
| 0
| 55
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,668
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1777446/python-3-three-line-solution
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
n = len(s)
s += (k - (n % k)) * fill
return [s[i:i+k] for i in range(0, n, k)]
|
divide-a-string-into-groups-of-size-k
|
python 3, three line solution
|
dereky4
| 0
| 43
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,669
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1739770/python-3-simple-solution
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
i=0
l=[]
while(i<len(s)-k):
l.append(s[i:i+k])
i=i+k
if(i<len(s)):
s1=s[i:]
while(len(s1)<k):
s1=s1+fill
l.append(s1)
return l
|
divide-a-string-into-groups-of-size-k
|
python 3 simple solution
|
Rajashekar_Booreddy
| 0
| 38
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,670
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1739398/Python3-using-Dictionary-(Run%3A-32ms-88-faster-Space%3A-13.9MB-99.82-less)-with-Explanation
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
res = []
remain = len(s) % k
if remain != 0:
s += fill * (k - remain) # check if the length is divisible by k
for i in range(0 , len(s) , k):
res.append(s[i : i+k]) # append substrings
return res
|
divide-a-string-into-groups-of-size-k
|
Python3 using Dictionary (Run: 32ms, 88% faster; Space: 13.9MB, 99.82% less) with Explanation
|
leetcodemaster2000
| 0
| 19
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,671
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1728903/Easy-python3-solution
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
res=[]
mylist=list(s)
rem=k-len(s)%k
if rem!=k:
for i in range(rem):
mylist.append(fill)
for i in range(0,len(mylist),k):
res.append("".join(mylist[i:i+k]))
return res
|
divide-a-string-into-groups-of-size-k
|
Easy python3 solution
|
Karna61814
| 0
| 26
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,672
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1728511/Python-Single-Line-Solution
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
return [str(s[i:i+k] + fill * k)[:k] for i in range(0, len(s), k)]
|
divide-a-string-into-groups-of-size-k
|
Python Single Line Solution
|
atiq1589
| 0
| 20
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,673
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1708277/Python3-accepted-solution
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
if(len(s)%k != 0):
s += fill*(k - len(s)%k)
li = []
for i in range(0,len(s),k):
li.append(s[i:i+k])
return li
|
divide-a-string-into-groups-of-size-k
|
Python3 accepted solution
|
sreeleetcode19
| 0
| 27
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,674
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1700614/Python3-or-EASY-SOLUTION-or-CLEAN
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
x = []
mod = len(s)%k
if mod :
s = s + (k-mod)*fill
i = 0
j = k
while j <= len(s):
x.append(s[i:j])
i += k
j += k
return x
|
divide-a-string-into-groups-of-size-k
|
Python3 | EASY SOLUTION | CLEAN
|
rohitkhairnar
| 0
| 28
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,675
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1699418/Python-Easy-Solution
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
res = [s[x : x+k] for x in range(0, len(s), k)]
last_str = res[-1]
if len(last_str) != k:
last_str += fill * (k- len(last_str))
res[-1] = last_str
return res
|
divide-a-string-into-groups-of-size-k
|
Python Easy Solution
|
aaffriya
| 0
| 28
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,676
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1695768/Python-easy-solution
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
n = len(s)
res = []
m = n // k
r = n - k*m
for i in range(m):
res.append(s[i*k:(i+1)*k])
if r > 0:
last = s[m*k:] + (k-r)*fill
if len(last) >= 1:
res.append(last)
return res
|
divide-a-string-into-groups-of-size-k
|
Python easy solution
|
byuns9334
| 0
| 30
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,677
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1695607/Python-easy-to-understand
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
retList = []
tempString = ""
tempK = 0
start = 0
end = k
while(end < len(s)):
retList.append(s[start:end])
start +=k
end += k
end = end - k
lastString = s[end:]
print(lastString)
remainder = k - len(lastString)
#while(len(lastString) != k):
lastString += fill*remainder
retList.append(lastString)
return retList
|
divide-a-string-into-groups-of-size-k
|
Python easy to understand
|
vinija
| 0
| 14
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,678
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1693707/Python-3-solution-100
|
class Solution:
def divide(self, s, k):
res = []
l = 0
for i in range(k, len(s)+k, k):
res.append(s[l:i])
l = i
return res;
def divideString(self, s: str, k: int, fill: str) -> List[str]:
n = len(s)
if n % k == 0:
return self.divide(s, k)
else:
l = k - (n % k)
while l:
s = s + fill
l = l - 1
return self.divide(s,k)
|
divide-a-string-into-groups-of-size-k
|
Python 3 solution 100%
|
loserboy2
| 0
| 23
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,679
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1693542/Easy-Python-Solution(100)
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
a=[]
if len(s)%k==0:
for i in range(0,len(s),k):
a.append(s[i:i+k])
else:
re=len(s)%k
last=0
for i in range(0,len(s)-re,k):
a.append(s[i:i+k])
last=i+k
final=s[last:]+fill*(k-re)
a.append(final)
return a
|
divide-a-string-into-groups-of-size-k
|
Easy Python Solution(100%)
|
Sneh17029
| 0
| 55
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,680
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1693362/Python-3-Easy-to-understand-solution
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
length = len(s)
count = length // k
remainder = length % k
result = []
for i in range(1, count+1):
left = i - 1
word = s[(left*k):(i*k)]
result.append(word)
if remainder == 0:
return result
lastword = s[count*k:] + (k-remainder)*fill
result.append(lastword)
return result
|
divide-a-string-into-groups-of-size-k
|
[Python 3] Easy to understand solution
|
Lanzhou
| 0
| 29
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,681
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1693331/Python-or-Code-Snippet-Lessons-1
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
# lesson1: code snippet lesson
# Split a string of size k in a list
chunks = [s[i:i+k] for i in range(0, len(s), k)]
last_word = chunks[-1]
if len(last_word) != k:
a = ""
count = k - len(last_word)
while count:
last_word += fill
count -=1
chunks[-1] = last_word
return chunks
|
divide-a-string-into-groups-of-size-k
|
Python | Code Snippet Lessons #1
|
WeaponXIV
| 0
| 23
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,682
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1693288/Python3-42ms-easy-iterative-one-pass
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
i = 0
n = len(s)
res = []
while i < n:
seg = s[i:i + k]
res.append(seg)
i += k
last_seg_len = len(res[-1])
if last_seg_len < k:
res[-1] = res[-1] + fill * (k - last_seg_len)
return res
|
divide-a-string-into-groups-of-size-k
|
[Python3] 42ms - easy iterative one-pass
|
dwschrute
| 0
| 28
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,683
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1693266/Python-ororeasy-to-understand
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
l1=[]
i=0
while(i<len(s)):
l1.append(s[i:i+k])
i+=k
t=len(l1[-1])
if(t<k):
st=l1[-1]
l1.remove(l1[-1])
while(t<k):
st+=fill
t+=1
l1.append(st)
return l1
```
|
divide-a-string-into-groups-of-size-k
|
Python ||easy to understand
|
ashish166
| 0
| 27
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,684
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1693179/Python
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
result = [s[i:i+k] for i in range(0, len(s), k)]
if len(result[-1]) < k:
result[-1] += (k - len(result[-1])) * fill
return result
|
divide-a-string-into-groups-of-size-k
|
Python
|
blue_sky5
| 0
| 35
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,685
|
https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/discuss/1693169/Simple-Python3-Solution-or-Easy-to-Understand
|
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
# s = "abcdefghij", k = 3, fill = "x"
res = []
for i in range(0, len(s), k):
res.append(s[i:i+k])
# print(res) ['abc', 'def', 'ghi', 'j']
while len(res[-1]) < k:
res[-1] += fill
return res
|
divide-a-string-into-groups-of-size-k
|
Simple Python3 Solution | Easy to Understand
|
reductorc
| 0
| 13
|
divide a string into groups of size k
| 2,138
| 0.652
|
Easy
| 29,686
|
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1722491/Easy-Solution-python-explained-30ms
|
class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
moves = 0
while maxDoubles > 0 and target > 1:
if target % 2 == 1:
target -= 1
else:
target //= 2
maxDoubles -= 1
moves += 1
moves += target - 1
return moves
|
minimum-moves-to-reach-target-score
|
Easy Solution python explained 30ms
|
Volver805
| 2
| 83
|
minimum moves to reach target score
| 2,139
| 0.484
|
Medium
| 29,687
|
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1693746/Python3-Recursive-%2B-Backwards-Solution-or-Greedy-Approach
|
class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
# If all maxDoubles are expired then number of -1 operations to go from target to 1 is (target-1)
# If target is 1 that means we return 0. Since target is 1, we return target-1
if maxDoubles == 0 or target == 1:
return target - 1
subracts = 0 # Considering no -1 operation required initially
if target%2 != 0: # Target is odd, we must do one -1 operation to make it even
subracts=1
target-=1 # Target is now even
target = target//2
maxDoubles -= 1 # Since we used a maxDouble operation we have one less /2 operation to work from
return self.minMoves(target, maxDoubles)+subracts+1
# we add 1 here as we will do the /2 operation always
|
minimum-moves-to-reach-target-score
|
[Python3] Recursive + Backwards Solution | Greedy Approach
|
anCoderr
| 1
| 67
|
minimum moves to reach target score
| 2,139
| 0.484
|
Medium
| 29,688
|
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1692938/Python3-Easy-solution
|
class Solution:
def minMoves(self, target: int, dob: int) -> int:
if dob==0: return target-1
ans=0
while dob>0 and target>1:
if target%2==0:
target//=2
dob-=1
else:
target-=1
ans+=1
return ans+target-1
|
minimum-moves-to-reach-target-score
|
[Python3] Easy solution
|
ro_hit2013
| 1
| 196
|
minimum moves to reach target score
| 2,139
| 0.484
|
Medium
| 29,689
|
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1692930/Python3-parity
|
class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
ans = 0
while target > 1 and maxDoubles:
ans += 1
if target&1: target -= 1
else:
target //= 2
maxDoubles -= 1
return ans + target - 1
|
minimum-moves-to-reach-target-score
|
[Python3] parity
|
ye15
| 1
| 57
|
minimum moves to reach target score
| 2,139
| 0.484
|
Medium
| 29,690
|
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/2824426/Python-Optimized-Greedy-O(N)
|
class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
moves = 0
while target != 1 and maxDoubles:
if target % 2:
target-=1
moves+=1
else:
target//=2
moves+=1
maxDoubles-=1
return moves + target-1
|
minimum-moves-to-reach-target-score
|
Python Optimized Greedy O(N)
|
iSyqozz512
| 0
| 1
|
minimum moves to reach target score
| 2,139
| 0.484
|
Medium
| 29,691
|
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/2551387/Reduce-target-to-1
|
class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
if maxDoubles == 0:
return target - 1
count = 0
while maxDoubles != 0 and target != 1:
maxDoubles -= 1
if target % 2 == 1:
target = target // 2
count += 1
else:
target = target // 2
count += 1
return count + target - 1
|
minimum-moves-to-reach-target-score
|
Reduce target to 1
|
sphilip1206
| 0
| 8
|
minimum moves to reach target score
| 2,139
| 0.484
|
Medium
| 29,692
|
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/2081420/Python3oror-Beginner-Friendly-approachoror-simple-oror-greedy-approach
|
class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
cnt=0
while target>1:
cnt+=1
if target%2==0 and maxDoubles:
target//=2
maxDoubles-=1
else:
if not maxDoubles:
cnt+=target-2 #as i have already incremented the cnt by 1 so uses 2
target=1
else:
target-=1
return cnt
|
minimum-moves-to-reach-target-score
|
Python3|| Beginner Friendly approach|| simple || greedy approach
|
aditya1292
| 0
| 36
|
minimum moves to reach target score
| 2,139
| 0.484
|
Medium
| 29,693
|
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1930304/python-3-oror-simple-greedy-solution
|
class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
res = 0
while target > 1 and maxDoubles:
if target % 2:
target -= 1
else:
target >>= 1
maxDoubles -= 1
res += 1
return res + target - 1
|
minimum-moves-to-reach-target-score
|
python 3 || simple greedy solution
|
dereky4
| 0
| 69
|
minimum moves to reach target score
| 2,139
| 0.484
|
Medium
| 29,694
|
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1788734/97.5-Lesser-Memory-or-Python-Simple-Solution-or-O(n)-Solution
|
class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
c = 0
while(target != 1):
if target%2 == 1:
c += 1
target -= 1
if maxDoubles:
target = target // 2
maxDoubles -= 1
c += 1
else:
c += (target-1)
break
return c
|
minimum-moves-to-reach-target-score
|
97.5% Lesser Memory | Python Simple Solution | O(n) Solution
|
Coding_Tan3
| 0
| 36
|
minimum moves to reach target score
| 2,139
| 0.484
|
Medium
| 29,695
|
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1728907/Easy-python3-solution
|
class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
count=0
while target!=1 and maxDoubles!=0:
if target%2==1:
count+=1
target-=1
else:
count+=1
maxDoubles-=1
target//=2
count+=(target-1)
return count
|
minimum-moves-to-reach-target-score
|
Easy python3 solution
|
Karna61814
| 0
| 37
|
minimum moves to reach target score
| 2,139
| 0.484
|
Medium
| 29,696
|
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1699404/Divmod-by-2-99-speed
|
class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
count = 0
while maxDoubles and target > 1:
target, rem = divmod(target, 2)
maxDoubles -= 1
count += 1 + rem
return count + target - 1
|
minimum-moves-to-reach-target-score
|
Divmod by 2, 99% speed
|
EvgenySH
| 0
| 23
|
minimum moves to reach target score
| 2,139
| 0.484
|
Medium
| 29,697
|
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1695384/24ms-or-100-Faster-or-100-Memory-or-Python-3
|
class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
steps = 0
while target > 1 and maxDoubles > 0:
if target%2 == 0:
target = target//2
maxDoubles -= 1
else:
target -= 1
steps += 1
steps += (target-1)
return steps
|
minimum-moves-to-reach-target-score
|
24ms | 100% Faster | 100% Memory | Python 3
|
jayeshdehankar
| 0
| 21
|
minimum moves to reach target score
| 2,139
| 0.484
|
Medium
| 29,698
|
https://leetcode.com/problems/minimum-moves-to-reach-target-score/discuss/1695064/Python-3-Solution-oror-100-faster-oror-100-less-memory-usage
|
class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
n=target
res=0
while(n>1):
if(maxDoubles==0):
res+=(n-1)
break
if(n%2==0 and maxDoubles>0):
n=n/2
maxDoubles-=1
res+=1
else:
n=n-1
res+=1
return int(res)
|
minimum-moves-to-reach-target-score
|
Python 3 Solution || 100% faster || 100% less memory usage
|
sankitgamer
| 0
| 19
|
minimum moves to reach target score
| 2,139
| 0.484
|
Medium
| 29,699
|
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.