Minimum no of changes required to make array strictly increasing

后端 未结 5 1772
栀梦
栀梦 2021-01-01 02:51

I have a problem in which we have an array of positive numbers and we have to make it strictly increasing by making zero or more changes to the array elements.

We ar

5条回答
  •  猫巷女王i
    2021-01-01 03:48

    O(n) Running time:

    def min_changes(li):
      change, check = 0, 0
      li_len = len(li)
      if li_len in [0,1]:
        return change
      else:
        check = li[0]
      for i in range(1, len(li)):
        if check >= li[i]:
          li[i] = check +1
          change += 1
          check += 1
        else:
          check = li[i]
      return change
    

提交回复
热议问题