LeetCode 1364: Number of Trusted Contacts of a Customer Solution

Master LeetCode problem 1364 (Number of Trusted Contacts of a Customer), 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.

1364. Number of Trusted Contacts of a Customer

Medium

Problem Explanation

Explanation

To solve this problem, we can use a hashmap to store the trusted contacts of each customer. We iterate through the list of customers and their trusted contacts, updating the hashmap accordingly. Then, we iterate through the hashmap to count the number of trusted contacts for each customer.

Solution Code

import java.util.*;

class Solution {
    public List<Integer> numOfTrustedContacts(List<List<Integer>> contacts) {
        Map<Integer, Set<Integer>> trustedContacts = new HashMap<>();
        
        for (List<Integer> contact : contacts) {
            int customer = contact.get(0);
            int trustedContact = contact.get(1);
            
            trustedContacts.putIfAbsent(customer, new HashSet<>());
            trustedContacts.get(customer).add(trustedContact);
        }
        
        List<Integer> result = new ArrayList<>();
        for (int customer : trustedContacts.keySet()) {
            int count = 0;
            for (Set<Integer> contactsSet : trustedContacts.values()) {
                if (contactsSet.contains(customer)) {
                    count++;
                }
            }
            result.add(count);
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1364 (Number of Trusted Contacts of a Customer)?

This page provides optimized solutions for LeetCode problem 1364 (Number of Trusted Contacts of a Customer) 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 1364 (Number of Trusted Contacts of a Customer)?

The time complexity for LeetCode 1364 (Number of Trusted Contacts of a Customer) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1364 on DevExCode?

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

Back to LeetCode Solutions