-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2892-minimizing-array-after-replacing-pairs-with-their-product.js
More file actions
52 lines (47 loc) · 1.4 KB
/
2892-minimizing-array-after-replacing-pairs-with-their-product.js
File metadata and controls
52 lines (47 loc) · 1.4 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
52
/**
* 2892. Minimizing Array After Replacing Pairs With Their Product
* https://leetcode.com/problems/minimizing-array-after-replacing-pairs-with-their-product/
* Difficulty: Medium
*
* Given an integer array nums and an integer k, you can perform the following operation on the
* array any number of times:
* - Select two adjacent elements of the array like x and y, such that x * y <= k, and replace
* both of them with a single element with value x * y (e.g. in one operation the array
* [1, 2, 2, 3] with k = 5 can become [1, 4, 3] or [2, 2, 3], but can't become [1, 2, 6]).
*
* Return the minimum possible length of nums after any number of operations.
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var minArrayLength = function(nums, k) {
if (nums.some(num => num === 0)) {
return 1;
}
const segments = [];
let currentProduct = 1;
let currentLength = 0;
for (const num of nums) {
if (num > k) {
if (currentLength > 0) {
segments.push(currentLength);
currentProduct = 1;
currentLength = 0;
}
segments.push(1);
} else if (currentProduct * num <= k) {
currentProduct *= num;
currentLength++;
} else {
segments.push(currentLength);
currentProduct = num;
currentLength = 1;
}
}
if (currentLength > 0) {
segments.push(currentLength);
}
return segments.length;
};