-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfirst-missing-positive.js
More file actions
40 lines (36 loc) · 918 Bytes
/
first-missing-positive.js
File metadata and controls
40 lines (36 loc) · 918 Bytes
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
var DEBUG = process.env.DEBUG;
/**
* @param {number[]} nums
* @return {number}
*/
var firstMissingPositive = function(nums) {
// make the array 1-indexed. If you dont like this trick,
// the following loop needs to be more carefully,
nums.unshift(-1);
for (var i = 1; i < nums.length; i++) {
if (nums[i] !== i && nums[i] > 0 && nums[i] < nums.length) {
var to = nums[i];
while (to > 0 && to < nums.length && nums[to] !== to && to !== i) {
var tmp = nums[to];
nums[to] = to;
to = tmp;
}
if (to === i) nums[i] = to;
}
}
console.log(nums);
for (var i = 1; i < nums.length; i++)
if (nums[i] !== i) return i;
return nums.length;
};
function test(f) {
[
[[1, 2, 0]],
[[3,4,-1,1]],
[[1]],
[[1,2,3,5]],
].forEach(function (input) {
console.log(f.apply(undefined, input));
});
}
if (DEBUG) test(firstMissingPositive);