Skip to content

Commit fe0b554

Browse files
authored
Write-up for problem 2677. Chunk Array (#401)
1 parent 8541031 commit fe0b554

File tree

18 files changed

+827
-58
lines changed

18 files changed

+827
-58
lines changed

workspaces/javascript-leetcode-month/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This directory contains the resources for the month of JavaScript on LeetCode, a
66

77
1. [2703. Return Length of Arguments Passed](problems/2703-return-length-of-arguments-passed/)
88
2. [2727. Is Object Empty](problems/2727-is-object-empty/)
9-
3. [2677. Chunk Array](https://leetcode.com/problems/chunk-array/)
9+
3. [2677. Chunk Array](problems/2677-chunk-array/)
1010
4. [2635. Apply Transform Over Each Element in Array](https://leetcode.com/problems/apply-transform-over-each-element-in-array/)
1111
5. [2634. Filter Elements from Array](https://leetcode.com/problems/filter-elements-from-array/)
1212
6. [2724. Sort By](https://leetcode.com/problems/sort-by/)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 2677. Chunk Array
2+
3+
[View Problem on LeetCode](https://leetcode.com/problems/chunk-array/)
4+
5+
Although the problem asks us not to use [`_.chunk`](https://lodash.com/docs/#chunk), I think there's no harm in using it to get a quick accept, as long as we then follow up with another strategy.
6+
7+
As far as manual implementations, the choice is likely between traversing the input one element at a time, or grabbing a larger _slice_ of input at once. Both approaches are viable and can result in an efficient solution.
8+
9+
Once you've worked on the problem, check out [the full write-up and solution](solution.md)!

workspaces/javascript-leetcode-month/problems/2677-chunk-array/solution.md

Lines changed: 581 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function chunk<T>(arr: readonly T[], size: number): T[][] {
2+
const res: T[][] = [];
3+
4+
for (let i = 0; i < arr.length; i += size) {
5+
res.push(arr.slice(i, i + size));
6+
}
7+
8+
return res;
9+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function* generateSlice<T>(
2+
arr: readonly T[],
3+
start: number,
4+
size: number,
5+
): Generator<T, void, void> {
6+
for (let i = start; size > 0 && i < arr.length; ++i, --size) {
7+
yield arr[i];
8+
}
9+
}
10+
11+
function* generateChunks<T>(
12+
arr: readonly T[],
13+
size: number,
14+
): Generator<T[], void, void> {
15+
for (let i = 0; i < arr.length; i += size) {
16+
yield Array.from(generateSlice(arr, i, size));
17+
}
18+
}
19+
20+
function chunk<T>(arr: readonly T[], size: number): T[][] {
21+
return Array.from(generateChunks(arr, size));
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function slice<T>(arr: readonly T[], start: number, end: number): T[] {
2+
const res: T[] = [];
3+
4+
for (let i = start; i < end && i < arr.length; ++i) {
5+
res.push(arr[i]);
6+
}
7+
8+
return res;
9+
}
10+
11+
function chunk<T>(arr: readonly T[], size: number): T[][] {
12+
const res: T[][] = [];
13+
14+
for (let i = 0; i < arr.length; i += size) {
15+
res.push(slice(arr, i, i + size));
16+
}
17+
18+
return res;
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function slice<T>(arr: readonly T[], start: number, size: number): T[] {
2+
const res: T[] = [];
3+
4+
for (let i = start; res.length < size && i < arr.length; ++i) {
5+
res.push(arr[i]);
6+
}
7+
8+
return res;
9+
}
10+
11+
function chunk<T>(arr: readonly T[], size: number): T[][] {
12+
const res: T[][] = [];
13+
14+
for (let i = 0; i < arr.length; i += size) {
15+
res.push(slice(arr, i, size));
16+
}
17+
18+
return res;
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function chunk<T>(arr: readonly T[], size: number): T[][] {
2+
const res: T[][] = [];
3+
4+
for (let i = 0; i < arr.length; ++i) {
5+
// New chunks start at indexes divisible by `size`.
6+
if (i % size === 0) {
7+
res.push([]);
8+
}
9+
10+
// We can be certain that `res` is not empty because we'll add something
11+
// to `res` during the very first iteration, when `i` is 0.
12+
const lastChunk = res.at(-1)!;
13+
lastChunk.push(arr[i]);
14+
}
15+
16+
return res;
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function chunk<T>(arr: readonly T[], size: number): T[][] {
2+
const res: T[][] = [];
3+
4+
for (const element of arr) {
5+
// If there's no last chunk, or if the last chunk is full, start a new
6+
// chunk.
7+
if (res.length === 0 || res.at(-1)!.length === size) {
8+
res.push([]);
9+
}
10+
11+
res.at(-1)!.push(element);
12+
}
13+
14+
return res;
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function chunk<T>(arr: readonly T[], size: number): T[][] {
2+
const res: T[][] = [];
3+
4+
for (const [index, element] of arr.entries()) {
5+
// New chunks start at indexes divisible by `size`.
6+
if (index % size === 0) {
7+
res.push([]);
8+
}
9+
10+
res.at(-1)!.push(element);
11+
}
12+
13+
return res;
14+
}

0 commit comments

Comments
 (0)