LeetCode 1549: The Most Recent Orders for Each Product Solution

Master LeetCode problem 1549 (The Most Recent Orders for Each Product), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1549. The Most Recent Orders for Each Product

Medium

Problem Explanation

Explanation:

To solve this problem, we need to group the orders by product id and then return the most recent orders for each product. We can achieve this by using a hashmap to store the orders grouped by product id, and then for each product id, we can retrieve the most recent orders using a priority queue.

  1. Create a hashmap to store orders grouped by product id.
  2. Iterate through the orders and store them in the hashmap.
  3. For each product id, create a priority queue to store the orders sorted by order time.
  4. Iterate through the orders for each product id and add them to the priority queue.
  5. Retrieve the most recent orders from the priority queue for each product id and add them to the result list.

Time complexity: O(n log k) where n is the number of orders and k is the number of distinct product ids. Space complexity: O(n) for storing the orders in the hashmap and priority queues. :

Solution Code

import java.util.*;

class Solution {
    public List<List<Integer>> mostRecentOrders(int[][] orders) {
        Map<Integer, PriorityQueue<int[]>> map = new HashMap<>();
        List<List<Integer>> result = new ArrayList<>();

        for (int[] order : orders) {
            int productId = order[1];
            int orderTime = order[0];
            map.putIfAbsent(productId, new PriorityQueue<>((a, b) -> b[0] - a[0]));
            map.get(productId).offer(new int[]{orderTime, order[2]});
        }

        for (int productId : map.keySet()) {
            List<Integer> recentOrders = new ArrayList<>();
            PriorityQueue<int[]> pq = map.get(productId);
            int count = 0;

            while (!pq.isEmpty() && count < 5) {
                recentOrders.add(pq.poll()[1]);
                count++;
            }
            result.add(recentOrders);
        }

        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1549 (The Most Recent Orders for Each Product)?

This page provides optimized solutions for LeetCode problem 1549 (The Most Recent Orders for Each Product) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1549 (The Most Recent Orders for Each Product)?

The time complexity for LeetCode 1549 (The Most Recent Orders for Each Product) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1549 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1549 in Java, C++, or Python.

Back to LeetCode Solutions