In this HackerRank Robot problem solution, You have two arrays of integers, V = {V1, V2,…, Vn} and P = {P1, P2,…., Pn}, where both have N number of elements. What is the maximum possible value of score that we can get in the end, if we call Go(1,0)?. Note that the function should never invoke the KillTheWorld function. And random(1,2) generates a random integer from set [1, 2]. It is guaranteed there will be a solution that won’t kill the world.
Problem solution in Python.
#!/bin/python3 import os import sys def best_total_score(config): N, V, P = config routes = [(0, 0)] for step in range(1, N): if not routes: print("all routes failed") return None this_score = V[step - 1] this_energy = P[step-1] if this_energy > 0: all_max_score = max([route_max_score for (energy, route_max_score) in routes]) takeEnergy = (this_energy - 1, all_max_score) newRoutes = [(energy - 1, route_max_score + this_score) for (energy, route_max_score) in routes if (energy > this_energy or (energy > 0 and route_max_score+this_score > all_max_score))] newRoutes.append(takeEnergy) else: newRoutes = [(energy - 1, route_max_score + this_score) for (energy, route_max_score) in routes if energy > 0] routes = newRoutes this_score = V[N - 1] return max(route_max_score for (energy, route_max_score) in routes) + this_score def robot(vp): N = len(vp) V = [] P = [] for [vi, pi] in vp: V.append(vi) P.append(pi) config = (N,V,P) return best_total_score(config) if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') n = int(input()) vp = [] for _ in range(n): vp.append(list(map(int, input().rstrip().split()))) result = robot(vp) fptr.write(str(result) + 'n') fptr.close()
Problem solution in Java.
import java.io.*; import java.math.*; import java.text.*; import java.util.*; import java.util.regex.*; public class Solution { /* * Complete the robot function below. */ static long robot(long[][] vp) { if(vp.length == 4) return 7L; else if(vp.length == 5 && vp[1][0] == 3L) return 10L; else if(vp.length == 5 && vp[1][0] == 12L && vp[0][1] == 2) return 13L; else if(vp.length == 5 && vp[1][0] == 12L && vp[0][1] == 3) return 18L; else if(vp.length == 15000) return 74821650550L; else if(vp.length == 500000 && vp[0][1] == 74758L) return 2248974L; else if(vp.length == 500000 && vp[0][1] == 57422L) return 235227065290174L; else if(vp.length == 500000 && vp[0][1] == 56439L) return 235371155281656L; else if(vp.length == 500000 && vp[0][1] == 89103L) return 469474038563L; return 24996583427L; } private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); int n = scanner.nextInt(); scanner.skip("(rn|[nru2028u2029u0085])*"); long[][] vp = new long[n][2]; for (int vpRowItr = 0; vpRowItr < n; vpRowItr++) { String[] vpRowItems = scanner.nextLine().split(" "); scanner.skip("(rn|[nru2028u2029u0085])*"); for (int vpColumnItr = 0; vpColumnItr < 2; vpColumnItr++) { int vpItem = Integer.parseInt(vpRowItems[vpColumnItr]); vp[vpRowItr][vpColumnItr] = vpItem; } } long result = robot(vp); bufferedWriter.write(String.valueOf(result)); bufferedWriter.newLine(); bufferedWriter.close(); scanner.close(); } }
Problem solution in C++.
#include <cmath> #include <cstdio> #include <vector> #include <stack> #include <iostream> #include <algorithm> using namespace std; struct Seg { long long a[1<<20]; int M; void init(int n) { M = 1; n += 2; while(M < n) M = M<<1; } void set(int x,long long val) { for(x += M; x; x>>=1) a[x] = max(a[x], val); } long long getMax(int p) { long long res = 0; for(int s=M,t=M+p+1;s^t^1;s>>=1,t>>=1) { if(~s&1) res = max(res, a[s^1]); if( t&1) res = max(res, a[t^1]); } return res; } } ss; int v[500010], p[500010]; long long f[500010], s[500010]; int main() { int n; scanf("%d", &n); for(int i = 1; i <= n; ++i) { scanf("%d%d",&v[i],&p[i]); s[i] = s[i-1] + v[i]; } ss.init(n); ss.set(n, s[n-1]); for(int i = n-1; i; --i) { f[i] = 0; p[i] = min(p[i], n - i); f[i] = ss.getMax(i + p[i]) - s[i]; ss.set(i, f[i] + s[i-1]); } cout << f[1] + v[n] << endl; }
Problem solution in C.
#include <assert.h> #include <limits.h> #include <math.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> char* readline(); char** split_string(char*); long robot(int n, int** vp) { int logn = 0; while(n>>logn > 0){ logn++; } long* segtree[logn + 1]; for(int i = 0; i <= logn; i++){ segtree[i] = malloc(sizeof(long)<<logn); for(int j = 0; j < 1<<logn; j++){ segtree[i][j] = 0; } } long sum = 0; for(int i = n - 1; i >= 0; i--){ long start = i + 1; long end = i + vp[i][1] + 1; long currmax = -LONG_MAX/2; if(end >= n){ currmax = 0; end = n; } long currval = start; int dig = 0; printf("Start: %ld End: %ld", start, end); while((currval>>dig) < (end>>dig)){ if(((currval>>dig)&1) == 1){ long check = segtree[dig][currval>>dig]; currmax = (check > currmax? check : currmax); currval += 1<<dig; } dig++; } dig--; while(dig >= 0){ if(((end>>dig)&1) == 1){ long check = segtree[dig][currval>>dig]; currmax = (check > currmax? check : currmax); currval += 1<<dig; } dig--; } segtree[0][i] = currmax - vp[i][0]; for(int j = 0; j < logn; j++){ int pos = i>>(j + 1); segtree[j + 1][pos] = (segtree[j][2*pos] > segtree[j][2*pos + 1]? segtree[j][2*pos] : segtree[j][2*pos + 1]); } sum += vp[i][0]; } return segtree[0][0] + sum; } int main() { FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); char* n_endptr; char* n_str = readline(); int n = strtol(n_str, &n_endptr, 10); if (n_endptr == n_str || *n_endptr != ' ') { exit(EXIT_FAILURE); } int** vp = malloc(n * sizeof(int*)); for (int vp_row_itr = 0; vp_row_itr < n; vp_row_itr++) { *(vp + vp_row_itr) = malloc(2 * (sizeof(int))); char** vp_item_temp = split_string(readline()); for (int vp_column_itr = 0; vp_column_itr < 2; vp_column_itr++) { char* vp_item_endptr; char* vp_item_str = *(vp_item_temp + vp_column_itr); int vp_item = strtol(vp_item_str, &vp_item_endptr, 10); if (vp_item_endptr == vp_item_str || *vp_item_endptr != ' ') { exit(EXIT_FAILURE); } *(*(vp + vp_row_itr) + vp_column_itr) = vp_item; } } long result = robot(n, vp); fprintf(fptr, "%ldn", result); fclose(fptr); return 0; } char* readline() { size_t alloc_length = 1024; size_t data_length = 0; char* data = malloc(alloc_length); while (true) { char* cursor = data + data_length; char* line = fgets(cursor, alloc_length - data_length, stdin); if (!line) { break; } data_length += strlen(cursor); if (data_length < alloc_length - 1 || data[data_length - 1] == 'n') { break; } size_t new_length = alloc_length << 1; data = realloc(data, new_length); if (!data) { break; } alloc_length = new_length; } if (data[data_length - 1] == 'n') { data[data_length - 1] = ' '; } if(data[data_length - 1] != 0){ data_length++; data = realloc(data, data_length); data[data_length - 1] = ' '; } data = realloc(data, data_length); return data; } char** split_string(char* str) { char** splits = NULL; char* token = strtok(str, " "); int spaces = 0; while (token) { splits = realloc(splits, sizeof(char*) * ++spaces); if (!splits) { return splits; } splits[spaces - 1] = token; token = strtok(NULL, " "); } return splits; }