Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
35 lines (34 loc) · 799 Bytes

File metadata and controls

35 lines (34 loc) · 799 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 2677. Chunk Array
// 🟠 Medium
//
// https://leetcode.com/problems/chunk-array/
//
// Tags: Javascript
// Iterate over the input array creating chunks of the given size and adding
// them to the result one at a time.
//
// Time complexity: O(n) - We will push every element of the input into the
// output array.
// Space complexity: O(n) - If we take into consideration the output array, or
// O(1) if we do not.
//
// Runtime 62 ms Beats 66.87%
// Memory 44.8 MB Beats 28.33%
/**
* @param {Array} arr
* @param {number} size
* @return {Array[]}
*/
var chunk = function (arr, size) {
let res = [];
let i = 0;
while (i < arr.length) {
let cur = [];
while (cur.length < size && i < arr.length) {
cur.push(arr[i]);
i++;
}
res.push(cur);
}
return res;
};
Morty Proxy This is a proxified and sanitized view of the page, visit original site.