-
Notifications
You must be signed in to change notification settings - Fork 89
Added tasks 2624-2629 #1535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Added tasks 2624-2629 #1535
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f9062bd
Added tasks 2624-2629
ThanhNIT ea294e4
Update solution.ts
javadev 919ecf5
Update solution.ts
javadev c2180e1
Update solution.ts
javadev c2f05ce
Update solution.ts
javadev 7f391b1
Update solution.ts
javadev 313dfb5
Update solution.ts
javadev ac66738
update
ThanhNIT 174845e
Update solution.ts
javadev 9bdfaad
Update solution.test.ts
javadev 5c3cf70
Update solution.test.ts
javadev 1c7b961
Update solution.test.ts
javadev b5734d5
Update solution.test.ts
javadev c95babe
Update solution.test.ts
javadev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
2624\. Snail Traversal | ||
|
||
Medium | ||
|
||
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. | ||
|
||
**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. | ||
|
||
 | ||
|
||
**Example 1:** | ||
|
||
**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 | ||
|
||
**Output:** | ||
|
||
[ | ||
[19,17,16,15], | ||
[10,1,14,4], | ||
[3,2,12,20], | ||
[7,5,18,11], | ||
[9,8,6,13] | ||
] | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [1,2,3,4] rowsCount = 1 colsCount = 4 | ||
|
||
**Output:** [[1, 2, 3, 4]] | ||
|
||
**Example 3:** | ||
|
||
**Input:** nums = [1,3] rowsCount = 2 colsCount = 2 | ||
|
||
**Output:** [] | ||
|
||
**Explanation:** 2 multiplied by 2 is 4, and the original array [1,3] has a length of 2; therefore, the input is invalid. | ||
|
||
**Constraints:** | ||
|
||
* `0 <= nums.length <= 250` | ||
* `1 <= nums[i] <= 1000` | ||
* `1 <= rowsCount <= 250` | ||
* `1 <= colsCount <= 250` |
27 changes: 27 additions & 0 deletions
27
src/main/java/g2601_2700/s2624_snail_traversal/solution.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// #Medium #2023_08_31_Time_175_ms_(92.96%)_Space_64.2_MB_(32.75%) | ||
|
||
declare global { | ||
interface Array<T> { | ||
snail(rowsCount: number, colsCount: number): number[][] | ||
} | ||
} | ||
|
||
Array.prototype.snail = function (rowsCount: number, colsCount: number): number[][] { //NOSONAR | ||
if (rowsCount * colsCount !== this.length) return [] | ||
let res: number[][] = [] | ||
for (let i = 0; i < this.length; i++) { | ||
let col = Math.floor(i / rowsCount) | ||
let row = i % rowsCount | ||
row = col % 2 === 0 ? row : rowsCount - row - 1 | ||
if (res[row] === undefined) res[row] = [] | ||
res[row].push(this[i]) | ||
} | ||
return res | ||
} | ||
|
||
/* | ||
* const arr = [1,2,3,4]; | ||
* arr.snail(1,4); // [[1,2,3,4]] | ||
*/ | ||
|
||
export {} |
43 changes: 43 additions & 0 deletions
43
src/main/java/g2601_2700/s2625_flatten_deeply_nested_array/readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
2625\. Flatten Deeply Nested Array | ||
|
||
Medium | ||
|
||
Given a **multi-dimensional** array `arr` and a depth `n`, return a **flattened** version of that array. | ||
|
||
A **multi-dimensional** array is a recursive data structure that contains integers or other **multi-dimensional** arrays. | ||
|
||
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`. | ||
|
||
Please solve it without the built-in `Array.flat` method. | ||
|
||
**Example 1:** | ||
|
||
**Input** arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]] n = 0 | ||
|
||
**Output:** [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]] | ||
|
||
**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. | ||
|
||
**Example 2:** | ||
|
||
**Input** arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]] n = 1 | ||
|
||
**Output:** [1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15] | ||
|
||
**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. | ||
|
||
**Example 3:** | ||
|
||
**Input** arr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]] n = 2 | ||
|
||
**Output:** [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] | ||
|
||
**Explanation:** The maximum depth of any subarray is 1. Thus, all of them are flattened. | ||
|
||
**Constraints:** | ||
|
||
* <code>0 <= count of numbers in arr <= 10<sup>5</sup></code> | ||
* <code>0 <= count of subarrays in arr <= 10<sup>5</sup></code> | ||
* `maxDepth <= 1000` | ||
* `-1000 <= each number <= 1000` | ||
* `0 <= n <= 1000` |
22 changes: 22 additions & 0 deletions
22
src/main/java/g2601_2700/s2625_flatten_deeply_nested_array/solution.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// #Medium #2023_08_31_Time_84_ms_(98.71%)_Space_61.8_MB_(94.52%) | ||
|
||
type MultiDimensionalArray = (number | MultiDimensionalArray)[] | ||
|
||
const flat = (arr: MultiDimensionalArray, depth: number): MultiDimensionalArray => { | ||
const result: MultiDimensionalArray = [] | ||
|
||
const flatten = (nestedArray: MultiDimensionalArray, currentDepth: number) => { | ||
for (const element of nestedArray) { | ||
if (Array.isArray(element) && currentDepth > 0 && currentDepth <= depth) { | ||
flatten(element, currentDepth - 1) | ||
} else { | ||
result.push(element) | ||
} | ||
} | ||
} | ||
|
||
flatten(arr, depth) | ||
return result | ||
} | ||
|
||
export { flat } |
61 changes: 61 additions & 0 deletions
61
src/main/java/g2601_2700/s2626_array_reduce_transformation/readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
2626\. Array Reduce Transformation | ||
|
||
Easy | ||
|
||
Given an integer array `nums`, a reducer function `fn`, and an initial value `init`, return a **reduced** array. | ||
|
||
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. | ||
|
||
If the length of the array is 0, it should return `init`. | ||
|
||
Please solve it without using the built-in `Array.reduce` method. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [1,2,3,4] fn = function sum(accum, curr) { return accum + curr; } init = 0 | ||
|
||
**Output:** 10 | ||
|
||
**Explanation:** initially, the value is init=0. | ||
|
||
(0) + nums[0] = 1 | ||
|
||
(1) + nums[1] = 3 | ||
|
||
(3) + nums[2] = 6 | ||
|
||
(6) + nums[3] = 10 | ||
|
||
The final answer is 10. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [1,2,3,4] fn = function sum(accum, curr) { return accum + curr \* curr; } init = 100 | ||
|
||
**Output:** 130 | ||
|
||
**Explanation:** initially, the value is init=100. | ||
|
||
(100) + nums[0]^2 = 101 | ||
|
||
(101) + nums[1]^2 = 105 | ||
|
||
(105) + nums[2]^2 = 114 | ||
|
||
(114) + nums[3]^2 = 130 | ||
|
||
The final answer is 130. | ||
|
||
**Example 3:** | ||
|
||
**Input:** nums = [] fn = function sum(accum, curr) { return 0; } init = 25 | ||
|
||
**Output:** 25 | ||
|
||
**Explanation:** For empty arrays, the answer is always init. | ||
|
||
**Constraints:** | ||
|
||
* `0 <= nums.length <= 1000` | ||
* `0 <= nums[i] <= 1000` | ||
* `0 <= init <= 1000` |
13 changes: 13 additions & 0 deletions
13
src/main/java/g2601_2700/s2626_array_reduce_transformation/solution.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// #Easy #2023_08_31_Time_52_ms_(91.40%)_Space_44.2_MB_(82.03%) | ||
|
||
type Fn = (accum: number, curr: number) => number | ||
|
||
function reduce(nums: number[], fn: Fn, init: number): number { | ||
let accumulator = init | ||
nums.forEach((num) => { | ||
accumulator = fn(accumulator, num) | ||
}) | ||
return accumulator | ||
} | ||
|
||
export { reduce } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
2627\. Debounce | ||
|
||
Medium | ||
|
||
Given a function `fn` and a time in milliseconds `t`, return a **debounced** version of that function. | ||
|
||
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. | ||
|
||
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`. | ||
|
||
 | ||
|
||
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. | ||
|
||
Please solve it without using lodash's `_.debounce()` function. | ||
|
||
**Example 1:** | ||
|
||
**Input:** t = 50 calls = [ {"t": 50, inputs: [1]}, {"t": 75, inputs: [2]} ] | ||
|
||
**Output:** [{"t": 125, inputs: [2]}] | ||
|
||
**Explanation:** | ||
|
||
let start = Date.now(); | ||
|
||
function log(...inputs) { | ||
console.log([Date.now() - start, inputs ]) | ||
} | ||
const dlog = debounce(log, 50); | ||
setTimeout(() => dlog(1), 50); | ||
setTimeout(() => dlog(2), 75); | ||
|
||
The 1st call is cancelled by the 2nd call because the 2nd call occurred before 100ms | ||
|
||
The 2nd call is delayed by 50ms and executed at 125ms. The inputs were (2). | ||
|
||
**Example 2:** | ||
|
||
**Input:** | ||
|
||
t = 20 | ||
calls = [ | ||
{"t": 50, inputs: [1]}, | ||
{"t": 100, inputs: [2]} | ||
] | ||
|
||
**Output:** [{"t": 70, inputs: [1]}, {"t": 120, inputs: [2]}] | ||
|
||
**Explanation:** The 1st call is delayed until 70ms. The inputs were (1). The 2nd call is delayed until 120ms. The inputs were (2). | ||
|
||
**Example 3:** | ||
|
||
**Input:** | ||
|
||
t = 150 | ||
calls = [ | ||
{"t": 50, inputs: [1, 2]}, | ||
{"t": 300, inputs: [3, 4]}, | ||
{"t": 300, inputs: [5, 6]} | ||
] | ||
|
||
**Output:** [{"t": 200, inputs: [1,2]}, {"t": 450, inputs: [5, 6]}] | ||
|
||
**Explanation:** | ||
|
||
The 1st call is delayed by 150ms and ran at 200ms. The inputs were (1, 2). | ||
|
||
The 2nd call is cancelled by the 3rd call | ||
|
||
The 3rd call is delayed by 150ms and ran at 450ms. The inputs were (5, 6). | ||
|
||
**Constraints:** | ||
|
||
* `0 <= t <= 1000` | ||
* `1 <= calls.length <= 10` | ||
* `0 <= calls[i].t <= 1000` | ||
* `0 <= calls[i].inputs.length <= 10` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// #Medium #2023_08_31_Time_50_ms_(98.23%)_Space_42.5_MB_(83.54%) | ||
|
||
type F = (...p: any[]) => any | ||
|
||
function debounce(fn: F, t: number): F { | ||
let ref = null | ||
return function (...args) { | ||
if (ref !== null) { | ||
clearTimeout(ref) | ||
} | ||
ref = setTimeout(() => fn(...args), t) | ||
} | ||
} | ||
|
||
/* | ||
* const log = debounce(console.log, 100); | ||
* log('Hello'); // cancelled | ||
* log('Hello'); // cancelled | ||
* log('Hello'); // Logged at t=100ms | ||
*/ | ||
|
||
export { debounce } |
59 changes: 59 additions & 0 deletions
59
src/main/java/g2601_2700/s2629_function_composition/readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
2629\. Function Composition | ||
|
||
Easy | ||
|
||
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. | ||
|
||
The **function composition** of `[f(x), g(x), h(x)]` is `fn(x) = f(g(h(x)))`. | ||
|
||
The **function composition** of an empty list of functions is the **identity function** `f(x) = x`. | ||
|
||
You may assume each function in the array accepts one integer as input and returns one integer as output. | ||
|
||
**Example 1:** | ||
|
||
**Input:** functions = [x => x + 1, x => x * x, x => 2 * x], x = 4 | ||
|
||
**Output:** 65 | ||
|
||
**Explanation:** | ||
|
||
Evaluating from right to left ... | ||
|
||
Starting with x = 4. | ||
|
||
2 * (4) = 8 | ||
|
||
(8) * (8) = 64 | ||
|
||
(64) + 1 = 65 | ||
|
||
**Example 2:** | ||
|
||
**Input:** functions = [x => 10 * x, x => 10 * x, x => 10 * x], x = 1 | ||
|
||
**Output:** 1000 | ||
|
||
**Explanation:** | ||
|
||
Evaluating from right to left ... | ||
|
||
10 * (1) = 10 | ||
|
||
10 * (10) = 100 | ||
|
||
10 * (100) = 1000 | ||
|
||
**Example 3:** | ||
|
||
**Input:** functions = [], x = 42 | ||
|
||
**Output:** 42 | ||
|
||
**Explanation:** The composition of zero functions is the identity function | ||
|
||
**Constraints:** | ||
|
||
* `-1000 <= x <= 1000` | ||
* `0 <= functions.length <= 1000` | ||
* `all functions accept and return a single integer` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.