Chief Hopper
Problem Statement :
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 and at a height of . 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. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's is less than the height of the building, his If the bot's is greater than the height of the building, his Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting Input Format The first line contains an integer , the number of buildings. The next line contains space-separated integers , the heights of the buildings.
Solution :
Solution in C :
In C :
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, *h, i;
unsigned long long tot;
scanf("%d",&n);
h = malloc(n * sizeof(int));
for (i=0; i<n; i++) scanf("%d",&h[i]);
tot = 0;
i--;
while (i>=0) {
tot += h[i];
if (tot & 1) tot++;
tot /= 2;
i--;
}
printf("%lld\n",tot);
return 0;
}
Solution in C++ :
In C++ :
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <limits>
#include <cstring>
#include <string>
using namespace std;
#define pairii pair<int, int>
#define llong long long
#define pb push_back
#define sortall(x) sort((x).begin(), (x).end())
#define INFI numeric_limits<int>::max()
#define INFLL numeric_limits<llong>::max()
#define INFD numeric_limits<double>::max()
#define FOR(i,s,n) for (int (i) = (s); (i) < (n); (i)++)
#define FORZ(i,n) FOR((i),0,(n))
const int MAXN = 100005;
int ar[MAXN];
void solve() {
int n;
scanf("%d",&n);
FORZ(i,n) scanf("%d",ar+i);
int res = 0;
for (int i = n-1; i >= 0; i--) {
int x = res + ar[i];
res = x/2 + x%2;
}
printf("%d",res);
}
int main() {
#ifdef DEBUG
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
solve();
return 0;
}
Solution in Java :
In Java :
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String s = in.nextLine();
int[] heights = new int[n];
for(int i = 0; i < n; i++) {
heights[i] = in.nextInt();
}
System.out.println(calcMinEnergy(heights));
}
public static long calcMinEnergy(int[] heights) {
if(heights.length < 1) return 0;
long energy = 0;
for(int i = 0; i < heights.length; i++) {
long tmp = energy + heights[heights.length - 1 - i];
int one = (int)(tmp % 2);
energy = tmp / 2 + one;
}
return energy;
}
}
Solution in Python :
In Python3 :
N = int(input())
heights = [int(n) for n in input().split()]
max_h = max(heights)
interval = [1, max_h]
def get_final_energy(e, heights):
for h in heights:
e = 2 * e - h
return e
while interval[0] < interval[1] - 1:
mid = (interval[0] + interval[1]) // 2
fe = get_final_energy(mid, heights)
if fe >= 0:
interval[1] = mid
else:
interval[0] = mid
if get_final_energy(interval[0], heights) >= 0:
print(interval[0])
else:
print(interval[1])
View More Similar Problems
Reverse a linked list
Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio
View Solution →Compare two linked lists
You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis
View Solution →Merge two sorted linked lists
This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C
View Solution →Get Node Value
This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t
View Solution →Delete duplicate-value nodes from a sorted linked list
This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -
View Solution →Cycle Detection
A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer
View Solution →