Citadel OA 面试真题解析:按天处理图片的最小成本

22次阅读
没有评论

Devise a strategy to minimize the cost of processing n images, where each image requires specific filters applied for a defined time frame, and the cost to apply filters to the ith image is filterCost[i].

Each image must be processed from startDay[i] to endDay[i] (inclusive).

Additionally, there is an exclusive offer to apply filters to all n images at a discounted rate of discountPrice per day.

Your goal is to create an efficient image processing plan that adheres to time constraints and budget considerations, and return the minimum cost modulo 10^9 + 7.

Example

Given n = 3, filterCost = [2, 3, 4], startDay = [1, 1, 2], endDay = [2, 3, 4], and discountPrice = 6.

  • Day 1: images [1, 2], total cost = 2 + 3 = 5
  • Day 2: images [1, 2, 3], total cost = 2 + 3 + 4 = 9
  • Day 3: images [2, 3], total cost = 3 + 4 = 7
  • Day 4: images [3], total cost = 4

Applying filters on all images at 6 per day on the 2nd and 3rd day will be optimal. The final cost is 5 + 6 + 6 + 4 = 21.

这道题本质上是“区间覆盖 + 扫描线”的最小成本计算:每个图片在自己的时间区间内都会贡献固定费用,而全体图片的每日折扣价则等价于把当天所有图片的总成本和 discountPrice 取较小值。最优做法是把每个图片的开始日和结束日 +1 转成事件点,按时间排序后维护当前活跃图片的费用和,在相邻事件之间累加区间长度乘以 min(当前费用和, discountPrice)。这样可以在 O(n log n) 内完成,适合 n 很大的 OA 场景。

正文完
 0