-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2464-minimum-subarrays-in-a-valid-split.js
More file actions
51 lines (46 loc) · 1.3 KB
/
2464-minimum-subarrays-in-a-valid-split.js
File metadata and controls
51 lines (46 loc) · 1.3 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* 2464. Minimum Subarrays in a Valid Split
* https://leetcode.com/problems/minimum-subarrays-in-a-valid-split/
* Difficulty: Medium
*
* You are given an integer array nums.
*
* Splitting of an integer array nums into subarrays is valid if:
* - the greatest common divisor of the first and last elements of each subarray is
* greater than 1, and
* - each element of nums belongs to exactly one subarray.
*
* Return the minimum number of subarrays in a valid subarray splitting of nums.
* If a valid subarray splitting is not possible, return -1.
*
* Note that:
* - The greatest common divisor of two numbers is the largest positive integer that
* evenly divides both numbers.
* - A subarray is a contiguous non-empty part of an array.
*/
/**
* @param {number[]} nums
* @return {number}
*/
var validSubarraySplit = function(nums) {
const n = nums.length;
const dp = new Array(n + 1).fill(Infinity);
dp[0] = 0;
for (let i = 0; i < n; i++) {
if (dp[i] === Infinity) continue;
for (let j = i; j < n; j++) {
if (gcd(nums[i], nums[j]) > 1) {
dp[j + 1] = Math.min(dp[j + 1], dp[i] + 1);
}
}
}
return dp[n] === Infinity ? -1 : dp[n];
function gcd(a, b) {
while (b !== 0) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}
};