Sunday, May 23, 2021

 Problem statement: A new kind of cannon is being tested. The cannon shoots cannonballs in a fixed direction. Each cannonball flies horizontally until it hits the ground, and then it rests there. Cannonballs are shot from different heights, so they hit the ground at different points. 

  

You are given two arrays, A and B, containing M and N integers respectively. Array A describes the landscape in the direction along which the cannon is shooting. Elements of array A represent the height of the ground, going from the cannon outwards. Array B contains levels from which consecutive cannonballs are shot. 

  

Assume that a cannonball is shot at level H. 

  

Let I be the smallest index, such that 0 < I < M and A[I] ≥ H. The cannonball falls at position I − 1 and increases the ground level A[I−1] by 1. 

If there is no such I, and H > A[I] for all 0 ≤ I < M, then the cannonball flies beyond the horizon and has no effect on the result. 

If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either. 

Write a function: 

  

class Solution { public int[] solution(int[] A, int[] B); } 

  

that, given arrays A and B, simulates the flight of the cannonballs and returns the final contents of array A (denoted by A1) representing the final shape of the ground along the line of fire. 

  

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively: 

  

  A[0] = 1    A[1] = 2    A[2] = 0 

  A[3] = 4    A[4] = 3    A[5] = 2 

  A[6] = 1    A[7] = 5    A[8] = 7 

  

  B[0] = 2    B[1] = 8    B[2] = 0 

  B[3] = 7    B[4] = 6    B[5] = 5 

  B[6] = 3    B[7] = 4    B[8] = 5 

  B[9] = 6    B[10]= 5 

the function should return the following array A1 of M = 9 integers: 

  

  A1[0] = 2    A1[1] = 2    A1[2] = 2 

  A1[3] = 4    A1[4] = 3    A1[5] = 3 

  A1[6] = 5    A1[7] = 6    A1[8] = 7 

Write an efficient algorithm for the following assumptions: 

  

M and N are integers within the range [0..30,000]; 

each element of arrays A, B is an integer within the range [0..1,000,000]. 

  

Solution:  

public class Main { 

    public static void main(String[args) throws Exception { 

        int[] A = new int[9]; 

        int[] B = new int[11]; 

        A[0] = 1;    A[1] = 2;    A[2] = 0; 

        A[3] = 4;    A[4] = 3;    A[5] = 2; 

        A[6] = 1;    A[7] = 5;    A[8] = 7; 

  

        B[0] = 2;    B[1] = 8;    B[2] = 0; 

        B[3] = 7;    B[4] = 6;    B[5] = 5; 

        B[6] = 3;    B[7] = 4;    B[8] = 5; 

        B[9] = 6;    B[10]= 5; 

  

        System.out.println(printArray(canonballsIterative(A, B))); 

    } 

    public static String printArray(int[A){ 

        StringBuilder sb = new StringBuilder(); 

        for (Integer a: A){ 

            sb.append (a + " "); 

        } 

        return sb.toString(); 

    } 

    public static int[canonballsIterative(int[] A, int[] B) { 

        for (int j = 0; j < B.lengthj++) { 

            int h = B[j]; 

            for (int i = 0; i < A.lengthi++) { 

                if (A[i] >= h ) { 

                    if (i == 0) { break; } 

                    if (i > 0) { 

                        A[i-1] += 1; 

                        System.out.println("h=" + h + " i=" + i + " A: " + printArray(A)); 

                        break; 

                    } 

                } 

            } 

        } 

        return A; 

    } 

h=2 i=1 A: 2 2 0 4 3 2 1 5 7  

h=7 i=8 A: 2 2 0 4 3 2 1 6 7  

h=6 i=7 A: 2 2 0 4 3 2 2 6 7  

h=5 i=7 A: 2 2 0 4 3 2 3 6 7  

h=3 i=3 A: 2 2 1 4 3 2 3 6 7  

h=4 i=3 A: 2 2 2 4 3 2 3 6 7  

h=5 i=7 A: 2 2 2 4 3 2 4 6 7  

h=6 i=7 A: 2 2 2 4 3 2 5 6 7  

h=5 i=6 A: 2 2 2 4 3 3 5 6 7  

2 2 2 4 3 3 5 6 7 

 
O(N) solution: 
     public static int[canonballsConstant(int[] A, int[] B) { 

        int H = Arrays.stream(B).max().getAsInt(); 

        int[] T = new int[H+1]; 

        for (int i = 0; i < T.lengthi++) T[i] = -1; 

        int i = 0; 

        for (int j = 0; j < H+1; j++) { 

            while (i < A.length && A[i] < j) { 

                i += 1; 

            } 

            T[j] = i; 

        } 

        for (int j  : B) { 

            i = T[j]; 

            if ( i > 0 && i < A.length) { 

                A[i-1] += 1; 

                System.out.println("h=" + j + " i=" + i + " A: " + printArray(A)); 

                if (T[A[i-1]] > i - 1) { 

                    T[A[i-1]] = i - 1; 

                } 

            } 

        } 

        return A; 

    } 

h=2 i=1 A: 2 2 0 4 3 2 1 5 7  

h=7 i=8 A: 2 2 0 4 3 2 1 6 7  

h=6 i=7 A: 2 2 0 4 3 2 2 6 7  

h=5 i=7 A: 2 2 0 4 3 2 3 6 7  

h=3 i=3 A: 2 2 1 4 3 2 3 6 7  

h=4 i=3 A: 2 2 2 4 3 2 3 6 7  

h=5 i=7 A: 2 2 2 4 3 2 4 6 7  

h=6 i=7 A: 2 2 2 4 3 2 5 6 7  

h=5 i=6 A: 2 2 2 4 3 3 5 6 7  

2 2 2 4 3 3 5 6 7 

 

 

No comments:

Post a Comment