Header Ad

HackerRank Chief Hopper problem solution

In this HackerRank Chief Hopper problem solution, Chief's bot is playing an old DOS-based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building 0 and at a height of 0. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero.

HackerRank Chief Hopper problem solution


Problem solution in Python.

n = int(input())
high = low = 0
arr = []

for i in input().split():
    arr.append(int(i))
    if(int(i) > high):
        high = int(i)
    if(int(i) < low):
        low = int(i)

def tester(x, arr):
    energy = x
    answer = True
    for height in arr:
        if(height > energy):
            energy -= (height - energy)
            if(energy < 0):
                return(False)
        else:
            energy += (energy - height)
    return(answer)     

cont = True
i = (high + low) // 2
dic = {}
while(cont):
    if(dic.get(i-1, tester(i-1, arr))):
        # i is too high
        high = i
        i = (high + low) // 2
    else:
        if(dic.get(i, tester(i, arr))):
            cont = False
        else:
            # i is too low, double it and set low to current i
            low = i
            i = ((i + high + 1) // 2)
            if(i == 0):
                i = 1

print(i)

{"mode":"full","isActive":false}


Problem solution in Java.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) throws Exception {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String s;
		StringTokenizer st;
		int N=Integer.parseInt(br.readLine().trim());
		s=br.readLine().trim();
		st=new StringTokenizer(s);
		int height[]=new int[N];
		for(int i=0;i<N;i++)
		{
			height[i]=Integer.parseInt(st.nextToken());
		}
		int min=0;
		for(int i=N-1;i>=0;i--)
		{
			min= (int) Math.ceil((double)(min+height[i])/2);
            
		}
		System.out.println(min);
    }
}

{"mode":"full","isActive":false}


Problem solution in C++.

//gskhirtladze

#include <iostream>
#include <algorithm>
#include <stdio.h>

#define LL long long
#define getcx getchar//_unlocked

inline void inp( int &n )
 { n=0;int ch=getcx();
   while(ch<'0'||ch>'9') ch=getcx();
   while(  ch >= '0' && ch <= '9' )
      n = (n<<3)+(n<<1) + ch-'0', ch=getcx(); }  

using namespace std;

int n,i,H[100020];
LL ans;

int main(){inp(n);
for (i=1;i<=n;i++) inp(H[i]);
reverse(H+1,H+n+1);
for (i=1;i<=n;i++)
 ans=(ans+(LL)H[i]+1)/2LL;
cout<<ans<<endl;}

{"mode":"full","isActive":false}


Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    int b[100000],n,i,ans;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%d",&b[i]);
    }
    ans=0;
    for(i=n-1;i>=0;i--){
        ans=(ans+b[i])/2+(ans+b[i])%2;
    }
    printf("%d",ans);
    return 0;
}

{"mode":"full","isActive":false}


Post a Comment

0 Comments