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/check-if-all-the-integers-in-a-range-are-covered/discuss/1851546/3-Lines-Python-Solution-oror-80-Faster-(44ms)oror-Memory-less-than-60
class Solution: def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool: for nbr in [i for i in range(left,right+1,1)]: if not any([True for r in ranges if r[0]<=nbr<=r[1]]): return False return True
check-if-all-the-integers-in-a-range-are-covered
3-Lines Python Solution || 80% Faster (44ms)|| Memory less than 60%
Taha-C
0
63
check if all the integers in a range are covered
1,893
0.508
Easy
26,800
https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered/discuss/1725186/WEEB-EXPLAINS-PYTHON-(SIMPLE-SOLUTION)
class Solution: def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool: ranges.sort() # we don't sort, we will miss out some intermediate numbers for i in range(len(ranges)): if ranges[i][0] <= left <= ranges[i][1]: # if left within the current interval left = ranges[i][1] + 1 # set the new left, but add 1 because ranges[i][1] is inclusive if ranges[i][0] <= right <= ranges[i][1]: # if right within the current interval right = ranges[i][0] - 1 # set the new right, but minus 1 becauase range[1][i] is inclusive # repeat the logic until we break the logic of left > right or right < left if left > right or right < left: # if the conditions are true, we covered all integers in the range [left, right] inclusive return True return False
check-if-all-the-integers-in-a-range-are-covered
WEEB EXPLAINS PYTHON (SIMPLE SOLUTION)
Skywalker5423
0
73
check if all the integers in a range are covered
1,893
0.508
Easy
26,801
https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered/discuss/1426455/Python3-Prefix-Array-For-Visited-Elements-Faster-Than-89.47-Memory-Less-Than-99.58
class Solution: def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool: mn, mx = 99999, 0 for i in ranges: mn, mx = min(mn, i[0]), max(mx, i[1]) if mn > left or mx < right: return False prefix = [0] * (mx + 2) for i in ranges: prefix[i[0] : (i[1] + 1)] = [1] * ((i[1] - i[0]) + 1) for i in range(left, right + 1): if prefix[i] == 0: return False return True
check-if-all-the-integers-in-a-range-are-covered
Python3 Prefix Array For Visited Elements, Faster Than 89.47%, Memory Less Than 99.58%
Hejita
0
93
check if all the integers in a range are covered
1,893
0.508
Easy
26,802
https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered/discuss/1316235/Simple-Python3-O(n)-solution-using-dp
class Solution: def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool: li=[0]*52 for x,y in ranges: li[x]+=1 li[y+1]-=1 for i in range(1,52): li[i]+=li[i-1] for i in range(left,right+1): if li[i]<=0: return False return True
check-if-all-the-integers-in-a-range-are-covered
Simple Python3 O(n) solution using dp
atm1504
0
62
check if all the integers in a range are covered
1,893
0.508
Easy
26,803
https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered/discuss/1269136/easy-to-understand-python3
class Solution: def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool: ranges.sort() rop=[False]*((right-left)+1) k=-1 for i in range(left,right+1): k+=1 for j in range(len(ranges)): if ranges[j][0]<=i<=ranges[j][1]: rop[k]=True break if False in rop: return(False) else: return (True)
check-if-all-the-integers-in-a-range-are-covered
easy to understand python3
janhaviborde23
0
81
check if all the integers in a range are covered
1,893
0.508
Easy
26,804
https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered/discuss/1268159/Python-Easy-solution-using-Set
class Solution: def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool: seen = set() for interval in ranges: start = interval[0] end = interval[1] # adds all numbers in range to the seen numbers for x in range(start, end+1): seen.add(x) # make sure all numbers from left to right have been seen for x in range(left, right+1): if x not in seen: return False return True
check-if-all-the-integers-in-a-range-are-covered
Python, Easy solution using Set
ayrleetcoder
0
74
check if all the integers in a range are covered
1,893
0.508
Easy
26,805
https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/discuss/1612394/Python-oror-Prefix-Sum-and-Binary-Search-oror-O(n)-time-O(n)-space
class Solution: def chalkReplacer(self, chalk: List[int], k: int) -> int: prefix_sum = [0 for i in range(len(chalk))] prefix_sum[0] = chalk[0] for i in range(1,len(chalk)): prefix_sum[i] = prefix_sum[i-1] + chalk[i] remainder = k % prefix_sum[-1] #apply binary search on prefix_sum array, target = remainder start = 0 end = len(prefix_sum) - 1 while start <= end: mid = start + (end - start) // 2 if remainder == prefix_sum[mid]: return mid + 1 elif remainder < prefix_sum[mid]: end = mid - 1 else: start = mid + 1 return start
find-the-student-that-will-replace-the-chalk
Python || Prefix Sum and Binary Search || O(n) time O(n) space
s_m_d_29
4
244
find the student that will replace the chalk
1,894
0.438
Medium
26,806
https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/discuss/1267418/Python-or-simple-O(N)
class Solution: def chalkReplacer(self, chalk: List[int], k: int) -> int: x = sum(chalk) if x<k: k = k%x if x == k: return 0 i = 0 n = len(chalk) while True: if chalk[i]<=k: k -= chalk[i] else: break i +=1 return i
find-the-student-that-will-replace-the-chalk
Python | simple O(N)
harshhx
4
248
find the student that will replace the chalk
1,894
0.438
Medium
26,807
https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/discuss/1439073/Python-3-or-O(N)-or-Explanation
class Solution: def chalkReplacer(self, chalk: List[int], k: int) -> int: k %= sum(chalk) for i, num in enumerate(chalk): if k >= num: k -= num else: return i return -1
find-the-student-that-will-replace-the-chalk
Python 3 | O(N) | Explanation
idontknoooo
3
138
find the student that will replace the chalk
1,894
0.438
Medium
26,808
https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/discuss/2131735/C%2B%2B-and-Python-solution%3A-Single-pass-and-Binary-search
class Solution: def chalkReplacer(self, chalk: List[int], k: int) -> int: return bisect.bisect(list(accumulate(chalk)), k % sum(A))
find-the-student-that-will-replace-the-chalk
C++ and Python solution: Single pass and Binary search
deleted_user
1
23
find the student that will replace the chalk
1,894
0.438
Medium
26,809
https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/discuss/2131735/C%2B%2B-and-Python-solution%3A-Single-pass-and-Binary-search
class Solution: def chalkReplacer(self, chalk: List[int], k: int) -> int: k %= sum(chalk) for i, ck in enumerate(chalk): if k < ck: return i k -= ck return 0
find-the-student-that-will-replace-the-chalk
C++ and Python solution: Single pass and Binary search
deleted_user
1
23
find the student that will replace the chalk
1,894
0.438
Medium
26,810
https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/discuss/1267594/Python3-O(n)-Solution
class Solution: def chalkReplacer(self, chalk: List[int], k: int) -> int: sumi = sum(chalk) k = k % sumi for i , j in enumerate(chalk): if(k < j): break k -= j return i
find-the-student-that-will-replace-the-chalk
[Python3] O(n) Solution
VoidCupboard
1
21
find the student that will replace the chalk
1,894
0.438
Medium
26,811
https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/discuss/1267388/Python3-remainder
class Solution: def chalkReplacer(self, chalk: List[int], k: int) -> int: k %= sum(chalk) for i, x in enumerate(chalk): k -= x if k < 0: return i
find-the-student-that-will-replace-the-chalk
[Python3] remainder
ye15
1
26
find the student that will replace the chalk
1,894
0.438
Medium
26,812
https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/discuss/2764771/Python-3-or-Easy-3-line-solution-or-O(N)-O(1)-or-Modulus
class Solution: def chalkReplacer(self, chalk: List[int], k: int) -> int: k=k%sum(chalk) for i in range(len(chalk)): if k<chalk[i]:return i else: k-=chalk[i]
find-the-student-that-will-replace-the-chalk
Python 3 | Easy 3 line solution | O(N), O(1) | Modulus
saa_73
0
4
find the student that will replace the chalk
1,894
0.438
Medium
26,813
https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/discuss/2686506/Super-Easy-Python-Solution
class Solution: def chalkReplacer(self, chalk: List[int], k: int) -> int: s = sum(chalk) k %= s n = len(chalk) for i in range(n): if chalk[i]>k: return i k -= chalk[i]
find-the-student-that-will-replace-the-chalk
Super Easy Python Solution
RajatGanguly
0
3
find the student that will replace the chalk
1,894
0.438
Medium
26,814
https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/discuss/2107403/Python-3-solution-with-binary-search-and-prefix-sum-technique
class Solution: def chalkReplacer(self, chalk: List[int], k: int) -> int: prefix_sum = [chalk[0]] for i in range(1,len(chalk)): prefix_sum.append(prefix_sum[i-1]+chalk[i]) left_chalk = k % prefix_sum[len(prefix_sum)-1] s , e = 0 , len(prefix_sum)-1 while(s<=e): mid = s+(e-s)//2 if prefix_sum[mid] == left_chalk: return mid+1 if prefix_sum[mid-1] <= left_chalk and left_chalk < prefix_sum[mid]: return mid elif prefix_sum[mid-1] < left_chalk and left_chalk > prefix_sum[mid]: s = mid+1 else: e=mid-1 return mid
find-the-student-that-will-replace-the-chalk
Python 3 solution with binary search and prefix sum technique
ronipaul9972
0
35
find the student that will replace the chalk
1,894
0.438
Medium
26,815
https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/discuss/1941199/Python-Binary-Search-(-89.28-99.2)
class Solution: def chalkReplacer(self, chalk, k: int) -> int: k = k % sum(chalk) left = 0 right = len(chalk) - 1 while left <= right: mid = left + (right - left) // 2 res = sum(chalk[0:mid]) if res == k: return mid if res > k: right = mid - 1 if res <= k: left = mid + 1 return right
find-the-student-that-will-replace-the-chalk
Python Binary Search ( 89.28%, 99.2%)
fy552005184
0
81
find the student that will replace the chalk
1,894
0.438
Medium
26,816
https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/discuss/1906818/Python-or-Math-or-Linear-or-Comments-or-Easy-Solution
class Solution: def chalkReplacer(self, chalk: List[int], k: int) -> int: chalk_sum = sum(chalk) #finding the remaining ans after some loops ans = k%chalk_sum #Now finding same at last loop for i in range(len(chalk)): if ans==0 or ans<chalk[i]: return i ans-=chalk[i]
find-the-student-that-will-replace-the-chalk
Python | Math | Linear | Comments | Easy Solution
Brillianttyagi
0
37
find the student that will replace the chalk
1,894
0.438
Medium
26,817
https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/discuss/1499605/Python-3-Solution-Faster-then-88-and-memory-usage-93
class Solution: def chalkReplacer(self, chalk: List[int], k: int) -> int: iteration = sum(chalk) remainder = k%iteration if remainder == 0: return 0 else: while remainder >= 0: for i in range(0,len(chalk)): remainder =remainder - chalk[i] if remainder < 0: return i
find-the-student-that-will-replace-the-chalk
Python 3 Solution, Faster then 88% and memory usage 93%
xevb
0
47
find the student that will replace the chalk
1,894
0.438
Medium
26,818
https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/discuss/1267582/Simple-python-answer
class Solution: def chalkReplacer(self, chalk: List[int], k: int) -> int: while k>=0: for j,i in enumerate(chalk): if k<0: return j-1 k = k-i
find-the-student-that-will-replace-the-chalk
Simple python answer
Sanyamx1x
0
23
find the student that will replace the chalk
1,894
0.438
Medium
26,819
https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk/discuss/1267793/Python-Solution
class Solution: def chalkReplacer(self, chalk: List[int], k: int) -> int: k %= sum(chalk) n = len(chalk) for i in range(n): if chalk[i] > k: return i k -= chalk[i]
find-the-student-that-will-replace-the-chalk
Python Solution
mariandanaila01
-1
30
find the student that will replace the chalk
1,894
0.438
Medium
26,820
https://leetcode.com/problems/largest-magic-square/discuss/1267452/Python3-prefix-sums
class Solution: def largestMagicSquare(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) # dimensions rows = [[0]*(n+1) for _ in range(m)] # prefix sum along row cols = [[0]*n for _ in range(m+1)] # prefix sum along column for i in range(m): for j in range(n): rows[i][j+1] = grid[i][j] + rows[i][j] cols[i+1][j] = grid[i][j] + cols[i][j] ans = 1 for i in range(m): for j in range(n): diag = grid[i][j] for k in range(min(i, j)): ii, jj = i-k-1, j-k-1 diag += grid[ii][jj] ss = {diag} for r in range(ii, i+1): ss.add(rows[r][j+1] - rows[r][jj]) for c in range(jj, j+1): ss.add(cols[i+1][c] - cols[ii][c]) ss.add(sum(grid[ii+kk][j-kk] for kk in range(k+2))) # anti-diagonal if len(ss) == 1: ans = max(ans, k+2) return ans
largest-magic-square
[Python3] prefix sums
ye15
6
449
largest magic square
1,895
0.519
Medium
26,821
https://leetcode.com/problems/largest-magic-square/discuss/1267452/Python3-prefix-sums
class Solution: def largestMagicSquare(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) # dimensions rows = [[0]*(n+1) for _ in range(m)] cols = [[0]*n for _ in range(m+1)] diag = [[0]*(n+1) for _ in range(m+1)] anti = [[0]*(n+1) for _ in range(m+1)] for i in range(m): for j in range(n): rows[i][j+1] = grid[i][j] + rows[i][j] cols[i+1][j] = grid[i][j] + cols[i][j] diag[i+1][j+1] = grid[i][j] + diag[i][j] anti[i+1][j] = grid[i][j] + anti[i][j+1] ans = 1 for i in range(m): for j in range(n): for k in range(1, min(i, j)+1): ii, jj = i-k, j-k val = diag[i+1][j+1] - diag[ii][jj] match = (val == anti[i+1][jj] - anti[ii][j+1]) for r in range(ii, i+1): match &amp;= (val == rows[r][j+1] - rows[r][jj]) for c in range(jj, j+1): match &amp;= (val == cols[i+1][c] - cols[ii][c]) if match: ans = max(ans, k+1) return ans
largest-magic-square
[Python3] prefix sums
ye15
6
449
largest magic square
1,895
0.519
Medium
26,822
https://leetcode.com/problems/largest-magic-square/discuss/1267470/DP-and-Prefix-Sum-oror-97-faster-oror-well-Explained
class Solution: def largestMagicSquare(self, grid: List[List[int]]) -> int: m=len(grid) n=len(grid[0]) # Row sum matrix rowPrefixSum=[[0]*(n+1) for r in range(m)] for r in range(m): for c in range(n): rowPrefixSum[r][c+1]=rowPrefixSum[r][c] + grid[r][c] #column sum Matrix columnPrefixSum=[[0]*(m+1) for c in range(n)] for c in range(n): for r in range(m): columnPrefixSum[c][r+1]=columnPrefixSum[c][r] + grid[r][c] k=min(m,n) while 1<k: for r in range(m-k+1): for c in range(n-k+1): z=rowPrefixSum[r][c+k] - rowPrefixSum[r][c] ok=1 for i in range(k): if z!=rowPrefixSum[r+i][c+k]-rowPrefixSum[r+i][c] or z!=columnPrefixSum[c+i][r+k]-columnPrefixSum[c+i][r]: ok=0 break if ok: # check both diagonals diagZ1=0 diagZ2=0 for i in range(k): diagZ1+=grid[r+i][c+i] diagZ2+=grid[r+i][c+k-i-1] if z==diagZ1==diagZ2: return k k-=1 return 1
largest-magic-square
🐍 DP and Prefix Sum || 97% faster || well-Explained 📌
abhi9Rai
4
341
largest magic square
1,895
0.519
Medium
26,823
https://leetcode.com/problems/largest-magic-square/discuss/1436127/Python-3-or-Very-Clean-Code-O(min(M-N)-2-*-M-*-N)-or-Explanation
class Solution: def largestMagicSquare(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) dp = [[(0, 0, 0, 0)] * (n+1) for _ in range(m+1)] # [prefix-row-sum, prefix-col-sum, prefix-major-diagonal, prefix-minor-diagonal] for i in range(1, m+1): # O(m*n) on prefix calculation for j in range(1, n+1): dp[i][j] = [ dp[i][j-1][0] + grid[i-1][j-1], dp[i-1][j][1] + grid[i-1][j-1], dp[i-1][j-1][2] + grid[i-1][j-1], (dp[i-1][j+1][3] if j < n else 0) + grid[i-1][j-1], ] for win in range(min(m, n), 0, -1): # for each edge size (window size) for i in range(win, m+1): # for each x-axis for j in range(win, n+1): # for each y-axis val = dp[i][j][2] - dp[i-win][j-win][2] # major diagonal if dp[i][j-win+1][3] - (dp[i-win][j+1][3] if j+1 <= n else 0) != val: continue # minor diagonal if any(dp[row][j][0] - dp[row][j-win][0] != val for row in range(i, i-win, -1)): continue # for each row if any(dp[i][col][1] - dp[i-win][col][1] != val for col in range(j, j-win, -1)): continue # for each col return win return 1
largest-magic-square
Python 3 | Very Clean Code, O(min(M, N) ^ 2 * M * N) | Explanation
idontknoooo
2
388
largest magic square
1,895
0.519
Medium
26,824
https://leetcode.com/problems/largest-magic-square/discuss/2553485/Python-Fast-by-PreFixSum-and-early-returns-(220-429ms)
class Solution: def largestMagicSquare(self, grid: List[List[int]]) -> int: # get matrix dimensions m, n = len(grid), len(grid[0]) # make the prefix sum for faster summation preSumRow = [[0] * (n + 1) for _ in range(m)] preSumCol = [[0] * (m + 1) for _ in range(n)] for rx in range(m): for cx in range(n): preSumRow[rx][cx + 1] = preSumRow[rx][cx] + grid[rx][cx] preSumCol[cx][rx + 1] = preSumCol[cx][rx] + grid[rx][cx] # just check all squares (with their size and position) # starting with the biggest one, because then we can # break as soon as find one (will be the biggest) for width in range(min(m,n), 1, -1): # size iteration for rx in range(m-width+1): # different starting rows for cx in range(n-width+1): # different starting columns if isMagic(rx, cx, width, grid, preSumRow, preSumCol): # return early once we found a magic square as the next # just be of equal size or smaller return width return 1 def isMagic(rx, cx, width, grid, preSumRow, preSumCol): # get sum of first row summed = preSumRow[rx][cx+width] - preSumRow[rx][cx] # now go through all rows and immediately return once # we encounter a sum unequal to the first one for trx in range(rx+1, rx+width): cursum = preSumRow[trx][cx+width] - preSumRow[trx][cx] if cursum != summed: return False # now go through all columns and immediately return once # we encounter a sum unequal to the first one for tcx in range(cx, cx+width): cursum = preSumCol[tcx][rx+width] - preSumCol[tcx][rx] if cursum != summed: return False # compute the diagonal sums # left upper to right lower corner cursum = 0 for mid in range(width): cursum += grid[rx+mid][cx+mid] if cursum > summed: return False if cursum != summed: return False # right upper to left lower corner cursum = 0 for mid in range(width): cursum += grid[rx+mid][cx+width-mid-1] if cursum > summed: return False if cursum != summed: return False return True
largest-magic-square
[Python] - Fast by PreFixSum and early returns - (220-429ms)
Lucew
0
28
largest magic square
1,895
0.519
Medium
26,825
https://leetcode.com/problems/largest-magic-square/discuss/1362476/Brute-force-approach
class Solution: @staticmethod def is_magic(mat: List[List[int]]): n, s = len(mat), sum(mat[0]) for r in range(1, n): if sum(mat[r]) != s: return False if any(sum(col) != s for col in zip(*mat)): return False d1 = d2 = 0 n1 = n - 1 for i in range(n): d1 += mat[i][i] d2 += mat[n1 - i][i] return d1 == d2 == s def largestMagicSquare(self, grid: List[List[int]]) -> int: rows, cols = len(grid), len(grid[0]) max_size = 1 for r in range(rows): if rows - r < max_size: break for c in range(cols): if cols - c < max_size: break max_len = min(rows - r, cols - c) + 1 for size in range(max_size + 1, max_len): sub_mat = [grid[row_id][c: c + size] for row_id in range(r, r + size)] if Solution.is_magic(sub_mat): max_size = size return max_size
largest-magic-square
Brute force approach
EvgenySH
0
134
largest magic square
1,895
0.519
Medium
26,826
https://leetcode.com/problems/minimum-cost-to-change-the-final-value-of-expression/discuss/1272620/Python3-divide-and-conquer
class Solution: def minOperationsToFlip(self, expression: str) -> int: loc = {} stack = [] for i in reversed(range(len(expression))): if expression[i] == ")": stack.append(i) elif expression[i] == "(": loc[stack.pop()] = i def fn(lo, hi): """Return value and min op to change value.""" if lo == hi: return int(expression[lo]), 1 if expression[hi] == ")" and loc[hi] == lo: return fn(lo+1, hi-1) # strip parenthesis mid = loc.get(hi, hi) - 1 v, c = fn(mid+1, hi) vv, cc = fn(lo, mid-1) if expression[mid] == "|": val = v | vv if v == vv == 0: chg = min(c, cc) elif v == vv == 1: chg = 1 + min(c, cc) else: chg = 1 else: # expression[k] == "&amp;" val = v &amp; vv if v == vv == 0: chg = 1 + min(c, cc) elif v == vv == 1: chg = min(c, cc) else: chg = 1 return val, chg return fn(0, len(expression)-1)[1]
minimum-cost-to-change-the-final-value-of-expression
[Python3] divide & conquer
ye15
0
57
minimum cost to change the final value of expression
1,896
0.548
Hard
26,827
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/1268522/Python-or-dictionary
class Solution: def makeEqual(self, words: List[str]) -> bool: map_ = {} for word in words: for i in word: if i not in map_: map_[i] = 1 else: map_[i] += 1 n = len(words) for k,v in map_.items(): if (v%n) != 0: return False return True
redistribute-characters-to-make-all-strings-equal
Python | dictionary
harshhx
14
1,200
redistribute characters to make all strings equal
1,897
0.599
Easy
26,828
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/1339018/PYTHON-3-%3A-or-98.48-or-EXPLAINED-orTWO-EASY-SOLUTIONSor
class Solution: def makeEqual(self, words: List[str]) -> bool: joint = ''.join(words) set1 = set(joint) for i in set1 : if joint.count(i) % len(words) != 0 : return False return True
redistribute-characters-to-make-all-strings-equal
PYTHON 3 : | 98.48% | EXPLAINED |TWO EASY SOLUTIONS|
rohitkhairnar
11
347
redistribute characters to make all strings equal
1,897
0.599
Easy
26,829
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/1339018/PYTHON-3-%3A-or-98.48-or-EXPLAINED-orTWO-EASY-SOLUTIONSor
class Solution: def makeEqual(self, words: List[str]) -> bool: joint = ''.join(words) dic = {} for i in joint : if i not in dic : dic[i] = joint.count(i) for v in dic.values() : if v % len(words) != 0 : return False return True
redistribute-characters-to-make-all-strings-equal
PYTHON 3 : | 98.48% | EXPLAINED |TWO EASY SOLUTIONS|
rohitkhairnar
11
347
redistribute characters to make all strings equal
1,897
0.599
Easy
26,830
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/1268699/Python3-freq-table
class Solution: def makeEqual(self, words: List[str]) -> bool: freq = defaultdict(int) for word in words: for ch in word: freq[ord(ch)-97] += 1 return all(x % len(words) == 0 for x in freq.values())
redistribute-characters-to-make-all-strings-equal
[Python3] freq table
ye15
2
80
redistribute characters to make all strings equal
1,897
0.599
Easy
26,831
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/1853047/Python-or-Count-All-Characters
class Solution: def makeEqual(self, words: List[str]) -> bool: n = len(words) d = defaultdict(int) for word in words: for ch in word: d[ch] += 1 for letter_count in d.values(): if letter_count % n != 0: return False return True
redistribute-characters-to-make-all-strings-equal
Python | Count All Characters
jgroszew
1
44
redistribute characters to make all strings equal
1,897
0.599
Easy
26,832
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/1617412/Python-1-line-Counter-Reduce-Map
class Solution: def makeEqual(self, words: List[str]) -> bool: return all(list(map(lambda a:a%len(words)==0, collections.Counter(functools.reduce(lambda a,b:a+b,words)).values())))
redistribute-characters-to-make-all-strings-equal
Python 1 line Counter, Reduce, Map
mikekaufman4
1
49
redistribute characters to make all strings equal
1,897
0.599
Easy
26,833
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/1271152/Easy-Python
class Solution: def makeEqual(self, arr: List[str]) -> bool: h = [0]*26 n = len(arr) for w in arr: for i in w: h[ord(i)-ord('a')] += 1 for i in h: if i%n!=0: return 0 return 1
redistribute-characters-to-make-all-strings-equal
Easy Python
lokeshsenthilkumar
1
104
redistribute characters to make all strings equal
1,897
0.599
Easy
26,834
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/2831353/Python-100-with-explanation
class Solution: def makeEqual(self, words: List[str]) -> bool: _ = ''.join(words) for c in set(_): if _.count(c) % len(words) != 0: return False return True
redistribute-characters-to-make-all-strings-equal
Python 100% with explanation
Kros-ZERO
0
2
redistribute characters to make all strings equal
1,897
0.599
Easy
26,835
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/2689807/Easy-To-Understand-Python-Solution
class Solution: def makeEqual(self, words: List[str]) -> bool: n = len(words) s = "".join(words) for count in Counter(s).values(): if count % n != 0: return False return True
redistribute-characters-to-make-all-strings-equal
Easy To Understand Python Solution
scifigurmeet
0
3
redistribute characters to make all strings equal
1,897
0.599
Easy
26,836
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/2682994/PYTHON-or-Easy-or-4-Lines-or-Counter-or-Beginner
class Solution: def makeEqual(self, words: List[str]) -> bool: d= (Counter(''.join(words))) for k,v in d.items(): if (v%len(words)) != 0: return False return True
redistribute-characters-to-make-all-strings-equal
PYTHON | Easy | 4 Lines | Counter | Beginner
envyTheClouds
0
8
redistribute characters to make all strings equal
1,897
0.599
Easy
26,837
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/2492035/Python-very-short-and-easy-without-Counter
class Solution: def makeEqual(self, words: List[str]) -> bool: joined = "".join(words) return all(joined.count(w) % len(words) == 0 for w in set(joined))
redistribute-characters-to-make-all-strings-equal
Python - very short and easy without Counter
yhc22593
0
11
redistribute characters to make all strings equal
1,897
0.599
Easy
26,838
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/2414306/Python3-Dictionary
class Solution: def makeEqual(self, words: List[str]) -> bool: l=len(words) if l==1: return True d={} for i in range(l): for j in words[i]: if j in d: d[j]+=1 else: d[j]=1 for c in d: if d[c]//l == d[c]/l: pass else: return False return True
redistribute-characters-to-make-all-strings-equal
[Python3] Dictionary
sunakshi132
0
34
redistribute characters to make all strings equal
1,897
0.599
Easy
26,839
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/2389807/Fast-One-liner
class Solution: def makeEqual(self, words: List[str]) -> bool: return all(count % len(words) == 0 for count in Counter(''.join(words)).values())
redistribute-characters-to-make-all-strings-equal
Fast One-liner
blest
0
16
redistribute characters to make all strings equal
1,897
0.599
Easy
26,840
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/2371757/Easy-Python-Solution-or-6-lines
class Solution: def makeEqual(self, words: List[str]) -> bool: n, s = len(words), "".join(words) d = Counter(s) for key in d: if d[key]%n != 0: return False return True
redistribute-characters-to-make-all-strings-equal
Easy Python Solution | 6 lines
RajatGanguly
0
25
redistribute characters to make all strings equal
1,897
0.599
Easy
26,841
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/2208646/Python3-simple-solution
class Solution: def makeEqual(self, words: List[str]) -> bool: d = {} for i in words: for j in i: d[j] = d.get(j,0) + 1 n = len(words) for i,j in d.items(): if j % n != 0: return False return True
redistribute-characters-to-make-all-strings-equal
Python3 simple solution
EklavyaJoshi
0
25
redistribute characters to make all strings equal
1,897
0.599
Easy
26,842
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/2020875/Python-Counter-Clean-and-Simple!
class Solution: def makeEqual(self, words): counter, n = reduce(add, map(Counter, words)), len(words) return all(v%n==0 for v in counter.values())
redistribute-characters-to-make-all-strings-equal
Python - Counter - Clean and Simple!
domthedeveloper
0
66
redistribute characters to make all strings equal
1,897
0.599
Easy
26,843
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/1875565/Python-(Simple-Approach-and-Beginner-Friendly)
class Solution: def makeEqual(self, words: List[str]) -> bool: dict = {} if len(words) == 1: return True for i in words: for j in i: if j in dict: dict[j]+=1 else: dict[j] = 1 for i, n in dict.items(): if n%len(words) == 0: continue else: return False return True
redistribute-characters-to-make-all-strings-equal
Python (Simple Approach and Beginner-Friendly)
vishvavariya
0
48
redistribute characters to make all strings equal
1,897
0.599
Easy
26,844
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/1872064/Python-dollarolution
class Solution: def makeEqual(self, words: List[str]) -> bool: d = Counter({}) x = len(words) for i in words: d += Counter(i) for i in d: if d[i]%x != 0: return False return True
redistribute-characters-to-make-all-strings-equal
Python $olution
AakRay
0
25
redistribute characters to make all strings equal
1,897
0.599
Easy
26,845
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/1588706/Python-3-faster-than-96-using-counter
class Solution: def makeEqual(self, words: List[str]) -> bool: c = collections.Counter(''.join(words)) n = len(words) return all(x % n == 0 for x in c.values())
redistribute-characters-to-make-all-strings-equal
Python 3 faster than 96% using counter
dereky4
0
60
redistribute characters to make all strings equal
1,897
0.599
Easy
26,846
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/1514703/python-O(n)-time-O(n)-space
class Solution: def makeEqual(self, words: List[str]) -> bool: n = len(words) if n ==1: return True count = defaultdict(int) for word in words: for w in word: count[w] += 1 for w in count: if count[w] % n != 0: return False return True
redistribute-characters-to-make-all-strings-equal
python O(n) time, O(n) space
byuns9334
0
77
redistribute characters to make all strings equal
1,897
0.599
Easy
26,847
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/1309805/Python-clean-and-Easy
class Solution: def makeEqual(self, words: List[str]) -> bool: tmp = ''.join(words) suposed_w = len(words) from collections import Counter d = Counter(tmp) return all([x%suposed_w == 0 for x in d.values()])
redistribute-characters-to-make-all-strings-equal
Python clean and Easy
StneFx
0
58
redistribute characters to make all strings equal
1,897
0.599
Easy
26,848
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/1270131/Python-or-2-Lines-or-Counter-%2B-Reduce
class Solution: def makeEqual(self, words: List[str]) -> bool: total_counts = reduce(lambda x, y: x + Counter(y), words, Counter()) return all(total_counts[char] % len(words) == 0 for char in ascii_lowercase)
redistribute-characters-to-make-all-strings-equal
Python | 2 Lines | Counter + Reduce
leeteatsleep
0
50
redistribute characters to make all strings equal
1,897
0.599
Easy
26,849
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/1269105/Python-Count-Chars-and-Take-Mod
class Solution: def makeEqual(self, words: List[str]) -> bool: if len(words) == 1: return True count = collections.defaultdict(int) for word in words: for char in word: count[char] += 1 for val in count.values(): if val % len(words) != 0: return False return min(count.values()) >= len(words)
redistribute-characters-to-make-all-strings-equal
[Python] Count Chars and Take Mod
LydLydLi
0
34
redistribute characters to make all strings equal
1,897
0.599
Easy
26,850
https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/discuss/1268738/Python3-Solution
class Solution: def makeEqual(self, words: List[str]) -> bool: x = {} for word in words: for i in word: if i in x: x[i]+=1 else: x[i]=1 return all(i%len(words) == 0 for i in x.values())
redistribute-characters-to-make-all-strings-equal
Python3 Solution
Sanyamx1x
0
34
redistribute characters to make all strings equal
1,897
0.599
Easy
26,851
https://leetcode.com/problems/maximum-number-of-removable-characters/discuss/1268727/Python3-binary-search
class Solution: def maximumRemovals(self, s: str, p: str, removable: List[int]) -> int: mp = {x: i for i, x in enumerate(removable)} def fn(x): """Return True if p is a subseq of s after x removals.""" k = 0 for i, ch in enumerate(s): if mp.get(i, inf) < x: continue if k < len(p) and ch == p[k]: k += 1 return k == len(p) lo, hi = -1, len(removable) while lo < hi: mid = lo + hi + 1 >> 1 if fn(mid): lo = mid else: hi = mid - 1 return lo
maximum-number-of-removable-characters
[Python3] binary search
ye15
10
467
maximum number of removable characters
1,898
0.393
Medium
26,852
https://leetcode.com/problems/maximum-number-of-removable-characters/discuss/2337738/Python-Plain-Binary-Search
class Solution: def maximumRemovals(self, s: str, p: str, removable: List[int]) -> int: def isSubseq(s,subseq,removed): i = 0 j = 0 while i<len(s) and j<len(subseq): if i in removed or s[i]!= subseq[j]: i+=1 continue i+=1 j+=1 return j == len(subseq) removed = set() l = 0 r = len(removable)-1 ans = 0 while l<=r: mid = l+(r-l)//2 removed = set(removable[:mid+1]) if isSubseq(s,p,removed): ans = max(ans,len(removed)) l = mid+1 else: r = mid-1 return ans
maximum-number-of-removable-characters
Python Plain Binary Search
Abhi_009
1
93
maximum number of removable characters
1,898
0.393
Medium
26,853
https://leetcode.com/problems/maximum-number-of-removable-characters/discuss/2575056/Easy-Binary-Search-Explained-%2B-Clean-Code.-Python-95-efficient-than-other-soln.
class Solution: def maximumRemovals(self, s: str, p: str, removable: List[int]) -> int: # You want to choose an integer k (0 <= k <= removable.length) such that, after removing k characters from s using the first k indices in removable. # as usual we can search through the answer. # we have to use a "seen" set. So we can skip all the "K" removable index. Look at 2nd Example left, right = 0, len(removable) # right can be len of removable as we can remove all the elements to check lenS, lenP = len(s), len(p) # checkGood : Is "p" still a subsequence of "s"? After removing of 1st "K or Target" characters def checkGood(target): seen = set(removable[:target]) # contains 1st "K/TARGET" indices if target == 0: # always True given in problem itself return True sIndx,pIndx = 0, 0 while sIndx < lenS and pIndx < lenP: # basic check matching stings chr by indexes. if sIndx in seen: pass # if this sIndx is in Seen so skip it elif s[sIndx] == p[pIndx]: pIndx += 1 # if chr matched pIndx is increased sIndx += 1 return pIndx == lenP # return true if still Subsequence else False while left <= right: mid = left + ((right-left)//2) if checkGood(mid): # if after removing 1st "mid" number of index frpm removable. left = mid + 1 # for True : left can we our answer but we want max so increase it. else: right = mid - 1 # for False : this mid is False. So check for Lower Value of mid/K. return right # return right Answer.
maximum-number-of-removable-characters
Easy Binary Search Explained + Clean Code. Python 95% efficient than other soln.
notxkaran
0
22
maximum number of removable characters
1,898
0.393
Medium
26,854
https://leetcode.com/problems/maximum-number-of-removable-characters/discuss/2575056/Easy-Binary-Search-Explained-%2B-Clean-Code.-Python-95-efficient-than-other-soln.
class Solution: def maximumRemovals(self, s: str, p: str, removable: List[int]) -> int: # You want to choose an integer k (0 <= k <= removable.length) such that, after removing k characters from s using the first k indices in removable. # as usual we can search through the answer. left = 0 right = len(removable) lenS, lenP = len(s), len(p) def checkGood(target): seen = set(removable[:target]) if target == 0: return True sIndx,pIndx = 0, 0 while sIndx < lenS and pIndx < lenP: if sIndx in seen: pass elif s[sIndx] == p[pIndx]: pIndx += 1 sIndx += 1 return pIndx == lenP while left <= right: mid = left + ((right-left)//2) if checkGood(mid): left = mid + 1 else: right = mid - 1 return right
maximum-number-of-removable-characters
Easy Binary Search Explained + Clean Code. Python 95% efficient than other soln.
notxkaran
0
22
maximum number of removable characters
1,898
0.393
Medium
26,855
https://leetcode.com/problems/maximum-number-of-removable-characters/discuss/2097593/Python-or-Clear-and-Simple-Using-Binary-Search-TC%3A-O(nlogk)
class Solution: # ////// TC: O(n * logk) ////// # //// Checking is subsequence or not //// def isSubsequence(self,s,subStr,removed): i,j = 0,0 while i < len(s) and j < len(subStr): if s[i] != subStr[j] or i in removed: i += 1 continue i += 1 j += 1 return j == len(subStr) def maximumRemovals(self, s: str, p: str, removable: List[int]) -> int: res = 0 l,r = 0,len(removable) - 1 while l <= r: mid = (l+r)//2 removed = set(removable[:mid+1]) if self.isSubsequence(s,p,removed): res = max(res,mid+1) l = mid + 1 else: r = mid - 1 return res
maximum-number-of-removable-characters
Python | Clear and Simple Using Binary Search TC: O(nlogk)
__Asrar
0
65
maximum number of removable characters
1,898
0.393
Medium
26,856
https://leetcode.com/problems/maximum-number-of-removable-characters/discuss/1918844/Python-or-Easy-or-Binary-Search-or-With-comments
class Solution: def maximumRemovals(self, s: str, p: str, removable: List[int]) -> int: #function to check that p is a subsequence of s def issubsequence(t,s): j = 0 if len(s)==0: return True for i in range(len(t)): if t[i]==s[j]: j+=1 if j==len(s): return True return False #finding if k value satisfy def helper(k,t,s): for i in range(k+1): t[removable[i]] = '' return issubsequence(''.join(t),''.join(s)) #Trying every possible value of k low = 0 high = len(removable) while low<high: mid = (high+low)//2 if helper(mid,list(s),list(p)): low = mid+1 else: high = mid return low
maximum-number-of-removable-characters
✔️✔️Python🐍 | Easy | Binary Search | With comments
Brillianttyagi
0
49
maximum number of removable characters
1,898
0.393
Medium
26,857
https://leetcode.com/problems/maximum-number-of-removable-characters/discuss/1906316/Python-Explained-Binary-Search-fast-solution-96-better-memory
class Solution: def maximumRemovals(self, s: str, p: str, removable: List[int]) -> int: n = len(removable) start, end = 0, n-1 ## (k+1) is what we will return. We set it to the lowest possible (k+1=0 => k = -1). ## k=-1 because of zero-indexing. k = -1 # Binary search for the location in removable for which the current element works, but, the next element does not work while start != end: mid = (start+end)//2 # Check if p is a subsequence after removing elements in removable from 0 to mid (incl. of both) if issubsequence(p, s, list(removable[:mid+1])): k = mid start = min(mid+1, end) else: end = max(mid-1, start) if issubsequence(p, s, removable[:start+1]) and k < start: # edge case k = start return k+1 def issubsequence(p, s, remove): ''' Check is p is a subsequence of s after removing elements with indexes mentioned in remove ''' remove.sort() j = 0 r = 0 nr = len(remove) n = len(p) check_remove = r < nr for i in range(len(s)): if check_remove and i == remove[r]: r += 1 if r == nr: check_remove = False elif s[i] == p[j]: j += 1 if j == n: return True return False
maximum-number-of-removable-characters
[Python] Explained - Binary Search fast solution 96% better memory
akhileshravi
0
30
maximum number of removable characters
1,898
0.393
Medium
26,858
https://leetcode.com/problems/maximum-number-of-removable-characters/discuss/1742618/Binary-search-87-speed
class Solution: def maximumRemovals(self, s: str, p: str, removable: List[int]) -> int: len_p = len(p) len_r = len(removable) def is_subsequence(idx_rem: set) -> bool: j = 0 for i, c in enumerate(s): if i not in idx_rem and c == p[j]: j += 1 if j == len_p: return True return False set_removable = set(removable) if is_subsequence(set_removable): return len_r left, right = 0, len_r while left < right - 1: middle = (left + right) // 2 set_removable = set(removable[:middle]) if is_subsequence(set_removable): left = middle else: right = middle return left
maximum-number-of-removable-characters
Binary search, 87% speed
EvgenySH
0
69
maximum number of removable characters
1,898
0.393
Medium
26,859
https://leetcode.com/problems/maximum-number-of-removable-characters/discuss/1268732/Binary-Search-(python)-O(NlogN)
class Solution: def maximumRemovals(self, s: str, p: str, removable: List[int]) -> int: l,r=0,len(removable)-1 def subseq(s,p,set_): i,j=0,0 while j<len(p) and i<len(s): if s[i]==p[j] and i not in set_: i+=1 j+=1 else: i+=1 return True if j==len(p) else False while l<r: mid=r-(r-l)//2 set_=set() for i in range(mid+1): set_.add(removable[i]) if subseq(s,p,set_): l=mid else: r=mid-1 set_=set() for i in range(l+1): set_.add(removable[i]) if subseq(s,p,set_): return l+1 return 0
maximum-number-of-removable-characters
Binary Search (python)- O(NlogN)
TarunAn
0
96
maximum number of removable characters
1,898
0.393
Medium
26,860
https://leetcode.com/problems/maximum-number-of-removable-characters/discuss/1583345/Python-or-Explained-or-Binary-Search-%2B-Dictionary-or-Intuitive
class Solution: def maximumRemovals(self, s: str, p: str, removable: List[int]) -> int: indices = {} for i in range(len(removable)): indices[removable[i]] = i def check(mid): i, j = 0, 0 while i < len(s) and j < len(p): if s[i] == p[j] and (i not in indices or indices[i] > mid): j += 1 i += 1 if j == len(p): return True else: return False l, r = 0, len(removable) - 1 ans = -1 while l <= r: mid = (l + r) // 2 if check(mid): ans = mid l = mid + 1 else: r = mid - 1 return ans + 1
maximum-number-of-removable-characters
Python | Explained | Binary Search + Dictionary | Intuitive
detective_dp
-1
80
maximum number of removable characters
1,898
0.393
Medium
26,861
https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/1268491/python-or-implementation-O(N)
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: i = 1 cur = [] for a,b,c in triplets: if a<=target[0] and b<=target[1] and c<= target[2]: cur = [a,b,c] break if not cur: return False while i<len(triplets): if cur == target: return True a,b,c = triplets[i] x,y,z = cur if max(a,x)<=target[0] and max(b,y)<=target[1] and max(c,z)<=target[2]: cur = [max(a,x), max(b,y), max(c,z)] i+= 1 if cur == target: return True return False
merge-triplets-to-form-target-triplet
python | implementation O(N)
harshhx
4
234
merge triplets to form target triplet
1,899
0.644
Medium
26,862
https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/1280409/Simple-and-clean-Greedy-Python-solution
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: a, b, c = 0, 0, 0 for i, (x, y, z) in enumerate(triplets): if not( x > target[0] or y > target[1] or z > target[2]): a, b, c = max(a, x), max(b, y), max(c, z) return [a, b, c] == target
merge-triplets-to-form-target-triplet
Simple and clean Greedy Python solution
jaipoo
3
131
merge triplets to form target triplet
1,899
0.644
Medium
26,863
https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/1268544/Greedy
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: m,n,o=0,0,0 for i,j,k in triplets: if i<=target[0] and j<=target[1] and k<=target[2]: m=max(m,i) n=max(n,j) o=max(o,k) return [m,n,o]==target ```
merge-triplets-to-form-target-triplet
Greedy
prashanthp0703
2
66
merge triplets to form target triplet
1,899
0.644
Medium
26,864
https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/1434298/Python3-simple-one-pass-solution
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: if target in triplets: return True a = b = c = 0 for i in triplets: if i[0] == target[0] and i[1] <= target[1] and i[2] <= target[2] or i[1] == target[1] and i[0] <= target[0] and i[2] <= target[2] or i[2] == target[2] and i[1] <= target[1] and i[0] <= target[0]: a = max(a,i[0]) b = max(b,i[1]) c = max(c,i[2]) return True if [a,b,c] == target else False
merge-triplets-to-form-target-triplet
Python3 simple one-pass solution
EklavyaJoshi
1
57
merge triplets to form target triplet
1,899
0.644
Medium
26,865
https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/1268619/Greedy-oror-well-explained-oror-98-faster-oror-Easy-undestanding
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: curr=[0]*3 for t in triplets: tmp=[max(curr[i],t[i]) for i in range(3)] f=0 for i in range(3): if tmp[i]>target[i]: f=1 break #if all tmp element is less then or equal to target element if f==0: curr=tmp if curr==target: return True return False
merge-triplets-to-form-target-triplet
🐍 Greedy || well-explained || 98% faster || Easy-undestanding 📌
abhi9Rai
1
59
merge triplets to form target triplet
1,899
0.644
Medium
26,866
https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/2798098/Python-simple-solution-with-O(N)-time-and-O(1)-space-complexity
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: set1 = set() a = b= c = 0 for i in range(len(triplets)): if triplets[i][0]>target[0] or triplets[i][1]>target[1] or triplets[i][2]>target[2]: continue else: a = max(triplets[i][0],a) b = max(triplets[i][1],b) c = max(triplets[i][2], c) return [a,b,c]==target
merge-triplets-to-form-target-triplet
Python simple solution with O(N) time and O(1) space complexity
Rajeev_varma008
0
3
merge triplets to form target triplet
1,899
0.644
Medium
26,867
https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/2688129/Neat-O(n)-solution-using-zip-in-Python
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: merge_triplet = [float("-inf")] * len(target) for cur_triplet in triplets: if all(cur_num <= target_num for cur_num, target_num in zip(cur_triplet, target)): merge_triplet = [max(prev, cur) for prev, cur in zip(merge_triplet, cur_triplet)] return merge_triplet == target
merge-triplets-to-form-target-triplet
Neat O(n) solution using zip in Python
ste42
0
6
merge triplets to form target triplet
1,899
0.644
Medium
26,868
https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/2670463/Python3-Easy-Solution
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: # loop trough the triplets to find any triplet that matches to one of the num in the target cur = [0] * 3 for triplet in triplets: if cur == target: return True if (triplet[0] > target[0] or triplet[1] > target[1] or triplet[2] > target[2]): continue cur = [max(triplet[0], cur[0]), max(triplet[1], cur[1]), max(triplet[2], cur[2])] return True if cur == target else False
merge-triplets-to-form-target-triplet
Python3 Easy Solution
a1ex_Yichen
0
31
merge triplets to form target triplet
1,899
0.644
Medium
26,869
https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/2640289/Easy-O(N)-Python-Solution
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: good=[] for x,y,z in triplets: if x>target[0] or y>target[1] or z>target[2]: continue good.append([x,y,z]) result=set() for item in good: for index in range(len(item)): if item[index]==target[index]: result.add(index) return len(result)==3
merge-triplets-to-form-target-triplet
Easy O(N) Python Solution
wahid_nlogn
0
1
merge triplets to form target triplet
1,899
0.644
Medium
26,870
https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/2607608/Simple-Python3-or-O(n)
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: ta = tb = tc = False for a, b, c in triplets: if a <= target[0] and b <= target[1] and c <= target[2]: if a == target[0]: ta = True if b == target[1]: tb = True if c == target[2]: tc = True return ta and tb and tc
merge-triplets-to-form-target-triplet
Simple Python3 | O(n)
ryangrayson
0
10
merge triplets to form target triplet
1,899
0.644
Medium
26,871
https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/2538221/Python-easy-to-read-and-understand-or-greedy
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: res = set() for t in triplets: if t[0] <= target[0] and t[1] <= target[1] and t[2] <= target[2]: for i, s in enumerate(t): if s == target[i]: res.add(i) return len(res) == 3
merge-triplets-to-form-target-triplet
Python easy to read and understand | greedy
sanial2001
0
22
merge triplets to form target triplet
1,899
0.644
Medium
26,872
https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/2340377/Fast-and-Super-Simple-Python-O(n).-Explained-in-comments
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: res=[0,0,0] for a,b,c in triplets: if(a>target[0] or b>target[1] or c>target[2]): # triplet has a value bigger than any value in target. Hence skip it continue res[0]=max(res[0],a) # update the max values in res res[1]=max(res[1],b) res[2]=max(res[2],c) if(res==target): return True # if res is equal to target, return True return False
merge-triplets-to-form-target-triplet
✅✅✅ Fast and Super Simple Python O(n). Explained in comments
arnavjaiswal149
0
25
merge triplets to form target triplet
1,899
0.644
Medium
26,873
https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/2080509/Python-O(n)-time-O(1)-space-solution
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: current = [-1, -1, -1] for triplet in triplets: if triplet[0] > target[0] or triplet[1] > target[1] or triplet[2] > target[2]: continue if triplet[0] == target[0] or triplet[1] == target[1] or triplet[2] == target[2]: current = [max(current[0],triplet[0]), max(current[1], triplet[1]), max(current[2], triplet[2]) ] return (current == target) # O(n) O(1)
merge-triplets-to-form-target-triplet
Python O(n) time O(1) space solution
CarnivalWilson
0
24
merge triplets to form target triplet
1,899
0.644
Medium
26,874
https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/1508088/One-pass-98-speed
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: a, b, c = target max_x = max_y = max_z = 0 for x, y, z in triplets: if x <= a and y <= b and z <= c: max_x, max_y, max_z = max(max_x, x), max(max_y, y), max(max_z, z) if max_x == a and max_y == b and max_z == c: return True return False
merge-triplets-to-form-target-triplet
One pass, 98% speed
EvgenySH
0
48
merge triplets to form target triplet
1,899
0.644
Medium
26,875
https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/1269101/Python-Simple-Solution-w-Brief-Explanation
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: first = second = third = False for trip in triplets: if trip == target: return True if trip[0] == target[0] and trip[1] <= target[1] and trip[2] <= target[2]: first = True if trip[1] == target[1] and trip[0] <= target[0] and trip[2] <= target[2]: second = True if trip[2] == target[2] and trip[0] <= target[0] and trip[1] <= target[1]: third = True return first and second and third
merge-triplets-to-form-target-triplet
[Python] Simple Solution w/ Brief Explanation
LydLydLi
0
29
merge triplets to form target triplet
1,899
0.644
Medium
26,876
https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/1268792/Python3oror-Greedy-oror-Easy-Understanding
class Solution: def mergeTriplets(self, e: List[List[int]], target: List[int]) -> bool: t=[] for i in range(len(e)): if(e[i][0]>target[0] or e[i][1]>target[1] or e[i][2]>target[2]): pass else: t.append([e[i][0],e[i][1],e[i][2]]) if(len(t)>0): p1=t[0][0] p2=t[0][1] p3=t[0][2] for i in range(len(t)): p1=max(t[i][0],p1) p2=max(t[i][1],p2) p3=max(t[i][2],p3) ok=[p1,p2,p3] if(ok==target): return True return False
merge-triplets-to-form-target-triplet
[Python3🐍]|| Greedy || Easy-Understanding 🚀
mukul_79
0
64
merge triplets to form target triplet
1,899
0.644
Medium
26,877
https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/1268703/Python3-greedy
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: x = y = z = -inf for a, b, c in triplets: if a <= target[0] and b <= target[1] and c <= target[2]: x, y, z = max(x, a), max(y, b), max(z, c) return [x, y, z] == target
merge-triplets-to-form-target-triplet
[Python3] greedy
ye15
0
30
merge triplets to form target triplet
1,899
0.644
Medium
26,878
https://leetcode.com/problems/the-earliest-and-latest-rounds-where-players-compete/discuss/1268788/Python3-bit-mask-dp
class Solution: def earliestAndLatest(self, n: int, firstPlayer: int, secondPlayer: int) -> List[int]: firstPlayer, secondPlayer = firstPlayer-1, secondPlayer-1 # 0-indexed @cache def fn(k, mask): """Return earliest and latest rounds.""" can = deque() for i in range(n): if mask &amp; (1 << i): can.append(i) cand = [] # eliminated player while len(can) > 1: p1, p2 = can.popleft(), can.pop() if p1 == firstPlayer and p2 == secondPlayer or p1 == secondPlayer and p2 == firstPlayer: return [k, k] # game of interest if p1 in (firstPlayer, secondPlayer): cand.append([p2]) # p2 eliminated elif p2 in (firstPlayer, secondPlayer): cand.append([p1]) # p1 eliminated else: cand.append([p1, p2]) # both could be elimited minn, maxx = inf, -inf for x in product(*cand): mask0 = mask for i in x: mask0 ^= 1 << i mn, mx = fn(k+1, mask0) minn = min(minn, mn) maxx = max(maxx, mx) return minn, maxx return fn(1, (1<<n)-1)
the-earliest-and-latest-rounds-where-players-compete
[Python3] bit-mask dp
ye15
7
265
the earliest and latest rounds where players compete
1,900
0.518
Hard
26,879
https://leetcode.com/problems/find-a-peak-element-ii/discuss/1446385/Python-3-or-Binary-Search-or-Explanation
class Solution: def findPeakGrid(self, mat: List[List[int]]) -> List[int]: m, n = len(mat), len(mat[0]) l, r = 0, n while l <= r: mid = (l + r) // 2 cur_max, left = 0, False for i in range(m): if i > 0 and mat[i-1][mid] >= mat[i][mid]: continue if i+1 < m and mat[i+1][mid] >= mat[i][mid]: continue if mid+1 < n and mat[i][mid+1] >= mat[i][mid]: cur_max, left = mat[i][mid], not mat[i][mid] > cur_max continue if mid > 0 and mat[i][mid-1] >= mat[i][mid]: cur_max, left = mat[i][mid], mat[i][mid] > cur_max continue return [i, mid] if left: r = mid-1 else: l = mid+1 return []
find-a-peak-element-ii
Python 3 | Binary Search | Explanation
idontknoooo
17
2,100
find a peak element ii
1,901
0.532
Medium
26,880
https://leetcode.com/problems/find-a-peak-element-ii/discuss/1958684/Python-oror-Binary-search
class Solution: def findPeakGrid(self, mat: List[List[int]]) -> List[int]: left, right = 0, len(mat[0]) - 1 while left <= right: midCol = (left + right)//2 maxRow = 0 for i in range(len(mat)): maxRow = i if mat[i][midCol] > mat[maxRow][midCol] else maxRow isLeftBig = midCol - 1 >= 0 and mat[maxRow][midCol - 1] > mat[maxRow][midCol] isRightBig = midCol + 1 <= len(mat[0]) and mat[maxRow][midCol + 1] > mat[maxRow][midCol] if not isLeftBig and not isRightBig: return [maxRow, midCol] elif isRightBig: left = midCol + 1 else: right = midCol - 1 return None
find-a-peak-element-ii
Python || Binary search
silviyavelani
2
223
find a peak element ii
1,901
0.532
Medium
26,881
https://leetcode.com/problems/find-a-peak-element-ii/discuss/1491197/Python3-simple-solution-or-explained
class Solution: def findPeakGrid(self, mat: List[List[int]]) -> List[int]: n = len(mat) m = len(mat[0]) for i in range(n): for j in range(m): if i-1 < 0: # checking up up = -1 else: up = mat[i-1][j] if i+1 >= n: # checking down down = -1 else: down = mat[i+1][j] if j-1 < 0: # checking left left = -1 else: left = mat[i][j-1] if j+1 >= m: # checking right right = -1 else: right = mat[i][j+1] if mat[i][j] > max(up, left, right, down): return [i,j] return False
find-a-peak-element-ii
Python3 simple solution | explained
FlorinnC1
2
325
find a peak element ii
1,901
0.532
Medium
26,882
https://leetcode.com/problems/find-a-peak-element-ii/discuss/1275466/Python-3-100-time-and-Space-oror-Explained
class Solution: def __init__(self): self.mat = 0 def findPeakGrid(self, mat: List[List[int]]) -> List[int]: self.mat = mat m = len(mat) n = len(mat[0]) i, j = 0, 0 while i < m: while j < n: if (self.mat_val(i, j-1)<self.mat_val(i, j) and self.mat_val(i, j)>self.mat_val(i, j+1) and self.mat_val(i, j)>self.mat_val(i-1, j) and self.mat_val(i, j)>self.mat_val(i+1, j)): return [i, j] # left elif self.mat_val(i, j) < self.mat_val(i, j-1): j = j-1 # right elif self.mat_val(i, j) < self.mat_val(i, j+1): j = j+1 # top elif self.mat_val(i, j) < self.mat_val(i-1, j): i = i-1 # bottom elif self.mat_val(i, j) < self.mat_val(i+1, j): i = i + 1 def mat_val(self, i, j) -> int: m, n = len(self.mat), len(self.mat[0]) if i<0 or j<0 or i>=m or j>=n: return -1 else: return self.mat[i][j]
find-a-peak-element-ii
Python 3, 100% time and Space || Explained
satyamsinha93
1
353
find a peak element ii
1,901
0.532
Medium
26,883
https://leetcode.com/problems/find-a-peak-element-ii/discuss/1273220/Python3-Binary-search-on-columns
class Solution: def findPeakGrid(self, mat: List[List[int]]) -> List[int]: self.r=0 self.c=0 rows=len(mat) col=len(mat[0]) def helper(l,r): if l>=r: return False mid=l+r>>1 for x in range(rows): top=mat[x-1][mid] if x-1>=0 else -1 bot=mat[x+1][mid] if x+1<rows else -1 right=mat[x][mid+1] if mid+1<col else -1 left=mat[x][mid-1] if mid-1>=0 else -1 if mat[x][mid]>top and mat[x][mid]>left and mat[x][mid]>right and mat[x][mid]>bot: self.r=x self.c=mid return True if helper(mid+1, r): return True if helper(l, mid-1): return True return False helper(0,col) return [self.r, self.c]
find-a-peak-element-ii
[Python3] Binary search on columns
abhinav4202
1
191
find a peak element ii
1,901
0.532
Medium
26,884
https://leetcode.com/problems/find-a-peak-element-ii/discuss/2789579/Simple-Python-Solution-oror-Easy-Understanding
class Solution: def findPeakGrid(self, mat: List[List[int]]) -> List[int]: def check(mat, i, j, r, c): if (i-1 >= 0 and i-1 < r): if (mat[i][j] < mat[i-1][j]): return False if (i+1 >= 0 and i+1 < r): if (mat[i][j] < mat[i+1][j]): return False if (j-1 >= 0 and j-1 < c): if (mat[i][j] < mat[i][j-1]): return False if (j+1 >= 0 and j+1 < c): if (mat[i][j] < mat[i][j+1]): return False return True r = len(mat); c = len(mat[0]); for i in range(r): for j in range(c): if (check(mat, i, j, r, c)): return [i, j]
find-a-peak-element-ii
Simple Python Solution || Easy Understanding
avinashdoddi2001
0
6
find a peak element ii
1,901
0.532
Medium
26,885
https://leetcode.com/problems/find-a-peak-element-ii/discuss/2664188/Find-a-Peak-Element-II
class Solution: def is_peak_ele(self, mat, a, row, col,m,n): # checking the edge condition di = [-1,1,0,0] dj = [0,0,-1,1] check_dir = [] # check up is possible if row != 0: check_dir.append(0) # check Down is possible if row != m-1: check_dir.append(1) # check left is possible if col != 0: check_dir.append(2) # check right is possible if col != n-1: check_dir.append(3) # take the only checki list present elements for d in check_dir: if mat[row + di[d]][col + dj[d]] > a[col]: return False return True def findPeakElement(self, mat, nums, row,m,n) -> int: # asking for log(n) --> so using the binary search approach if len(nums) == 1: return 0 low = 0; high = n-1 while high >= low: mid = (low + high) // 2 # if mid is a peak return it. if self.is_peak_ele(mat, nums, row,mid, m,n): return [row,mid] else: # if not -- go along the greater element side. # if mid is in left edge --no way to got to left anymore -- go right. if mid == 0: low = mid +1 # if mid is in right edge --no way to got to right anymore -- go left. elif mid == n-1: high = mid -1 # if in middle got on the greater ele side elif nums[mid+1] > nums[mid-1]: # checking right side ele is greater. low = mid + 1 else: high = mid - 1 def findPeakGrid(self, mat): # for every row find the peak element m = len(mat) n = len(mat[0]) for row in range(m): peak_ele = self.findPeakElement(mat, mat[row], row, m,n) if peak_ele: return peak_ele print(Solution().findPeakGrid([[1,4],[3,2]])) print(Solution().findPeakGrid([[10,20,15],[21,30,14],[7,16,32]])) print(Solution().findPeakGrid([[25,37,23,37,19],[45,19,2,43,26],[18,1,37,44,50]])) print(Solution().findPeakGrid([[10,50,40,30,20],[1,500,2,3,4]])) print(Solution().findPeakGrid([[10,30,40,50,20],[1,3,2,500,4]]))
find-a-peak-element-ii
Find a Peak Element II
Ninjaac
0
20
find a peak element ii
1,901
0.532
Medium
26,886
https://leetcode.com/problems/find-a-peak-element-ii/discuss/2610908/Python3-or-Solved-Using-Binary-Search-on-Search-Space-Based-on-Rows-Runtime(O(nlog(m))
class Solution: #Time-Complexity: O(log(m) * n) #Space-Complexity: O(1) def findPeakGrid(self, mat: List[List[int]]) -> List[int]: #Approach: You can think of this problem in two ways! Either column-based or row-based! #Idea is that we perform binary serach on mat grid and utilize search space on rows. #for the current middle row we are on, find the maximum element, as it will be bigger than #both of its left and right neighboring elements. To verify this element is a peak, we need #to make sure that it is also bigger than the immediate top and bottom neighboring elements! #however, we need to becareful of the bottom and top neighboring elements being out of bounds! #in this case, the current peak candidate will always be bigger than such elements since #all ellements within mat are gauranteed to be positive numbers! #Right when we find peak element, we should return its position immediately! #define my search space by row! L, H = 0, len(mat) - 1 #as long as we have at least one row left to consider, continue binary search! while L <= H: mid = (L + H) // 2 mid_row = mat[mid] #initialize max_pos and store column pos later! max_pos = None max_val = float(-inf) #iterate linearly through the row since it's not sorted and find the maximum element as well #as its position! for j in range(len(mid_row)): if(mid_row[j] > max_val): max_val = mid_row[j] max_pos = j continue #once we have max_pos, then we have to compare relative to top and bottom neighbors! top_val = -1 if mid - 1 < 0 else mat[mid-1][max_pos] bottom_val = -1 if mid + 1 >= len(mat) else mat[mid+1][max_pos] #then it's a peak! if(top_val < max_val and bottom_val < max_val): return [mid, max_pos] #top neighboring element is bigger! -> it has better chance to be peak element! if(top_val >= max_val): H = mid - 1 continue if(bottom_val >= max_val): L = mid + 1 continue
find-a-peak-element-ii
Python3 | Solved Using Binary Search on Search Space Based on Rows Runtime(O(nlog(m))
JOON1234
0
56
find a peak element ii
1,901
0.532
Medium
26,887
https://leetcode.com/problems/find-a-peak-element-ii/discuss/2495165/Python3-clever-solution-oror-Easy-to-understand
class Solution: def findPeakGrid(self, mat: List[List[int]]) -> List[int]: m, n = len(mat), len(mat[0]) l, r = 0, n while l <= r: mid = (l + r) // 2 cur_max, left = 0, False for i in range(m): if i > 0 and mat[i-1][mid] >= mat[i][mid]: continue if i+1 < m and mat[i+1][mid] >= mat[i][mid]: continue if mid+1 < n and mat[i][mid+1] >= mat[i][mid]: cur_max, left = mat[i][mid], not mat[i][mid] > cur_max continue if mid > 0 and mat[i][mid-1] >= mat[i][mid]: cur_max, left = mat[i][mid], mat[i][mid] > cur_max continue return [i, mid] if left: r = mid-1 else: l = mid+1 return []
find-a-peak-element-ii
✔️ [Python3] clever solution || Easy to understand
Omegang
0
91
find a peak element ii
1,901
0.532
Medium
26,888
https://leetcode.com/problems/find-a-peak-element-ii/discuss/2396608/simple-oror-python
class Solution: def findPeakGrid(self, mat: List[List[int]]) -> List[int]: m=[] for i in range(len(mat)): for j in range(len(mat[0])): m.append(mat[i][j]) ma=max(m) print(ma) for i in range(len(mat)): for j in range(len(mat[0])): if mat[i][j]==ma: return [i,j]
find-a-peak-element-ii
simple || python
Sneh713
0
121
find a peak element ii
1,901
0.532
Medium
26,889
https://leetcode.com/problems/find-a-peak-element-ii/discuss/1279122/Intuitive-approach-by-DFS
class Solution: def findPeakGrid(self, mat: List[List[int]]) -> List[int]: m, n = len(mat), len(mat[0]) vset = set() def dfs(i, j): v = mat[i][j] vset.add((i, j)) for ni, nj in filter( lambda t: t[0] >= 0 and t[0] < m and t[1] >= 0 and t[1] < n, [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]): if mat[ni][nj] > v: return dfs(ni, nj) elif (ni, nj) in vset: continue return [i, j] return dfs(0, 0)
find-a-peak-element-ii
Intuitive approach by DFS
puremonkey2001
0
150
find a peak element ii
1,901
0.532
Medium
26,890
https://leetcode.com/problems/find-a-peak-element-ii/discuss/1276998/Python3-divide-and-conquer-O(MlogN)
class Solution: def findPeakGrid(self, mat: List[List[int]]) -> List[int]: m, n = len(mat), len(mat[0]) # dimensions def fn(lo, hi): """Return a peak element between column lo (inclusive) and hi (exlusive).""" if lo == hi: return mid = lo + hi >> 1 if left := fn(lo, mid): return left if right := fn(mid+1, hi): return right for i in range(m): if (i == 0 or mat[i-1][mid] < mat[i][mid]) and (i+1 == m or mat[i][mid] > mat[i+1][mid]) and (mid == 0 or mat[i][mid-1] < mat[i][mid]) and (mid+1 == n or mat[i][mid] > mat[i][mid+1]): return [i, mid] return fn(0, n)
find-a-peak-element-ii
[Python3] divide & conquer O(MlogN)
ye15
0
184
find a peak element ii
1,901
0.532
Medium
26,891
https://leetcode.com/problems/find-a-peak-element-ii/discuss/1273663/Python3%3A-find-row-by-bisection
class Solution: def findPeakGrid(self, mat: List[List[int]]) -> List[int]: M = len(mat) N = len(mat[0]) @cache def rmax(r): return max(mat[r]) l,r = 0,M while r-l>1: m = l+(r-l)//2 assert(m>0) if rmax(m)>rmax(m-1): l = m else: r = m return [l, mat[l].index(rmax(l))]
find-a-peak-element-ii
Python3: find row by bisection
vsavkin
0
97
find a peak element ii
1,901
0.532
Medium
26,892
https://leetcode.com/problems/largest-odd-number-in-string/discuss/1338138/PYTHON-3%3A-99.34-FASTER-EASY-EXPLANATION
class Solution: def largestOddNumber(self, num: str) -> str: for i in range(len(num) - 1, -1, -1) : if num[i] in {'1','3','5','7','9'} : return num[:i+1] return ''
largest-odd-number-in-string
PYTHON 3: 99.34% FASTER, EASY EXPLANATION
rohitkhairnar
30
1,400
largest odd number in string
1,903
0.557
Easy
26,893
https://leetcode.com/problems/largest-odd-number-in-string/discuss/1284227/Python3-Simple-Readable-Solution-With-Comments-(Finding-Leftmost-odd-number)
class Solution: def largestOddNumber(self, num: str) -> str: # We have to just find the last index of an odd number, then slice the number upto that index, becuase an odd number always ends with a number which is not divisible by 2 :) # Lets take the last index of an odd number as -1 last_ind = -1 # Iterate through all the numbers for finding a odd number that appears on the last. for i , j in enumerate(num[::-1]): if(int(j) % 2 != 0): last_ind = len(num) - i break # If there is no odd number, return empty string. if(last_ind == -1): return "" # Or return the string upto that index. return(num[:last_ind])
largest-odd-number-in-string
[Python3] Simple, Readable Solution With Comments (Finding Leftmost odd number)
VoidCupboard
4
473
largest odd number in string
1,903
0.557
Easy
26,894
https://leetcode.com/problems/largest-odd-number-in-string/discuss/1284268/Python3-greedy
class Solution: def largestOddNumber(self, num: str) -> str: ii = -1 for i, x in enumerate(num): if int(x) &amp; 1: ii = i return num[:ii+1]
largest-odd-number-in-string
[Python3] greedy
ye15
2
100
largest odd number in string
1,903
0.557
Easy
26,895
https://leetcode.com/problems/largest-odd-number-in-string/discuss/1284268/Python3-greedy
class Solution: def largestOddNumber(self, num: str) -> str: for i in reversed(range(len(num))): if int(num[i]) &amp; 1: return num[:i+1] return ""
largest-odd-number-in-string
[Python3] greedy
ye15
2
100
largest odd number in string
1,903
0.557
Easy
26,896
https://leetcode.com/problems/largest-odd-number-in-string/discuss/2242822/Easy-to-understand-or-Python3-Solution
class Solution: def largestOddNumber(self, num: str) -> str: res = -1 for i in range(len(num)): if int(num[i]) % 2 == 1: res = i if res == -1: return "" else: return str(num[0:res+1])
largest-odd-number-in-string
Easy to understand | Python3 Solution
irapandey
1
147
largest odd number in string
1,903
0.557
Easy
26,897
https://leetcode.com/problems/largest-odd-number-in-string/discuss/1539701/EASY-BEGINNER-FRIENDLY-SOLN
class Solution: def largestOddNumber(self, num: str): i = 1 while True: if i > len(num): return "" break n = num[-i] if n in '02468': i += 1 continue elif n not in '02468': return (num[0:len(num)-i+1]) break
largest-odd-number-in-string
EASY BEGINNER FRIENDLY SOLN
anandanshul001
1
108
largest odd number in string
1,903
0.557
Easy
26,898
https://leetcode.com/problems/largest-odd-number-in-string/discuss/1395663/python-soln-oror-Beginners-oror-easy-oror
class Solution: def largestOddNumber(self, num: str) -> str: ans="" lst=[] for i in range(len(num)-1,-1,-1): if int(num[i])%2 !=0: return num[:i+1] return ans
largest-odd-number-in-string
python soln || Beginners || easy ||
minato_namikaze
1
100
largest odd number in string
1,903
0.557
Easy
26,899