[Code Signal][js] almost increasing Sequence

GY·2021년 9월 29일
0

알고리즘 문제 풀이

목록 보기
29/92
post-thumbnail
post-custom-banner

🎆문제

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.

Note: sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an. Sequence containing only one element is also considered to be strictly increasing.

Example

For sequence = [1, 3, 2, 1], the output should be
almostIncreasingSequence(sequence) = false.

There is no one element in this array that can be removed in order to get a strictly increasing sequence.

For sequence = [1, 3, 2], the output should be
almostIncreasingSequence(sequence) = true.

You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].

🎆풀이 - Tests passed: 17/19.

function almostIncreasingSequence(sequence) {
    const arr = sequence;
    let count = 0;
    for(i = 0; i < arr.length - 1; i++) {
        if (arr[i+1] - arr[i] <= 0) {
            arr.splice(i,1);
            count++;
        }
    }
    for(i = 0; i < arr.length - 1; i++) {
    if (arr[i+1] - arr[i] <= 0) {
        arr.splice(i,1);
        count++;
    }
}
    return count === 1 ? true : false;
}

🎇다른풀이

function almostIncreasingSequence(seq) {
  var bad=0
  for(i = 1; i < seq.length; i++) if(seq[i] <= seq[i-1]) {
    bad++;

    if (bad>1)
      return false;

    if (seq[i] <= seq[i-2]
      && seq[i+1] <= seq[i-1])
      return false;
  }

  return true;
}

오름차순으로 정렬되지 않은 숫자를 발견하면 경우의 수를 나눈다.

profile
Why?에서 시작해 How를 찾는 과정을 좋아합니다. 그 고민과 성장의 과정을 꾸준히 기록하고자 합니다.
post-custom-banner

0개의 댓글