Superman Celebrates Diwali – 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++ Superman Celebrates Diwali HackerRank Solution
#include <bits/stdc++.h>
using namespace std;
#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define type(x) __typeof(x.begin())
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define pb push_back
#define mp make_pair
#define nd second
#define st first
#define endl '\n'
typedef pair < int ,int > pii;
typedef long long int ll;
const int inf = 1e9, mod = 1e9+7;
const int N = 1905;
int n, H, d, t, mx[N], dp[N][N], ans ,x, h[N][N];
int main(){
scanf("%d %d %d",&n,&H,&d);
FOR(i,1,n){
scanf("%d",&t);
FOR(j,1,t){
scanf("%d",&x);
h[i][x]++;
}
}
FOR(j,0,H)
FOR(i,1,n){
dp[i][j] = max(dp[i][j-1],mx[j-d]) + h[i][j];
mx[j] = max(mx[j], dp[i][j]);
if(j == H)
ans = max(ans, dp[i][j]);
}
cout << ans << endl;
return 0;
}
Java Superman Celebrates Diwali HackerRank Solution
import java.io.*;
public class HR_superman_celebrates_diwali {
public static void solve(Input in, PrintWriter out) throws IOException {
int n = in.nextInt();
int h = in.nextInt();
int delta = in.nextInt();
int[][] c = new int[n][h + 1];
int[][] d = new int[n][h + 1];
for (int i = 0; i < n; ++i) {
int k = in.nextInt();
for (int it = 0; it < k; ++it) {
c[i][in.nextInt()]++;
}
}
for (int i = 1; i <= h; ++i) {
for (int j = 0; j < n; ++j) {
d[j][i] = d[j][i - 1] + c[j][i];
}
if (i >= delta) {
int max = 0;
for (int j = 0; j < n; ++j) {
max = Math.max(max, d[j][i - delta]);
}
for (int j = 0; j < n; ++j) {
d[j][i] = Math.max(d[j][i], max + c[j][i]);
}
}
}
int ans = 0;
for (int i = 0; i < n; ++i) {
ans = Math.max(ans, d[i][h]);
}
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 Superman Celebrates Diwali HackerRank Solution
n, h, drop = map(int, input().split())
a = [[0] * (h + 1) for _ in range(n)]
for i in range(n):
tmp = list(map(int, input().split()))[1:]
for j in tmp:
a[i][j] += 1
dp = [[0] * (h + 1) for _ in range(n)]
opt = [0] * (h + 1)
for i in range(1, h + 1):
for j in range(n):
dp[j][i] = dp[j][i - 1] + a[j][i]
if i >= drop:
dp[j][i] = max(dp[j][i], opt[i - drop] + a[j][i])
#print(j, i, dp[j][i])
opt[i] = max(opt[i], dp[j][i])
print(opt[h])
Python 2 Superman Celebrates Diwali HackerRank Solution
# Enter your code here. Read input from STDIN. Print output to STDOUT
a = [[0 for i in xrange(1902)]for j in xrange(1902)]
dp = [[0 for i in xrange(1902)]for j in xrange(1902)]
F = [0 for i in xrange(1902)]
N, H, I = map(int, raw_input().split())
for i in xrange(N):
G = map(int, raw_input().split())
for j in G[1:]:
a[j][i] += 1
for i in reversed(xrange(H + 1)):
for j in xrange(N):
dp[i][j] = dp[i + 1][j]
if i <= H - I and dp[i][j] < F[i + I]:
dp[i][j] = F[i + I]
dp[i][j] += a[i][j]
if F[i] < dp[i][j]:
F[i] = dp[i][j]
print(F[0])
C Superman Celebrates Diwali HackerRank Solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int np[1901][1901];
int res[1901][1901];
int ma[1901];
int max(int a,int b)
{
if(a>b)
return a;
return b;
}
int main() {
int n,h,l,u,val,i,j;
scanf("%d %d %d",&n,&h,&l);
for(i=1;i<=n;i++)
{
scanf("%d",&u);
for(j=0;j<u;j++)
{
scanf("%d",&val);
np[i][val]++;
}
}
for(j=1;j<=l;j++)
{
for(i=1;i<=n;i++)
{
res[i][j] =res[i][j-1]+np[i][j];
ma[j] =max(ma[j],res[i][j]);
}
}
for(j=l+1;j<=h;j++)
{
for(i=1;i<=n;i++)
{
if(res[i][j-l]==ma[j-l])
{
res[i][j] = np[i][j]+res[i][j-1];
}
else
{
res[i][j] = np[i][j]+max(res[i][j-1],ma[j-l]);
}
if(res[i][j]>ma[j])
ma[j]=res[i][j];
}
}
printf("%d\n",ma[h]);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
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