-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
114 lines (98 loc) · 2.7 KB
/
index.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) 2023 by Shakhbozbek Usmonov Miracle Programmer.
// leetcode.com 30 Days of JavaScript challenge
// ----- Started -----
/**
* @param {Function} fn
*/
function memoize(fn) {
const cache = {};
return function (...args) {
const key = JSON.stringify(args);
if (key in cache) {
return cache[key];
}
const result = fn.apply(this, args);
cache[key] = result;
return result;
};
}
/**
* let callCount = 0;
* const memoizedFn = memoize(function (a, b) {
* callCount += 1;
* return a + b;
* })
* memoizedFn(2, 3) // 5
* memoizedFn(2, 3) // 5
* console.log(callCount) // 1
*/
// --------------------------------
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function (s) {
// Store counters for the biggest string, the start of the window, and the current letter's position (end of window)
let longestStringLength = 0,
startOfWindow = 0,
currentPosition = 0;
// Initialise a Set to store the characters
let characterSet = new Set();
// Loop through the provided string
while (currentPosition < s.length) {
// Check if the current character exists in the Set
if (characterSet.has(s[currentPosition])) {
// If so, delete it from the Set (as it will be picked up on the next run), and move the window's start forwards
characterSet.delete(s[startOfWindow++]);
} else {
// If not, add the current character to the Set, and move the current character forwards
characterSet.add(s[currentPosition++]);
longestStringLength = Math.max(
longestStringLength,
characterSet.size
);
}
}
return longestStringLength;
};
// -----------------------------------------------------
/**
* @param {Function} fn
* @param {Array} args
* @param {number} t
* @return {Function}
*/
var cancellable = function (fn, args, t) {
let timerID = setTimeout(() => {
fn(...args);
}, t);
return function () {
clearTimeout(timerID);
};
};
/**
* const result = []
*
* const fn = (x) => x * 5
* const args = [2], t = 20, cancelT = 50
*
* const start = performance.now()
*
* const log = (...argsArr) => {
* const diff = Math.floor(performance.now() - start);
* result.push({"time": diff, "returned": fn(...argsArr))
* }
*
* const cancel = cancellable(log, args, t);
*
* const maxT = Math.max(t, cancelT)
*
* setTimeout(() => {
* cancel()
* }, cancelT)
*
* setTimeout(() => {
* console.log(result) // [{"time":20,"returned":10}]
* }, maxT + 15)
*/
// ----------------- END -----------------------