Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0a63669

Browse files
committed
feat(solution): Added solutions for problem 2620, 2634, 2635
1 parent 2975dfb commit 0a63669

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

1001-9999.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Problems 1001-9999 <!-- omit from toc -->
2+
3+
## Algorithms <!-- omit from toc -->
4+
5+
- [JavaScript/TypeScript](#javascripttypescript)
6+
7+
## JavaScript/TypeScript
8+
9+
| # | Title | Solution | Time | Space | Difficulty | Tag | Note|
10+
|-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----|
11+
|2620|[Counter](https://leetcode.com/problems/counter/)| [TypeScript](./TypeScript/2620-Counter.ts)| $O(1)$| $O(1)$|Easy||`Closure` </br>`Higher Order Function` </br> `Function Factory`|
12+
|2634|[Filter Elements From Array](https://leetcode.com/problems/filter-elements-from-array/)| [TypeScript](./TypeScript/2634-Filter_Elements_From_Array.ts)| $O(n)$| $O(n)$|Easy|||
13+
|2635|[Apply Transform Over Each Element in Array](https://leetcode.com/problems/apply-transform-over-each-element-in-array/)| [TypeScript](./TypeScript/2635-Apply_Transform_Over_Each_Element_In_Array.ts)| $O(n)$| $O(n)$|Easy||`Higher Order Function` </br> `Callback Function`|

TypeScript/2620-Counter.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// URL: https://leetcode.com/problems/counter/
2+
// Time Complexity: O(1)
3+
// Space Complexity: O(1)
4+
5+
// Solution: Create a closure function that returns a function that increments the counter by 1 each time it is called.
6+
function createCounter(n: number): () => number {
7+
let counter: number = n;
8+
9+
return function () {
10+
return counter++;
11+
};
12+
}
13+
14+
/**
15+
* const counter = createCounter(10)
16+
* counter() // 10
17+
* counter() // 11
18+
* counter() // 12
19+
*/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// URL: https://leetcode.com/problems/filter-elements-from-array/
2+
// Time Complexity: O(n)
3+
// Space Complexity: O(n)
4+
5+
type Fn = (n: number, i: number) => any;
6+
7+
function filter(arr: number[], fn: Fn): number[] {
8+
const tempArr: number[] = [];
9+
10+
arr.forEach((element, index) => {
11+
if (fn(element, index)) {
12+
tempArr.push(element);
13+
}
14+
});
15+
16+
return tempArr;
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// URL: https://leetcode.com/problems/apply-transform-over-each-element-in-array/
2+
// Time Complexity: O(n)
3+
// Space Complexity: O(n)
4+
5+
function map(arr: number[], fn: (n: number, i: number) => number): number[] {
6+
const transformedArr: number[] = [];
7+
8+
arr.forEach((num, index) => {
9+
transformedArr[index] = fn(num, index);
10+
});
11+
12+
return transformedArr;
13+
}

0 commit comments

Comments
 (0)