LeetCode 题解工作台

间隔取消

现给定一个函数 fn ,一个参数数组 args 和一个时间间隔 t ,返回一个取消函数 cancelFn 。 在经过 cancelTimeMs 毫秒的延迟后,将调用返回的取消函数 cancelFn 。 setTimeout(cancelFn, cancelTimeMs) 函数 fn 应立即使用参数 …

category

0

题型

code_blocks

1

代码语言

hub

0

相关题

当前训练重点

简单 · 区间·cancellation·core·interview·pattern

bolt

答案摘要

function cancellable(fn: Function, args: any[], t: number): Function { fn(...args);

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 区间·cancellation·core·interview·pattern 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

现给定一个函数 fn,一个参数数组 args 和一个时间间隔 t,返回一个取消函数 cancelFn

在经过 cancelTimeMs 毫秒的延迟后,将调用返回的取消函数 cancelFn

setTimeout(cancelFn, cancelTimeMs)

函数 fn 应立即使用参数 args 调用,然后每隔 t 毫秒调用一次,直到在 cancelTimeMs 毫秒时调用 cancelFn

 

示例 1:

输入:fn = (x) => x * 2, args = [4], t = 35, cancelT = 190
输出:
[
   {"time": 0, "returned": 8},
   {"time": 35, "returned": 8},
   {"time": 70, "returned": 8},
   {"time": 105, "returned": 8},
   {"time": 140, "returned": 8},
   {"time": 175, "returned": 8}
]
解释: 
const cancelTimeMs = 190;
const cancelFn = cancellable((x) => x * 2, [4], 35);
setTimeout(cancelFn, cancelTimeMs);

每隔 35ms,调用 fn(4)。直到 t=190ms,然后取消。
第一次调用 fn 是在 0ms。fn(4) 返回 8。
第二次调用 fn 是在 35ms。fn(4) 返回 8。
第三次调用 fn 是在 70ms。fn(4) 返回 8。
第四次调用 fn 是在 105ms。fn(4) 返回 8。
第五次调用 fn 是在 140ms。fn(4) 返回 8。
第六次调用 fn 是在 175ms。fn(4) 返回 8。
在 t=190ms 时取消

示例 2:

输入:fn = (x1, x2) => (x1 * x2), args = [2, 5], t = 30, cancelT = 165
输出: 
[
   {"time": 0, "returned": 10},
   {"time": 30, "returned": 10},
   {"time": 60, "returned": 10},
   {"time": 90, "returned": 10},
   {"time": 120, "returned": 10},
   {"time": 150, "returned": 10}
]
解释:
const cancelTimeMs = 165; 
const cancelFn = cancellable((x1, x2) => (x1 * x2), [2, 5], 30) 
setTimeout(cancelFn, cancelTimeMs)

每隔 30ms,调用 fn(2, 5)。直到 t=165ms,然后取消。
第一次调用 fn 是在 0ms
第二次调用 fn 是在 30ms
第三次调用 fn 是在 60ms
第四次调用 fn 是在 90ms
第五次调用 fn 是在 120ms
第六次调用 fn 是在 150ms
在 165ms 取消

示例 3:

输入:fn = (x1, x2, x3) => (x1 + x2 + x3), args = [5, 1, 3], t = 50, cancelT = 180
输出:
[
   {"time": 0, "returned": 9},
   {"time": 50, "returned": 9},
   {"time": 100, "returned": 9},
   {"time": 150, "returned": 9}
]
解释:
const cancelTimeMs = 180;
const cancelFn = cancellable((x1, x2, x3) => (x1 + x2 + x3), [5, 1, 3], 50)
setTimeout(cancelFn, cancelTimeMs)

每隔 50ms,调用 fn(5, 1, 3)。直到 t=180ms,然后取消。
第一次调用 fn 是在 0ms
第二次调用 fn 是在 50ms
第三次调用 fn 是在 100ms
第四次调用 fn 是在 150ms
在 180ms 取消

 

提示:

  • fn 是一个函数
  • args 是一个有效的 JSON 数组
  • 1 <= args.length <= 10
  • 30 <= t <= 100
  • 10 <= cancelT <= 500
lightbulb

解题思路

方法一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function cancellable(fn: Function, args: any[], t: number): Function {
    fn(...args);
    const time = setInterval(() => fn(...args), t);
    return () => clearInterval(time);
}

/**
 *  const result = []
 *
 *  const fn = (x) => x * 2
 *  const args = [4], t = 20, cancelT = 110
 *
 *  const log = (...argsArr) => {
 *      result.push(fn(...argsArr))
 *  }
 *
 *  const cancel = cancellable(fn, args, t);
 *
 *  setTimeout(() => {
 *     cancel()
 *     console.log(result) // [
 *                         //      {"time":0,"returned":8},
 *                         //      {"time":20,"returned":8},
 *                         //      {"time":40,"returned":8},
 *                         //      {"time":60,"returned":8},
 *                         //      {"time":80,"returned":8},
 *                         //      {"time":100,"returned":8}
 *                         //  ]
 *  }, cancelT)
 */
speed

复杂度分析

指标
时间complexity is dominated by the interval calls, where each interval is triggered once every `t` milliseconds until cancellation. Space complexity is constant, as we're only maintaining a reference to the set interval and the cancel function.
空间**
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for correct handling of repeated execution and cancellation at the right moment.

  • question_mark

    Watch for the proper use of `setInterval` for periodic execution and `setTimeout` for cancellation.

  • question_mark

    Ensure that the solution works even for the edge cases involving small or large values for `t` and `cancelTimeMs`.

warning

常见陷阱

外企场景
  • error

    Failing to clear the interval properly can cause the function to continue executing beyond the cancellation time.

  • error

    Not accounting for edge cases where the cancellation time is too short for multiple executions.

  • error

    Incorrect handling of the cancel function could lead to premature or delayed cancellation of the repeated function calls.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Modify the function to allow multiple functions to be cancelled in sequence.

  • arrow_right_alt

    Introduce a delay before starting the repeated function calls, not just an immediate execution.

  • arrow_right_alt

    Add support for dynamic changes to the interval time during execution.

help

常见问题

外企场景

间隔取消题解:区间·cancellation·core·in… | LeetCode #2725 简单