Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f82f120

Browse files
authored
Added tasks 2624-2629
1 parent e0823c0 commit f82f120

File tree

15 files changed

+502
-0
lines changed

15 files changed

+502
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2624\. Snail Traversal
2+
3+
Medium
4+
5+
Write code that enhances all arrays such that you can call the `snail(rowsCount, colsCount)` method that transforms the 1D array into a 2D array organised in the pattern known as **snail traversal order**. Invalid input values should output an empty array. If `rowsCount * colsCount !== nums.length`, the input is considered invalid.
6+
7+
**Snail traversal order**starts at the top left cell with the first value of the current array. It then moves through the entire first column from top to bottom, followed by moving to the next column on the right and traversing it from bottom to top. This pattern continues, alternating the direction of traversal with each column, until the entire current array is covered. For example, when given the input array `[19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]` with `rowsCount = 5` and `colsCount = 4`, the desired output matrix is shown below. Note that iterating the matrix following the arrows corresponds to the order of numbers in the original array.
8+
9+
![Traversal Diagram](https://assets.leetcode.com/uploads/2023/04/10/screen-shot-2023-04-10-at-100006-pm.png)
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15] rowsCount = 5 colsCount = 4
14+
15+
**Output:**
16+
17+
[
18+
[19,17,16,15],
19+
[10,1,14,4],
20+
[3,2,12,20],
21+
[7,5,18,11],
22+
[9,8,6,13]
23+
]
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [1,2,3,4] rowsCount = 1 colsCount = 4
28+
29+
**Output:** [[1, 2, 3, 4]]
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [1,3] rowsCount = 2 colsCount = 2
34+
35+
**Output:** []
36+
37+
**Explanation:** 2 multiplied by 2 is 4, and the original array [1,3] has a length of 2; therefore, the input is invalid.
38+
39+
**Constraints:**
40+
41+
* `0 <= nums.length <= 250`
42+
* `1 <= nums[i] <= 1000`
43+
* `1 <= rowsCount <= 250`
44+
* `1 <= colsCount <= 250`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// #Medium #2023_08_31_Time_175_ms_(92.96%)_Space_64.2_MB_(32.75%)
2+
3+
declare global {
4+
interface Array<T> {
5+
snail(rowsCount: number, colsCount: number): number[][]
6+
}
7+
}
8+
9+
Array.prototype.snail = function (rowsCount: number, colsCount: number): number[][] { //NOSONAR
10+
if (rowsCount * colsCount !== this.length) return []
11+
let res: number[][] = []
12+
for (let i = 0; i < this.length; i++) {
13+
let col = Math.floor(i / rowsCount)
14+
let row = i % rowsCount
15+
row = col % 2 === 0 ? row : rowsCount - row - 1
16+
if (res[row] === undefined) res[row] = []
17+
res[row].push(this[i])
18+
}
19+
return res
20+
}
21+
22+
/*
23+
* const arr = [1,2,3,4];
24+
* arr.snail(1,4); // [[1,2,3,4]]
25+
*/
26+
27+
export {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2625\. Flatten Deeply Nested Array
2+
3+
Medium
4+
5+
Given a **multi-dimensional** array `arr` and a depth `n`, return a **flattened** version of that array.
6+
7+
A **multi-dimensional** array is a recursive data structure that contains integers or other **multi-dimensional** arrays.
8+
9+
A **flattened** array is a version of that array with some or all of the sub-arrays removed and replaced with the actual elements in that sub-array. This flattening operation should only be done if the current depth of nesting is less than `n`. The depth of the elements in the first array are considered to be `0`.
10+
11+
Please solve it without the built-in `Array.flat` method.
12+
13+
**Example 1:**
14+
15+
**Input** arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]] n = 0
16+
17+
**Output:** [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
18+
19+
**Explanation:** Passing a depth of n=0 will always result in the original array. This is because the smallest possible depth of a subarray (0) is not less than n=0. Thus, no subarray should be flattened.
20+
21+
**Example 2:**
22+
23+
**Input** arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]] n = 1
24+
25+
**Output:** [1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]
26+
27+
**Explanation:** The subarrays starting with 4, 7, and 13 are all flattened. This is because their depth of 0 is less than 1. However [9, 10, 11] remains unflattened because its depth is 1.
28+
29+
**Example 3:**
30+
31+
**Input** arr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]] n = 2
32+
33+
**Output:** [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
34+
35+
**Explanation:** The maximum depth of any subarray is 1. Thus, all of them are flattened.
36+
37+
**Constraints:**
38+
39+
* <code>0 <= count of numbers in arr <= 10<sup>5</sup></code>
40+
* <code>0 <= count of subarrays in arr <= 10<sup>5</sup></code>
41+
* `maxDepth <= 1000`
42+
* `-1000 <= each number <= 1000`
43+
* `0 <= n <= 1000`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// #Medium #2023_08_31_Time_84_ms_(98.71%)_Space_61.8_MB_(94.52%)
2+
3+
type MultiDimensionalArray = (number | MultiDimensionalArray)[]
4+
5+
const flat = (arr: MultiDimensionalArray, depth: number): MultiDimensionalArray => {
6+
const result: MultiDimensionalArray = []
7+
8+
const flatten = (nestedArray: MultiDimensionalArray, currentDepth: number) => {
9+
for (const element of nestedArray) {
10+
if (Array.isArray(element) && currentDepth > 0 && currentDepth <= depth) {
11+
flatten(element, currentDepth - 1)
12+
} else {
13+
result.push(element)
14+
}
15+
}
16+
}
17+
18+
flatten(arr, depth)
19+
return result
20+
}
21+
22+
export { flat }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2626\. Array Reduce Transformation
2+
3+
Easy
4+
5+
Given an integer array `nums`, a reducer function `fn`, and an initial value `init`, return a **reduced** array.
6+
7+
A **reduced** array is created by applying the following operation: `val = fn(init, nums[0])`, `val = fn(val, nums[1])`, `val = fn(val, nums[2])`, `...` until every element in the array has been processed. The final value of `val` is returned.
8+
9+
If the length of the array is 0, it should return `init`.
10+
11+
Please solve it without using the built-in `Array.reduce` method.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,3,4] fn = function sum(accum, curr) { return accum + curr; } init = 0
16+
17+
**Output:** 10
18+
19+
**Explanation:** initially, the value is init=0.
20+
21+
(0) + nums[0] = 1
22+
23+
(1) + nums[1] = 3
24+
25+
(3) + nums[2] = 6
26+
27+
(6) + nums[3] = 10
28+
29+
The final answer is 10.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [1,2,3,4] fn = function sum(accum, curr) { return accum + curr \* curr; } init = 100
34+
35+
**Output:** 130
36+
37+
**Explanation:** initially, the value is init=100.
38+
39+
(100) + nums[0]^2 = 101
40+
41+
(101) + nums[1]^2 = 105
42+
43+
(105) + nums[2]^2 = 114
44+
45+
(114) + nums[3]^2 = 130
46+
47+
The final answer is 130.
48+
49+
**Example 3:**
50+
51+
**Input:** nums = [] fn = function sum(accum, curr) { return 0; } init = 25
52+
53+
**Output:** 25
54+
55+
**Explanation:** For empty arrays, the answer is always init.
56+
57+
**Constraints:**
58+
59+
* `0 <= nums.length <= 1000`
60+
* `0 <= nums[i] <= 1000`
61+
* `0 <= init <= 1000`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// #Easy #2023_08_31_Time_52_ms_(91.40%)_Space_44.2_MB_(82.03%)
2+
3+
type Fn = (accum: number, curr: number) => number
4+
5+
function reduce(nums: number[], fn: Fn, init: number): number {
6+
let accumulator = init
7+
nums.forEach((num) => {
8+
accumulator = fn(accumulator, num)
9+
})
10+
return accumulator
11+
}
12+
13+
export { reduce }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2627\. Debounce
2+
3+
Medium
4+
5+
Given a function `fn` and a time in milliseconds `t`, return a **debounced** version of that function.
6+
7+
A **debounced** function is a function whose execution is delayed by `t` milliseconds and whose execution is cancelled if it is called again within that window of time. The debounced function should also receive the passed parameters.
8+
9+
For example, let's say `t = 50ms`, and the function was called at `30ms`, `60ms`, and `100ms`. The first 2 function calls would be cancelled, and the 3rd function call would be executed at `150ms`. If instead `t = 35ms`, The 1st call would be cancelled, the 2nd would be executed at `95ms`, and the 3rd would be executed at `135ms`.
10+
11+
![Debounce Schematic](https://assets.leetcode.com/uploads/2023/04/08/screen-shot-2023-04-08-at-11048-pm.png)
12+
13+
The above diagram shows how debounce will transform events. Each rectangle represents 100ms and the debounce time is 400ms. Each color represents a different set of inputs.
14+
15+
Please solve it without using lodash's `_.debounce()` function.
16+
17+
**Example 1:**
18+
19+
**Input:** t = 50 calls = [ {"t": 50, inputs: [1]}, {"t": 75, inputs: [2]} ]
20+
21+
**Output:** [{"t": 125, inputs: [2]}]
22+
23+
**Explanation:**
24+
25+
let start = Date.now();
26+
27+
function log(...inputs) {
28+
console.log([Date.now() - start, inputs ])
29+
}
30+
const dlog = debounce(log, 50);
31+
setTimeout(() => dlog(1), 50);
32+
setTimeout(() => dlog(2), 75);
33+
34+
The 1st call is cancelled by the 2nd call because the 2nd call occurred before 100ms
35+
36+
The 2nd call is delayed by 50ms and executed at 125ms. The inputs were (2).
37+
38+
**Example 2:**
39+
40+
**Input:**
41+
42+
t = 20
43+
calls = [
44+
{"t": 50, inputs: [1]},
45+
{"t": 100, inputs: [2]}
46+
]
47+
48+
**Output:** [{"t": 70, inputs: [1]}, {"t": 120, inputs: [2]}]
49+
50+
**Explanation:** The 1st call is delayed until 70ms. The inputs were (1). The 2nd call is delayed until 120ms. The inputs were (2).
51+
52+
**Example 3:**
53+
54+
**Input:**
55+
56+
t = 150
57+
calls = [
58+
{"t": 50, inputs: [1, 2]},
59+
{"t": 300, inputs: [3, 4]},
60+
{"t": 300, inputs: [5, 6]}
61+
]
62+
63+
**Output:** [{"t": 200, inputs: [1,2]}, {"t": 450, inputs: [5, 6]}]
64+
65+
**Explanation:**
66+
67+
The 1st call is delayed by 150ms and ran at 200ms. The inputs were (1, 2).
68+
69+
The 2nd call is cancelled by the 3rd call
70+
71+
The 3rd call is delayed by 150ms and ran at 450ms. The inputs were (5, 6).
72+
73+
**Constraints:**
74+
75+
* `0 <= t <= 1000`
76+
* `1 <= calls.length <= 10`
77+
* `0 <= calls[i].t <= 1000`
78+
* `0 <= calls[i].inputs.length <= 10`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// #Medium #2023_08_31_Time_50_ms_(98.23%)_Space_42.5_MB_(83.54%)
2+
3+
type F = (...p: any[]) => any
4+
5+
function debounce(fn: F, t: number): F {
6+
let ref = null
7+
return function (...args) {
8+
if (ref !== null) {
9+
clearTimeout(ref)
10+
}
11+
ref = setTimeout(() => fn(...args), t)
12+
}
13+
}
14+
15+
/*
16+
* const log = debounce(console.log, 100);
17+
* log('Hello'); // cancelled
18+
* log('Hello'); // cancelled
19+
* log('Hello'); // Logged at t=100ms
20+
*/
21+
22+
export { debounce }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2629\. Function Composition
2+
3+
Easy
4+
5+
Given an array of functions <code>[f1, f<sub>2</sub>, f<sub>3</sub>, ..., f<sub>n</sub>]</code>, return a new function `fn` that is the **function composition** of the array of functions.
6+
7+
The **function composition** of `[f(x), g(x), h(x)]` is `fn(x) = f(g(h(x)))`.
8+
9+
The **function composition** of an empty list of functions is the **identity function** `f(x) = x`.
10+
11+
You may assume each function in the array accepts one integer as input and returns one integer as output.
12+
13+
**Example 1:**
14+
15+
**Input:** functions = [x => x + 1, x => x * x, x => 2 * x], x = 4
16+
17+
**Output:** 65
18+
19+
**Explanation:**
20+
21+
Evaluating from right to left ...
22+
23+
Starting with x = 4.
24+
25+
2 * (4) = 8
26+
27+
(8) * (8) = 64
28+
29+
(64) + 1 = 65
30+
31+
**Example 2:**
32+
33+
**Input:** functions = [x => 10 * x, x => 10 * x, x => 10 * x], x = 1
34+
35+
**Output:** 1000
36+
37+
**Explanation:**
38+
39+
Evaluating from right to left ...
40+
41+
10 * (1) = 10
42+
43+
10 * (10) = 100
44+
45+
10 * (100) = 1000
46+
47+
**Example 3:**
48+
49+
**Input:** functions = [], x = 42
50+
51+
**Output:** 42
52+
53+
**Explanation:** The composition of zero functions is the identity function
54+
55+
**Constraints:**
56+
57+
* `-1000 <= x <= 1000`
58+
* `0 <= functions.length <= 1000`
59+
* `all functions accept and return a single integer`

0 commit comments

Comments
 (0)