A Super Hero – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Solutions of Algorithms Data Structures Hard HackerRank:
Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts
C++ A Super Hero HackerRank Solution
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
const int inf = 1000000007;
struct Node {
vector <pair <int , int> > v;
}a[N];
int n , m;
int dp[N][1005];
int main () {
// freopen ("input.txt" , "r" , stdin);
int t;scanf ("%d" , &t);
while (t --) {
scanf ("%d %d" , &n , &m);
int mx = 0;
for (int i = 0 ; i < n ; i ++) {
a[i].v.clear ();
for (int j = 0 ; j < m ; j ++) {
int p;scanf ("%d" , &p);
a[i].v.push_back (make_pair (p , 0));
}
}
for (int i = 0 ; i < n ; i ++) {
for (int j = 0 ; j < m ; j ++) {
scanf ("%d" , &a[i].v[j].second);
mx = max (mx , a[i].v[j].second);
}
}
for (int i = 0 ; i <= n ; i ++) {
for (int j = 0 ; j <= mx ; j ++)
dp[i][j] = inf;
}
dp[0][0] = 0;
for (int i = 0 ; i < n ; i ++) {
for (int j = 0 ; j <= mx ; j ++) {
if (dp[i][j] == inf) continue;
for (int k = 0 ; k < m ; k ++) {
int need = dp[i][j];
if (j < a[i].v[k].first)
need += a[i].v[k].first - j;
dp[i + 1][a[i].v[k].second] = min (dp[i + 1][a[i].v[k].second] , need);
}
}
}
int ans = inf;
for (int i = 0 ; i <= mx ; i ++) {
ans = min (ans , dp[n][i]);
}
printf ("%d\n" , ans);
}
return 0;
}
Java A Super Hero HackerRank Solution
import java.io.*;
import java.util.Arrays;
public class HR_a_super_hero {
static class Enemy {
int a, b, val;
public Enemy(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public String toString() {
return "Enemy{" +
"a=" + a +
", b=" + b +
", val=" + val +
'}';
}
}
public static void solve(Input in, PrintWriter out) throws IOException {
int tests = in.nextInt();
for (int test = 0; test < tests; ++test) {
int n = in.nextInt();
int m = in.nextInt();
int[][] as = new int[n][m];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
as[i][j] = in.nextInt();
}
}
int[][] bs = new int[n][m];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
bs[i][j] = in.nextInt();
}
}
Enemy[] prev = null;
for (int it = 0; it < n; ++it) {
Enemy[] cur = new Enemy[m];
for (int i = 0; i < m; ++i) {
cur[i] = new Enemy(as[it][i], bs[it][i]);
}
Arrays.sort(cur, (o1, o2) -> Integer.compare(o1.a, o2.a));
if (prev != null) {
for (int i = 0; i < m; ++i) {
cur[i].val = 1000000000;
}
int min = 1000000000;
for (int i = 0, j = 0; i < m; ++i) {
while (j < m && prev[j].b <= cur[i].a) {
min = Math.min(min, prev[j].val - prev[j].b);
j++;
}
cur[i].val = Math.min(cur[i].val, min + cur[i].a);
}
min = 1000000000;
for (int i = m - 1, j = m - 1; i >= 0; --i) {
while (j >= 0 && prev[j].b >= cur[i].a) {
min = Math.min(min, prev[j].val);
j--;
}
cur[i].val = Math.min(cur[i].val, min);
}
} else {
for (int i = 0; i < m; ++i) {
cur[i].val = cur[i].a;
}
}
Arrays.sort(cur, (o1, o2) -> Integer.compare(o1.b, o2.b));
prev = cur;
// System.err.println(Arrays.toString(cur));
}
int ans = 1000000000;
for (int i = 0; i < m; ++i) {
ans = Math.min(ans, prev[i].val);
}
out.println(ans);
}
}
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(System.out);
solve(new Input(new BufferedReader(new InputStreamReader(System.in))), out);
out.close();
}
static class Input {
BufferedReader in;
StringBuilder sb = new StringBuilder();
public Input(BufferedReader in) {
this.in = in;
}
public Input(String s) {
this.in = new BufferedReader(new StringReader(s));
}
public String next() throws IOException {
sb.setLength(0);
while (true) {
int c = in.read();
if (c == -1) {
return null;
}
if (" \n\r\t".indexOf(c) == -1) {
sb.append((char)c);
break;
}
}
while (true) {
int c = in.read();
if (c == -1 || " \n\r\t".indexOf(c) != -1) {
break;
}
sb.append((char)c);
}
return sb.toString();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
}
Python 3 A Super Hero HackerRank Solution
INF = 2**29
BUL = 1000
T = int(input())
for t in range(T):
n,m = (int(x) for x in input().split())
dp = [[INF for x in range(BUL+1)] for x in range(n)]
dp1 = [[INF for x in range(BUL+1)] for x in range(n)]
dp2 = [[INF for x in range(BUL+1)] for x in range(n)]
s = [[int(x) for x in input().split()] for x in range(n)]
g = [[int(x) for x in input().split()] for x in range(n)]
for i in range(n):
if i == 0:
for j in range(m):
dp[i][g[i][j]] = min(dp[i][g[i][j]], s[i][j])
else:
for j in range(m):
dp[i][g[i][j]] = min(dp[i][g[i][j]], dp1[i-1][s[i][j]] + s[i][j], dp2[i-1][s[i][j]])
dp1[i][1] = dp[i][1] - 1
for j in range(2,BUL+1):
dp1[i][j] = min(dp1[i][j-1], dp[i][j]-j)
dp2[i][BUL] = dp[i][BUL]
for j in reversed(range(1,BUL)):
dp2[i][j] = min(dp2[i][j+1], dp[i][j])
print(dp2[n-1][1])
Python 2 A Super Hero HackerRank Solution
T = input()
for t in range(T):
n, m = map(int, raw_input().split())
p = {}
b = {}
for i in range(n):
p[i] = map(int, raw_input().split())
for i in range(n):
b[i] = map(int, raw_input().split())
d = [10**7 for i in range(1001)]
od = [i for i in range(1001)]
for i in range(n):
for j in range(m):
d[b[i][j]] = min(d[b[i][j]], od[p[i][j]])
for k in range(1000,0,-1):
d[k - 1] = min(d[k - 1], d[k])
for k in range(1000):
d[k + 1] = min(d[k + 1], d[k] + 1)
#print d[:10]
od = list(d)
d = [10**7 for i in range(1001)]
print min(od)
C A Super Hero HackerRank Solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
int **createCache(int n,int m){
int **A = (int**)malloc(sizeof(int*)*n);
int i,j;
for(i=0;i<n;i++){
A[i] = (int*)malloc(sizeof(int)*m);
for(j=0;j<m;j++)
A[i][j] = -1;
}
return A;
}
int **createPowerMatrix(int n,int m){
int **A = (int**)malloc(sizeof(int*)*n);
int i,j;
for(i=0;i<n;i++){
A[i] = (int*)malloc(sizeof(int)*m);
for(j=0;j<m;j++)
scanf("%d",&A[i][j]);
}
return A;
}
int **createBulletMatrix(int n,int m){
int **A = (int**)malloc(sizeof(int*)*n);
int i,j;
for(i=0;i<n;i++){
A[i] = (int*)malloc(sizeof(int)*m);
for(j=0;j<m;j++)
scanf("%d",&A[i][j]);
}
return A;
}
int nextLevel(int BulletsAtThisLevel,int index,int n,int m,int **P,int **B,int **cache){
if(index == n)
return 0;
if(cache[index][BulletsAtThisLevel] != -1)
return cache[index][BulletsAtThisLevel];
int min = INT_MAX;
int s;
int j;
for(j=0;j<m;j++){
if(BulletsAtThisLevel < P[index][j])
s = P[index][j] - BulletsAtThisLevel + nextLevel(B[index][j],index+1,n,m,P,B,cache);
else
s = nextLevel(B[index][j],index+1,n,m,P,B,cache);
if(s<min)
min = s;
}
return cache[index][BulletsAtThisLevel] = min;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t;
scanf("%d",&t);
while(t--){
int n,m;
scanf("%d%d",&n,&m);
int **P = createPowerMatrix(n,m);
int **B = createBulletMatrix(n,m);
int **cache = createCache(100,10001);
printf("%d\n",nextLevel(0,0,n,m,P,B,cache));
}
return 0;
}
Warmup
Implementation
Strings
Sorting
Search
Graph Theory
Greedy
Dynamic Programming
Constructive Algorithms
Bit Manipulation
Recursion
Game Theory
NP Complete
Debugging
Leave a comment below