|
| 1 | +// Question Link: https://leetcode.com/problems/interval-cancellation/?envType=study-plan-v2&envId=30-days-of-javascript |
| 2 | +// Solution Link: https://leetcode.com/problems/interval-cancellation/solutions/5439620/easy-javascript-solution/ |
| 3 | + |
| 4 | +/* |
| 5 | +2725. Interval Cancellation |
| 6 | +
|
| 7 | +Given a function fn, an array of arguments args, and an interval time t, return a cancel function cancelFn. |
| 8 | +After a delay of cancelTimeMs, the returned cancel function cancelFn will be invoked. |
| 9 | +setTimeout(cancelFn, cancelTimeMs) |
| 10 | +The function fn should be called with args immediately and then called again every t milliseconds until cancelFn is called at cancelTimeMs ms. |
| 11 | +
|
| 12 | +Example 1: |
| 13 | +Input: fn = (x) => x * 2, args = [4], t = 35 |
| 14 | +Output: |
| 15 | +[ |
| 16 | + {"time": 0, "returned": 8}, |
| 17 | + {"time": 35, "returned": 8}, |
| 18 | + {"time": 70, "returned": 8}, |
| 19 | + {"time": 105, "returned": 8}, |
| 20 | + {"time": 140, "returned": 8}, |
| 21 | + {"time": 175, "returned": 8} |
| 22 | +] |
| 23 | +Explanation: |
| 24 | +const cancelTimeMs = 190; |
| 25 | +const cancelFn = cancellable((x) => x * 2, [4], 35); |
| 26 | +setTimeout(cancelFn, cancelTimeMs); |
| 27 | +Every 35ms, fn(4) is called. Until t=190ms, then it is cancelled. |
| 28 | +1st fn call is at 0ms. fn(4) returns 8. |
| 29 | +2nd fn call is at 35ms. fn(4) returns 8. |
| 30 | +3rd fn call is at 70ms. fn(4) returns 8. |
| 31 | +4th fn call is at 105ms. fn(4) returns 8. |
| 32 | +5th fn call is at 140ms. fn(4) returns 8. |
| 33 | +6th fn call is at 175ms. fn(4) returns 8. |
| 34 | +Cancelled at 190ms |
| 35 | +
|
| 36 | +Example 2: |
| 37 | +Input: fn = (x1, x2) => (x1 * x2), args = [2, 5], t = 30 |
| 38 | +Output: |
| 39 | +[ |
| 40 | + {"time": 0, "returned": 10}, |
| 41 | + {"time": 30, "returned": 10}, |
| 42 | + {"time": 60, "returned": 10}, |
| 43 | + {"time": 90, "returned": 10}, |
| 44 | + {"time": 120, "returned": 10}, |
| 45 | + {"time": 150, "returned": 10} |
| 46 | +] |
| 47 | +Explanation: |
| 48 | +const cancelTimeMs = 165; |
| 49 | +const cancelFn = cancellable((x1, x2) => (x1 * x2), [2, 5], 30) |
| 50 | +setTimeout(cancelFn, cancelTimeMs) |
| 51 | +Every 30ms, fn(2, 5) is called. Until t=165ms, then it is cancelled. |
| 52 | +1st fn call is at 0ms |
| 53 | +2nd fn call is at 30ms |
| 54 | +3rd fn call is at 60ms |
| 55 | +4th fn call is at 90ms |
| 56 | +5th fn call is at 120ms |
| 57 | +6th fn call is at 150ms |
| 58 | +Cancelled at 165ms |
| 59 | +
|
| 60 | +Example 3: |
| 61 | +Input: fn = (x1, x2, x3) => (x1 + x2 + x3), args = [5, 1, 3], t = 50 |
| 62 | +Output: |
| 63 | +[ |
| 64 | + {"time": 0, "returned": 9}, |
| 65 | + {"time": 50, "returned": 9}, |
| 66 | + {"time": 100, "returned": 9}, |
| 67 | + {"time": 150, "returned": 9} |
| 68 | +] |
| 69 | +Explanation: |
| 70 | +const cancelTimeMs = 180; |
| 71 | +const cancelFn = cancellable((x1, x2, x3) => (x1 + x2 + x3), [5, 1, 3], 50) |
| 72 | +setTimeout(cancelFn, cancelTimeMs) |
| 73 | +Every 50ms, fn(5, 1, 3) is called. Until t=180ms, then it is cancelled. |
| 74 | +1st fn call is at 0ms |
| 75 | +2nd fn call is at 50ms |
| 76 | +3rd fn call is at 100ms |
| 77 | +4th fn call is at 150ms |
| 78 | +Cancelled at 180ms |
| 79 | + |
| 80 | +Constraints: |
| 81 | +fn is a function |
| 82 | +args is a valid JSON array |
| 83 | +1 <= args.length <= 10 |
| 84 | +30 <= t <= 100 |
| 85 | +10 <= cancelTimeMs <= 500 |
| 86 | +*/ |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +/** |
| 91 | + * @param {Function} fn |
| 92 | + * @param {Array} args |
| 93 | + * @param {number} t |
| 94 | + * @return {Function} |
| 95 | + */ |
| 96 | + |
| 97 | +var cancellable = function(fn, args, t) { |
| 98 | + fn(...args); |
| 99 | + |
| 100 | + const timeOver = setInterval(() => fn(...args), t); |
| 101 | + const cancelFn = () => clearInterval(timeOver); |
| 102 | + |
| 103 | + return cancelFn; |
| 104 | +}; |
| 105 | + |
| 106 | +/** |
| 107 | + * const result = []; |
| 108 | + * |
| 109 | + * const fn = (x) => x * 2; |
| 110 | + * const args = [4], t = 35, cancelTimeMs = 190; |
| 111 | + * |
| 112 | + * const start = performance.now(); |
| 113 | + * |
| 114 | + * const log = (...argsArr) => { |
| 115 | + * const diff = Math.floor(performance.now() - start); |
| 116 | + * result.push({"time": diff, "returned": fn(...argsArr)}); |
| 117 | + * } |
| 118 | + * |
| 119 | + * const cancel = cancellable(log, args, t); |
| 120 | + * |
| 121 | + * setTimeout(cancel, cancelTimeMs); |
| 122 | + * |
| 123 | + * setTimeout(() => { |
| 124 | + * console.log(result); // [ |
| 125 | + * // {"time":0,"returned":8}, |
| 126 | + * // {"time":35,"returned":8}, |
| 127 | + * // {"time":70,"returned":8}, |
| 128 | + * // {"time":105,"returned":8}, |
| 129 | + * // {"time":140,"returned":8}, |
| 130 | + * // {"time":175,"returned":8} |
| 131 | + * // ] |
| 132 | + * }, cancelTimeMs + t + 15) |
| 133 | + */ |
0 commit comments