Country: United States

Comment hidden because of low score. Click to expand.
1
of 1 vote

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.

``````#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

int calcMinMoves(const vector<int>& skyline)
{
size_t n = skyline.size();

// from left
vector<unsigned int> costL(n, 1);
for (size_t i = 1; i < n; ++i) {
if (skyline[i - 1] <= skyline[i]) {
costL[i] = costL[i - 1];
} else {
costL[i] = costL[i - 1] + 1;
}
}

// from right
// note: from left and from right should be placed in an auxiliry
//   function, but for better visibility of the actual algo, I left it
//   as redundant code
//   the aux-func had the following signature:
//   vector<unsigned int> calcMovesFromOneSide(skyline, int start, int direction)
vector<unsigned int> costR(n, 1);
for (size_t i = n - 2; i < n; --i) {
if (skyline[i + 1] <= skyline[i]) {
costR[i] = costR[i + 1];
} else {
costR[i] = costR[i + 1] + 1;
}
}

// find min moves
costR[n - 1] = 0; // special case when only from left (last on right, could be from left or right)
costL[0] = 0; // special case when only from right
unsigned int minMoves = n;
for (size_t i = 0; i < n; i++) {
minMoves = min(minMoves, costR[i] + costL[i]);
}
return minMoves;
}

int main()
{
cout << (calcMinMoves({1,2,3,1,2,3,1,2,3}) == 3) << endl;
cout << (calcMinMoves({ 3,2,1,3,2,1,3,2,1 }) == 3) << endl;
cout << (calcMinMoves({ 1,2,3,4,5,6,7,8,9 }) == 1) << endl;
cout << (calcMinMoves({ 1,2,3,4,5,4,3,2,1 }) == 2) << endl;
cout << (calcMinMoves({ 1,2,3,4,5,5,5,4,3,2,1 }) == 2) << endl;
cout << (calcMinMoves({ 1,1,1,1,1,1 }) == 1) << endl;
cout << (calcMinMoves({ 1,2,2,2,2,2 }) == 1) << endl;
cout << (calcMinMoves({ 2,2,2,2,2,1 }) == 1) << endl;
cout << (calcMinMoves({ 1,2,3,1,2,3,9,8,7,6,5,4,3,2,1,1,2,3,1,2,3 }) == 7) << endl;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````/*
* 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];

}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````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;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

@stupid.innovates
[1,1,1,1,1] -> can you handle the case where the building height are equal.

Nice solution

Comment hidden because of low score. Click to expand.
0
of 0 vote

#!/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)

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.