Skip to content

Latest commit

 

History

History
53 lines (46 loc) · 1.3 KB

1599.经营摩天轮的最大利润.md

File metadata and controls

53 lines (46 loc) · 1.3 KB

1599.经营摩天轮的最大利润

/*
 * @lc app=leetcode.cn id=1599 lang=typescript
 *
 * [1599] 经营摩天轮的最大利润
 */

// @lc code=start
function minOperationsMaxProfit(customers: number[], boardingCost: number, runningCost: number): number {}
// @lc code=end

解法 1: 模拟

function minOperationsMaxProfit(customers: number[], boardingCost: number, runningCost: number): number {
  const n = customers.length
  let res = -1,
    cnt = 0,
    cur = 0,
    rem = 0
  for (let i = 0; i < n || rem; i++) {
    if (i < n) rem += customers[i]
    cur -= runningCost
    const d = Math.min(4, rem)
    rem -= d
    cur += d * boardingCost
    if (cur > cnt) {
      cnt = cur
      res = i + 1
    }
  }

  return res
}

Case

test.each([
  { input: { customers: [8, 3], boardingCost: 5, runningCost: 6 }, output: 3 },
  { input: { customers: [10, 9, 6], boardingCost: 6, runningCost: 4 }, output: 7 },
  { input: { customers: [3, 4, 0, 5, 1], boardingCost: 1, runningCost: 92 }, output: -1 },
])(
  'input: customers = $input.customers, boardingCost = $input.boardingCost, runningCost = $input.runningCost',
  ({ input: { customers, boardingCost, runningCost }, output }) => {
    expect(minOperationsMaxProfit(customers, boardingCost, runningCost)).toEqual(output)
  },
)