-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.js
102 lines (95 loc) · 2.71 KB
/
Solution.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// https://leetcode.com/problems/split-array-into-consecutive-subsequences/
// https://leetcode.com/problems/split-array-into-consecutive-subsequences/discuss/2447421/Javascript-solution-using-hashmap
/**
* @param {number[]} nums
* @return {boolean}
*/
var isPossible1 = function (nums) {
if (nums.length < 3) return false;
let a = [];
let last = 0;
for (let i = 0; i < nums.length; i++) {
if (!a.includes(nums[i])) {
if (a.length == 3) {
break;
}
last = i;
a.push(nums[i]);
}
}
if (nums.length == 3 && a.length == 3) return true;
let b = [];
for (let i = nums.length - 1; i > 0; i--) {
if (a.length == 3 && b.length == 3) {
return true;
}
if (last == i) {
return false;
}
if (!b.includes(nums[i])) {
if (b.length == 3) {
break;
}
b.push(nums[i]);
}
}
};
// reference
var isPossible = function (nums) {
let fMap = {};
let aMap = {};
for (let i = 0; i < nums.length; i++) {
fMap[nums[i]] = (fMap[nums[i]] || 0) + 1;
}
for (let i of nums) {
if (fMap[i] == 0) continue;
else if (aMap[i] > 0) {
aMap[i] = (aMap[i] || 0) - 1;
aMap[i + 1] = (aMap[i + 1] || 0) + 1;
} else if (fMap[i + 1] && fMap[i + 2]) {
fMap[i + 1] = (fMap[i + 1] || 0) - 1;
fMap[i + 2] = (fMap[i + 2] || 0) - 1;
aMap[i + 3] = (aMap[i + 3] || 0) + 1;
}
else {
return false;
}
fMap[i] -= 1;
}
return true;
};
// optimized
/*
Runtime: 142 ms, faster than 53.75% of JavaScript online submissions for Split Array into Consecutive Subsequences.
Memory Usage: 51.2 MB, less than 40.00% of JavaScript online submissions for Split Array into Consecutive Subsequences.
*/
var isPossible = function (n) {
let a = {};
let b = {};
for (let i = 0; i < n.length; i++) {
a[n[i]] = (a[n[i]] || 0) + 1;
}
for (let i of n) {
if (a[i] == 0) continue;
else if (b[i] > 0) {
b[i] = (b[i] || 0) - 1;
b[i + 1] = (b[i + 1] || 0) + 1;
} else if (a[i + 1] && a[i + 2]) {
a[i + 1] = (a[i + 1] || 0) - 1;
a[i + 2] = (a[i + 2] || 0) - 1;
b[i + 3] = (b[i + 3] || 0) + 1;
}
else {
return false;
}
a[i] -= 1;
}
return true;
};
let nums = [1, 2, 3, 3, 4, 5]; //true
// nums = [1, 2, 3, 3, 4, 4, 5, 5] // true
// nums = [1, 2, 3, 4, 4, 5] // false
nums = [1, 2, 3, 4]; //true
nums = [1] //false
nums = [1, 2, 3, 5, 5, 6, 7] //false
console.log(isPossible(nums));