Skip to content

Commit 42d9ad3

Browse files
committed
add 2619, 2620, 2623, 2626
1 parent 3743a6c commit 42d9ad3

4 files changed

+98
-0
lines changed

2619-array-prototype-last.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
2619. Array Prototype Last
3+
4+
Submitted: January 14, 2025
5+
6+
Runtime: 47 ms (beats 75.77%)
7+
Memory: 48.37 MB (beats 88.52%)
8+
*/
9+
10+
/**
11+
* @return {null|boolean|number|string|Array|Object}
12+
*/
13+
Array.prototype.last = function() {
14+
return this.length == 0 ? -1 : this[this.length - 1];
15+
}
16+
17+
/**
18+
* const arr = [1, 2, 3];
19+
* arr.last(); // 3
20+
*/

2620-counter.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
2620. Counter
3+
4+
Submitted: January 14, 2025
5+
6+
Runtime: 36 ms (beats 99.12%)
7+
Memory: 48.94% (beats 48.41%)
8+
*/
9+
10+
/**
11+
* @param {number} n
12+
* @return {Function} counter
13+
*/
14+
var createCounter = function(n) {
15+
return function() {
16+
return n++;
17+
};
18+
};
19+
20+
/**
21+
* const counter = createCounter(10)
22+
* counter() // 10
23+
* counter() // 11
24+
* counter() // 12
25+
*/

2623-memoize.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
2623. Memoize
3+
4+
Submitted: January 14, 2025
5+
6+
Runtime: 357 ms (beats 10.01%)
7+
Memory: 87.11 MB (beats 68.94%)
8+
*/
9+
10+
/**
11+
* @param {Function} fn
12+
* @return {Function}
13+
*/
14+
function memoize(fn) {
15+
let cache = {}
16+
return function(...args) {
17+
if (args in cache) return cache[args];
18+
else return (cache[args] = fn(...args));
19+
}
20+
}
21+
22+
23+
/**
24+
* let callCount = 0;
25+
* const memoizedFn = memoize(function (a, b) {
26+
* callCount += 1;
27+
* return a + b;
28+
* })
29+
* memoizedFn(2, 3) // 5
30+
* memoizedFn(2, 3) // 5
31+
* console.log(callCount) // 1
32+
*/

2626-array-reduce-transformation.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
2626. Array Reduce Transformation
3+
4+
Submitted: January 14, 2025
5+
6+
Runtime: 48 ms (beats 86.37%)
7+
Memory: 50.66 MB (beats 8.79%)
8+
*/
9+
10+
/**
11+
* @param {number[]} nums
12+
* @param {Function} fn
13+
* @param {number} init
14+
* @return {number}
15+
*/
16+
var reduce = function(nums, fn, init) {
17+
for (const n of nums) {
18+
init = fn(init, n);
19+
}
20+
return init;
21+
};

0 commit comments

Comments
 (0)