
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find minimum cost to buy all books in C++
Suppose we have an array of n elements. These are the ratings of them. Find the minimum cost to buy all books, with the following condition −
- Cost of each book would be at-least 1 dollar
- A book has higher cost than an adjacent (left or right) if rating is more than the adjacent.
So for example, if the rating array is like [1, 3, 4, 3, 7, 1], Then the output is 10, As 1 + 2 + 3 + 1 + 2 + 1 = 10
To solve this, we have to make two arrays called LtoR, and RtoL, and fill them with 1, now follow these steps −
- Traverse left to right, then fill LtoR, and update it by seeing previous rating of the given array. We are not caring about the next rating of given array
- Traverse right to left, then fill RtoL, and update it by seeing previous rating of the given array. We are not caring about the next rating of given array
- For maximum value of ith position in both array LtoR and RtoL, then add it to the result.
Example
#include<iostream> using namespace std; int getMinCost(int ratings[], int n) { int res = 0; int LtoR[n]; int RtoL[n]; for(int i = 0; i<n; i++){ LtoR[i] = RtoL[i] = 1; } for (int i = 1; i < n; i++) if (ratings[i] > ratings[i - 1]) LtoR[i] = LtoR[i - 1] + 1; for (int i = n - 2; i >= 0; i--) if (ratings[i] > ratings[i + 1]) RtoL[i] = RtoL[i + 1] + 1; for (int i = 0; i < n; i++) res += max(LtoR[i], RtoL[i]); return res; } int main() { int ratings[] = { 1, 6, 8, 3, 4, 1, 5, 7 }; int n = sizeof(ratings) / sizeof(ratings[0]); cout << "Minimum cost is: " << getMinCost(ratings, n); }
Output
Minimum cost is: 15
- Related Articles
- C++ Program to find minimum moves to get all books contiguous
- Find the minimum and maximum amount to buy all N candies in Python
- Program to find minimum cost to connect all points in Python
- Program to Find Out the Minimum Cost to Purchase All in Python
- Program to find minimum cost to merge stones in Python
- C++ program to find minimum how many coins needed to buy binary string
- Program to find out the sum of minimum cost within a graph among all vertices in Python
- Program to find minimum cost for painting houses in Python
- If the cost of $25$ bools is $₹ 100$, then find the cost of $2$ books?
- 5 books and 7 pens together cost ₹ 79 whereas 7 books and 5 pens together cost ₹ 77. Find the total cost of 1 book and 2 pens.
- Program to find minimum cost to cut a stick in Python
- Program to find minimum cost to hire k workers in Python
- Program to find minimum deletion cost to avoid repeating letters in Python
- Program to find minimum cost to connect each Cartesian coordinates in C++
- Find minimum adjustment cost of an array in Python

Advertisements