post_href
stringlengths 57
213
| python_solutions
stringlengths 71
22.3k
| slug
stringlengths 3
77
| post_title
stringlengths 1
100
| user
stringlengths 3
29
| upvotes
int64 -20
1.2k
| views
int64 0
60.9k
| problem_title
stringlengths 3
77
| number
int64 1
2.48k
| acceptance
float64 0.14
0.91
| difficulty
stringclasses 3
values | __index_level_0__
int64 0
34k
|
|---|---|---|---|---|---|---|---|---|---|---|---|
https://leetcode.com/problems/minimum-operations-to-make-a-uni-value-grid/discuss/2665809/O(mn-*-log(mn))
|
class Solution:
def minOperations(self, grid: List[List[int]], x: int) -> int:
m=len(grid)
n=len(grid[0])
mn=float('inf')
numlist=[]
for i in range(m):
for j in range(n):
numlist.append(grid[i][j])
numlist.sort()
target=numlist[m*n//2]
res=0
for n in numlist:
temp=abs(n-target)
if temp%x!=0:
return -1
res+=temp//x
return res
|
minimum-operations-to-make-a-uni-value-grid
|
O(mn * log(mn))
|
ConfusedMoe
| 0
| 2
|
minimum operations to make a uni value grid
| 2,033
| 0.524
|
Medium
| 28,300
|
https://leetcode.com/problems/minimum-operations-to-make-a-uni-value-grid/discuss/2049908/python-3-oror-sorting-and-median-solution
|
class Solution:
def minOperations(self, grid: List[List[int]], x: int) -> int:
m, n = len(grid), len(grid[0])
nums = sorted(grid[i][j] for i in range(m) for j in range(n))
firstModX = nums[0] % x
median = nums[(m*n - 1) // 2]
operations = 0
for num in nums:
if num % x != firstModX:
return -1
operations += abs(num - median)
return operations // x
|
minimum-operations-to-make-a-uni-value-grid
|
python 3 || sorting and median solution
|
dereky4
| 0
| 43
|
minimum operations to make a uni value grid
| 2,033
| 0.524
|
Medium
| 28,301
|
https://leetcode.com/problems/minimum-operations-to-make-a-uni-value-grid/discuss/1886433/Similar-questions-like-this-%3A
|
class Solution:
def minOperations(self, grid: List[List[int]], x: int) -> int:
nums=[]
for i in grid:
nums+=i
nums.sort()
ans=0
median=nums[len(nums)//2]
for i in nums:
dist=abs(i-median)
if dist%x!=0:
return -1
ans+=dist//x
return ans
|
minimum-operations-to-make-a-uni-value-grid
|
Similar questions like this :
|
goxy_coder
| 0
| 65
|
minimum operations to make a uni value grid
| 2,033
| 0.524
|
Medium
| 28,302
|
https://leetcode.com/problems/minimum-operations-to-make-a-uni-value-grid/discuss/1538881/Easy-Methord-oror-Begineer
|
class Solution:
def minOperations(self, grid: List[List[int]], x: int) -> int:
nums = []
for row in grid:
for num in row:
nums.append(num)
nums.sort()
m1=nums[0]%x
for i in nums:
if i%x!=m1:return -1
median = nums[len(nums)//2]
print(median)
res = 0
for n in nums:
res += abs(n-median)//x
print(res)
return res
|
minimum-operations-to-make-a-uni-value-grid
|
Easy Methord || Begineer
|
vkn84527
| 0
| 87
|
minimum operations to make a uni value grid
| 2,033
| 0.524
|
Medium
| 28,303
|
https://leetcode.com/problems/minimum-operations-to-make-a-uni-value-grid/discuss/1519881/Python3-easy-solution-using-sorting
|
class Solution:
def minOperations(self, grid: List[List[int]], x: int) -> int:
temp = []
for i in grid:
temp += i
temp.sort()
start = temp[0]%x
if any(y % x != start for y in temp):
return -1
mid = len(temp)//2 if len(temp) % 2 else (len(temp)//2)-1
res = 0
for i in temp:
if i == temp[mid]:
continue
else:
res += abs(temp[mid]-i)//x
return res
|
minimum-operations-to-make-a-uni-value-grid
|
Python3 easy solution using sorting
|
ermolushka2
| 0
| 58
|
minimum operations to make a uni value grid
| 2,033
| 0.524
|
Medium
| 28,304
|
https://leetcode.com/problems/minimum-operations-to-make-a-uni-value-grid/discuss/1513614/For-Beginners-oror-Median-oror-Easy-and-Greedy-Approach
|
class Solution:
def minOperations(self, grid: List[List[int]], x: int) -> int:
nums = []
for r in grid:
for v in r:
nums.append(v)
mi = min(nums)
for n in nums:
if (n-mi)%x:
return -1
nums.sort()
median = nums[len(nums)//2]
res = 0
for n in nums:
res += abs(n-median)//x
return res
|
minimum-operations-to-make-a-uni-value-grid
|
📌📌 For Beginners || Median || Easy & Greedy Approach 🐍
|
abhi9Rai
| 0
| 75
|
minimum operations to make a uni value grid
| 2,033
| 0.524
|
Medium
| 28,305
|
https://leetcode.com/problems/minimum-operations-to-make-a-uni-value-grid/discuss/1513337/Python-Solution-Minimum-operations-to-Make-uni-valued-Grid
|
class Solution:
def minOperations(self, grid: List[List[int]], x: int) -> int:
m,n = len(grid),len(grid[0])
count = 0
li = []
for i in range(m):
for j in range(n):
li.append(grid[i][j])
li.sort()
med = li[len(li)//2]
for num in li:
if num < med or num > med:
need = abs( (med - num)//x)
if need == 0:
return -1
count += need
return count
|
minimum-operations-to-make-a-uni-value-grid
|
[Python] Solution - Minimum operations to Make uni-valued Grid
|
SaSha59
| 0
| 69
|
minimum operations to make a uni value grid
| 2,033
| 0.524
|
Medium
| 28,306
|
https://leetcode.com/problems/partition-array-into-two-arrays-to-minimize-sum-difference/discuss/1513435/Python-or-Easy-Explanation-or-Meet-in-the-Middle
|
class Solution:
def minimumDifference(self, nums: List[int]) -> int:
N = len(nums) // 2 # Note this is N/2, ie no. of elements required in each.
def get_sums(nums): # generate all combinations sum of k elements
ans = {}
N = len(nums)
for k in range(1, N+1): # takes k element for nums
sums = []
for comb in combinations(nums, k):
s = sum(comb)
sums.append(s)
ans[k] = sums
return ans
left_part, right_part = nums[:N], nums[N:]
left_sums, right_sums = get_sums(left_part), get_sums(right_part)
ans = abs(sum(left_part) - sum(right_part)) # the case when taking all N from left_part for left_ans, and vice versa
total = sum(nums)
half = total // 2 # the best sum required for each, we have to find sum nearest to this
for k in range(1, N):
left = left_sums[k] # if taking k no. from left_sums
right = right_sums[N-k] # then we have to take remaining N-k from right_sums.
right.sort() # sorting, so that we can binary search the required value
for x in left:
r = half - x # required, how much we need to add in x to bring it closer to half.
p = bisect.bisect_left(right, r) # we are finding index of value closest to r, present in right, using binary search
for q in [p, p-1]:
if 0 <= q < len(right):
left_ans_sum = x + right[q]
right_ans_sum = total - left_ans_sum
diff = abs(left_ans_sum - right_ans_sum)
ans = min(ans, diff)
return ans
|
partition-array-into-two-arrays-to-minimize-sum-difference
|
Python | Easy Explanation | Meet in the Middle
|
malraharsh
| 117
| 8,800
|
partition array into two arrays to minimize sum difference
| 2,035
| 0.183
|
Hard
| 28,307
|
https://leetcode.com/problems/partition-array-into-two-arrays-to-minimize-sum-difference/discuss/1514580/Python-3-Hint-solution-binary-search
|
class Solution:
def minimumDifference(self, nums: List[int]) -> int:
n = len(nums)
tot = sum(nums)
l, r = nums[:n//2], nums[n//2:]
lsum, rsum = defaultdict(set), defaultdict(set)
for k in range(n // 2 + 1):
lsum[k] |= set(map(sum, combinations(l, k)))
rsum[k] |= set(map(sum, combinations(r, k)))
ans = float('inf')
for k in lsum:
rsum_cand = sorted(rsum[n // 2 - k])
for ls in lsum[k]:
cand = tot // 2 - ls
loc = bisect.bisect(rsum_cand, cand)
if loc == 0:
rs = rsum_cand[loc]
ans = min(ans, abs(tot - 2 * (rs + ls)))
elif loc == len(rsum_cand):
rs = rsum_cand[loc-1]
ans = min(ans, abs(tot - 2 * (rs + ls)))
else:
rs1, rs2 = rsum_cand[loc-1], rsum_cand[loc]
ans = min(ans, abs(tot - 2 * (rs1 + ls)), abs(tot - 2 * (rs2 + ls)))
return ans
|
partition-array-into-two-arrays-to-minimize-sum-difference
|
[Python 3] Hint solution binary search
|
chestnut890123
| 3
| 825
|
partition array into two arrays to minimize sum difference
| 2,035
| 0.183
|
Hard
| 28,308
|
https://leetcode.com/problems/partition-array-into-two-arrays-to-minimize-sum-difference/discuss/2346227/Runtime%3A-1740-ms-or-Memory-Usage%3A-17.2-MB
|
class Solution:
def minimumDifference(self, nums: List[int]) -> int:
n = len(nums) // 2;
sum1, sum2 = sum(nums[:n]), sum(nums[n:]);
psum1, psum2 = [{0}], [{0}];
for ns, ps in zip([nums[:n], nums[n:]], [psum1, psum2]):
for i, x in enumerate(ns):
if len(ps) <= n//2: ps.append({s + x for s in ps[-1]});
for j in range(len(ps) - 1, 0, -1):
ps[j] = ps[j].union({s + x for s in ps[j-1]});
target = sum1 - sum2;
answer = abs(target);
# 2p1 - 2p2 ~ sum1 - sum2
for i in range(len(psum1)):
p1, p2 = sorted(list(psum1[i])), sorted(list(psum2[i]));
idx1, idx2 = 0, 0;
len1, len2 = len(p1), len(p2);
while idx1 < len1 and idx2 < len2:
diff = p1[idx1] - p2[idx2];
offset = 2 * diff - target;
answer = min(answer, abs(offset));
if offset < 0: idx1 += 1;
else: idx2 += 1;
return answer;
|
partition-array-into-two-arrays-to-minimize-sum-difference
|
Runtime: 1740 ms | Memory Usage: 17.2 MB
|
vimla_kushwaha
| 1
| 145
|
partition array into two arrays to minimize sum difference
| 2,035
| 0.183
|
Hard
| 28,309
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1539518/O(n)-counting-sort-in-Python
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
return sum(abs(seat - student) for seat, student in zip(seats, students))
|
minimum-number-of-moves-to-seat-everyone
|
O(n) counting sort in Python
|
mousun224
| 7
| 841
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,310
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1539518/O(n)-counting-sort-in-Python
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats_cnt, students_cnt = [0] * (max(seats) + 1), [0] * (max(students) + 1)
for seat in seats:
seats_cnt[seat] += 1
for student in students:
students_cnt[student] += 1
ans = 0
i = j = 1
while i < len(students_cnt):
if students_cnt[i]:
# find the next available seat
while j < len(seats_cnt) and not seats_cnt[j]:
j += 1
ans += abs(i - j)
seats_cnt[j] -= 1
students_cnt[i] -= 1
else:
i += 1
return ans
|
minimum-number-of-moves-to-seat-everyone
|
O(n) counting sort in Python
|
mousun224
| 7
| 841
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,311
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1987191/Python-Easiest-Solution-With-Explanation-or-Sorting-or-Beg-to-adv
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
total_moves = 0 # taking a int variable to store the value
sorted_seats = sorted(seats) # sorting the seat list
sorted_students = sorted(students) #sorting the student list
diff_1 = [] # taking empty list for storing difference of both the list
for i in range(len(seats)):
diff = abs(sorted_seats[i] - sorted_students[i]) # calculating diff of both the list elements, to calculate numbers of move.
diff_1.append(diff) # appending the Difference to the empty list declared by us.
for i in diff_1: # loop for traversing the diff of elems from both the lists.
total_moves += i # adding them to calculate the moves.
return total_moves
|
minimum-number-of-moves-to-seat-everyone
|
Python Easiest Solution With Explanation | Sorting | Beg to adv
|
rlakshay14
| 3
| 235
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,312
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1524161/Python3-1-line
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
return sum(abs(x-y) for x, y in zip(sorted(seats), sorted(students)))
|
minimum-number-of-moves-to-seat-everyone
|
[Python3] 1-line
|
ye15
| 3
| 190
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,313
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2822070/Python-oror-99.81-Faster-oror-5-Lines-oror-Sorting
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
c,n=0,len(seats)
for i in range(n): c+=abs(seats[i]-students[i])
return c
|
minimum-number-of-moves-to-seat-everyone
|
Python || 99.81% Faster || 5 Lines || Sorting
|
DareDevil_007
| 2
| 50
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,314
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1827464/Simple-Python-solution-or-Easy-to-understand-or-90-Faster
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
c = 0
for i, j in zip(seats, students):
c += abs(j-i)
return c
|
minimum-number-of-moves-to-seat-everyone
|
✔Simple Python solution | Easy to understand | 90% Faster
|
Coding_Tan3
| 1
| 106
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,315
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1589575/Python-3-Brute-Force-Solution
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats = sorted(seats)
students = sorted(students)
ans = 0
for i in range(len(seats)):
ans += abs(seats[i] - students[i])
return ans
|
minimum-number-of-moves-to-seat-everyone
|
[Python 3] Brute Force Solution
|
terrencetang
| 1
| 117
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,316
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1525887/Python3-Easy-solution-sorting
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
moves = 0
for seat, student in zip(sorted(seats), sorted(students)):
moves += abs(seat - student)
return moves
|
minimum-number-of-moves-to-seat-everyone
|
Python3 Easy solution, sorting
|
frolovdmn
| 1
| 87
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,317
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2846625/Python-or-Simple-or-Easy
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
total_moves = 0
for i in range(len(seats)):
total_moves += abs(students[i] - seats[i])
return total_moves
|
minimum-number-of-moves-to-seat-everyone
|
Python | Simple | Easy
|
pawangupta
| 0
| 1
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,318
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2843436/PYTHON-Clean-or-With-Comments
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats = sorted(seats)
students = sorted(students)
# redefine the varaibles to be sorted [min->max]
counter = 0
# set up the counter for amount of moves
for x in range(len(seats)):
counter += abs(seats[x]-students[x])
# add the amount of moves that the person moved
# use absolute in the case seats[x]<students[x]
return counter
# return the amount of moves
|
minimum-number-of-moves-to-seat-everyone
|
[PYTHON] Clean | With Comments
|
omkarxpatel
| 0
| 1
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,319
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2840269/Python-1-line%3A-Optimal-and-Clean-with-explanation-2-ways%3A-O(nlogn)-time-and-O(1)-space
|
class Solution:
# hint: if you sort both arrays, does greedy work? try to reason about a counterexample.
# O(nlogn) time : O(1) space
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
return sum(abs(seat - student) for seat, student in zip(seats, students))
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
return sum(abs(seat - student) for seat, student in zip(sorted(seats), sorted(students)))
|
minimum-number-of-moves-to-seat-everyone
|
Python 1 line: Optimal and Clean with explanation - 2 ways: O(nlogn) time and O(1) space
|
topswe
| 0
| 3
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,320
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2836103/Python-or-Simple-Understandable-Solution
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
p=sorted(seats)
s=sorted(students)
ps=[]
for i in range(len(students)):
if p[i]-s[i]<0:
ps.append(s[i]-p[i])
else:
ps.append(p[i]-s[i])
sp=0
for i in ps:
sp+=i
return sp
|
minimum-number-of-moves-to-seat-everyone
|
Python | Simple Understandable Solution
|
priyanshupriyam123vv
| 0
| 1
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,321
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2833776/3Line-code
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats = sorted(seats)
students = sorted(students)
return sum([abs(seats[i] - students[i]) for i in range(len(students))])
|
minimum-number-of-moves-to-seat-everyone
|
3Line code
|
zsigmondy
| 0
| 2
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,322
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2833441/PYTHON3-BEST
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
return sum(abs(i - j) for i, j in zip(seats, students))
|
minimum-number-of-moves-to-seat-everyone
|
PYTHON3 BEST
|
Gurugubelli_Anil
| 0
| 1
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,323
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2818126/Python-simple-sorting-solution
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
res = 0
for i in range(len(seats)):
res += abs(students[i]-seats[i])
return res
|
minimum-number-of-moves-to-seat-everyone
|
Python simple sorting solution
|
ankurkumarpankaj
| 0
| 1
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,324
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2811510/Simple-program-which-beats-100-in-terms-of-speed
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
s=0
for i in range(len(seats)):
n=seats[i]-students[i]
if n<0:
n=-1*n
s+=n
return s
|
minimum-number-of-moves-to-seat-everyone
|
Simple program which beats 100% in terms of speed
|
gowdavidwan2003
| 0
| 1
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,325
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2760983/python-solution-94.75-faster.
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
min_number_of_moves = 0
for student, seat in zip(students, seats):
diff = abs(student - seat)
min_number_of_moves += diff
return min_number_of_moves
|
minimum-number-of-moves-to-seat-everyone
|
python solution 94.75% faster.
|
samanehghafouri
| 0
| 4
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,326
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2727054/Python3-Simple-Solution%3A-abs-differences-of-items
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
return sum([abs(seats[i] - students[i]) for i in range(0,len(seats))])
|
minimum-number-of-moves-to-seat-everyone
|
Python3 Simple Solution: abs differences of items
|
sipi09
| 0
| 3
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,327
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2677689/Python-Sort-One-line-solution
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
return sum([abs(i - j) for i,j in zip(sorted(seats), sorted(students))])
|
minimum-number-of-moves-to-seat-everyone
|
Python Sort One line solution
|
zzjoshua
| 0
| 15
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,328
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2622337/Simple-and-fast-python3-solution
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
res = 0
seats.sort()
students.sort()
for i in range(len(students)):
res += abs(students[i] - seats[i])
return res
|
minimum-number-of-moves-to-seat-everyone
|
Simple and fast python3 solution
|
Gilbert770
| 0
| 16
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,329
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2601747/Easiest-way-to-find-minimum-numbers-of-moves-or-Python-or-Faster
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
count=0
seats.sort()
students.sort()
for i in range(len(seats)):
count+=abs(seats[i]-students[i])
return count
|
minimum-number-of-moves-to-seat-everyone
|
Easiest way to find minimum numbers of moves | Python | Faster
|
msherazedu
| 0
| 45
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,330
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2387705/Python3-Solution-with-using-sorting
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
res = 0
for i in range(len(seats)):
res += abs(seats[i] - students[i])
return res
|
minimum-number-of-moves-to-seat-everyone
|
[Python3] Solution with using sorting
|
maosipov11
| 0
| 21
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,331
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2296635/Python-oror-Easy-oror-6-Liner-oror-70-Faster
|
'''class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
moves=0
for i in range(len(seats)):
n=abs(seats[i]-students[i])
moves=moves+n
return moves'''
|
minimum-number-of-moves-to-seat-everyone
|
Python || Easy || 6 Liner || 70% Faster
|
keertika27
| 0
| 36
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,332
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2160510/Simple-Logic
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
return sum(abs(e-t) for e, t in zip(seats, students))
|
minimum-number-of-moves-to-seat-everyone
|
Simple Logic
|
writemeom
| 0
| 61
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,333
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2040399/Python-(Simple-Approach-and-Beginner-Friendly)
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
op = 0;
seats.sort()
students.sort()
for i in range(0,len(seats)):
op += abs(students[i] - seats[i])
return op
|
minimum-number-of-moves-to-seat-everyone
|
Python (Simple Approach and Beginner-Friendly)
|
vishvavariya
| 0
| 106
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,334
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/2019069/Python-or-simple-or-easy-solution
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
count = 0
for i, j in zip(sorted(seats), sorted(students)):
count+=abs(i-j)
return count
|
minimum-number-of-moves-to-seat-everyone
|
Python | simple | easy solution
|
manikanthgoud123
| 0
| 26
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,335
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1991622/Python-Solution
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
return sum(map(lambda x: abs(x[0] - x[1]), zip(sorted(seats), sorted(students))))
|
minimum-number-of-moves-to-seat-everyone
|
Python Solution
|
hgalytoby
| 0
| 59
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,336
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1991622/Python-Solution
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
return reduce(lambda x, y: x + abs(y[0] - y[1]), zip(sorted(seats), sorted(students)), 0)
|
minimum-number-of-moves-to-seat-everyone
|
Python Solution
|
hgalytoby
| 0
| 59
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,337
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1865789/Python-solution-faster-than-99-memory-less-than-77
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
count_moves = 0
for i in range(len(seats)):
count_moves += abs(students[i] - seats[i])
return count_moves
|
minimum-number-of-moves-to-seat-everyone
|
Python solution faster than 99%, memory less than 77%
|
alishak1999
| 0
| 100
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,338
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1857672/3-Lines-Python-Solution-oror-80-Faster-oror-Memory-less-than-85
|
class Solution:
def minMovesToSeat(self, SE: List[int], ST: List[int]) -> int:
ST.sort() ; SE.sort() ; ans=0
for i in range(len(SE)): ans+=abs(ST[i]-SE[i])
return ans
|
minimum-number-of-moves-to-seat-everyone
|
3-Lines Python Solution || 80% Faster || Memory less than 85%
|
Taha-C
| 0
| 44
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,339
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1834119/Simple-Python-Solution
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
moves = 0
for i, j in zip(seats, students):
moves += abs(i-j)
return moves
```
|
minimum-number-of-moves-to-seat-everyone
|
Simple Python Solution
|
himanshu11sgh
| 0
| 30
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,340
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1762586/WEEB-DOES-PYTHONC%2B%2B
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
result = 0
for student, seat in zip(students, seats):
result += abs(student - seat)
return result
|
minimum-number-of-moves-to-seat-everyone
|
WEEB DOES PYTHON/C++
|
Skywalker5423
| 0
| 50
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,341
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1723912/Easiest-Python3-solution-with-99-better-memory-usage
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
out=0
for i in range(0,len(seats)):
if seats[i]>=students[i]:
out=out+seats[i]-students[i]
elif students[i]>seats[i]:
out=out+students[i]-seats[i]
return out
|
minimum-number-of-moves-to-seat-everyone
|
Easiest Python3 solution with 99% better memory usage
|
rajatkumarrrr
| 0
| 35
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,342
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1620416/Python-3-sorting-solution-faster-than-96
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
return sum(abs(seat - student) for seat, student in zip(seats, students))
|
minimum-number-of-moves-to-seat-everyone
|
Python 3 sorting solution, faster than 96%
|
dereky4
| 0
| 205
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,343
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1611651/Python-Easy-to-understand
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
final=0
seats.sort()
students.sort()
for i in range(len(seats)):
final+=abs(seats[i] - students[i])
return final
|
minimum-number-of-moves-to-seat-everyone
|
Python, Easy to understand
|
japnitS
| 0
| 128
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,344
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1573718/python-solution-or-faster-than-96
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
ans = [abs(seats[i]-students[i]) for i in range(len(seats))]
return sum(ans)
|
minimum-number-of-moves-to-seat-everyone
|
python solution | faster than 96%
|
anandanshul001
| 0
| 103
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,345
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1544679/Straightforward-Python-solution
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort(reverse=True), students.sort()
moves = 0
for student in students:
moves += abs(student - seats[-1])
seats.pop()
return moves
|
minimum-number-of-moves-to-seat-everyone
|
Straightforward Python solution
|
akshaykumar19002
| 0
| 87
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,346
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1524399/Python-Sorting-O(nlogn)
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
return sum(abs(seats[i]-students[i]) for i in range(len(seats)))
|
minimum-number-of-moves-to-seat-everyone
|
Python-Sorting-O(nlogn)
|
zoro_55
| 0
| 59
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,347
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1524374/Python-one-liner
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
return sum(abs(i-j) for i, j in zip(sorted(seats), sorted(students)))
|
minimum-number-of-moves-to-seat-everyone
|
Python, one-liner
|
blue_sky5
| 0
| 63
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,348
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1524267/Python-sorting-solution
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats.sort()
students.sort()
return sum(abs(seats[i]-students[i]) for i in range(len(seats)))
|
minimum-number-of-moves-to-seat-everyone
|
Python sorting solution
|
abkc1221
| 0
| 72
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,349
|
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/1883258/Python-dollarolution
|
class Solution:
def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
seats = sorted(seats)
students = sorted(students)
count = 0
for i in range(len(seats)):
count += abs(seats[i] - students[i])
return count
|
minimum-number-of-moves-to-seat-everyone
|
Python $olution
|
AakRay
| -1
| 44
|
minimum number of moves to seat everyone
| 2,037
| 0.821
|
Easy
| 28,350
|
https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/discuss/1524153/C%2B%2BPythonJava-Count-%22AAA%22-and-%22BBB%22
|
class Solution:
def winnerOfGame(self, s: str) -> bool:
a = b = 0
for i in range(1,len(s)-1):
if s[i-1] == s[i] == s[i+1]:
if s[i] == 'A':
a += 1
else:
b += 1
return a>b
|
remove-colored-pieces-if-both-neighbors-are-the-same-color
|
[C++/Python/Java] Count "AAA" and "BBB"
|
lokeshsenthilkumar
| 75
| 5,000
|
remove colored pieces if both neighbors are the same color
| 2,038
| 0.582
|
Medium
| 28,351
|
https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/discuss/2112058/PYTHON-oror-EXPLANATION-FOR-ALL-oror
|
class Solution:
def winnerOfGame(self, s: str) -> bool:
a=[]
p="C"
for i in s:
if i==p:
a[-1]+=1
else:
p=i
a.append(1)
odd,even=0,0
for i in range(len(a)):
if i%2:
odd += max(0,a[i]-2)
else:
even += max (0,a[i]-2)
if s[0]=="A" and even>odd:
return True
if s[0]=="B" and odd>even:
return True
return False
|
remove-colored-pieces-if-both-neighbors-are-the-same-color
|
PYTHON || ✔️ EXPLANATION FOR ALL ✔️||
|
karan_8082
| 9
| 589
|
remove colored pieces if both neighbors are the same color
| 2,038
| 0.582
|
Medium
| 28,352
|
https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/discuss/1524171/Python3-greedy-5-line
|
class Solution:
def winnerOfGame(self, colors: str) -> bool:
diff = 0
for k, grp in groupby(colors):
if k == "A": diff += max(0, len(list(grp)) - 2)
else: diff -= max(0, len(list(grp)) - 2)
return diff > 0
|
remove-colored-pieces-if-both-neighbors-are-the-same-color
|
[Python3] greedy 5-line
|
ye15
| 1
| 108
|
remove colored pieces if both neighbors are the same color
| 2,038
| 0.582
|
Medium
| 28,353
|
https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/discuss/2577253/Python3-or-greedy-with-split-or-O(n)
|
class Solution:
def winnerOfGame(self, colors: str) -> bool:
a_segs = colors.split("B")
b_segs = colors.split("A")
a_removal = 0
for seg in a_segs:
if len(seg) > 2:
a_removal += len(seg) - 2
b_removal = 0
for seg in b_segs:
if len(seg) > 2:
b_removal += len(seg) - 2
return a_removal > b_removal
|
remove-colored-pieces-if-both-neighbors-are-the-same-color
|
Python3 | greedy with split | O(n)
|
colors92window
| 0
| 65
|
remove colored pieces if both neighbors are the same color
| 2,038
| 0.582
|
Medium
| 28,354
|
https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/discuss/2397422/Python-oror-O(n)-oror-Easy
|
class Solution:
def winnerOfGame(self, colors: str) -> bool:
acount=0
bcount=0
for i in range(len(colors)):
c=colors[i]
if(c=='A' and i-1>=0 and i+1<len(colors) and colors[i-1]=='A' and colors[i+1]=='A'):
acount+=1
if(c=='B' and i-1>=0 and i+1<len(colors) and colors[i-1]=='B' and colors[i+1]=='B'):
bcount+=1
if(acount<=bcount):
return False
else:
return True
|
remove-colored-pieces-if-both-neighbors-are-the-same-color
|
Python || O(n) || Easy
|
ausdauerer
| 0
| 71
|
remove colored pieces if both neighbors are the same color
| 2,038
| 0.582
|
Medium
| 28,355
|
https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/discuss/2362262/Python-straightforward-solution
|
class Solution:
def winnerOfGame(self, colors: str) -> bool:
a_moves = b_moves = 0
for i in range(2, len(colors)):
if colors[i-2] == colors[i-1] == colors[i]:
if colors[i] == 'A':
a_moves += 1
else:
b_moves += 1
return a_moves > b_moves
|
remove-colored-pieces-if-both-neighbors-are-the-same-color
|
Python, straightforward solution
|
blue_sky5
| 0
| 51
|
remove colored pieces if both neighbors are the same color
| 2,038
| 0.582
|
Medium
| 28,356
|
https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/discuss/2111439/counting-consectives
|
class Solution:
def winnerOfGame(self, colors: str) -> bool:
if len(colors) < 3:
return False
bobsTurn = []
alicesTrun = []
currLetter = colors[0]
count = 0
for i in range(len(colors)):
if colors[i] == currLetter:
count +=1
if i+1 >= len(colors):
if currLetter == 'B':
bobsTurn.append(count)
else:
alicesTrun.append(count)
continue
else:
if currLetter == 'B':
bobsTurn.append(count)
else:
alicesTrun.append(count)
currLetter = colors[i]
count = 1
continue
bobTotal = 0
aliceTotal = 0
for i in bobsTurn:
p = i - 2
if p < 0 : continue
bobTotal += i - 2
for i in alicesTrun:
p = i - 2
if p < 0 : continue
aliceTotal += i - 2
return aliceTotal > bobTotal
|
remove-colored-pieces-if-both-neighbors-are-the-same-color
|
counting consectives
|
zoudarren7
| 0
| 19
|
remove colored pieces if both neighbors are the same color
| 2,038
| 0.582
|
Medium
| 28,357
|
https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/discuss/1659491/Intuition-Explained
|
class Solution:
def winnerOfGame(self, colors: str) -> bool:
aliceMoves = 0
bobMoves = 0
consecutiveA = 0
consecutiveB = 0
for color in colors:
if color == "A":
consecutiveA += 1
consecutiveB = 0
else:
consecutiveA = 0
consecutiveB += 1
if consecutiveA >= 3:
aliceMoves += 1
if consecutiveB >= 3:
bobMoves += 1
return aliceMoves > bobMoves
|
remove-colored-pieces-if-both-neighbors-are-the-same-color
|
Intuition Explained
|
jdot593
| 0
| 69
|
remove colored pieces if both neighbors are the same color
| 2,038
| 0.582
|
Medium
| 28,358
|
https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/discuss/1527980/Using-groupby-71-speed
|
class Solution:
def winnerOfGame(self, colors: str) -> bool:
count_a = count_b = 0
for key, g in groupby(colors):
len_g = len(list(g))
if key == "A":
if len_g > 2:
count_a += len_g - 2
else:
if len_g > 2:
count_b += len_g - 2
return count_a > count_b
|
remove-colored-pieces-if-both-neighbors-are-the-same-color
|
Using groupby, 71% speed
|
EvgenySH
| 0
| 89
|
remove colored pieces if both neighbors are the same color
| 2,038
| 0.582
|
Medium
| 28,359
|
https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/discuss/1524245/Python-2-pointers
|
class Solution:
def winnerOfGame(self, arr: str) -> bool:
n = len(arr)
def count(char):
i, j = 0, 0
res = 0
while j < n:
temp = 0
while j < n and arr[j] == char:
temp += 1
j += 1
res += (temp-2) if temp > 2 else 0
j += 1
i = j
return res
return count('A') > count('B')
|
remove-colored-pieces-if-both-neighbors-are-the-same-color
|
Python 2 pointers
|
abkc1221
| 0
| 73
|
remove colored pieces if both neighbors are the same color
| 2,038
| 0.582
|
Medium
| 28,360
|
https://leetcode.com/problems/the-time-when-the-network-becomes-idle/discuss/1524183/Python3-graph
|
class Solution:
def networkBecomesIdle(self, edges: List[List[int]], patience: List[int]) -> int:
graph = {}
for u, v in edges:
graph.setdefault(u, []).append(v)
graph.setdefault(v, []).append(u)
dist = [-1]*len(graph)
dist[0] = 0
val = 0
queue = [0]
while queue:
val += 2
newq = []
for u in queue:
for v in graph[u]:
if dist[v] == -1:
dist[v] = val
newq.append(v)
queue = newq
ans = 0
for d, p in zip(dist, patience):
if p:
k = d//p - int(d%p == 0)
ans = max(ans, d + k*p)
return ans + 1
|
the-time-when-the-network-becomes-idle
|
[Python3] graph
|
ye15
| 5
| 176
|
the time when the network becomes idle
| 2,039
| 0.508
|
Medium
| 28,361
|
https://leetcode.com/problems/the-time-when-the-network-becomes-idle/discuss/2526479/Python-bfs-oror-fast-and-very-easy-to-understand
|
class Solution:
def networkBecomesIdle(self, edges: List[List[int]], patience: List[int]) -> int:
graph = defaultdict(set)
for a, b in edges:
graph[a].add(b)
graph[b].add(a)
dis = {}
queue = deque([(0, 0)])
visited = set([0])
while queue:
cur, length = queue.popleft()
dis[cur] = length * 2
for nxt in graph[cur]:
if nxt not in visited:
queue.append((nxt, length + 1))
visited.add(nxt)
ans = -float("inf")
for i in range(1, len(patience)):
if patience[i] < dis[i]:
rem = dis[i] % patience[i]
lastCall = dis[i] - (rem) if rem > 0 else dis[i] - patience[i]
ans = max(ans, lastCall + dis[i])
else:
ans = max(ans, dis[i])
return ans + 1
# time and space complexity
# time: O(n + m)
# space: O(n)
# n = number of nodes
# m = len(edges)
|
the-time-when-the-network-becomes-idle
|
Python bfs || fast and very easy to understand
|
Yared_betsega
| 1
| 34
|
the time when the network becomes idle
| 2,039
| 0.508
|
Medium
| 28,362
|
https://leetcode.com/problems/the-time-when-the-network-becomes-idle/discuss/1524256/For-Beginners-oror-Greedy-Approach-oror-Well-Explained
|
class Solution:
def networkBecomesIdle(self, edges: List[List[int]], patience: List[int]) -> int:
graph = defaultdict(list)
for e1,e2 in edges:
graph[e1].append(e2)
graph[e2].append(e1)
dist = [-1]*len(graph)
dist[0] = 0
queue = [0]
d = 0
while queue:
d+=2
newq = []
for u in queue:
for v in graph[u]:
if dist[v]==-1:
dist[v]=d
newq.append(v)
queue = newq
res = 0
for d,p in zip(dist[1:],patience[1:]):
k = d//p
if(d%p==0):
k-=1
res = max(res,d+k*p)
return res+1
|
the-time-when-the-network-becomes-idle
|
📌📌 For-Beginners || Greedy-Approach || Well-Explained 🐍
|
abhi9Rai
| 1
| 70
|
the time when the network becomes idle
| 2,039
| 0.508
|
Medium
| 28,363
|
https://leetcode.com/problems/the-time-when-the-network-becomes-idle/discuss/2847951/Easiest-code-with-best-explanation-for-the-formula
|
class Solution:
def networkBecomesIdle(self, edges: List[List[int]], patience: List[int]) -> int:
#part-1 of code
#Djikstra for getting the shortest time
graph = defaultdict(list)
for u,v in edges:
graph[u].append(v)
graph[v].append(u)
time_tracker = [float("inf")]*len(patience)
time_tracker[0] = 0
heap = []
heappush(heap,(0,0))
while heap:
time,node = heappop(heap)
for nei in graph[node]:
if time+1 < time_tracker[nei]:
heappush(heap,(time+1,nei))
time_tracker[nei] = time+1
#part-2 of code
#To find the max time needed
max_time_needed = 0
for i in range(1,len(time_tracker)):
#time needed will be 2 times what it takes one way
time_needed = 2*time_tracker[i]
#number of msgs sent will me time_needed/patience
#why ceil? if time_needed -> 6 and patience -> 5
#time_needed/patience -> 1.2, we can send 0.2 msg,we have
#to send entire msg. So we take ceil
msgs_sent = ceil(time_needed/patience[i])
#total time needed will be the time when last msg is sent
#plus the time it will take to come back(i.e. time_needed)
#the first msg is sent at 0th second. that leaves us with
#msgs_sent - 1. We multiply it by patience to get when
#the last msg will be sent.
total_time_needed = (patience[i])*(msgs_sent-1) +time_needed
max_time_needed = max(max_time_needed,total_time_needed)
return max_time_needed + 1
|
the-time-when-the-network-becomes-idle
|
Easiest code with best explanation for the formula
|
shriyansnaik
| 0
| 1
|
the time when the network becomes idle
| 2,039
| 0.508
|
Medium
| 28,364
|
https://leetcode.com/problems/the-time-when-the-network-becomes-idle/discuss/2272867/Python-3-BFS
|
class Solution:
def networkBecomesIdle(self, edges: List[List[int]], patience: List[int]) -> int:
n=len(patience)
G=[[] for _ in range(n)]
for x,y in edges:
G[x].append(y)
G[y].append(x)
vis,q=[False]*n,[0]
vis[0]=True
res,d=0,1
while q:
new_q=[]
for node in q:
for child in G[node]:
if not vis[child]:
res=max(res,2*d+((2*d-1)//patience[child])*patience[child])
vis[child]=True
new_q.append(child)
q=new_q
d+=1
return res+1
|
the-time-when-the-network-becomes-idle
|
[Python 3] BFS
|
gabhay
| 0
| 17
|
the time when the network becomes idle
| 2,039
| 0.508
|
Medium
| 28,365
|
https://leetcode.com/problems/the-time-when-the-network-becomes-idle/discuss/1670121/Python-Simple-BFS-Implementation.
|
class Solution:
def networkBecomesIdle(self, edges: List[List[int]], patience: List[int]) -> int:
# create adjacency list
adj_list = defaultdict(list)
for i,j in edges:
adj_list[i].append(j)
adj_list[j].append(i)
# min_time is the time taken for a message to reach master and come back. Set to -1
min_time = {i:-1 for i in adj_list.keys()}
# set master to node 0
master = 0
queue = [master]
visited = {master}
# time variable keeps track of distance from master
time = 0
min_time[master] = time
# BFS to get minimum distance for each node to master
while queue:
new_queue = []
for node in queue:
for neighbor in adj_list[node]:
if neighbor not in visited:
# Multiply time by 2 to account for roundabout time
min_time[neighbor] = 2*(time+1)
visited.add(neighbor)
new_queue.append(neighbor)
time += 1
queue = new_queue
# remove master from further calculations as it will always be 0
del min_time[master]
# (time-1) - (time-1)%patience[i] is used to calculate the time the last message was sent
# before the first reply reached the server i
# time - 1 is used because if the message reaches the server at the same time the message is
# supposed to be transmitted, the message is not transmitted anymore
min_time = {i:(time-1) - (time-1)%patience[i] + time for i,time in min_time.items()}
return max(min_time.values()) + 1
|
the-time-when-the-network-becomes-idle
|
Python - Simple BFS Implementation.
|
kaustav43
| 0
| 64
|
the time when the network becomes idle
| 2,039
| 0.508
|
Medium
| 28,366
|
https://leetcode.com/problems/the-time-when-the-network-becomes-idle/discuss/1531159/Python-3-or-Clean-BFS-Deque-or-Explanation
|
class Solution:
def networkBecomesIdle(self, edges: List[List[int]], patience: List[int]) -> int:
graph = collections.defaultdict(list)
for a, b in edges: # build graph
graph[a].append(b)
graph[b].append(a)
dq = collections.deque([(0, 0)]) # prepare bfs using deque
ans, n = 0, 1
visited = set()
while dq:
for _ in range(n):
cur, step = dq.popleft()
n -= 1 # keep track of size of dq
visited.add(cur)
for nei in graph[cur]:
if nei in visited: continue
visited.add(nei)
n += 1
dq.append((nei, step+1))
if cur:
time = step * 2 # first msg round trip time
mod = time % patience[cur]
finished = patience[cur] if not mod else mod # last msg finished moves
total = time + (time - finished) # total time = time + last_msg_unfinished_moves
ans = max(ans, total)
return ans + 1
|
the-time-when-the-network-becomes-idle
|
Python 3 | Clean BFS, Deque | Explanation
|
idontknoooo
| 0
| 87
|
the time when the network becomes idle
| 2,039
| 0.508
|
Medium
| 28,367
|
https://leetcode.com/problems/kth-smallest-product-of-two-sorted-arrays/discuss/1524190/Python3-binary-search
|
class Solution:
def kthSmallestProduct(self, nums1: List[int], nums2: List[int], k: int) -> int:
def fn(val):
"""Return count of products <= val."""
ans = 0
for x in nums1:
if x < 0: ans += len(nums2) - bisect_left(nums2, ceil(val/x))
elif x == 0:
if 0 <= val: ans += len(nums2)
else: ans += bisect_right(nums2, floor(val/x))
return ans
lo, hi = -10**10, 10**10 + 1
while lo < hi:
mid = lo + hi >> 1
if fn(mid) < k: lo = mid + 1
else: hi = mid
return lo
|
kth-smallest-product-of-two-sorted-arrays
|
[Python3] binary search
|
ye15
| 8
| 1,300
|
kth smallest product of two sorted arrays
| 2,040
| 0.291
|
Hard
| 28,368
|
https://leetcode.com/problems/kth-smallest-product-of-two-sorted-arrays/discuss/1524190/Python3-binary-search
|
class Solution:
def kthSmallestProduct(self, nums1: List[int], nums2: List[int], k: int) -> int:
neg = [x for x in nums1 if x < 0]
pos = [x for x in nums1 if x >= 0]
def fn(val):
"""Return count of products <= val."""
ans = 0
lo, hi = 0, len(nums2)-1
for x in neg[::-1] + pos if val >= 0 else neg + pos[::-1]:
if x < 0:
while lo < len(nums2) and x*nums2[lo] > val: lo += 1
ans += len(nums2) - lo
elif x == 0:
if 0 <= val: ans += len(nums2)
else:
while 0 <= hi and x*nums2[hi] > val: hi -= 1
ans += hi+1
return ans
lo, hi = -10**10, 10**10 + 1
while lo < hi:
mid = lo + hi >> 1
if fn(mid) < k: lo = mid + 1
else: hi = mid
return lo
|
kth-smallest-product-of-two-sorted-arrays
|
[Python3] binary search
|
ye15
| 8
| 1,300
|
kth smallest product of two sorted arrays
| 2,040
| 0.291
|
Hard
| 28,369
|
https://leetcode.com/problems/kth-smallest-product-of-two-sorted-arrays/discuss/2163920/Python-3-AC-Solution-with-detailed-math-derivation
|
class Solution:
def kthSmallestProduct(self, nums1: List[int], nums2: List[int], k: int) -> int:
"""
Binary Search
find first number that makes countSmallerOrEqual(number) >= k
return number
"""
boundary = [nums1[0]*nums2[0], nums1[0] * nums2[-1], nums1[-1] * nums2[0], nums1[-1] * nums2[-1]]
low, high = min(boundary), max(boundary)
while low + 1 < high:
mid = low + (high - low) // 2
if self.countSmallerOrEqual(mid, nums1, nums2) >= k:
high = mid
else:
low = mid + 1
if self.countSmallerOrEqual(low, nums1, nums2) >= k:
return low
else:
return high
def countSmallerOrEqual(self, m, nums1, nums2):
"""
Two pointers solution
use monotonic property of both nums1 and nums2
return the number of products nums1[i] * nums2[j] <= m
l1, l2 = len(nums1), len(nums2)
1) if m >= 0,
***Given nums1[i] < 0***
find j such that nums2[j] >= m/nums1[i]
i increases - > nums1[i] increases -> m/nums1[i] decreases -> index j decreases
j monotonically moves left
return j:l2 -> l2 - j + 1
*** Given nums1[i] = 0
return l2
***Given nums1[i] > 0***
find j such that nums2[j] <= m/nums1[i]
i increases - > nums1[i] increases -> m/nums1[i] decreases -> index j decreases
j monotonically moves left
return 0:j -> j + 1
2) if m < 0,
***Given nums1[i] < 0***
find j such that nums2[j] >= m/nums1[i]
i increases - > nums1[i] increases -> m/nums1[i] increases -> index j increases
j monotonically moves right
return j:l2 -> l2 - j + 1
*** Given nums1[i] = 0
return 0
***Given nums1[i] > 0***
find j such that nums2[j] <= m/nums1[i]
i increases - > nums1[i] increases -> m/nums1[i] increases -> index j increases
j monotonically moves right
return 0:j -> j + 1
"""
l1, l2 = len(nums1), len(nums2)
ans = 0
if m >= 0:
j1, j2 = l2-1, l2-1
for i in range(l1):
if nums1[i] < 0:
while j1 >=0 and nums2[j1] >= m/nums1[i]:
j1 -= 1
ans += l2 - j1 - 1
elif nums1[i] > 0:
while j2 >=0 and nums2[j2] > m/nums1[i]:
j2 -= 1
ans += j2 + 1
else:
ans += l2
else:
j1, j2 = 0, 0
for i in range(l1):
if nums1[i] < 0:
while j1 < l2 and nums2[j1] < m/nums1[i]:
j1 += 1
ans += l2 - j1
elif nums1[i] > 0:
while j2 < l2 and nums2[j2] <= m/nums1[i]:
j2 += 1
ans += j2
return ans
|
kth-smallest-product-of-two-sorted-arrays
|
Python 3 AC Solution with detailed math derivation
|
Cara22
| 2
| 189
|
kth smallest product of two sorted arrays
| 2,040
| 0.291
|
Hard
| 28,370
|
https://leetcode.com/problems/kth-smallest-product-of-two-sorted-arrays/discuss/1596349/Python3-Brute-Force-in-TLE-passing-82112-test-cases
|
class Solution:
def kthSmallestProduct(self, nums1: List[int], nums2: List[int], k: int) -> int:
result = []
for i in range(len(nums1)):
for j in range(len(nums2)):
temp = nums1[i]*nums2[j]
result.append(temp)
result.sort()
return result[k-1]
|
kth-smallest-product-of-two-sorted-arrays
|
[Python3] Brute Force in TLE passing 82/112 test cases
|
yugo9081
| 0
| 361
|
kth smallest product of two sorted arrays
| 2,040
| 0.291
|
Hard
| 28,371
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/1525219/Python3-2-line
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
nums = [int(w) for w in s.split() if w.isdigit()]
return all(nums[i-1] < nums[i] for i in range(1, len(nums)))
|
check-if-numbers-are-ascending-in-a-sentence
|
[Python3] 2-line
|
ye15
| 24
| 1,400
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,372
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/1525691/Python-3-Simple-regex-solution-or-2-lines!
|
class Solution:
def areNumbersAscending(self, s):
nums = re.findall(r'\d+', s)
return nums == sorted(set(nums), key=int)
|
check-if-numbers-are-ascending-in-a-sentence
|
[Python 3] Simple regex solution | 2 lines!
|
JK0604
| 3
| 147
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,373
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/1526154/Python-optimal-one-pass-solution
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
prev = 0
for token in s.split():
if token.isnumeric():
if (curr := int(token)) <= prev:
return False
prev = curr
return True
|
check-if-numbers-are-ascending-in-a-sentence
|
Python, optimal one pass solution
|
blue_sky5
| 2
| 97
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,374
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/1525901/Python3-Easy-solution-re
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
nums = re.findall('\d+', s)
nums = [int(num) for num in nums]
if nums == sorted(nums) and len(nums) == len(set(nums)):
return True
else:
return False
|
check-if-numbers-are-ascending-in-a-sentence
|
Python3 Easy solution, re
|
frolovdmn
| 2
| 52
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,375
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/2670447/Python-or-O(n)
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
st = ''
res= []
for i in range(len(s)):
if s[i].isnumeric():
st += s[i]
else:
if len(st)>0:
res.append(int(st))
st =''
if len(st)>0:
res.append(int(st))
for i in range(0,len(res)-1):
if res[i]>=res[i+1]:
return False
return True
|
check-if-numbers-are-ascending-in-a-sentence
|
Python | O(n)
|
naveenraiit
| 1
| 123
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,376
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/1697770/Python3-accepted-solution
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
num = [int(i) for i in s.split() if(i.isnumeric())]
return True if(num == sorted(num) and len(num)==len(set(num))) else False
|
check-if-numbers-are-ascending-in-a-sentence
|
Python3 accepted solution
|
sreeleetcode19
| 1
| 55
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,377
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/1662416/Python3-3-line
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
lst = [int(x) for x in s.split() if x.isdigit()]
a = sorted(set(lst))
return lst == a
|
check-if-numbers-are-ascending-in-a-sentence
|
[Python3] 3-line
|
Cheems_Coder
| 1
| 68
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,378
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/1589768/Using-regex-97-speed
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
nums = list(map(int, re.findall(r"\b\d+\b", s)))
return len(nums) < 2 or all(n2 > n1 for n1, n2 in zip(nums, nums[1:]))
|
check-if-numbers-are-ascending-in-a-sentence
|
Using regex, 97% speed
|
EvgenySH
| 1
| 60
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,379
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/2837421/Python3-Sample
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
number, curr = "", -1
for i in range(len(s)):
if s[i] in ("0","1","2","3","4","5","6","7","8","9"):
number += s[i]
elif len(number) > 0:
if curr >= int(number):
return False
curr = int(number)
number = ""
if i == len(s)-1 and number != "":
if curr >= int(number):
return False
curr = int(number)
return True
|
check-if-numbers-are-ascending-in-a-sentence
|
Python3 Sample
|
DNST
| 0
| 2
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,380
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/2834109/Simple-intuitive-python-solution
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
arr = []
i = 0
while i < len(s):
if s[i].isdigit():
j = i + 1
while j < len(s) and s[j].isdigit():
j += 1
arr.append(int(s[i:j]))
i = j
else:
i += 1
print(arr)
return arr == sorted(arr) and len(arr) == len(set(arr))
|
check-if-numbers-are-ascending-in-a-sentence
|
Simple intuitive python solution
|
aruj900
| 0
| 3
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,381
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/2813520/Solution-with-O(1)-space
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
def getTokens(s):
i = 0
currentToken = []
while i < len(s):
if s[i] != ' ':
currentToken.append(s[i])
else:
yield ''.join(currentToken)
currentToken = []
i += 1
if currentToken:
yield ''.join(currentToken)
prev = None
for token in getTokens(s):
try:
num = int(token)
except:
continue
if prev:
if num <= prev:
return False
prev = num
return True
|
check-if-numbers-are-ascending-in-a-sentence
|
Solution with O(1) space
|
Mik-100
| 0
| 2
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,382
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/2781864/Python-Solution
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
previous_number = -1
for char in s.split(" "):
if char.isdigit():
current_number = int(char)
if current_number <= previous_number:
return False
previous_number = current_number
return True
|
check-if-numbers-are-ascending-in-a-sentence
|
Python Solution
|
namashin
| 0
| 3
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,383
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/2752405/Python-Solution-oror-Beats-92
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
res=[int(i) for i in s.split() if i.isdigit()]
if(len(set(res))!=len(res)):
return False
else:
return sorted(res)==res
|
check-if-numbers-are-ascending-in-a-sentence
|
Python Solution || Beats 92%
|
shivammenda2002
| 0
| 3
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,384
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/2726575/Python3-simple-approach
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
l = []
for i in s.split():
if i.isdigit():
l.append(int(i))
for i in range(1,len(l)):
if l[i-1] > l[i] or l[i-1] == l[i]:
return False
return True
|
check-if-numbers-are-ascending-in-a-sentence
|
Python3 simple approach
|
chakalivinith
| 0
| 3
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,385
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/2662218/Python-Solution-without-Regex-or-Split-or-O(n)-time-O(1)-space
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
prevNum = currNum = -1
for char in s:
if char.isdigit():
if currNum == -1:
currNum = int(char)
else:
currNum = (currNum * 10) + int(char)
continue
if char == ' ' and currNum != -1:
if currNum <= prevNum:
return False
prevNum = currNum
currNum = -1
if currNum != -1:
return currNum > prevNum
return True
|
check-if-numbers-are-ascending-in-a-sentence
|
Python Solution without Regex or Split | O(n) time, O(1) space
|
kcstar
| 0
| 2
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,386
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/2657272/Python-3-liner-easy-peasy-solution-with-explanation
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
#Get rid of any spaces and put them in a list
str_list = s.split()
# Filter out the numeric and non numeric characters in a list
new_list = [int(i) for i in str_list if i.isnumeric()]
# By default return false. Return true only if :
# 1. There are at least 2 numeric elements in the list
# 2. No number repeats itself
# 3. The numbers in the list are in sorted in increasing order
return [True if len(set(new_list)) > 1 and len(set(new_list)) == len(new_list) and new_list == sorted(new_list) else False][0]
|
check-if-numbers-are-ascending-in-a-sentence
|
Python 3 liner easy peasy solution with explanation
|
code_snow
| 0
| 9
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,387
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/2569535/Solution
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
arr = []
c1 = 0
c2 = 0
s = s.split()
for i in range(len(s)):
if s[i].isalpha():
continue
else:
c2 = int(s[i])
if c1 >= c2: return False
c1 = c2
return True
|
check-if-numbers-are-ascending-in-a-sentence
|
Solution
|
fiqbal997
| 0
| 7
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,388
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/2384976/Easy-python3-fast-solution
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
prev=-1
i=0
l=len(s)
num=""
while i < l:
if s[i].isdigit():
num+=s[i]
if (i!=l-1) and (s[i+1].isdigit()):
pass
else:
if int(num) > int(prev):
prev=num
else:
return False
else:
num=""
i+=1
return True
|
check-if-numbers-are-ascending-in-a-sentence
|
Easy [python3] fast solution
|
sunakshi132
| 0
| 20
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,389
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/2302978/Simple-Python3-Solution
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
nums = []
s_list = s.split()
for word in s_list:
if word.isdigit():
nums.append(word)
for i in range(1, len(nums)):
if int(nums[i-1]) >= int(nums[i]):
return False
return True
|
check-if-numbers-are-ascending-in-a-sentence
|
Simple Python3 Solution
|
vem5688
| 0
| 31
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,390
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/2260014/faster-than-40-or-python3
|
'''class Solution:
def areNumbersAscending(self, s: str) -> bool:
lst=[]
c=[]
ans=False
l=list(s.split(' '))
for i in l:
if i.isdigit():
lst.append(int(i))
c.append(int(i))
if len(set(lst))==len(lst): #check for strictly increasing order
lst.sort()
if lst==c:
ans=True
return ans
print(areNumbersAscending(s))'''
|
check-if-numbers-are-ascending-in-a-sentence
|
faster than 40% | python3
|
keertika27
| 0
| 18
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,391
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/2090512/Simple-Python-Solution
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
words = s.split()
numbers = []
for i in words:
if i.isdigit():
numbers.append(int(i))
for i in range(len(numbers)-1):
if numbers[i] < numbers[i+1]:
continue
else:
return False
return True
|
check-if-numbers-are-ascending-in-a-sentence
|
Simple Python Solution
|
kn_vardhan
| 0
| 38
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,392
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/1956004/Python3-simple-solution
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
s = s.split()
c = -1
for i in s:
if c == -1 and i.isdigit():
c = int(i)
elif c != -1 and i.isdigit() and c >= int(i):
return False
elif c != -1 and i.isdigit() and c < int(i):
c = int(i)
return True
|
check-if-numbers-are-ascending-in-a-sentence
|
Python3 simple solution
|
EklavyaJoshi
| 0
| 26
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,393
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/1913003/Python-easy-to-read-and-understand
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
words = s.split(" ")
prev = 0
for word in words:
if word.isnumeric():
if int(word) <= prev:
return False
prev = int(word)
return True
|
check-if-numbers-are-ascending-in-a-sentence
|
Python easy to read and understand
|
sanial2001
| 0
| 40
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,394
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/1902096/Python-Clean-and-Concise!
|
class Solution:
def areNumbersAscending(self, s):
nums = list(map(int, filter(str.isdigit, s.split())))
return all(i < j for i,j in zip(nums, nums[1:]))
|
check-if-numbers-are-ascending-in-a-sentence
|
Python - Clean and Concise!
|
domthedeveloper
| 0
| 28
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,395
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/1902096/Python-Clean-and-Concise!
|
class Solution:
def areNumbersAscending(self, s):
nums = [int(i) for i in s.split() if i.isdigit()]
return all(i < j for i,j in zip(nums, nums[1:]))
|
check-if-numbers-are-ascending-in-a-sentence
|
Python - Clean and Concise!
|
domthedeveloper
| 0
| 28
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,396
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/1902096/Python-Clean-and-Concise!
|
class Solution:
def areNumbersAscending(self, s):
return (lambda nums : all(i < j for i,j in zip(nums, nums[1:])))([int(i) for i in s.split() if i.isdigit()])
|
check-if-numbers-are-ascending-in-a-sentence
|
Python - Clean and Concise!
|
domthedeveloper
| 0
| 28
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,397
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/1883280/Python-dollarolution-(95-Faster)
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
s = s.split(' ')
x = -1
for i in s:
if i.isnumeric():
if int(i) > x:
x = int(i)
continue
else:
return False
return True
|
check-if-numbers-are-ascending-in-a-sentence
|
Python $olution (95% Faster)
|
AakRay
| 0
| 32
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,398
|
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/1815282/Python-Solution-with-2-ways
|
class Solution:
def areNumbersAscending(self, s: str) -> bool:
a = s.split()
res = []
for item in a:
if item.isdigit():
if int(item) in res:
return False
res.append(int(item))
temp = [str(x) for x in res]
res.sort()
out = [str(x) for x in res]
return out == temp
|
check-if-numbers-are-ascending-in-a-sentence
|
Python Solution with 2 ways
|
iamamirhossein
| 0
| 43
|
check if numbers are ascending in a sentence
| 2,042
| 0.661
|
Easy
| 28,399
|
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.