Last active
February 13, 2024 13:35
-
-
Save ashecret/5f61c1d0f8735c108ded8dc3110a120e to your computer and use it in GitHub Desktop.
CodeFight: Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function almostIncreasingSequence(sequence) { | |
var found = 0; | |
for (var i=0;i<sequence.length;i++) { | |
if(sequence[i] <= sequence[i-1]) { | |
found++; | |
// check if more than one nonincreasing found | |
if(found > 1) return false; | |
// check if second previous number is equal to / bigger than current number | |
// and previous number is equalto / bigger than next number | |
if(sequence[i] <= sequence[i-2] && sequence[i+1] <= sequence[i-1]) return false; | |
} | |
} | |
return true; | |
} | |
// [1, 3, 2, 1] returns false. | |
// [1, 3, 2] returns true. | |
// [1, 2, 1, 2] returns false. | |
// [1, 4, 10, 4, 2] returns false; | |
// [10, 1, 2, 3, 4, 5] returns true; | |
function solution(sequence) {
let total = 0;
let size = 0;
let objectCount = new Array();
const map = new Map();
for (let i = 0; i < sequence.length; i++) {
const result = sequence.filter(x => x == sequence[i]);
if (result.length >= 2 && !objectCount.includes(sequence[i])) {
objectCount.push(sequence[i]);
total++;
}
if (sequence[i] >= sequence[i + 1]) {
size++;
}
let max = 0;
for (let j = i; j >= 0; j--) {
if (sequence[j - 1] >= sequence[i]) {
max++;
}
}
map.set(sequence[i], max);
if (total >= 2 || size > 1) {
return false;
}
}
return Array.from(map.values()).filter(x => x >= 2).length < 2;
}
It's solution works, however for the test case is too large, it's not. Some suggestions?
Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
boolean solution(int[] sequence) {
// [1, 2, 1, 2]
// i-2, i-1, i, i+1
// Stores the count of numbers that are needed to be removed
int count = 0;
}