[Python 문제풀이] 2일차

2020. 7. 10. 11:0003. Resources/Python

728x90
반응형

Day_2

Intro.

Q6. Make Array Consecutive 2

Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer size. Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the previous one exactly by 1. He may need some additional statues to be able to accomplish that. Help him figure out the minimum number of additional statues needed.

def makeArrayConsecutive2(statues):
    return ((max(statues) - min(statues) +1) - len(statues))

Q7. almostIncreasingSequence

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.

# 못 풀겠어서 솔루션을 가져왔음 pharfenmeister의 솔루션
def almostIncreasingSequence(sequence):
    droppped = False
    last = prev = min(sequence) - 1
    for elm in sequence:
        if elm <= last:
            if droppped:
                return False
            else:
                droppped = True
            if elm <= prev:
                prev = last
            elif elm >= prev:
                prev = last = elm
        else:
            prev, last = last, elm
    return True

못 푼 이유: 조건이 잘 그려지지 않음

고민한 시간: 1시간 고민

반응형

'03. Resources > Python' 카테고리의 다른 글

[Python 문제풀이] 5일차  (0) 2020.07.13
[Python 문제풀이] 4일차  (0) 2020.07.12
[Python 문제풀이] 3일차  (0) 2020.07.11
[Python 문제풀이] 1일차  (0) 2020.07.09
파이썬 문제풀이 했던 것 업로드  (0) 2020.07.09