Skip to content

Latest commit

 

History

History
77 lines (71 loc) · 1.15 KB

2432.处理用时最长的那个任务的员工.md

File metadata and controls

77 lines (71 loc) · 1.15 KB

2432.处理用时最长的那个任务的员工

/*
 * @lc app=leetcode.cn id=2432 lang=typescript
 *
 * [2432] 处理用时最长的那个任务的员工
 */

// @lc code=start
function hardestWorker(n: number, logs: number[][]): number {}
// @lc code=end

解法 1: 枚举

function hardestWorker(n: number, logs: number[][]): number {
  let pre = 0,
    res = Infinity,
    max = -Infinity
  for (let [id, t] of logs) {
    const d = t - pre
    if (d > max) {
      max = d
      res = id
    } else if (d === max && id < res) {
      res = id
    }
    pre = t
  }
  return res
}

Case

test.each([
  {
    input: {
      n: 10,
      logs: [
        [0, 3],
        [2, 5],
        [0, 9],
        [1, 15],
      ],
    },
    output: 1,
  },
  {
    input: {
      n: 26,
      logs: [
        [1, 1],
        [3, 7],
        [2, 12],
        [7, 17],
      ],
    },
    output: 3,
  },
  {
    input: {
      n: 2,
      logs: [
        [0, 10],
        [1, 20],
      ],
    },
    output: 0,
  },
])('input: n = $input.n, logs = $input.logs', ({ input: { n, logs }, output }) => {
  expect(hardestWorker(n, logs)).toEqual(output)
})