aNotHeRNo0b
Senior Member
Q3 segment tree
cay lỏ thiệc. Tìm vị trí xa nhất có giá trị nhỏ hơn nums 
cay lỏ thiệc. Tìm vị trí xa nhất có giá trị nhỏ hơn nums 
cay lỏ thiệc. Tìm vị trí xa nhất có giá trị nhỏ hơn nums 
Q3 dùng binary seach + prefix sum nè bácQ3 segment treecay lỏ thiệc. Tìm vị trí xa nhất có giá trị nhỏ hơn nums
![]()
Ảo vậy fen, cho mình xin í tưởng được không, bí quá @@Q3 dùng binary seach + prefix sum nè bác
class Solution:
def maxValue(self, nums: List[int]) -> List[int]:
# Determine how far in can jump forward
prefix_max = [nums[0]]
curr = nums[0]
n = len(nums)
for num in nums[1:]:
if num > curr:
curr = num
prefix_max.append(curr)
prefix_min = [(n - 1, nums[-1])]
curr = nums[-1]
for i in range(n - 2, -1, -1):
if curr > nums[i]:
curr = nums[i]
prefix_min.append((i, curr))
prefix_min = prefix_min[::-1]
res = []
l_pm = len(prefix_min)
def find_jump(index, num):
result = index
l, r = 0, l_pm - 1
while l <= r:
mid = (l + r) // 2
if num > prefix_min[mid][1]:
result = prefix_min[mid][0]
l = mid + 1
else:
r = mid - 1
return result
# Do update res until no more update
# Hope this will end before TLE
res = [0] * n
changed = True
iteration = 0
while changed:
changed = False
updated_max = -1
new_res = [0] * n
new_prefix_max = []
for i, num in enumerate(nums):
jump_index = max(find_jump(i, num), i)
current_max = prefix_max[jump_index]
if i > 0:
current_max = max(current_max, res[i-1])
updated_max = max(current_max, updated_max)
new_prefix_max.append(updated_max)
new_res[i] = updated_max
if res[i] != new_res[i]:
changed = True
res = new_res[:]
prefix_max = new_prefix_max
return res©leetcode

class Solution:
def maxValue(self, nums: List[int]) -> List[int]:
n = len(nums)
largestFromTheLeft = [-inf]*n
indx = -1
for i in range(n):
if indx == -1:
indx = i
else:
if nums[i] > nums[indx]:
indx = i
largestFromTheLeft[i] = indx
stack = []
sl = SortedList()
ans = [-1]*n
for i in range(n - 1, -1, -1):
while stack and nums[largestFromTheLeft[i]] <= nums[stack[-1]]:
indx = stack.pop()
sl.remove(nums[largestFromTheLeft[indx]])
if sl:
ans[i] = max(nums[largestFromTheLeft[i]], sl[-1])
else:
ans[i] = nums[largestFromTheLeft[i]]
sl.add(nums[largestFromTheLeft[i]])
stack.append(i)
return ans
©leetcode
Em cũng có ý tưởng dùng monotoic nhưng mà lại chọn nhảy về min xa nhất rồi mới về max.Q3 của mình đây ae
Ý tưởng là ở mỗi index thì có thể nhảy về max phía trái, sau đó thì có thể nhảy về các index nhỏ hơn max phía trái để tìm ra số max.Python:class Solution: def maxValue(self, nums: List[int]) -> List[int]: n = len(nums) largestFromTheLeft = [-inf]*n indx = -1 for i in range(n): if indx == -1: indx = i else: if nums[i] > nums[indx]: indx = i largestFromTheLeft[i] = indx stack = [] sl = SortedList() ans = [-1]*n for i in range(n - 1, -1, -1): while stack and nums[largestFromTheLeft[i]] <= nums[stack[-1]]: indx = stack.pop() sl.remove(nums[largestFromTheLeft[indx]]) if sl: ans[i] = max(nums[largestFromTheLeft[i]], sl[-1]) else: ans[i] = nums[largestFromTheLeft[i]] sl.add(nums[largestFromTheLeft[i]]) stack.append(i) return ans ©leetcode
Mình đi ngược lại từ n - 1, dùng 1 monotonic stack để keep track tất cả các số nhỏ hơn max phía trái, và maintain thêm 1 cái sortedlist để lưu giá trị lớn nhất ở mỗi index về phía trái. Kết quả sẽ là max value trong cái sorted list cho mỗi index.
Bác tự host cái riêng cho ae vozer điThằng clist.by crawl bảng rank lâu nhỉ, chắc infra không đủ mạnh nên kéo khá lâu 30 phút mà mới hơn 3500 record
e cũng đợi nãy hBác tự host cái riêng cho ae vozer đie cũng đợi nãy h
nhìn không ra tức là học gạo. pattern solver rĐể ý gần đây Leetcode toàn cho ra mấy câu Dp mà ko nhìn ra là Dp nhé, khá khoai nhưng hay đỡ ngán![]()
via theNEXTvoz for iPhone
