import java.io.*;
import java.util.*;

/*
 * To execute Java, please define "static void main" on a class
 * named Solution.
 *
 * If you need more classes, simply define them inline.
 */

class Solution {
  public static void main(String[] args) {
    String[][] userContentLikes = new String[][]{
        {"http://yahoo.com", "user3", "201999"},
        {"http://google.com/maps", "user2", "201004"},
        {"http://google.com/maps", "user1", "201015"},
        {"http://google.com", "user4", "201004"},
        {"http://google.com", "user2", "201012"},
        {"http://google.com/maps", "user2", "201008"},
        {"http://google.com/maps", "user2", "201013"},
        {"http://google.com/maps", "user2", "201030"},
        {"http://altavista.net/q?f=12344", "user3", "100002"},
        {"http://google.com/maps", "user3", "201015"},
        {"http://yahoo.com", "user2", "201035"},
        {"http://altavista.net/q?f=12344", "user1", "100001"},
        {"http://altavista.net/q?f=12344", "user1", "100004"},
        {"http://geocities.com", "user1", "100007"},
        {"http://geocities.com", "user3", "100009"},
    };

    System.out.println(findSimilarUser(userContentLikes, "user1"));

  }



  public static String findSimilarUser(String[][] likes, String userName) {

    Set<String> set = new HashSet<String>();
    for(String[] like : likes) {

      if(like[1].equals(userName)) {
        set.add(like[0]);
      }
    }



    //
    Map<String, Set<String>> map = new HashMap<String, Set<String>>(); // user-cnt


    for(String[] like : likes) {

      if(!like[1].equals(userName) && set.contains(like[0])) {

        Set<String> urls = map.get(like[1]);
        if (urls == null) {
          urls = new HashSet<String>();
        }

        urls.add(like[0]);
        map.put(like[1], urls);

      }
    }

    int maxCnt = Integer.MIN_VALUE;
    String result = null;

    for(Map.Entry<String, Set<String>> entry : map.entrySet()) {
      int cnt = entry.getValue().size();
      //System.out.format("%s, %d\n",entry.getKey(), cnt);
      if (result == null || cnt > maxCnt) {
        result = entry.getKey();
        maxCnt = cnt;

      }

    }

    return result;
  }

  static class Pair{

    String userName;
    String timeStamp;

    public Pair(String userName, String timeStamp) {
      this.userName = userName;
      this.timeStamp = timeStamp;
    }
  }

  public static Map<String, Pair> findLikes(String[][] likes) {

    Map<String, Pair> map = new HashMap<String, Pair>();

    for(String[] like : likes) {

      Pair cur = map.get(like[0]);
      if(cur == null) {
        map.put(like[0], new Pair(like[1], like[2]));
      } else {

        if (Integer.parseInt(cur.timeStamp) > Integer.parseInt(like[2])) {

          map.put(like[0], new Pair(like[1], like[2]));
        }
      }

    }

    return map;

  }
}


/* 

Q1

We are building a social network where users can like and share content from the web.

Given a list showing users who "liked" the content of a URL at a given timestamp, write a function that returns a collection showing each unique piece of content and the user who liked it at the earliest time.


Expected output [in any order]:
  http://yahoo.com => user2
  http://google.com => user4
  http://google.com/maps => user2
  http://altavista.net/q?f=12344 => user1
  http://geocities.com => user1

Q2

Given a list of users and content that each user liked, find the user most similar to User X. "Most similar to X" means the person who liked the greatest number of unique pieces of content that User X also liked.

For example, User1 liked 3 distinct pages. User3 liked all 3 of those pages, more than anyone else, making them the most similar user to User1.

findSimilarUser(userContentLikes, "user1") => user3
findSimilarUser(userContentLikes, "user2") => user3
findSimilarUser(userContentLikes, "user3") => user1
findSimilarUser(userContentLikes, "user4") => user2


 */

results matching ""

    No results matching ""