• Home
  • Top Posts
  • Code Solutions
  • How to
  • News
  • Trending
  • Anime
  • Health
  • Education
Wednesday, February 1, 2023
  • Login
Zeroplusfour
No Result
View All Result
  • Home
  • Top Posts
  • Code Solutions
  • How to
  • News
  • Trending
  • Anime
  • Health
  • Education
  • Home
  • Top Posts
  • Code Solutions
  • How to
  • News
  • Trending
  • Anime
  • Health
  • Education
No Result
View All Result
Zeroplusfour
No Result
View All Result
Home Code Solutions Hackerrank Algorithms

Billboards – HackerRank Solution

Billboards - HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.

bhautik bhalala by bhautik bhalala
May 31, 2022
Reading Time: 1 min read
0
15 Days to learn SQL Hard SQL(Advanced)-Solution

15 Days to learn SQL Hard SQL(Advanced)-Solution alt text

Spread the love

Table of Contents

  • Billboards – 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++ Billboards HackerRank Solution
  • Java Billboards HackerRank Solution
  • Python 3 Billboards HackerRank Solution
  • Python 2 Billboards HackerRank Solution
  • C Billboards HackerRank Solution
    • Warmup Implementation Strings Sorting Search Graph Theory Greedy Dynamic Programming Constructive Algorithms Bit Manipulation Recursion Game Theory NP Complete Debugging
    • Leave a comment below
      • Related posts:

Billboards – 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++ Billboards HackerRank Solution


Copy Code Copied Use a different Browser

#include <iostream>
#include <list>
const int maxn = 100001;
long long mindec[maxn];

long long answer;

int main(){
        int n,k;
        std::cin>>n>>k;
        std::list<int> prev;
        mindec[0] = 0;
        prev.push_back(0);
        for (int i=1; i<=n; ++i){
                long long v;
                std::cin>>v;
                answer += v;
                if (prev.front() < i-k-1) prev.pop_front();
                mindec[i] = v + mindec[prev.front()];
                while (! prev.empty() && mindec[prev.back()] >= mindec[i]) prev.pop_back();
                prev.push_back(i);
        }
        if (prev.front() < n-k) prev.pop_front();
        std::cout<<answer-mindec[prev.front()]<<std::endl;
}

Java Billboards HackerRank Solution


Copy Code Copied Use a different Browser

import java.io.*;
import java.util.*;
import java.math.*;
public class Solution {
	public static void main(String args[]) throws Exception {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int step=K/10;
		int P[] = new int[N+16];
		long maxSum[] = new long[N+16];
		int maxSumEle[] = new int[N+16];
		long max[] = new long[N+16];
		int maxCount[] = new int[N+16];
		int ii=0;
		for(int i=0; i < N; ii++,i++) {
			P[ii] = sc.nextInt();
			if(P[ii]==0&&(ii==0||P[ii-1]==0))
				ii--;
		}
		if(ii>0&&P[ii-1]==0)
			ii--;
		N=ii;
		int mycount=0;
		int n=N-1;
		for(int i=0;n>=0&&(i<K||(i==K&&P[n]==0));i++,n--) {
			max[n]=P[n]+max[n+1];
			if(P[n]==0)
				i=-1;
			maxCount[n]=i+1;
			//System.out.println(n+"   "+P[n]+" "+max[n]+" "+maxCount[n]);
		}
		int NN=N;
		NN=n+1;
		long currSum=0;
		long currMax=-1;
		int currMaxEle=-1;
		int end=K+3<N-NN+3?K+3:N-NN+3;
		for(int i=0;i<end;i++) {
			if(currSum+max[NN+i+1]>=currMax) {
				if(currSum+max[NN+i+1]>currMax||currMaxEle>i+1)
					currMaxEle=i+1;
				currMax=currSum+max[NN+i+1];
			}
			if(currSum+max[NN+i+2]>=currMax) {
				if(currSum+max[NN+i+2]>currMax||currMaxEle>i+2)
					currMaxEle=i+2;
				currMax=currSum+max[NN+i+2];
			}
			maxSum[i]=currMax;
			//System.out.println(N+" "+K+" "+P[i+NN]+" "+i+" "+maxSum[i]+" "+maxSumEle[i]);
			maxSumEle[i]=currMaxEle;
			currSum+=P[i+NN];
		}
		//System.out.println(i+"            "+n+" "+P[n]+" "+max[n]);
		int mid=-1;
		if(step>=1)
			mid=NN-step;
		for(;n>=0;n--) {
			long res=max[n+1];
			int resCount=0;
			currSum=0;
			int i=0;
			end=K<NN-n?K:NN-n;
			for(i=0;i<end;i++) {
				currSum+=P[n+i];
				if(res<=max[n+i+2]+currSum) {
					if(res<max[n+i+2]+currSum||resCount>i+1)
						resCount=i+1;
					res=max[n+i+2]+currSum;
				}
				//System.out.println(i+" "+n+" "+P[n]+" "+res+" "+resCount);
			}
				
			if((i+n)<N) {
				if(i<K) {
					long currSum2=currSum+maxSum[K-i];
					int currSumEle=i-1+maxSumEle[K-i];
					if(res<=currSum2) {
						if(res<currSum2||resCount>currSumEle)
							resCount=currSumEle;
						res=currSum2;
					}
					//System.out.println(i+" "+n+" "+P[n]+" "+res+" "+resCount);
				} else {
					i--;
					if(i>=0&&res<=max[n+i+3]+currSum) {
						if(res<max[n+i+3]+currSum||resCount>i+1)
							resCount=i+1;
						res=max[n+i+3]+currSum;
					}
				}
			}
			if(maxCount[n+1]<K&&res<=max[n+1]+P[n]) {
				if(res<max[n+1]+P[n]||resCount>maxCount[n+1]+1)
					resCount=maxCount[n+1]+1;
				res=max[n+1]+P[n];
			}
			max[n]=res;
			maxCount[n]=resCount;
			if(n==mid) {
				NN=n+1;
				currSum=0;
				currMax=-1;
				currMaxEle=-1;
				end=K+3<N-NN+3?K+3:N-NN+3;
				for(i=0;i<end;i++) {
					if(currSum+max[NN+i+1]>=currMax) {
						if(currSum+max[NN+i+1]>currMax||currMaxEle>i+1)
							currMaxEle=i+1;
						currMax=currSum+max[NN+i+1];
					}
					if(currSum+max[NN+i+2]>=currMax) {
						if(currSum+max[NN+i+2]>currMax||currMaxEle>i+2)
							currMaxEle=i+2;
						currMax=currSum+max[NN+i+2];
					}
					maxSum[i]=currMax;
					//System.out.println(N+" "+K+" "+P[i+NN]+" "+i+" "+maxSum[i]+" "+maxSumEle[i]);
					maxSumEle[i]=currMaxEle;
					currSum+=P[i+NN];
				}
				//if(mid>1000)
					mid-=step;//mid*9/10;
				//else
				//	mid=-1;
				if(mid<=0)
					mid=-1;
				mycount++;			
			}
			//System.out.println(n+" "+P[n]+" "+res+" "+resCount);
		}	
		System.out.println(max[0]);//+" "+mycount);
	}
}
/*
time java Solution < myin10
99915808645418

real	0m26.662s
user	0m32.146s
sys	0m17.673s
*/

 



Python 3 Billboards HackerRank Solution


Copy Code Copied Use a different Browser

Suggest in comments



Python 2 Billboards HackerRank Solution


Copy Code Copied Use a different Browser

# Enter your code here. Read input from STDIN. Print output to STDOUT


size, limit = [ int(i) for i in raw_input().split(' ') if i ]

cache = {0: 0}
count = 0

while count < size:
	count += 1
	i = int(raw_input())
	
	max_value = 0
	for j in cache:
		v = cache[j]
		if v > max_value:
			max_value = v
	
	new_cache = {}
	for j in cache:
		if j == limit:
			continue
		v = cache[j] + i
		if v > max_value:
			new_cache[j + 1] = v
	cache = new_cache
	cache[0] = max_value

print max(cache.values())



C Billboards HackerRank Solution


Copy Code Copied Use a different Browser

/* Enter your code here. Read input from STDIN. Print output to STDOUT */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

typedef long long LONG_T;

char buf[200000];
//#define INPUT   fgets(buf,sizeof(buf),FD)
//#define USEFILEIO
#define INPUT gets(buf)

#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define MIN(x,y) ((x) < (y) ? (x) : (y))

#define MAXN (100000)
#define GROUPSIZE (316)
#define MAXGROUP ((MAXN/GROUPSIZE) + 1)
#define MAXINT (1ULL<<62)
LONG_T BOARD[MAXN];
LONG_T MAXVAL[MAXN+2];
LONG_T LASTCUT[MAXN+2];
LONG_T COST[MAXN*2];
LONG_T GROUP[MAXGROUP];

LONG_T
calc_max (int N, int K) {
  memset(MAXVAL,0,sizeof(MAXVAL));

  MAXVAL[N-1] = BOARD[N-1];
  MAXVAL[N] = 0;
  MAXVAL[N+1] = 0;
  int i;
  for (i = N-2;i >=0;i--) {
    LONG_T max = MAXVAL[i+1];
    LONG_T ltotal = 0;
    int j;
    for (j = 0; j < K;j++) {
      if (i+j >= N) {
        break;
      }
      ltotal += BOARD[i+j];
      max = MAX(ltotal+MAXVAL[i+j+2],max);
    }
    MAXVAL[i] = max;
  }
  return (MAXVAL[0]);
}

LONG_T
calc_max_fast (int N, int K) {
  MAXVAL[N] = 0;
  MAXVAL[N+1] = 0;
  int i;
  for (i=N-1;i>= N-K;i--) {
    MAXVAL[i] = BOARD[i] + MAXVAL[i+1];
  }
  if (i < 0) {
    return (MAXVAL[0]);
  }
  for (;i >=0;i--) {
    LONG_T max = MAXVAL[i+1];
    LONG_T ltotal = 0;
    int j;
    for (j = 0; j < K;j++) {
      if (i+j >= N) {
        break;
      }
      ltotal += BOARD[i+j];
      if (ltotal+MAXVAL[i+j+2] >= max) {
        max = ltotal+MAXVAL[i+j+2];
      }
    }
    MAXVAL[i] = max;
  }
  return (MAXVAL[0]);
}

LONG_T
calc_max_fastest (int N, int K) {
  int i,j,min_index,group,nextgroup;
  LONG_T min,groupmin;
  LONG_T total=0;
  COST[i] = 0;
  min = 0;
  min_index = N;
  group = N / GROUPSIZE;
  GROUP[group] = 0;
  if (N % GROUPSIZE) {
    groupmin = 0; 
  } else {
    groupmin = MAXINT;
  }
  for (i=N-1;i>=0;i--) {
    // brute search
    min = MAXINT;
    for (j=i+1; j<=i+1+K;j++) {
      if ((j % GROUPSIZE) == 0) {
        break;
      }
      if (j > N) {
        break;
      }
      min = MIN(min,COST[j]);
    }
    for (; j<=i+1+K;j+=GROUPSIZE) {
      if (j + GROUPSIZE > i+1+K) {
        break;
      }
      if (j + GROUPSIZE > N) {
        break;
      }
      assert ((j % GROUPSIZE) == 0);
      group = j / GROUPSIZE;
      min = MIN(min,GROUP[group]);
    }
    if (j <= i+1+K) { 
      for (; j<=i+1+K;j++) {
        if (j > N) {
          break;
        }
        min = MIN(min,COST[j]);
      }
    }
    COST[i] = min + BOARD[i];
    groupmin = MIN(groupmin,COST[i]);
    if ((i % GROUPSIZE) == 0) {
      group = i / GROUPSIZE;
      GROUP[group] = groupmin;
      min = MAXINT;
      for (j = i; j < i+GROUPSIZE;j++) {
        if (j > N) {
          break;
        }
        min = MIN(min,COST[j]);
      }
      assert(groupmin == min);
      groupmin = MAXINT;
    }
    total += BOARD[i];
  }
  min = (1ULL << 62);
  for (i=0;i<=K;i++) {
    min = MIN(min,COST[i]);
  }
  return (total - min);
}

LONG_T
calc_max_faster (int N, int K) {
  int i,j,min_index;
  LONG_T min;
  LONG_T total=0;
  COST[N] = 0;
  min = 0;
  min_index = N;
  for (i=N-1;i>=0;i--) {
    if (i+1+K < min_index) {
      // brute search
      min = MAXINT;
      for (j=i+1; j<=i+1+K;j++) {
        if (j > N) {
          break;
        }
        if (COST[j] < min) {
          min = COST[j];
          min_index = j;
        }
      }
    } 
    COST[i] = min + BOARD[i];
    total += BOARD[i];
  }
  min = (1ULL << 62);
  for (i=0;i<=K;i++) {
    min = MIN(min,COST[i]);
  }
  return (total - min);
}

LONG_T
calc_max_slow (int N, int K) {
  int i,j;
  LONG_T min;
  LONG_T total=0;
  COST[N] = 0;
  for (i=N-1;i>=0;i--) {
    min = MAXINT;
    for (j=i+1; j<=i+1+K;j++) {
      min = MIN(min,COST[j]);
    }
    COST[i] = min + BOARD[i];
    total += BOARD[i];
  }
  min = (1ULL << 62);
  for (i=0;i<=K;i++) {
    min = MIN(min,COST[i]);
  }
  return (total - min);
}

int main (int argc, char *argv[]) {
  FILE *FD;
  char *saveptr;
  char *tok;
  int i;
  int N, K; 
    
#ifdef USEFILEIO
  FD = fopen(argv[1], "r");
  
  if (!FD) {
    printf("error cannot open %s\n",argv[1]);
    return -1;
  }
#endif
  
  INPUT;
  tok = strtok_r(buf," ",&saveptr);
  N = atoi(tok);
  tok = strtok_r(NULL," ",&saveptr);
  K = atoi(tok);

  for (i = 0; i < N; i++) {
    INPUT;
    int val;
    tok = strtok_r(buf," ",&saveptr);
    val = atoi(tok);
    BOARD[i] = val;
  }
#if 0
  int testn;
  for (testn = 0; testn < 100;testn++) {
    N = rand() % 100000;
    N = 100000;
    for (i = 0; i < N; i++) {
      BOARD[i] = rand() % 1000000000;
    }  
    K = rand() % 100000;
    K = 100000;
    K = MIN(N,K);
    LONG_T total;
    total = calc_max_faster(N,K);
    printf("%lld\n",total);
    LONG_T fast_total;
    fast_total = calc_max_fastest(N,K);
    printf("%lld\n",fast_total);
//        total = fast_total;
    if (total != fast_total) {
      assert (0);
    }
  }
#endif
  LONG_T fast_total;
  fast_total = calc_max_fastest(N,K);
  printf("%lld\n",fast_total);
  
  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

 

Related posts:

15 Days to learn SQL Hard SQL(Advanced)-SolutionSimilar Pair – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionCounting Sort 1 – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionDijkstra: Shortest Reach 2 – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionJim and his LAN Party – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionTara’s Beautiful Permutations – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionMatrix Land – HackerRank Solution
Tags: BillboardsCc++14full solutionGoHackerRank Solutionjavajava 15java 7java 8java8javascriptpypy 3Python 2python 3
ShareTweetPin
bhautik bhalala

bhautik bhalala

Related Posts

Leetcode All Problems Solutions
Code Solutions

Exclusive Time of Functions – LeetCode Solution

by admin
October 5, 2022
0
30

Exclusive Time of Functions - LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions...

Read more
Leetcode All Problems Solutions

Smallest Range Covering Elements from K Lists – LeetCode Solution

October 5, 2022
32
Leetcode All Problems Solutions

Course Schedule III – LeetCode Solution

October 5, 2022
25
Leetcode All Problems Solutions

Maximum Product of Three Numbers – LeetCode Solution

September 11, 2022
52
Leetcode All Problems Solutions

Task Scheduler – LeetCode Solution

September 11, 2022
119
Leetcode All Problems Solutions

Valid Triangle Number – LeetCode Solution

September 11, 2022
28
Next Post

1 Month Preparation Kit Solutions - HackerRank

15 Days to learn SQL Hard SQL(Advanced)-Solution

Plus Minus- HackerRank Solution

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may also like

15 Days to learn SQL Hard SQL(Advanced)-SolutionSimilar Pair – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionCounting Sort 1 – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionDijkstra: Shortest Reach 2 – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionJim and his LAN Party – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionTara’s Beautiful Permutations – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionMatrix Land – HackerRank Solution

Categories

  • Algorithms
  • Anime
  • Biography
  • Business
  • Code Solutions
  • Cosmos
  • Countdowns
  • Culture
  • Economy
  • Education
  • Entertainment
  • Finance
  • Games
  • Hackerrank
  • Health
  • How to
  • Investment
  • LeetCode
  • Lifestyle
  • LINUX SHELL
  • Manga
  • News
  • Opinion
  • Politics
  • Sports
  • SQL
  • Tech
  • Travel
  • Uncategorized
  • Updates
  • World
  • DMCA
  • Home
  • My account
  • Privacy Policy
  • Top Posts

Recent Blogs

Leetcode All Problems Solutions

Exclusive Time of Functions – LeetCode Solution

October 5, 2022
Leetcode All Problems Solutions

Smallest Range Covering Elements from K Lists – LeetCode Solution

October 5, 2022
15 Days to learn SQL Hard SQL(Advanced)-Solution
Algorithms

Counter game – HackerRank Solution

May 31, 2022
7
15 Days to learn SQL Hard SQL(Advanced)-Solution
Algorithms

Ice Cream Parlor – HackerRank Solution

May 31, 2022
4
Leetcode All Problems Solutions
Code Solutions

Summary Ranges – LeetCode Solution

September 6, 2022
25
Leetcode All Problems Solutions
Code Solutions

Best Time to Buy and Sell Stock II – LeetCode Solution

September 5, 2022
31

© 2022 ZeroPlusFour - Latest News & Blog.

No Result
View All Result
  • Home
  • Category
    • Business
    • Culture
    • Economy
    • Lifestyle
    • Health
    • Travel
    • Opinion
    • Politics
    • Tech
  • Landing Page
  • Support Forum
  • Contact Us

© 2022 ZeroPlusFour - Latest News & Blog.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?