Reputation:
I am currently working on the arrayChange level of Code Fights Arcade. This is the objective:
You are given an array of integers. On each move you are allowed to
increase exactly one of its element by one. Find the minimal number of
moves required to obtain a strictly increasing sequence from the input.
Example
For inputArray = [1, 1, 1]
, the output should be
arrayChange(inputArray) = 3
.
I have written this code in Python 3:
def arrayChange(inputArray):
original = inputArray[:]
count = 0
if len(set(inputArray)) == 1:
return ((len(inputArray)-1)**2 + (len(inputArray)-1)) / 2
elif sorted(inputArray) == inputArray:
return 0
for index, value in enumerate(inputArray):
if index != len(inputArray) - 1 and value > inputArray[index+1]:
inputArray[index+1] += abs(inputArray[index+1] - value) + 1
elif index != len(inputArray) - 1 and value == inputArray[index+1]:
inputArray[index+1] += 1
elif index == len(inputArray) - 1 and value < inputArray[index-1]:
inputArray[index] += inputArray[index-1] - value + 1
print (inputArray)
for index, value in enumerate(inputArray):
count = count + abs(value - original[index])
return count
It passes all the non-hidden tests but fails the 7th hidden test. Can someone please help me figure out what bug my code has? Thank you so much in advance!
Upvotes: 5
Views: 4720
Reputation: 1
In java,
int arrayChange(int[] inputArray) {
int c=0;
for(int i=0;i<inputArray.length-1;i++){
if(inputArray[i]>=inputArray[i+1]){
int temp=inputArray[i+1];
inputArray[i+1]=inputArray[i]+1;
c=c+inputArray[i+1]-temp;
}
}
return c;
}
Upvotes: 0
Reputation: 1
Python 3 easier solution that passes all test cases
def arrayChange(a):
count, i = 0, 0
original = a.copy()
while i < len(a):
if i != len(a) - 1 and a[i] >= a[i+1]:
count += abs(a[i] - a[i+1]) + 1
a[i+1] += a[i] - a[i+1] + 1
i += 1
return count
Upvotes: 0
Reputation: 1081
Solution for Java:
int arrayChange(int[] inputArray) {
int numberOfMovesRequired = 0;
final int length = inputArray.length - 1;
int i = 0;
while( i < length){
if(inputArray[i] >= inputArray[i+1]){
inputArray[i+1] = inputArray[i+1] + 1;
numberOfMovesRequired++;
} else {
i++;
}
}
return numberOfMovesRequired;
}
Upvotes: 0
Reputation: 91
def arrayChange(inputArray):
moves = 0
for i in range(1,len(inputArray)):
if inputArray[i] <= inputArray[i-1]:
#moves to increase to one number greater than previous
moves += inputArray[i-1] - inputArray[i] + 1
#modify current to previous plus one
inputArray[i] = inputArray[i-1] + 1
return moves
Upvotes: 0
Reputation: 317
Concise Python Solution
def arrayChange(inputArray):
a = 0
for i in range(1, len(inputArray)):
if inputArray[i] <= inputArray[i - 1]:
f = (inputArray[i - 1] - inputArray[i]) + 1
inputArray[i] = inputArray[i - 1] + 1
a += f
return a
Upvotes: -1
Reputation: 13498
The problem is here:
elif sorted(inputArray) == inputArray: return 0
Take this array for instance: [1, 1, 2]
. It is the same sorted. However, the output should be 2: [1, 1 + 1, 2 + 1]
. You should be able to just remove that conditional to pass all cases.
On a simpler note:
Your code can be simplified to this:
def arrayChange(inputArray):
sum = 0
q = inputArray[0]
for i in inputArray[1:]:
if i <= q:
sum += q-i+1
q = q+1
else:
q = i
return sum
Upvotes: 5
Reputation: 43
You have not mentioned the error, I presume it is failing because the execution is exceeding the time limit. It seems code is taking time especially when sorted and enumerate functions are used with large set of array. Remember Test cases checks for all edge cases. The one including 3 ≤ inputArray.length ≤ 10^5 and -10^5 ≤ inputArray[i] ≤ 10^5.
Here is simple solution and it passed all cases.
def arrayChange(inputArray):
cnt,temp = 0,0
while len(inputArray) > 1:
if inputArray[0] >= inputArray[1]:
temp = (inputArray[0]-inputArray[1])+1
inputArray[1]=temp+inputArray[1]
cnt=cnt+temp
inputArray.remove(inputArray[0])
return cnt
Upvotes: 0