Repository navigation
Expand file tree
/
Copy pathfind-smallest-missing-positive-integer.js
More file actions
77 lines (57 loc) · 1.91 KB
/
Copy pathfind-smallest-missing-positive-integer.js
File metadata and controls
77 lines (57 loc) · 1.91 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
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
'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'findSmallestMissingPositive' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY orderNumbers as parameter.
*/
function findSmallestMissingPositive(orderNumbers) {
for (let i = 0; i < orderNumbers.length; i++) {
while (
orderNumbers[i] > 0 &&
orderNumbers[i] <= orderNumbers.length &&
orderNumbers[orderNumbers[i] - 1] !== orderNumbers[i]
) {
const value = orderNumbers[i];
orderNumbers[i] = orderNumbers[value - 1];
orderNumbers[value - 1] = value;
}
}
for (let i = 0; i < orderNumbers.length; i++) {
if (orderNumbers[i] !== i + 1) {
return i + 1;
}
}
return orderNumbers.length + 1;
}
function main() {
const orderNumbersCount = parseInt(readLine().trim(), 10);
let orderNumbers = [];
for (let i = 0; i < orderNumbersCount; i++) {
const orderNumbersItem = parseInt(readLine().trim(), 10);
orderNumbers.push(orderNumbersItem);
}
const result = findSmallestMissingPositive(orderNumbers);
process.stdout.write(result + '\n');
}
// My mistake:
// I only checked whether `i < value`, but I didn't properly verify
// that `value` is a valid positive number for the array and that
// its correct position doesn't already contain the same value.
// For cyclic placement, value `x` should be placed at index `x - 1`.
// So I need to keep swapping until each value is either in its
// correct position or cannot be placed.