Untitled

 avatar
unknown
plain_text
9 months ago
3.4 kB
20
Indexable
import java.util.*;

public class Solution {

    /**
     * Given a list of records, where each record has a "name" and a list of "emails",
     * return a list of name groups such that two names belong to the same group
     * if they share an email (directly or transitively).
     *
     * Example:
     * Input:
     * [
     *   { "name": "c1", "emails": ["[email protected]", "[email protected]"] },
     *   { "name": "c2", "emails": ["[email protected]", "[email protected]"] },
     *   { "name": "c3", "emails": ["[email protected]"] },
     *   { "name": "c4", "emails": ["[email protected]", "[email protected]"] }
     * ]
     *
     * Output:
     * [
     *   ["c1", "c2", "c4"],
     *   ["c3"]
     * ]
     */
    public static List<List<String>> groupPropertiesBySharedEmails(List<Map<String, Object>> records) {
        Map<String, Set<String>> cluster_emails = new HashMap<>();
        Map<String, Set<String>> email_cluster = new HashMap<>();
        Map<String, String> group_cluster = new HashMap<>();
        
        int cluster_id = 1;
        for(Map<String, Object> record : records) {
        //Map.of("property", "c1", "emails", List.of("[email protected]", "[email protected]"))
        List<String> curr_emails = (List<String>)record.get("emails");
            for(String email : curr_emails) {//"[email protected]"
                //"[email protected]" => [] new set
                Set<String> curr_clusters = email_cluster.getOrDefault(email, new HashSet<>());  
                curr_clusters.add(""+cluster_id);
                //in email_cluster, "[email protected]" => ["1"]
                email_cluster.put(email,curr_clusters);
                //in group_cluster, "c1" => "1"
                group_cluster.put(record.get("property").toString(), ""+cluster_id);
                
                //now for cluster_emails
                Set<String> emails_of_curr_cluster = cluster_emails.getOrDefault(""+cluster_id, new HashSet<>());
                emails_of_curr_cluster.add(email);
                cluster_emails.put(""+cluster_id, emails_of_curr_cluster);
                
                cluster_id++;
            }
        }
        
        consolidateMap(cluster_emails);
        
        return new ArrayList<>();
    }
    
    public static void consolidateMap(Map<String, Set<String>> cluster_emails) {
        //2-layer for loop 
        //fix first index, fix i, traverse j from i+1 till n, see if i and j can be merged(when i and j have shared value); if they do, i addAll of j, turn j into an empty set,
        
         
    }
    public static void main(String[] args) {
        // --- Test Case 1 ---
        List<Map<String, Object>> records1 = new ArrayList<>();
        records1.add(Map.of("property", "c1", "emails", List.of("[email protected]", "[email protected]")));
        records1.add(Map.of("property", "c2", "emails", List.of("[email protected]", "[email protected]")));
        records1.add(Map.of("property", "c3", "emails", List.of("[email protected]")));
        records1.add(Map.of("property", "c4", "emails", List.of("[email protected]", "[email protected]")));
        List<List<String>> expected1 = new ArrayList<>();
        expected1.add(new ArrayList<>(List.of("c1", "c2", "c4")));
        expected1.add(new ArrayList<>(List.of("c3")));
    
        //runTestCase("1", records1, expected1);
    }
}

// "transitively"
// group1 : alice, bob
// group2 : bob, allen
// group3 : allen, alex
// => 1,2,3 grouped together

Editor is loading...
Leave a Comment