Cutting Boards – 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++ Cutting Boards HackerRank Solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(pair<char,unsigned long long> A,pair<char,unsigned long long> B)
{
return A.second > B.second;
}
int main()
{
int T;
cin>>T;
for(;T--;)
{
int M,N,cost;
cin>>M>>N;
vector<pair<char,unsigned long long> > cutCost;
for (int i = 0; i < M-1; ++i)
{
cin>>cost;
cutCost.push_back(make_pair('y',cost));
}
for (int i = 0; i < N-1; ++i)
{
cin>>cost;
cutCost.push_back(make_pair('x',cost));
}
sort(cutCost.begin(),cutCost.end(),compare);
unsigned long long vcut = 1, hcut = 1, totalcost = 0;
for (int i = 0; i < cutCost.size(); ++i)
{
if(cutCost[i].first == 'y')
{
totalcost = (totalcost + ((vcut*cutCost[i].second)%1000000007))%1000000007;
++hcut;
}
else if(cutCost[i].first == 'x')
{
totalcost = (totalcost + ((hcut*cutCost[i].second)%1000000007))%1000000007;
++vcut;
}
}
cout<<totalcost<<"\n";
}
return 0;
}
Java Cutting Boards HackerRank Solution
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int m = sc.nextInt();
int n = sc.nextInt();
Integer[] yi = new Integer[m-1];
Integer[] xi = new Integer[n-1];
for(int j=0;j<m-1;j++){
yi[j]= sc.nextInt();
}
for(int j=0;j<n-1;j++){
xi[j]= sc.nextInt();
}
Arrays.sort(yi,Collections.reverseOrder());
Arrays.sort(xi,Collections.reverseOrder());
int ny=1,nx=1;
long c=0;
while(ny<m || nx<n) {
if(ny<m && (nx>=n || yi[ny-1]>xi[nx-1])) {
c= (c + ((long)nx*(long)yi[ny-1])%1000000007)%1000000007;
ny++;
} else if(nx<n && (ny>=m || xi[nx-1]>=yi[ny-1])) {
c= (c + ((long)ny*(long)xi[nx-1])%1000000007)%1000000007;
nx++;
}
}
System.out.println(c);
}
}
}
Python 3 Cutting Boards HackerRank Solution
for t in range(int(input())):
m,n = map(int,input().split())
hor = sorted(map(int,input().split()),reverse=True)
ver = sorted(map(int,input().split()),reverse=True)
h = v = 0
ret = 0
modulo = 10**9 + 7
for i in range(m+n):
if h>=len(hor) or v>=len(ver): break
if ver[v] > hor[h] :
ret += ((h+1)*ver[v])%modulo
v += 1
else:
ret += (hor[h] *(v+1)) % modulo
h += 1
if h<len(hor):
ret += (sum(hor[h:])*(v+1)) % modulo
elif v<len(ver):
ret += (sum(ver[v:])*(h+1)) % modulo
print(ret% modulo)
Python 2 Cutting Boards HackerRank Solution
a = int(raw_input())
for asdfa in range(a):
x, y = map(int, raw_input().split())
xCost = map(int, raw_input().split())
yCost = map(int, raw_input().split())
hacks = []
for i in xCost:
hacks.append((i, 0))
for i in yCost:
hacks.append((i, 1))
pieces = [1, 1]
ans = 0
for i in sorted(hacks, reverse=True):
ans += (i[0] * pieces[1 - i[1]]) % (10**9 + 7)
ans %= 10**9 + 7
pieces[i[1]] += 1
print ans
C Cutting Boards HackerRank Solution
#include<stdio.h>
int compare (const void *a, const void *b)
{
const int *ia = (const int *)a; // casting pointer types
const int *ib = (const int *)b;
return *ia - *ib;
}
int main()
{
int t,m,n;
int x[1000000],y[1000000],i,j;
long long int count,xcount,ycount;
scanf("%d",&t);
while(t--)
{
count=0;
xcount=1;
ycount=1;
scanf("%d%d",&m,&n);
for(i=0;i<m-1;i++)
{
scanf("%d",&x[i]);
}
for(i=0;i<n-1;i++)
{
scanf("%d",&y[i]);
}
qsort (x, m-1, sizeof(int), compare);
qsort (y, n-1, sizeof(int), compare);
i=m-2;
j=n-2;
while(i!=-1 && j!=-1)
{
if(x[i] > y[j])
{
count=(count+(ycount*x[i])%1000000007)%1000000007;
xcount=xcount+1;
i=i-1;
}
else if(x[i] < y[j])
{
count=(count+(xcount*y[j])%1000000007)%1000000007;
ycount=ycount+1;
j=j-1;
}
else if(xcount > ycount)
{
count=(count+(xcount*y[j])%1000000007)%1000000007;
ycount=ycount+1;
j=j-1;
}
else
{
count=(count+(ycount*x[i])%1000000007)%1000000007;
xcount=xcount+1;
i=i-1;
}
}
while(i!=-1)
{
count=(count+(ycount*x[i])%1000000007)%1000000007;
xcount=xcount+1;
i=i-1;
}
while(j!=-1)
{
count=(count+(xcount*y[j])%1000000007)%1000000007;
ycount=ycount+1;
j=j-1;
}
printf("%lld\n",count);
}
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