Skip to content

Latest commit

 

History

History
63 lines (57 loc) · 1.1 KB

1817.查找用户活跃分钟数.md

File metadata and controls

63 lines (57 loc) · 1.1 KB

1817.查找用户活跃分钟数

/*
 * @lc app=leetcode.cn id=1817 lang=typescript
 *
 * [1817] 查找用户活跃分钟数
 */

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

解法 1: 哈希表

function findingUsersActiveMinutes(logs: number[][], k: number): number[] {
  const cnt = new Map<number, Set<number>>()
  for (let [id, t] of logs) {
    if (!cnt.has(id)) cnt.set(id, new Set())
    cnt.get(id)!.add(t)
  }
  const res = new Array(k).fill(0)
  for (let [id, ts] of cnt) {
    if (ts.size <= k) res[ts.size - 1]++
  }
  return res
}

Case

test.each([
  {
    input: {
      logs: [
        [0, 5],
        [1, 2],
        [0, 2],
        [0, 5],
        [1, 3],
      ],
      k: 5,
    },
    output: [0, 2, 0, 0, 0],
  },
  {
    input: {
      logs: [
        [1, 1],
        [2, 2],
        [2, 3],
      ],
      k: 4,
    },
    output: [1, 1, 0, 0],
  },
])('input: logs = $input.logs, k = $input.k', ({ input: { logs, k }, output }) => {
  expect(findingUsersActiveMinutes(logs, k)).toEqual(output)
})