PythonCodeFight-Day2

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.

1
2
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 못 풀겠어서 솔루션을 가져왔음 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시간 고민