LeetCode 2051: The Category of Each Member in the Store Solution

Master LeetCode problem 2051 (The Category of Each Member in the Store), 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.

2051. The Category of Each Member in the Store

Medium

Problem Explanation

Explanation:

Given a list of products and their categories, we need to find the category of each member in the store based on the products they bought. The category of each member is determined by the product they bought the most.

To solve this problem, we can iterate through the list of products and their categories, and keep track of the count of each product in each category. Then, for each member in the store, we can determine their category based on the product they bought the most.

Solution Code

import java.util.*;

class Solution {
    public List<String> findMemberCategories(List<String> products, List<String> categories, List<String> members) {
        Map<String, Map<String, Integer>> categoryCount = new HashMap<>();
        
        for (int i = 0; i < products.size(); i++) {
            String product = products.get(i);
            String category = categories.get(i);
            
            if (!categoryCount.containsKey(category)) {
                categoryCount.put(category, new HashMap<>());
            }
            categoryCount.get(category).put(product, categoryCount.get(category).getOrDefault(product, 0) + 1);
        }
        
        List<String> result = new ArrayList<>();
        
        for (String member : members) {
            Map<String, Integer> memberCount = new HashMap<>();
            for (int i = 0; i < products.size(); i++) {
                if (member.equals(products.get(i))) {
                    String category = categories.get(i);
                    memberCount.put(category, memberCount.getOrDefault(category, 0) + 1);
                }
            }
            
            int maxCount = 0;
            String memberCategory = "";
            for (Map.Entry<String, Integer> entry : memberCount.entrySet()) {
                if (entry.getValue() > maxCount) {
                    maxCount = entry.getValue();
                    memberCategory = entry.getKey();
                }
            }
            
            result.add(memberCategory);
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2051 (The Category of Each Member in the Store)?

This page provides optimized solutions for LeetCode problem 2051 (The Category of Each Member in the Store) 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 2051 (The Category of Each Member in the Store)?

The time complexity for LeetCode 2051 (The Category of Each Member in the Store) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2051 on DevExCode?

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

Back to LeetCode Solutions