LeetCode: Form Array By Concatenating Subarrays Of Another Array Solution

1const arrayEqual = (a, b) =>
2 a.length === b.length && a.every((el, i) => el === b[i])
3
4/**
5 * @param {number[][]} groups
6 * @param {number[]} nums
7 * @return {boolean}
8 */
9var canChoose = function (groups, nums) {
10 let i = 0
11
12 groupsLoop: for (const group of groups) {
13 for (let j = i; j < nums.length; j++) {
14 if (arrayEqual(nums.slice(j, j + group.length), group)) {
15 i = j + group.length
16 continue groupsLoop
17 }
18 }
19 return false
20 }
21
22 return true
23}

Comments

Loading comments...

Tags

leetcode

array

greedy

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Roman To Integer

Feb 21, 2021

Previous Post

HoningJS

Search Posts