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/node-with-highest-edge-score/discuss/2422372/Python-oror-Easy-Approach-oror-Hashmap | class Solution:
def edgeScore(self, edges: List[int]) -> int:
n = len(edges)
cnt = defaultdict(int)
ans = 0
// we have the key stores the node edges[i], and the value indicates the edge score.
for i in range(n):
cnt[edges[i]] += i
m = max(cnt.values()... | node-with-highest-edge-score | ✅Python || Easy Approach || Hashmap | chuhonghao01 | 4 | 226 | node with highest edge score | 2,374 | 0.462 | Medium | 32,500 |
https://leetcode.com/problems/node-with-highest-edge-score/discuss/2422159/Python-or-Simple-counting | class Solution:
def edgeScore(self, edges: List[int]) -> int:
n = len(edges)
score = [0] * n
for i, val in enumerate(edges):
score[val] += i
return score.index(max(score)) | node-with-highest-edge-score | Python | Simple counting | thoufic | 2 | 50 | node with highest edge score | 2,374 | 0.462 | Medium | 32,501 |
https://leetcode.com/problems/node-with-highest-edge-score/discuss/2434913/Linear-solution-98-speed | class Solution:
def edgeScore(self, edges: List[int]) -> int:
sums = [0] * len(edges)
for i, n in enumerate(edges):
sums[n] += i
return sums.index(max(sums)) | node-with-highest-edge-score | Linear solution, 98% speed | EvgenySH | 0 | 17 | node with highest edge score | 2,374 | 0.462 | Medium | 32,502 |
https://leetcode.com/problems/node-with-highest-edge-score/discuss/2424663/Easy-Python | class Solution:
def edgeScore(self, edges: List[int]) -> int:
res = []
dic = collections.defaultdict(int)
for i, n in enumerate(edges):
dic[n] = i + dic.get(n, 0)
for i, n in dic.items():
if len(res) == 0:
res = [n, i]
else:
... | node-with-highest-edge-score | Easy Python | iampravat01 | 0 | 7 | node with highest edge score | 2,374 | 0.462 | Medium | 32,503 |
https://leetcode.com/problems/node-with-highest-edge-score/discuss/2423124/Python-Solution-with-Dictionary | class Solution:
def edgeScore(self, edges: List[int]) -> int:
d=collections.defaultdict(list)
for i in range(len(edges)):
d[edges[i]].append(i)
l=list()
s=0
c=0
for u,v in d.items():
if sum(v)>s:
s=sum(v)
c=u
... | node-with-highest-edge-score | Python Solution with Dictionary | a_dityamishra | 0 | 3 | node with highest edge score | 2,374 | 0.462 | Medium | 32,504 |
https://leetcode.com/problems/node-with-highest-edge-score/discuss/2423120/O(n)-counting-node-score-at-every-edges | class Solution:
def edgeScore(self, edges: List[int]) -> int:
s = {}
for i in range(len(edges)):
s[edges[i]] = s.get(edges[i], 0) + i
an = sorted(s.keys())
ans = -1
for ni in an:
if ans == -1 or s[ni]>s[ans]:
ans = ni
return ans | node-with-highest-edge-score | O(n) counting node score at every edges | dntai | 0 | 5 | node with highest edge score | 2,374 | 0.462 | Medium | 32,505 |
https://leetcode.com/problems/node-with-highest-edge-score/discuss/2422499/Python3-Easy-approach-short-code | class Solution:
def edgeScore(self, edges: List[int]) -> int:
n = len(edges)
scores = [0] * n
for i in range(n):
scores[edges[i]] += i
return scores.index(max(scores)) | node-with-highest-edge-score | [Python3] Easy approach, short code | celestez | 0 | 5 | node with highest edge score | 2,374 | 0.462 | Medium | 32,506 |
https://leetcode.com/problems/node-with-highest-edge-score/discuss/2422466/Secret-Python-Answer-Linear-Time-Counting-and-Max | class Solution:
def edgeScore(self, edges: List[int]) -> int:
res = 0
n = len(edges)
sc = [0 for i in range(n)]
for i,n in enumerate(edges):
sc[n] += i
m = max(sc)
for i,e in enumerate(sc):
if e == m:
return i | node-with-highest-edge-score | [Secret Python Answer🤫🐍👌😍] Linear Time Counting and Max | xmky | 0 | 5 | node with highest edge score | 2,374 | 0.462 | Medium | 32,507 |
https://leetcode.com/problems/node-with-highest-edge-score/discuss/2422465/Python-Simple-Python-Solution-Using-HashMap-or-Dictionary | class Solution:
def edgeScore(self, edges: List[int]) -> int:
d = {}
for i in range(len(edges)):
if edges[i] not in d:
d[edges[i]] = [i]
else:
d[edges[i]].append(i)
key = 100000000
value = -100000000
for k in d:
current_sum = sum(d[k])
if current_sum == value:
value = curren... | node-with-highest-edge-score | [ Python ] ✅✅ Simple Python Solution Using HashMap | Dictionary 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 12 | node with highest edge score | 2,374 | 0.462 | Medium | 32,508 |
https://leetcode.com/problems/node-with-highest-edge-score/discuss/2422418/python3-count-scores-then-find-max-then-iterate-index-and-return-first-that-equals-max | class Solution:
def edgeScore(self, edges: List[int]) -> int:
n = len(edges)
scores = [0 for _ in range(n)] # scores[i] is the total score that node 'i' has
for score, pointed in enumerate(edges):
scores[pointed] += score
mx = max(scores)
for i in range(... | node-with-highest-edge-score | python3 count scores then find max then iterate index and return first that equals max | Pinfel | 0 | 2 | node with highest edge score | 2,374 | 0.462 | Medium | 32,509 |
https://leetcode.com/problems/node-with-highest-edge-score/discuss/2422291/Python-3-O(n)-Time-and-Space-or-Easy-to-read | class Solution:
def edgeScore(self, edges: List[int]) -> int:
n = len(edges)
sums = [0] * n
for src, dst in enumerate(edges):
sums[dst] += src
max_sum = max(sums)
return sums.index(max_sum)
``` | node-with-highest-edge-score | [Python 3] O(n) Time & Space | Easy to read | leet_aiml | 0 | 7 | node with highest edge score | 2,374 | 0.462 | Medium | 32,510 |
https://leetcode.com/problems/node-with-highest-edge-score/discuss/2422229/Python3-simulations | class Solution:
def edgeScore(self, edges: List[int]) -> int:
score = [0]*len(edges)
for i, x in enumerate(edges): score[x] += i
return score.index(max(score)) | node-with-highest-edge-score | [Python3] simulations | ye15 | 0 | 5 | node with highest edge score | 2,374 | 0.462 | Medium | 32,511 |
https://leetcode.com/problems/node-with-highest-edge-score/discuss/2422196/Python3-Solution | class Solution:
def edgeScore(self, edges: List[int]) -> int:
score_map = {}
#Iterate edges and find the score for all the nodes.
for i,num in enumerate(edges):
score_map[num] = score_map.get(num,0) + i
result,max_score = sys.maxsize,0
for k,v in score_map.items():
... | node-with-highest-edge-score | Python3 Solution | parthberk | 0 | 8 | node with highest edge score | 2,374 | 0.462 | Medium | 32,512 |
https://leetcode.com/problems/node-with-highest-edge-score/discuss/2422196/Python3-Solution | class Solution:
def edgeScore(self, edges: List[int]) -> int:
scores = [0]*len(edges)
for i,num in enumerate(edges):
scores[num] += i
result,max_score = 0,-sys.maxsize
for i,num in enumerate(scores):
if scores[i] > max_score:
result = i
... | node-with-highest-edge-score | Python3 Solution | parthberk | 0 | 8 | node with highest edge score | 2,374 | 0.462 | Medium | 32,513 |
https://leetcode.com/problems/node-with-highest-edge-score/discuss/2422093/Python3-Counter-and-Return-Max | class Solution:
def edgeScore(self, edges: List[int]) -> int:
edge_cnts = defaultdict(int)
max_idx, max_val = 0, 0
for inp, out in enumerate(edges):
edge_cnts[out] += inp
for idx in range(len(edges)):
if edge_cnts[idx] > max_val:
... | node-with-highest-edge-score | [Python3] Counter & Return Max | 0xRoxas | 0 | 24 | node with highest edge score | 2,374 | 0.462 | Medium | 32,514 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2422249/Python3-greedy | class Solution:
def smallestNumber(self, pattern: str) -> str:
ans = [1]
for ch in pattern:
if ch == 'I':
m = ans[-1]+1
while m in ans: m += 1
ans.append(m)
else:
ans.append(ans[-1])
for i in r... | construct-smallest-number-from-di-string | [Python3] greedy | ye15 | 22 | 994 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,515 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2422249/Python3-greedy | class Solution:
def smallestNumber(self, pattern: str) -> str:
ans = []
stack = []
for i in range(len(pattern)+1):
stack.append(str(i+1))
if i == len(pattern) or pattern[i] == 'I':
while stack: ans.append(stack.pop())
return ''.join(ans) | construct-smallest-number-from-di-string | [Python3] greedy | ye15 | 22 | 994 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,516 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2425788/Python-easy-to-understand-greedy-solution. | class Solution:
def smallestNumber(self, pattern: str) -> str:
ans = []
dec_count = 0
for i in range(len(pattern)):
if pattern[i] == "I":
for j in range(i, i-dec_count-1,-1):
ans.append(str(j+1))
dec_count = 0
elif p... | construct-smallest-number-from-di-string | [Python] easy to understand greedy solution. | tolimitiku | 5 | 188 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,517 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2800344/Python3-Solution-with-using-stack | class Solution:
def smallestNumber(self, pattern: str) -> str:
res, stack = [], []
for i in range(len(pattern) + 1):
stack.append(str(i + 1))
if i == len(pattern) or pattern[i] == 'I':
while stack:
res.append(stack.pop())
... | construct-smallest-number-from-di-string | [Python3] Solution with using stack | maosipov11 | 0 | 3 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,518 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2781262/One-pass-method-with-Easy-to-understand-beats-98 | class Solution:
def smallestNumber(self, s: str) -> str:
res = [0] * (len(s) + 1)
stack = []
cur = 1
for i in range(len(res)):
if i == len(s) or s[i] == 'D':
stack.append(i)
if i < len(s):
continue
else:
... | construct-smallest-number-from-di-string | One pass method with Easy to understand beats 98% | RanjithRagul | 0 | 6 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,519 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2748096/Python-Greedy-Solution-O(n)-Time-O(1)-extra-space | class Solution:
def smallestNumber(self, pattern: str) -> str:
res = []
n = len(pattern)
cur = 1
i = 0
while i < n:
d_count = 0
while i < n and pattern[i] == 'D':
d_count += 1
i += 1
for x in ran... | construct-smallest-number-from-di-string | Python Greedy Solution O(n) Time O(1) extra space | nevergiveup2 | 0 | 1 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,520 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2748095/Python-Greedy-Solution-O(n)-Time-O(1)-extra-space | class Solution:
def smallestNumber(self, pattern: str) -> str:
res = []
n = len(pattern)
cur = 1
i = 0
while i < n:
d_count = 0
while i < n and pattern[i] == 'D':
d_count += 1
i += 1
for x in ran... | construct-smallest-number-from-di-string | Python Greedy Solution O(n) Time O(1) extra space | nevergiveup2 | 0 | 1 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,521 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2666308/Python3O(n)O(1)-Linear-Scan-with-constant-space-with-explaincomments | class Solution:
def smallestNumber(self, pattern: str) -> str:
# the result
res = ''
# current number
curr = 0
# how many Ds we see so far
decrease_count = 0
# we are adding 'I' to our pattern so that we can avoid
# doing the inner for loop again afte... | construct-smallest-number-from-di-string | [Python3][O(n)/O(1)] Linear Scan with constant space with explain/comments | eaglediao | 0 | 4 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,522 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2451111/Python3-or-Index-Out-of-Bounds-Help(Can't-Identify-Cause-of-Bug) | class Solution:
def smallestNumber(self, pattern: str) -> str:
#Approach: Basically, given pattern string input of length n, generate all possible
#distinct length n+1 num strings, check if each of them is a valid DI string, then
#check it against built-up answer an... | construct-smallest-number-from-di-string | Python3 | Index Out of Bounds Help(Can't Identify Cause of Bug) | JOON1234 | 0 | 13 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,523 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2451069/Python3-or-Why-Am-I-Failing-Edge-Case(%22DDDIII%22) | class Solution:
def smallestNumber(self, pattern: str) -> str:
#Approach: I will define a recursive helper function that will build up the valid num
#string based on specified pattern string!
#It will involve 3 parameters: current built up string cur, index... | construct-smallest-number-from-di-string | Python3 | Why Am I Failing Edge Case("DDDIII") | JOON1234 | 0 | 11 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,524 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2425749/backtrack-oror-Python3 | class Solution:
def smallestNumber(self, pattern: str) -> str:
# flag works as backtrack ending signal
self.flag = False
self.res = ""
# num: current digit string up to this step
# string: remaining pattern need to be deal with
# unused: remaining digits that are not used in num
... | construct-smallest-number-from-di-string | backtrack || Python3 | xmmm0 | 0 | 7 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,525 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2425098/Python3-or-Need-Help-Figuring-Why-I-am-getting-Index-Out-of-Bounds-Error! | class Solution:
#define a helper function that checks whether given streets meets the given pattern!
def helper(self, string: str, pattern: str)->bool:
n = len(pattern)
#basically, for every index position i, check whether string[i] > string[i+1] if pattern[i]
#is 'D' or opposite otherwi... | construct-smallest-number-from-di-string | Python3 | Need Help Figuring Why I am getting Index Out of Bounds Error! | JOON1234 | 0 | 6 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,526 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2424538/Python3-Intuitive-O(n)-solution | class Solution:
def smallestNumber(self, pattern: str) -> str:
n = len(pattern)
# Since we want the minimal string satisfying the conditions, we can restrict our search to the smallest n+1 values:
numbers = [i for i in range(n+1,0,-1)]
# Next, we initialize the arra... | construct-smallest-number-from-di-string | Python3 Intuitive O(n) solution | yshulz | 0 | 13 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,527 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2423894/python3-Stack-sol-for-reference | class Solution:
def smallestNumber(self, pattern: str) -> str:
s = ["1"]
chars = list("23456789")
for index, p in enumerate(pattern):
i = index
if p == 'I':
tmp = []
while s and chars[0] < s[-1] and p... | construct-smallest-number-from-di-string | [python3] Stack sol for reference | vadhri_venkat | 0 | 6 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,528 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2423182/Switch-Digits | class Solution:
def smallestNumber(self, pattern: str) -> str:
res = []
def switch(s,d):
nonlocal res
while s<d:
res[s], res[d] = res[d], res[s]
s += 1
d -= 1
i = 1
while i<len(p... | construct-smallest-number-from-di-string | Switch Digits | leetcodeman117 | 0 | 2 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,529 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2423097/Back-tracking-with-pattern-and-unique-constrains | class Solution:
def smallestNumber(self, pattern: str) -> str:
def fsol(i, n, ans):
if len(ans)>0:
return
for j in range(1, 10):
if chk[j] is False and (i==0 or (pattern[i-1]=="I" and s[i-1]<j) or (pattern[i-1]=="D" and s[i-1]>j)):
... | construct-smallest-number-from-di-string | Back-tracking with pattern and unique constrains | dntai | 0 | 4 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,530 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2422427/Python-Simple-Python-Solution-By-Generating-All-Permutation | class Solution:
def smallestNumber(self, pattern: str) -> str:
l = [x for x in range(1,len(pattern)+2)]
check = []
from itertools import permutations
perm = permutations(l)
for i in list(perm):
num = list(i)
count = 0
for j in range(len(pattern)):
if pattern[j] == 'I':
if num[... | construct-smallest-number-from-di-string | [ Python ] ✅✅ Simple Python Solution By Generating All Permutation 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 27 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,531 |
https://leetcode.com/problems/construct-smallest-number-from-di-string/discuss/2422390/Secret-Python-Answer-Recursive-%2B-Backtracking-Set-of-Visited | class Solution:
def smallestNumber(self, pattern: str) -> str:
l = len(pattern)
sol = ""
visited = set([1,2,3,4,5,6,7,8,9])
def backtrack(cur, i ):
nonlocal l
nonlocal sol
if len(cur) == l+1... | construct-smallest-number-from-di-string | [Secret Python Answer🤫🐍👌😍] Recursive + Backtracking Set of Visited | xmky | 0 | 19 | construct smallest number from di string | 2,375 | 0.739 | Medium | 32,532 |
https://leetcode.com/problems/count-special-integers/discuss/2422258/Python3-dp | class Solution:
def countSpecialNumbers(self, n: int) -> int:
vals = list(map(int, str(n)))
@cache
def fn(i, m, on):
"""Return count at index i with mask m and profile flag (True/False)"""
ans = 0
if i == len(vals): return 1
for v in... | count-special-integers | [Python3] dp | ye15 | 5 | 466 | count special integers | 2,376 | 0.363 | Hard | 32,533 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2454458/Python-oror-Easy-Approach-oror-Sliding-Window-oror-Count | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
ans = 0
res = 0
for i in range(len(blocks) - k + 1):
res = blocks.count('B', i, i + k)
ans = max(res, ans)
ans = k - ans
return ans | minimum-recolors-to-get-k-consecutive-black-blocks | ✅Python || Easy Approach || Sliding Window || Count | chuhonghao01 | 3 | 318 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,534 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2469113/Python-Elegant-and-Short-or-One-pass-or-Sliding-window | class Solution:
"""
Time: O(n)
Memory: O(k)
"""
def minimumRecolors(self, blocks: str, k: int) -> int:
min_cost = cost = blocks[:k].count('W')
for i in range(k, len(blocks)):
cost = cost - (blocks[i - k] == 'W') + (blocks[i] == 'W')
min_cost = min(min_cost, cost)
return min_cost | minimum-recolors-to-get-k-consecutive-black-blocks | Python Elegant & Short | One pass | Sliding window | Kyrylo-Ktl | 2 | 123 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,535 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2488377/Python-for-beginners-Nice-question-to-learn-for-sliding-window-algorithm-Commented-solution!! | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
#Simple-Approach (Sliding Window)
#Runtime:42ms
lis=[]
for i in range(0,len(blocks)):
count_b=blocks[i:i+k].count("B") #Count Blacks
if(count_b>=k): #If Count Blacks > d... | minimum-recolors-to-get-k-consecutive-black-blocks | Python for beginners [Nice question to learn for sliding window algorithm] Commented solution!! | mehtay037 | 1 | 91 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,536 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2488377/Python-for-beginners-Nice-question-to-learn-for-sliding-window-algorithm-Commented-solution!! | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
#Simple-Approach (Sliding Window)
#Runtime:32ms
minimum_change=k #Worst Case scenario if all blocks are white minimum change required is k times
for i in range(0,len(blocks)):
count_b=blocks[i:i+k... | minimum-recolors-to-get-k-consecutive-black-blocks | Python for beginners [Nice question to learn for sliding window algorithm] Commented solution!! | mehtay037 | 1 | 91 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,537 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2478962/Python-easy-solution-28-ms-oror-faster-than-100 | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
min_operation, step = 0, 0
while step < len(blocks) - k + 1:
temp_arr = blocks[step : step + k]
if step == 0:
min_operation += temp_arr.count("W")
else:
min_operation = min(min_operation, temp_arr.count("W"))
step += 1
re... | minimum-recolors-to-get-k-consecutive-black-blocks | ✅ Python easy solution 28 ms || faster than 100% | sezanhaque | 1 | 61 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,538 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2471572/Python-easy-sliding-window-solution-faster-than-80 | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
res = len(blocks)
for i in range(len(blocks)):
select = blocks[i:i+k]
if len(select) == k:
if select.count('W') < res:
res = select.count('W')
else:
... | minimum-recolors-to-get-k-consecutive-black-blocks | Python easy sliding window solution faster than 80% | alishak1999 | 1 | 54 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,539 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2455631/Python-simple-sliding-window-solution | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
ans = []
for i in range(0, len(blocks)-k+1):
ans.append(blocks[i:i+k].count('W'))
return min(ans) | minimum-recolors-to-get-k-consecutive-black-blocks | Python simple sliding window solution | StikS32 | 1 | 41 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,540 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2454177/Python3-Sliding-window | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
ans = inf
rsm = 0
for i, ch in enumerate(blocks):
if ch == 'W': rsm += 1
if i >= k and blocks[i-k] == 'W': rsm -= 1
if i >= k-1: ans = min(ans, rsm)
return ans | minimum-recolors-to-get-k-consecutive-black-blocks | [Python3] Sliding window | ye15 | 1 | 52 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,541 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2844998/Python-Easy-or-Sliding-window-or-Simple-O(N) | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
length = len(blocks)
w, ans = 0, float('inf')
for j in range(length):
if j<k:
if blocks[j]=='W': w+=1
if j == k-1: ans = min(w, ans)
else:
if blocks[... | minimum-recolors-to-get-k-consecutive-black-blocks | [Python] Easy | Sliding window | Simple O(N) | girraj_14581 | 0 | 2 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,542 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2832475/Python-or-Clearly-Explained-or-Faster-than-95 | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
i = 0
chan = 0
minChain = inf
while i+k <= len(blocks):
ss = blocks[i:i+k]
chan = ss.count('W')
minChain = min(chan,minChain)
i += 1
return minChain | minimum-recolors-to-get-k-consecutive-black-blocks | Python | Clearly Explained | Faster than 95% | Chityanj | 0 | 2 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,543 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2827321/Python-Sliding-Window-Easy-to-Understand | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
mn = inf
for i in range(k, len(blocks) + 1):
window = blocks[i-k:i]
wcount = window.count('W')
mn = min(mn, wcount)
return mn | minimum-recolors-to-get-k-consecutive-black-blocks | Python Sliding Window - Easy to Understand | ziqinyeow | 0 | 2 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,544 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2814476/Python-Solution-with-0(n) | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
i, ans, count = 0,0,0
for j in range(len(blocks)):
if j < k:
if blocks[j] == 'W':
count+=1
ans = count
else:
if blocks[j] == "W":
... | minimum-recolors-to-get-k-consecutive-black-blocks | Python Solution with 0(n) | duresafeyisa2022 | 0 | 1 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,545 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2808316/Simple-Sliding-Window-Solution | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
left, right = 0 , 0
ans = k
count = 0
blocks = blocks.replace('W','1').replace('B','0')
while right < len(blocks):
count += int(blocks[right])
while right - left + 1 > k:
... | minimum-recolors-to-get-k-consecutive-black-blocks | Simple Sliding Window Solution | vijay_2022 | 0 | 3 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,546 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2804084/Solution-based-on-fixed-size-sliding-window-template | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
count = float('inf')
start = 0
end = 0
recolorCount = 0
while end < len(blocks):
if blocks[end] == 'W':
recolorCount += 1
if end - start + 1 >= k:
... | minimum-recolors-to-get-k-consecutive-black-blocks | Solution based on fixed size sliding window template | disturbedbrown1 | 0 | 1 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,547 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2799445/SLIDING-WINDOW-SOLUTION | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
res=[]
for i in range(len(blocks)-k+1):
subarr=blocks[i:i+k]
count=subarr.count("W")
res.append(count)
if len(blocks)<k:
return 0
else:
return min(res) | minimum-recolors-to-get-k-consecutive-black-blocks | SLIDING WINDOW SOLUTION | vanshikasharma24__ | 0 | 3 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,548 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2784869/sliding-window-with-dict | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
count = defaultdict(int)
l, res = 0, float('inf')
for r in range(len(blocks)):
count[blocks[r]] += 1
if r - l + 1 > k:
count[blocks[l]] -= 1
l += 1
if r ... | minimum-recolors-to-get-k-consecutive-black-blocks | sliding window with dict | JasonDecode | 0 | 2 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,549 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2728935/Simple-Python-Sliding-Window-Solution-95-Time-75-Space | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
# use sliding window to find max number of B in k substring
m = curr = blocks[:k].count('B')
for i in range(1, len(blocks)-k+1):
curr -= 1 if blocks[i-1] == 'B' else 0 #last char of prev substring
... | minimum-recolors-to-get-k-consecutive-black-blocks | Simple Python Sliding Window Solution, 95% Time, 75% Space | Parth86 | 0 | 9 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,550 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2723402/Simple-Python-Solution | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
ans = []
for i in range(len(blocks)-k+1):
ans.append(blocks[i:i+k].count('W'))
return min(ans) | minimum-recolors-to-get-k-consecutive-black-blocks | Simple Python Solution | sbaguma | 0 | 4 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,551 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2695258/sliding-window-easy-concepts-in-python | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
ws=0
we=0
mini=1000000000000000
flips=0
count=0
for we in range(len(blocks)):
if blocks[we]=="W":
flips+=1
count+=1
elif blocks[we]=="B":
... | minimum-recolors-to-get-k-consecutive-black-blocks | sliding window easy concepts in python | insane_me12 | 0 | 4 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,552 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2635177/Very-Simple-solution-using-SLIDING-WINDOW-with-explanation-or-Python-or-HashMap | class Solution(object):
def minimumRecolors(self, blocks, k):
"""
:type blocks: str
:type k: int
:rtype: int
"""
#in this problem, k is the size of window. We need to check 'W' counts in each window of length k. And need to update minimum w.r.t each window
min... | minimum-recolors-to-get-k-consecutive-black-blocks | Very Simple solution using SLIDING WINDOW with explanation | Python | HashMap | msherazedu | 0 | 23 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,553 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2509266/Python-two-solutions | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
return min(blocks[i:i+k].count('W') for i in range((len(blocks)-k)+1)) | minimum-recolors-to-get-k-consecutive-black-blocks | Python two solutions | omarihab99 | 0 | 59 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,554 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2509266/Python-two-solutions | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
no_operations=0
min_operations=math.inf
window_start=0
block_freq=defaultdict(int)
for window_end in range(len(blocks)):
right=blocks[window_end]
block_freq[right]+=1
if right... | minimum-recolors-to-get-k-consecutive-black-blocks | Python two solutions | omarihab99 | 0 | 59 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,555 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2499396/Python-or-sliding-window-or-easy | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
if 'B'*k in blocks:
return 0
curr=0
least=n=len(blocks)
i=j=0
blocks2=list(blocks)
while j<n:
if blocks2[j]=='W':
blocks2[j]='B'
curr+=1
... | minimum-recolors-to-get-k-consecutive-black-blocks | Python | sliding window | easy | ayushigupta2409 | 0 | 74 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,556 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2498913/Easy-python-solution | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
ans = float('inf')
for i in range(0, len(blocks) - k + 1):
ans = min(ans, blocks[i:i+k].count('W'))
return ans | minimum-recolors-to-get-k-consecutive-black-blocks | Easy python solution | Maxonchikkkk | 0 | 24 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,557 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2461357/Python-Sliding-Window | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
start = 0
end = 0
op = 0
minop = float("inf")
for end in range(len(blocks)):
if blocks[end] == 'W':
op += 1
if end - start + 1 == k:
mi... | minimum-recolors-to-get-k-consecutive-black-blocks | Python Sliding Window | theReal007 | 0 | 23 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,558 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2460524/Python-or-Sliding-Window-or-6-Lines | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
max_black = black_count = blocks[:k-1].count('B')
for i in range(k-1, len(blocks)):
black_count += blocks[i] == 'B'
max_black = max(max_black, black_count)
black_count -= blocks[i - k + 1] == '... | minimum-recolors-to-get-k-consecutive-black-blocks | Python | Sliding Window | 6 Lines | leeteatsleep | 0 | 34 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,559 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2455702/Python-sliding-window | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
result = count = blocks[:k].count('W')
for i in range(k, len(blocks)):
count += (blocks[i] == 'W') - (blocks[i-k] == 'W')
result = min(result, count)
return result | minimum-recolors-to-get-k-consecutive-black-blocks | Python, sliding window | blue_sky5 | 0 | 34 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,560 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2455429/Sliding-window-one-pass | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
whites = blocks[:k].count("W")
ans = whites
if ans == 0:
return 0
for i in range(k, len(blocks)):
whites += (blocks[i] == "W") - (blocks[i - k] == "W")
ans = min(ans, whites)
... | minimum-recolors-to-get-k-consecutive-black-blocks | Sliding window, one pass | EvgenySH | 0 | 7 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,561 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2454601/Python3-or-Sliding-Window-Approach | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
#Approach: Just utilize sliding window technique!
#Keep track of number of white blocks in current sliding window and once you reach stopping condition:
#when you have exactly length of sliding window consisting of k bloc... | minimum-recolors-to-get-k-consecutive-black-blocks | Python3 | Sliding Window Approach | JOON1234 | 0 | 13 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,562 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2454591/Python3-Sliding-window-commented-Solution | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
i = w = front = 0
minimum = 10**6
while i < len(blocks):
if blocks[i] =="W":
w += 1 # Count of w since we need to repaint them. We can also use B... | minimum-recolors-to-get-k-consecutive-black-blocks | Python3 Sliding window commented Solution | abhisheksanwal745 | 0 | 15 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,563 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2454590/Easiest-solution-in-the-Python-for-beginners-O(n) | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
a=[]
for i in range(0,len(blocks)):
x=blocks[i:k+i]
if len(x)==k:
g=x.count("W")
a.append(g)
else:
break
return min(a) | minimum-recolors-to-get-k-consecutive-black-blocks | ✔️✔️Easiest solution in the Python for beginners O(n) ✔️✔️ | giridharan7 | 0 | 24 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,564 |
https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/discuss/2454161/Python3 | class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
start_index = 0
length = len(blocks)
res = float("inf")
count = 0
for i in range(start_index, k):
if blocks[i] == "W":
count += 1
res = min(res, count)
... | minimum-recolors-to-get-k-consecutive-black-blocks | [Python3] | zonda_yang | 0 | 32 | minimum recolors to get k consecutive black blocks | 2,379 | 0.568 | Easy | 32,565 |
https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/discuss/2454195/Python3-O(N)-dp-approach | class Solution:
def secondsToRemoveOccurrences(self, s: str) -> int:
ans = prefix = prev = 0
for i, ch in enumerate(s):
if ch == '1':
ans = max(prev, i - prefix)
prefix += 1
if ans: prev = ans+1
return ans | time-needed-to-rearrange-a-binary-string | [Python3] O(N) dp approach | ye15 | 49 | 2,200 | time needed to rearrange a binary string | 2,380 | 0.482 | Medium | 32,566 |
https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/discuss/2454424/Python-oror-Easy-Approach-oror-Replace-(5-lines) | class Solution:
def secondsToRemoveOccurrences(self, s: str) -> int:
ans = 0
while '01' in s:
ans += 1
s = s.replace('01', '10')
return ans | time-needed-to-rearrange-a-binary-string | ✅Python || Easy Approach || Replace (5 lines) | chuhonghao01 | 11 | 551 | time needed to rearrange a binary string | 2,380 | 0.482 | Medium | 32,567 |
https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/discuss/2677222/PYTHON-or-Beginner-or-Easy-or-81-faster | class Solution:
def secondsToRemoveOccurrences(self, s: str) -> int:
c=0
for i in range(len(s)):
if "01" in s:
s=s.replace("01","10")
c+=1
return(c)
``` | time-needed-to-rearrange-a-binary-string | PYTHON | Beginner | Easy | 81% faster | envyTheClouds | 1 | 52 | time needed to rearrange a binary string | 2,380 | 0.482 | Medium | 32,568 |
https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/discuss/2478370/Python-brute-force-step-by-step-solution-and-small-solution | class Solution:
def secondsToRemoveOccurrences(self, s: str) -> int:
count = 0
temp = ""
ones = s.count("1") # get the count of 1
for _ in range(ones):
"""
make a string with total number of 1
"""
temp += "1"
while s[:ones] != temp:
"""
loop through index 0 to count of 1 while the string i... | time-needed-to-rearrange-a-binary-string | Python brute force step by step solution & small solution | sezanhaque | 1 | 65 | time needed to rearrange a binary string | 2,380 | 0.482 | Medium | 32,569 |
https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/discuss/2478370/Python-brute-force-step-by-step-solution-and-small-solution | class Solution:
def secondsToRemoveOccurrences(self, s: str) -> int:
count = 0
while "01" in s:
"""
While we are getting "01" in the string
we will replace them into "10"
and count the number of occurrence
"""
s = s.replace("01", "10")
count += 1
return count | time-needed-to-rearrange-a-binary-string | Python brute force step by step solution & small solution | sezanhaque | 1 | 65 | time needed to rearrange a binary string | 2,380 | 0.482 | Medium | 32,570 |
https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/discuss/2795812/Python-(Simple-Maths) | class Solution:
def secondsToRemoveOccurrences(self, s):
total = zeros = 0
for i in range(len(s)):
zeros += 1 if s[i] == "0" else 0
if s[i] == "1" and zeros:
total = max(total+1,zeros)
return total | time-needed-to-rearrange-a-binary-string | Python (Simple Maths) | rnotappl | 0 | 14 | time needed to rearrange a binary string | 2,380 | 0.482 | Medium | 32,571 |
https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/discuss/2618579/Python-Easy-solution | class Solution:
def secondsToRemoveOccurrences(self, s: str) -> int:
ans=zeros=0
for i in range(len(s)):
zeros+=1 if s[i] == '0' else 0
if s[i] == '1' and zeros:
ans=max(ans+1,zeros)
return ans | time-needed-to-rearrange-a-binary-string | Python Easy solution | beingab329 | 0 | 44 | time needed to rearrange a binary string | 2,380 | 0.482 | Medium | 32,572 |
https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/discuss/2540242/O(N)-Solution | class Solution(object):
def secondsToRemoveOccurrences(self, s):
"""
:type s: str
:rtype: int
"""
res = 0
index = 0
pre = 0
for i in range(len(s)):
if s[i] == '0':
pre = max(pre-1, 0)
else:
if i =... | time-needed-to-rearrange-a-binary-string | O(N) Solution | ryhurain | 0 | 122 | time needed to rearrange a binary string | 2,380 | 0.482 | Medium | 32,573 |
https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/discuss/2456430/Python-Solution | class Solution:
def secondsToRemoveOccurrences(self, s: str) -> int:
c=0
while "01" in s:
s=s.replace("01","10")
c+=1
return c | time-needed-to-rearrange-a-binary-string | Python Solution | a_dityamishra | 0 | 21 | time needed to rearrange a binary string | 2,380 | 0.482 | Medium | 32,574 |
https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/discuss/2455905/DP-approach-Time-complexity-O(n)-100-fast | class Solution(object):
def secondsToRemoveOccurrences(self, s):
zeroes = ones = ans = 0
for c in s:
if c == "1":
if zeroes:
ans = max(ans+1,zeroes)
ones+=1
else:
zeroes+=1
return ans | time-needed-to-rearrange-a-binary-string | DP approach, Time complexity O(n), 100% fast | Virus_003 | 0 | 47 | time needed to rearrange a binary string | 2,380 | 0.482 | Medium | 32,575 |
https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/discuss/2455615/Python3-or-Brute-Force-Approach | class Solution:
def secondsToRemoveOccurrences(self, s: str) -> int:
#Brute Force this!
count = 0
while "01" in s:
s = s.replace("01", "10")
count += 1
return count | time-needed-to-rearrange-a-binary-string | Python3 | Brute Force Approach | JOON1234 | 0 | 6 | time needed to rearrange a binary string | 2,380 | 0.482 | Medium | 32,576 |
https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/discuss/2454499/Python3-or-Brute-force-or-Easy-to-understand | class Solution:
def secondsToRemoveOccurrences(self, s: str) -> int:
seconds = 0
my_list = [*s]
my_string = s
while '01' in my_string:
i = 0
while i < len(my_list) - 1:
if my_list[i] == '0' and my_list[i + 1] == '1':
... | time-needed-to-rearrange-a-binary-string | Python3 | Brute force | Easy to understand | Amunra43 | 0 | 22 | time needed to rearrange a binary string | 2,380 | 0.482 | Medium | 32,577 |
https://leetcode.com/problems/shifting-letters-ii/discuss/2454404/Python-Cumulative-Sum-oror-Easy-Solution | class Solution:
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
cum_shifts = [0 for _ in range(len(s)+1)]
for st, end, d in shifts:
if d == 0:
cum_shifts[st] -= 1
cum_shifts[end+1] += 1
else:
cum_shif... | shifting-letters-ii | ✅ Python Cumulative Sum || Easy Solution | idntk | 15 | 850 | shifting letters ii | 2,381 | 0.342 | Medium | 32,578 |
https://leetcode.com/problems/shifting-letters-ii/discuss/2465791/Python3-oror-8-lines-letter-indexing-wexplanation-oror-TM%3A-100-60 | class Solution: # Here's the plan:
# 1) 1a: Initiate an array offsets with length len(s)+1.
# 1b: Iterate through shifts and collect the endpts and the direction
# of each shift. (Note that 2*(0)-1 = -1 and 2*(1)-1 = 1.)
... | shifting-letters-ii | Python3 || 8 lines, letter indexing, w/explanation || T/M: 100%/ 60% | warrenruud | 5 | 158 | shifting letters ii | 2,381 | 0.342 | Medium | 32,579 |
https://leetcode.com/problems/shifting-letters-ii/discuss/2492482/Python-3-or-Prefix-Sum-or-Explanation | class Solution:
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
n = len(s)
d = collections.Counter()
for st, e, right in shifts:
d[st] += 1 if right else -1 # Mark at the beginning to indicate everything after it need to be shifted
if e+1 < ... | shifting-letters-ii | Python 3 | Prefix Sum | Explanation | idontknoooo | 1 | 84 | shifting letters ii | 2,381 | 0.342 | Medium | 32,580 |
https://leetcode.com/problems/shifting-letters-ii/discuss/2454506/Python3-Sum-Changes-per-Element | class Solution:
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
n = len(s)
dp = [0]*(n + 1)
res = ""
#Get changes
for u, v, w in shifts:
if w:
dp[u] += 1
dp[v + 1] -= 1
else:
d... | shifting-letters-ii | [Python3] Sum Changes per Element | 0xRoxas | 1 | 51 | shifting letters ii | 2,381 | 0.342 | Medium | 32,581 |
https://leetcode.com/problems/shifting-letters-ii/discuss/2454215/Python3-Line-sweep | class Solution:
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
ops = []
for start, end, direction in shifts:
direction = 2*direction-1
ops.append((start, direction))
ops.append((end+1, -direction))
ops.sort()
ans = []
... | shifting-letters-ii | [Python3] Line sweep | ye15 | 1 | 99 | shifting letters ii | 2,381 | 0.342 | Medium | 32,582 |
https://leetcode.com/problems/shifting-letters-ii/discuss/2764071/python-3-or-line-sweep-or-O(n)O(n) | class Solution:
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
n = len(s)
totalShifts = [0] * (n + 1)
for start, end, direction in shifts:
if not direction:
direction = -1
totalShifts[start] += direction
totalShifts[end ... | shifting-letters-ii | python 3 | line sweep | O(n)/O(n) | dereky4 | 0 | 4 | shifting letters ii | 2,381 | 0.342 | Medium | 32,583 |
https://leetcode.com/problems/shifting-letters-ii/discuss/2668723/Python3-or-Line-Sweep | class Solution:
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
sz=len(s)
freq=[0 for i in range(sz+1)]
for a,b,c in shifts:
if c==1:
freq[a]+=1
freq[b+1]-=1
else:
freq[a]-=1
freq[b+1]+... | shifting-letters-ii | [Python3] | Line Sweep | swapnilsingh421 | 0 | 10 | shifting letters ii | 2,381 | 0.342 | Medium | 32,584 |
https://leetcode.com/problems/shifting-letters-ii/discuss/2455661/Segment-Tree-with-updating-on-range | class Solution:
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
def update(a, x, y, p, v, l, r):
"""
update at position p
"""
if x>=r or l>=y:
return
if x<=l and r<=y:
tree[v]["full"] ... | shifting-letters-ii | Segment Tree with updating on range | dntai | 0 | 30 | shifting letters ii | 2,381 | 0.342 | Medium | 32,585 |
https://leetcode.com/problems/shifting-letters-ii/discuss/2455166/Reverse-Pref-Sum-Diff-Sum | class Solution:
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
b, n = [], len(s)
list_s = [0] + [ord(i) - ord('a') for i in s]
for i in range(n):
b.append(list_s[i + 1] - list_s[i])
for start, end, direction in shifts:
if end >= n - 1:
... | shifting-letters-ii | Reverse Pref Sum - Diff Sum | Che4pSc1ent1st | 0 | 10 | shifting letters ii | 2,381 | 0.342 | Medium | 32,586 |
https://leetcode.com/problems/shifting-letters-ii/discuss/2455020/Python3-or-Need-Help-Understanding-Why-I-am-Failing-2nd-example-test-case! | class Solution:
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
#Try brute force!
#use a hashmap to keep track of how many times we have to forward or backward shift:
#neg value -> shift that many times backwards!
#0 -> no shift!
#pos -> s... | shifting-letters-ii | Python3 | Need Help Understanding Why I am Failing 2nd example test case! | JOON1234 | 0 | 20 | shifting letters ii | 2,381 | 0.342 | Medium | 32,587 |
https://leetcode.com/problems/shifting-letters-ii/discuss/2454613/prefix_sum | class Solution:
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
s = list(s)
prefix = defaultdict(int)
for i in shifts:
prefix[i[0]]+=(2*i[2]-1)
prefix[i[1]+1] -=(2*i[2]-1)
prefix_sum = 0
for i in range(len(s)):
prefix_su... | shifting-letters-ii | prefix_sum | shashichintu | 0 | 17 | shifting letters ii | 2,381 | 0.342 | Medium | 32,588 |
https://leetcode.com/problems/shifting-letters-ii/discuss/2454423/Secret-Python-Answer-Simple-Sort-Solution | class Solution:
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
change = [[0,0]] * (len(shifts) *2)
i = 0
for start,end,dir in shifts:
change[i] = [start,1 if dir == 1 else -1]
i+=1
change[i] = [end+1,-1 if d == 1 else ... | shifting-letters-ii | [Secret Python Answer🤫🐍👌😍] Simple Sort Solution | xmky | 0 | 46 | shifting letters ii | 2,381 | 0.342 | Medium | 32,589 |
https://leetcode.com/problems/shifting-letters-ii/discuss/2454368/Accumulating-Deltas-for-Ranges-and-then-Processing | class Solution:
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
ops = [0]*(len(s)+1)
for start, end, direction in shifts:
ops[start] += 1 if direction == 1 else -1
ops[end+1] += -1 if direction == 1 else 1
runningDelta = 0
... | shifting-letters-ii | Accumulating Deltas for Ranges and then Processing | user2867d | 0 | 20 | shifting letters ii | 2,381 | 0.342 | Medium | 32,590 |
https://leetcode.com/problems/maximum-segment-sum-after-removals/discuss/2454397/Do-it-backwards-O(N) | class Solution:
def maximumSegmentSum(self, nums: List[int], removeQueries: List[int]) -> List[int]:
mp, cur, res = {}, 0, []
for q in reversed(removeQueries[1:]):
mp[q] = (nums[q], 1)
rv, rLen = mp.get(q+1, (0, 0))
lv, lLen = mp.get(q-1, (0, 0))
... | maximum-segment-sum-after-removals | Do it backwards O(N) | user9591 | 29 | 1,100 | maximum segment sum after removals | 2,382 | 0.476 | Hard | 32,591 |
https://leetcode.com/problems/maximum-segment-sum-after-removals/discuss/2454234/Python3-Priority-queue | class Solution:
def maximumSegmentSum(self, nums: List[int], removeQueries: List[int]) -> List[int]:
n = len(nums)
sl = SortedList([-1, n])
prefix = list(accumulate(nums, initial=0))
mp = {-1 : n}
pq = [(-prefix[-1], -1, n)]
ans = []
for q in removeQ... | maximum-segment-sum-after-removals | [Python3] Priority queue | ye15 | 7 | 476 | maximum segment sum after removals | 2,382 | 0.476 | Hard | 32,592 |
https://leetcode.com/problems/maximum-segment-sum-after-removals/discuss/2475939/python-3-or-union-find | class Solution:
def maximumSegmentSum(self, nums: List[int], removeQueries: List[int]) -> List[int]:
n = len(nums)
res = [0] * n
parent = [i for i in range(n)]
rank = [1] * n
sums = [0] * n
def union(i, j):
i, j = find(i), find(j)
if r... | maximum-segment-sum-after-removals | python 3 | union find | dereky4 | 0 | 35 | maximum segment sum after removals | 2,382 | 0.476 | Hard | 32,593 |
https://leetcode.com/problems/maximum-segment-sum-after-removals/discuss/2454430/Python3-or-Prefix-Sum-and-Bisect-or-O(n-log(n)) | class Solution:
def maximumSegmentSum(self, nums: List[int], removeQueries: List[int]) -> List[int]:
running, sum_before = 0, []
for num in nums:
running += num
sum_before.append(running)
sum_before.append(0) # [-1]
def get_sum(start, end):
... | maximum-segment-sum-after-removals | Python3 | Prefix Sum & Bisect | O(n log(n)) | ryangrayson | 0 | 48 | maximum segment sum after removals | 2,382 | 0.476 | Hard | 32,594 |
https://leetcode.com/problems/minimum-hours-of-training-to-win-a-competition/discuss/2456759/Python-oror-Easy-Approach | class Solution:
def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:
ans = 0
n = len(energy)
for i in range(n):
while initialEnergy <= energy[i] or initialExperience <= experience[i]:
if... | minimum-hours-of-training-to-win-a-competition | ✅Python || Easy Approach | chuhonghao01 | 6 | 460 | minimum hours of training to win a competition | 2,383 | 0.41 | Easy | 32,595 |
https://leetcode.com/problems/minimum-hours-of-training-to-win-a-competition/discuss/2722230/Python-Java-Elegant-and-Short-or-One-pass | class Solution:
"""
Time: O(n)
Memory: O(1)
"""
def minNumberOfHours(self, energy: int, experience: int, energies: List[int], experiences: List[int]) -> int:
hours = 0
for eng, exp in zip(energies, experiences):
# Adding the missing amount of energy
extra_... | minimum-hours-of-training-to-win-a-competition | Python, Java Elegant & Short | One pass | Kyrylo-Ktl | 2 | 103 | minimum hours of training to win a competition | 2,383 | 0.41 | Easy | 32,596 |
https://leetcode.com/problems/minimum-hours-of-training-to-win-a-competition/discuss/2456615/Python3-simulation | class Solution:
def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:
ans = 0
for x, y in zip(energy, experience):
if initialEnergy <= x:
ans += x + 1 - initialEnergy
initialEnergy = x ... | minimum-hours-of-training-to-win-a-competition | [Python3] simulation | ye15 | 2 | 78 | minimum hours of training to win a competition | 2,383 | 0.41 | Easy | 32,597 |
https://leetcode.com/problems/minimum-hours-of-training-to-win-a-competition/discuss/2834506/Simple-python-solution | class Solution:
def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:
energy_gain = experience_gain = 0
for ene in energy:
if initialEnergy - ene < 1:
energy_gain += ene + 1 - initialEnergy
... | minimum-hours-of-training-to-win-a-competition | Simple python solution | StacyAceIt | 0 | 1 | minimum hours of training to win a competition | 2,383 | 0.41 | Easy | 32,598 |
https://leetcode.com/problems/minimum-hours-of-training-to-win-a-competition/discuss/2816970/python3-solution | class Solution:
def minNumberOfHours(self, initialEnergy: int, ie: int, energy: List[int], experience: List[int]) -> int:
hours=sum(energy)-initialEnergy+1
if hours<0:
hours=0
for x in experience:
if ie>x:
ie=ie+x
... | minimum-hours-of-training-to-win-a-competition | python3 solution | giftyaustin | 0 | 1 | minimum hours of training to win a competition | 2,383 | 0.41 | Easy | 32,599 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.