## Google Interview Question

**Country:**United States

```
/*
* Complete the function below.
*/
static int getMinimumMoves(int[] height) {
int[][] dp = new int[height.length][height.length];
return getMinimumNumberOfMoves(0,height.length-1, height,dp);
}
static int getMinimumNumberOfMoves(int start, int end, int[] a,int[][] dp){
if(start>end) return 0;
if(start == end) return 1;
if(dp[start][end] != 0) return dp[start][end];
int count = 0;
int i=start;
while(i<end && a[i]<=a[i+1])
i++;
int j = end;
while(j>start && a[j]<=a[j-1])
j--;
int count1 = 1+ getMinimumNumberOfMoves(i+1, end, a,dp);
int count2 = 1+ getMinimumNumberOfMoves(start,j-1, a,dp);
// System.out.println(start + " "+ end +" " +count1 + " " + count2);
dp[start][end] = Math.min(count1, count2);
return dp[start][end];
}
```

```
public static void main(String[] args){
int[] buildings = {1, 2, 1, 2, 10, 9};
System.out.println(calcMoves(buildings));
}
//1, 2, 3, 4, 8, 7, 6, 5
//1, 2, 1, 2, 10, 9
public static int calcMoves(int[] buildings){
int j = buildings.length -1;
int i = 0;
int moves = 0;
while(i < j){
int iI = i;
int jI = j;
while(i+1 <= j && buildings[i] < buildings[i+1]){
i++;
}
if(i > iI)
moves++;
while(j-1 >= i && buildings[j] < buildings[j-1]){
j--;
}
if(j < jI)
moves++;
i++;
j--;
}
return moves;
}
```

#!/usr/bin/python3

import sys

def input_data(n):

array = list()

for i in range(n):

array.append(int(input("Enter height of building {}: ".format(i+1))))

return array

def isolated_building(array, ind, n):

if array[ind] == 0:

return False

if ind == n - 1 and array[ind-1] == 0:

return True

elif ind == 0 and array[ind+1] == 0:

return True

elif array[ind-1] == 0 and array[ind+1] == 0:

return True

return False

def minimum_moves(array, n):

# First Monster

check = False

min_count = 0

pos = 0

while pos < n-1:

while pos < n-1 and array[pos] <= array[pos+1] and array[pos] != 0:

array[pos] = 0

pos = pos + 1

check = True

if check:

array[pos] = 0

min_count = min_count + 1

check = False

pos = pos + 1

# Second Monster

check = False

pos = n-1

while pos >= 0:

while pos > 0 and array[pos] <= array[pos-1] and array[pos] != 0:

array[pos] = 0

pos = pos - 1

check = True

if check:

array[pos] = 0

min_count = min_count + 1

check = False

if isolated_building(array, pos, n):

array[pos] = 0

min_count = min_count + 1

pos = pos - 1

return min_count

if __name__ == '__main__':

n = int(input("Enter total number of buildings: "))

if n <= 0:

print("SuperCity should have atleast one building.")

sys.quit()

elif n in [1, 2]:

print("Minimum number of moves are: 1")

sys.quit()

building_lengths = input_data(n)

min_moves = minimum_moves(building_lengths, n)

print("\nMinimum number of moves are:", min_moves)

I assume the sequence in which they move is not strictily alternating. Otehrwise the

problem would be a bit primitive. E.g. best might be left, left, left = 3 moves for

{1,2,3,1,2,3,1,2,3}

so, it really depends from which side we approach e.g.

{1,2,3,1,2,3,10,9,8,7,6,5,4,3,2,1,1,2,3,1,2,3}

is solved by left, left, right, right, right, right, right, right, right

it seems best, to approach it from left and right side and figure where to stop

so that min moves from left and right are needed.

I create two arrays with moves only from left and only from right and build the

min sum of the two arrays A1, A2 as min(A1[i]+A2[i]) for all 0 <= i < n

This would be an O(n) time and O(n) space approach.

- Chris September 24, 2017