package leetcode;

/**
 * https://leetcode.com/problems/count-unhappy-friends/
 */
public class Problem1583 {
    public int unhappyFriends(int n, int[][] preferences, int[][] pairs) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1583 prob = new Problem1583();
        System.out.println(prob.unhappyFriends(
            4,
            new int[][]{{1, 2, 3}, {3, 2, 0}, {3, 1, 0}, {1, 2, 0}},
            new int[][]{{0, 1}, {2, 3}})); // 2
        System.out.println(prob.unhappyFriends(
            2,
            new int[][]{{1}, {0}},
            new int[][]{{1, 0}})); // 0
        System.out.println(prob.unhappyFriends(
            4,
            new int[][]{{1, 3, 2}, {2, 3, 0}, {1, 3, 0}, {0, 2, 1}},
            new int[][]{{1, 3}, {0, 2}})); // 4
        System.out.println(prob.unhappyFriends(
            4,
            new int[][]{{1, 3, 2}, {2, 3, 0}, {1, 3, 0}, {0, 2, 1}},
            new int[][]{{0, 1}, {2, 3}})); // 2
        System.out.println(prob.unhappyFriends(
            4,
            new int[][]{{1, 3, 2}, {2, 3, 0}, {1, 3, 0}, {0, 2, 1}},
            new int[][]{{0, 3}, {1, 2}})); // 0
    }
}

@@ -12,16 +12,65 @@
 tcode;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A * h
@@ -239,179 +239,150 @@
     
-// TODO%0A        return 0
+int%5B%5D pairMap = new int%5Bn%5D
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1583 prob = new Problem1583();%0A        System.out.println(prob.unhappyFriends(
+for (int%5B%5D pair : pairs) %7B%0A            pairMap%5Bpair%5B0%5D%5D = pair%5B1%5D;%0A            pairMap%5Bpair%5B1%5D%5D = pair%5B0%5D;
 %0A   
@@ -378,38 +378,33 @@
 air%5B0%5D;%0A        
-    4,
+%7D
 %0A            new
@@ -400,302 +400,265 @@
     
-    new int%5B%5D%5B%5D%7B%7B1, 2, 3%7D, %7B3, 2, 0%7D, %7B3, 1, 0%7D, %7B1, 2, 0%7D%7D,%0A            new int%5B%5D%5B%5D%7B%7B0, 1%7D, %7B2, 3%7D%7D)); // 2%0A        System.out.println(prob.unhappyFriends(%0A            2,%0A            new int%5B%5D%5B%5D%7B%7B1%7D, %7B0%7D%7D,%0A            new int%5B%5D%5B%5D%7B%7B1, 0%7D%7D)); // 0%0A        System.out.println(prob.unhappyFriends(
+Map%3CInteger, Integer%3E%5B%5D preferenceMap = new Map%5Bn%5D;%0A        for (int i = 0; i %3C n; i++) %7B%0A            preferenceMap%5Bi%5D = new HashMap%3C%3E();%0A            for (int j = 0; j %3C n - 1; j++) %7B%0A                preferenceMap%5Bi%5D.put(preferences%5Bi%5D%5Bj%5D, j);%0A            %7D
 %0A   
@@ -666,179 +666,196 @@
     
+%7D%0A
     
-4,%0A
     
-        new int%5B%5D%5B%5D%7B%7B1, 3, 2%7D, %7B2, 3, 0%7D, %7B1, 3, 0%7D, %7B0, 2, 1%7D%7D,%0A            new int%5B%5D%5B%5D%7B%7B1, 3%7D, %7B0, 2%7D%7D)); // 4%0A        System.out.println(prob.unhappyFriends(
+int answer = 0;%0A        for (int i = 0; i %3C n; i++) %7B%0A            for (int j : preferences%5Bi%5D) %7B%0A                if (preferenceMap%5Bj%5D.get(i) %3C preferenceMap%5Bj%5D.get(pairMap%5Bj%5D) &&
 %0A   
@@ -859,35 +859,32 @@
             
-4,%0A
         
     new int%5B
@@ -875,164 +875,126 @@
     
-    new int%5B%5D%5B%5D%7B%7B1, 3, 2%7D, %7B2, 3, 0%7D, %7B1, 3, 0%7D, %7B0, 2, 1%7D%7D,%0A            new int%5B%5D%5B%5D%7B%7B0, 1%7D, %7B2, 3%7D%7D)); // 2%0A        System.out.println(prob.unhappyFriends(
+preferenceMap%5Bi%5D.get(pairMap%5Bi%5D) %3E preferenceMap%5Bi%5D.get(j)) %7B%0A                    answer++;%0A                    break;
 %0A   
@@ -1006,10 +1006,13 @@
     
-4,
+    %7D
 %0A   
@@ -1024,112 +1024,42 @@
     
-new int%5B%5D%5B%5D%7B%7B1, 3, 2%7D, %7B2, 3, 0%7D, %7B1, 3, 0%7D, %7B0, 2, 1%7D%7D,%0A            new int%5B%5D%5B%5D%7B%7B0, 3%7D, %7B1, 2%7D%7D)); // 0
+%7D%0A        %7D%0A        return answer;
 %0A   

Solve problem 1583