Skip to content

Commit 2decb83

Browse files
committed
Add solution #1764
1 parent 0375d6b commit 2decb83

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@
425425
1716|[Calculate Money in Leetcode Bank](./1716-calculate-money-in-leetcode-bank.js)|Easy|
426426
1732|[Find the Highest Altitude](./1732-find-the-highest-altitude.js)|Easy|
427427
1748|[Sum of Unique Elements](./1748-sum-of-unique-elements.js)|Easy|
428+
1764|[Form Array by Concatenating Subarrays of Another Array](./1764-form-array-by-concatenating-subarrays-of-another-array.js)|Medium|
428429
1765|[Map of Highest Peak](./1765-map-of-highest-peak.js)|Medium|
429430
1768|[Merge Strings Alternately](./1768-merge-strings-alternately.js)|Easy|
430431
1780|[Check if Number is a Sum of Powers of Three](./1780-check-if-number-is-a-sum-of-powers-of-three.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 1764. Form Array by Concatenating Subarrays of Another Array
3+
* https://leetcode.com/problems/form-array-by-concatenating-subarrays-of-another-array/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 2D integer array groups of length n. You are also given an integer array nums.
7+
*
8+
* You are asked if you can choose n disjoint subarrays from the array nums such that the ith
9+
* subarray is equal to groups[i] (0-indexed), and if i > 0, the (i-1)th subarray appears before
10+
* the ith subarray in nums (i.e. the subarrays must be in the same order as groups).
11+
*
12+
* Return true if you can do this task, and false otherwise.
13+
*
14+
* Note that the subarrays are disjoint if and only if there is no index k such that nums[k]
15+
* belongs to more than one subarray. A subarray is a contiguous sequence of elements within
16+
* an array.
17+
*/
18+
19+
/**
20+
* @param {number[][]} groups
21+
* @param {number[]} nums
22+
* @return {boolean}
23+
*/
24+
var canChoose = function(groups, nums) {
25+
const rows = groups.map(row => ',' + row.join(',') + ',');
26+
const joined = `,${nums.join(',')},`;
27+
28+
for (let i = 0, offset = 0; i < rows.length; i++) {
29+
offset = joined.indexOf(rows[i], offset);
30+
if (offset === -1) {
31+
return false;
32+
}
33+
offset += rows[i].length - 1;
34+
}
35+
36+
return true;
37+
};

0 commit comments

Comments
 (0)