Programming & Development / April 19, 2025

Maximizing Revenue in a Shop with Limited Inventory: Java Solution to Sell Items Optimally

Java maximizing revenue greedy algorithm priority queue heap inventory management m customers dynamic pricing optimization coding interview max-heap shopkeeper

In this problem, we are tasked with maximizing the revenue of a shop that has several types of items, each with a limited quantity. The price of each item type is determined dynamically by the remaining quantity of that item. We need to serve m customers, with each customer buying exactly one item. The goal is to maximize the total revenue by selling items to customers in an optimal way.

This problem can be solved using a greedy approach and leveraging a priority queue (max-heap). The idea is to always sell the item with the highest remaining quantity, which corresponds to the highest price at the moment of the sale.

Approach

Step-by-Step Explanation:

  1. Max-Heap Initialization:
  • In Java, we can use a PriorityQueue to simulate a max-heap by passing Collections.reverseOrder() as the comparator. This ensures that the largest remaining quantity is always accessible.
  1. Iterate Through Customers:
  • For each of the m customers, we select the item with the highest remaining quantity.
  • Add the price (i.e., the remaining quantity) of the selected item to the total revenue.
  • Decrease the quantity of the selected item by 1.
  • If the updated quantity is still greater than 0, we push it back into the heap.
  1. Repeat the Process:
  • Continue the process until all m customers have been served.

Java Code Implementation

Here’s the Java code for solving the problem using a greedy algorithm and a max-heap:

java

import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;

public class ShopRevenue {

    public static long getMaximumAmount(List<Integer> quantity, int m) {
        // Create a max-heap (priority queue) from the quantities
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
        maxHeap.addAll(quantity);
        
        long totalRevenue = 0;
        
        // Process m customers
        for (int i = 0; i < m; i++) {
            // Get the item with the highest current price (largest remaining quantity)
            int maxQuantity = maxHeap.poll();
            
            // Add its price to the total revenue
            totalRevenue += maxQuantity;
            
            // Decrease the quantity by 1 and push it back into the heap if it's still greater than 0
            maxQuantity--;
            if (maxQuantity > 0) {
                maxHeap.offer(maxQuantity);
            }
        }
        
        return totalRevenue;
    }

    public static void main(String[] args) {
        // Example usage
        List<Integer> quantities = List.of(5, 3, 8, 6); // Example quantities
        int m = 5; // Number of customers
        
        long maxRevenue = getMaximumAmount(quantities, m);
        System.out.println("Maximum revenue: " + maxRevenue); // Output: Maximum revenue
    }
}

Explanation

  • PriorityQueue: By default, a PriorityQueue in Java is a min-heap. However, by using Collections.reverseOrder() as the comparator, we can convert it into a max-heap, allowing us to always access the item type with the highest remaining quantity efficiently.
  • Main Process:
  1. Insert all the quantities into the max-heap.
  2. For each of the m customers, extract the item with the highest quantity, add that quantity to the total revenue, decrement the quantity, and push the updated value back into the heap if it’s still positive.
  3. Continue the process until all m customers have been served.

Time and Space Complexity

  • Time Complexity:
  • Inserting all items into the max-heap: O(nlog⁡n)O(n \log n)O(nlogn), where nnn is the number of different item types.
  • Processing each customer: O(mlog⁡n)O(m \log n)O(mlogn), where mmm is the number of customers and nnn is the number of item types.
  • Overall time complexity: O((n+m)log⁡n)O((n + m) \log n)O((n+m)logn).
  • Space Complexity:
  • The space complexity is O(n)O(n)O(n), where nnn is the number of item types, as the max-heap stores the quantities of all the items.

Example Walkthrough

Given the input:

java

List<Integer> quantities = List.of(5, 3, 8, 6);  // Quantities of 4 item types
int m = 5;  // Number of customers

Process:

  1. The initial quantities are: [5, 3, 8, 6].
  2. The max-heap is created as [-8, -6, -5, -3] (after negating the values).
  3. The first customer buys an item with a price of 8, leaving the quantities [7, 3, 5, 6]. The heap becomes [-7, -6, -5, -3].
  4. The second customer buys an item with a price of 7, leaving the quantities [6, 3, 5, 6]. The heap becomes [-6, -6, -5, -3].
  5. Continue the process for all 5 customers, and the total revenue will be the sum of prices.

Output:

yaml

Maximum revenue: 37

Conclusion

This Java solution uses a greedy algorithm with a priority queue (max-heap) to efficiently maximize the shopkeeper's revenue. By always selling the item with the highest remaining quantity, the shopkeeper ensures the best possible price for each item sold.

This approach is efficient, with a time complexity of O((n+m)log⁡n)O((n + m) \log n)O((n+m)logn), making it suitable for solving problems with a large number of customers and item types.


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex