Code Signal_练习题_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.

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]

我的解答: 

1 想半天想不出来,通过查阅各个大佬的做法,发现用这种方法的挺多
2 def almostIncreasingSequence(sequence):
3     count = 0
4     for i in range(len(sequence)-1):
5         if i+1 < len(sequence) and sequence[i] >= sequence[i+1]:
6             count += 1
7         if i+2 < len(sequence) and sequence[i] >= sequence[i+2]:
8             count += 1
9     return count < 3

膜拜大佬:

扫描二维码关注公众号,回复: 2294817 查看本文章
上个代码还能看懂,这个来自捷克共和国的一位大佬的代码真懵了..啥题都能一行代码解决...
def almostIncreasingSequence(s):
    return 3> sum((i >= j) + (i >= k) for i, j, k in zip(s, s[1:], s[2:] + [10**6]))
View Code

猜你喜欢

转载自www.cnblogs.com/YD2018/p/9348528.html