defmaxValue(self, events: List[List[int]], k: int) -> int: events.sort(key=lambda x: x[0]) prev = [0] * len(events) for i inrange(len(prev)): j = i while j < len(prev) and events[j][0] <= events[i][1]: j += 1 prev[i] = j dp = [[0] * len(events) for _ inrange(k + 1)] for i inrange(1, len(dp)): dp[i][-1] = events[-1][2] for i inrange(1, k + 1): for j inrange(len(dp[0]) - 2, -1, -1): value = events[j][2] index = prev[j] if index < len(prev): value += dp[i-1][index] dp[i][j] = max(dp[i][j+1], value) return dp[k][0]
defsubarraysWithKDistinct(self, A: List[int], K: int) -> int: left, right = 0, 0 window = {} bingo = -1 ans = 0 while right < len(A): # add A[right] to window if A[right] in window: window[A[right]].append(right) else: window[A[right]] = deque([right]) iflen(window) < K: right += 1 eliflen(window) == K: ans += 1 if bingo == -1: bingo = right right += 1 else: iflen(window[A[left]]) == 1: bingo = right right += 1 window.pop(A[left]) else: bingo = max(bingo, window[A[left]][1]) window.pop(A[right]) window[A[left]].popleft() ans += right - bingo left += 1 # print(ans) if bingo == -1: return ans while left < right: iflen(window[A[left]]) == 1: break window[A[left]].popleft() bingo = max(bingo, window[A[left]][0]) ans += right - bingo left += 1 return ans