Q2 chép 1 bài đã từng làm vào làm helper có tính là cheat ko nhỉ

Contest này k hủy thì hơi phí, bữa trước t làm full 4 bài bị hủy đây,Đm giải 3Q trong 15ph, huỷ chắc tức điên nha


class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
ret = abs(nums[0] - k)
pre = {nums[0]}
for x in nums:
next_set = {x}
ret = min(ret, abs(x - k))
for y in pre:
next_set.add(y & x)
ret = min(ret, abs((y & x) - k))
pre = next_set
return ret
t cũng ăn 2 bọ câu đó, cũng chỗ merge interval. Mà nay làm mãi mới xong. Đêm qua thức coi C1, sáng nay hơi ngáo,Má nó câu 2 ăn 2 bọ do merge intervals ngáo, cay thật chứ nhỉ
1 lần đặt lộn dấu 1 lần đặt lộn end.

Ủa lưu cả đống prefix vô set được hả fence, ảo thế nhỉ.Bài 4 t dùng set để tính prefix and. Hết contest mới làm xong,
Python:class Solution: def minimumDifference(self, nums: List[int], k: int) -> int: ret = abs(nums[0] - k) pre = {nums[0]} for x in nums: next_set = {x} ret = min(ret, abs(x - k)) for y in pre: next_set.add(y & x) ret = min(ret, abs((y & x) - k)) pre = next_set return ret
tụi nó đi cái DP đơn giản vãi cứcb4 e cũng làm sliding window, chưa biết dp thế nàoỦa lưu cả đống prefix vô set được hả fence, ảo thế nhỉ.
Má còn non quá hèn gì ko làm ra, tụi kia nó dùng dp 2 states giải cũng ra
Cứ tưởng TLE nên phải làm sliding windows
Tại vì cái AND ấy, nên nó sẽ bị trùng rất nhiều.Ủa lưu cả đống prefix vô set được hả fence, ảo thế nhỉ.
Má còn non quá hèn gì ko làm ra, tụi kia nó dùng dp 2 states giải cũng ra
Cứ tưởng TLE nên phải làm sliding windows
Mình sliding windows bị TLE còn 10 test cases cuối, tụi nó giải DP đây fence. Vì & opeartion nó bị trùng nhiều thì phải, ko hiểu saob4 e cũng làm sliding window, chưa biết dp thế nào
from functools import cache
class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
n = len(nums)
# Recursive funtion with the current index and past bitwise AND value
@cache
def solve(idx, curr_and):
# If index reached at the end simply pass inf
if idx >= n:
return inf
# No need to check the other case since if we include curr_and it will always give us 0 value
if curr_and & nums[idx] == 0:
return min(abs(0-k), abs(nums[idx]-k), solve(idx+1, nums[idx]))
return min(abs((curr_and & nums[idx]) - k), abs(nums[idx]-k), solve(idx+1, nums[idx]), solve(idx+1, curr_and&nums[idx]))
return solve(0, nums[0])
e làm bruteforce TLE có 7 testcase thôi mà sliding windows những 10 tc à bácMình sliding windows bị TLE còn 10 test cases cuối, tụi nó giải DP đây fence. Vì & opeartion nó bị trùng nhiều thì phải, ko hiểu sao
Python:from functools import cache class Solution: def minimumDifference(self, nums: List[int], k: int) -> int: n = len(nums) # Recursive funtion with the current index and past bitwise AND value @cache def solve(idx, curr_and): # If index reached at the end simply pass inf if idx >= n: return inf # No need to check the other case since if we include curr_and it will always give us 0 value if curr_and & nums[idx] == 0: return min(abs(0-k), abs(nums[idx]-k), solve(idx+1, nums[idx])) return min(abs((curr_and & nums[idx]) - k), abs(nums[idx]-k), solve(idx+1, nums[idx]), solve(idx+1, curr_and&nums[idx])) return solve(0, nums[0])

, quá đơn giản mà ko nghĩ ra