You are a skier participating in a giant slalom. The slalom track is located on a ski slope, goes downhill and is fenced by barriers on both sides. The barriers are perpendicular to the starting line located at the top of the slope. There are N slalom gates on the track. Each gate is placed at a distinct distance from the starting line and from the barrier on the right-hand side (looking downhill).
You start from any place on the starting line, ski down the track passing as many gates as possible, and finish the slalom at the bottom of the slope. Passing a gate means skiing through the position of the gate.
You can ski downhill in either of two directions: to the left or to the right. When you ski to the left, you pass gates of increasing distances from the right barrier, and when you ski to the right, you pass gates of decreasing distances from the right barrier. You want to ski to the left at the beginning.
Unfortunately, changing direction (left to right or vice versa) is exhausting, so you have decided to change direction at most two times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.
The arrangement of the gates is given as an array A consisting of N integers, whose elements specify the positions of the gates: gate K (for 0 ≤ K < N) is at a distance of K+1 from the starting line, and at a distance of A[K] from the right barrier.
For example, consider array A such that:
A[0] = 15 A[1] = 13 A[2] = 5 A[3] = 7 A[4] = 4 A[5] = 10 A[6] = 12 A[7] = 8 A[8] = 2 A[9] = 11 A[10] = 6 A[11] = 9 A[12] = 3The picture above illustrates the example track with N = 13 gates and a course that passes eight gates. After starting, you ski to the left (from your own perspective). You pass gates 2, 3, 5, 6 and then change direction to the right. After that you pass gates 7, 8 and then change direction to the left. Finally, you pass gates 10, 11 and finish the slalom. There is no possible way of passing more gates using at most two changes of direction.
Write a function:
function solution(A);
that, given an array A consisting of N integers, describing the positions of the gates on the track, returns the maximum number of gates that you can pass during one ski run.
For example, given the above data, the function should return 8, as explained above.
For the following array A consisting of N = 2 elements:
A[0] = 1 A[1] = 5the function should return 2.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of array A is an integer within the range [1..1,000,000,000];
- the elements of A are all distinct.
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N =))
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: M})
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length }, ())
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length }, () => -1)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length }, () => -1);
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length }, () => -1);
for (let i = 0; i)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length }, () => -1);
for (let i = 0; i < multiverse.length)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
for (let i = 0; i < multiverse.length)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
for (let i = 0; i < multiverse.length;)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
for (let i = 0; i < multiverse.length; i++) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end =
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < )
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
upper = mid - 1;
} else if ()
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
upper = mid - 1;
} else {
}
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
upper = mid - 1;
} else {
start = mid + 1;
}
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] =)
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], )
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multivers.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
ReferenceError: multivers is not defined at solution (solution.js:11:9) at solutionWrapper (/tmp/exec.js:402:28) at Promise.resolve.then (/tmp/exec.js:428:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multiverse.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multiverse.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multiverse.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multiverse.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multiverse.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multiverse.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multiverse.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multiverse.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
[1]
[1, 4, 7, 10, 13, 12, 9, 6, 3, 2, 5, 8, 11, 14]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multiverse.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
[1]
[1, 4, 7, 10, 13, 12, 9, 6, 3, 2, 5, 8, 11, 14, 0]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multiverse.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multiverse.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multiverse.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
[1]
[1, 4, 7, 10, 13, 12, 9, 6, 3, 2, 5, 8, 11, 14, 0]
[4, 7, 10, 13, 1, 12, 9, 6, 3, 2, 5, 8, 11, 14, 0]
function result: 1
function result: 14
function result: 13
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multiverse.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multiverse.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multiverse.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
[1]
[1, 4, 7, 10, 13, 12, 9, 6, 3, 2, 5, 8, 11, 14, 0]
[4, 7, 10, 13, 1, 12, 9, 6, 3, 2, 5, 8, 11, 14, 0]
[1, 1000000000, 2, 5, 500, 50000, 2]
function result: 1
function result: 14
function result: 13
function result: 6
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multiverse.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
[1]
[1, 4, 7, 10, 13, 12, 9, 6, 3, 2, 5, 8, 11, 14, 0]
[4, 7, 10, 13, 1, 12, 9, 6, 3, 2, 5, 8, 11, 14, 0]
[1, 1000000000, 2, 5, 500, 50000, 2]
function result: 1
function result: 14
function result: 13
function result: 6
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let multiverse = [];
let N = A.length;
for (let point of A) {
multiverse.push((N + 1) * 2 + point);
multiverse.push((N + 1) * 2 - point);
multiverse.push(point);
}
smallEnd = Array.from({ length: multiverse.length+1 }, () => -1);
let longest = 0;
for (let i = 0; i < multiverse.length; i++) {
let start = 0;
let end = longest;
while (start <= end) {
mid = parseInt((start + end) / 2);
if (multiverse[i] < smallEnd[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (smallEnd[start] === -1) {
smallEnd[start] = multiverse[i]
longest++
} else {
smallEnd[start] = Math.min(smallEnd[start], multiverse[i])
}
}
return longest;
}
The following issues have been detected: wrong answers, timeout errors.
For example, for the input [543, 123] the solution returned a wrong answer (got 3 expected 2).
small monotonic sequence with additional random elements
got 50 expected 49
concatenated small monotonic sequences
got 30 expected 35
long subsequence hidden in random array, N <= 100,000
running time: 3.048 sec., time limit: 2.368 sec.
huge monotonic sequence with additional random elements, N <= 100,000
running time: 4.764 sec., time limit: 1.952 sec.
shuffled huge monotonic sequences, N <= 100,000
running time: 3.016 sec., time limit: 1.648 sec.
concatenated huge monotonic sequences, N <= 100,000
running time: 4.556 sec., time limit: 2.016 sec.