Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2fe08f2

Browse files
committedOct 2, 2021
Add solution #1296
1 parent 5e3f6fb commit 2fe08f2

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
1291|[Sequential Digits](./1291-sequential-digits.js)|Medium|
117117
1292|[Maximum Side Length of a Square with Sum Less than or Equal to Threshold](./1292-maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold.js)|Medium|
118118
1295|[Find Numbers with Even Number of Digits](./1295-find-numbers-with-even-number-of-digits.js)|Easy|
119+
1296|[Divide Array in Sets of K Consecutive Numbers](./1296-divide-array-in-sets-of-k-consecutive-numbers.js)|Medium|
119120
1297|[Maximum Number of Occurrences of a Substring](./1297-maximum-number-of-occurrences-of-a-substring.js)|Medium|
120121
1304|[Find N Unique Integers Sum up to Zero](./1304-find-n-unique-integers-sum-up-to-zero.js)|Easy|
121122
1309|[Decrypt String from Alphabet to Integer Mapping](./1309-decrypt-string-from-alphabet-to-integer-mapping.js)|Easy|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 1296. Divide Array in Sets of K Consecutive Numbers
3+
* https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
4+
* Difficulty: Medium
5+
*
6+
* Given an array of integers nums and a positive integer k, find whether it is
7+
* possible to divide this array into sets of k consecutive numbers.
8+
*
9+
* Return true if it is possible. Otherwise, return false.
10+
*/
11+
12+
/**
13+
* @param {number[]} nums
14+
* @param {number} k
15+
* @return {boolean}
16+
*/
17+
var isPossibleDivide = function(nums, k) {
18+
if (nums.length % k) {
19+
return false;
20+
}
21+
22+
const map = {};
23+
const set = new Set(nums);
24+
nums.forEach(x => map[x] ? map[x]++ : map[x] = 1);
25+
26+
let count = nums.length / k;
27+
while (count--) {
28+
let min = Math.min(...set);
29+
for (let i = min; i < min + k; i++) {
30+
if (!map[i]) {
31+
return false;
32+
}
33+
if (!--map[i]) {
34+
set.delete(i);
35+
}
36+
}
37+
}
38+
39+
return true;
40+
};

0 commit comments

Comments
 (0)
Please sign in to comment.