package leetcode;

/**
 * https://leetcode.com/problems/maximum-population-year/
 */
public class Problem1854 {
    public int maximumPopulation(int[][] logs) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1854 prob = new Problem1854();
        System.out.println(prob.maximumPopulation(new int[][]{
            {1993,1999},{2000,2010}
        })); // 1993
        System.out.println(prob.maximumPopulation(new int[][]{
            {1950,1961},{1960,1971},{1970,1981}
        })); // 1960
    }
}

@@ -12,16 +12,120 @@
 tcode;%0A%0A
+import java.util.Arrays;%0Aimport java.util.Collections;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A * h
@@ -270,383 +270,1098 @@
     
-// TODO%0A        return 0
+Arrays.sort(logs, (a, b) -%3E %7B%0A            int cmp = Integer.compare(a%5B0%5D, b%5B0%5D)
 ;%0A    
+ 
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1854 prob = new Problem1854();%0A        System.out.println(prob.maximumPopulation(new int%5B%5D%5B%5D%7B%0A            %7B1993,1999%7D,%7B2000,2010%7D%0A        %7D)); // 1993%0A        System.out.println(prob.maximumPopulation(new int%5B%5D%5B%5D%7B%0A            %7B1950,1961%7D,%7B1960,1971%7D,%7B1970,1981%7D%0A        %7D)); // 1960
+   if (cmp == 0) %7B%0A                return Integer.compare(a%5B1%5D, b%5B1%5D);%0A            %7D%0A            return cmp;%0A        %7D);%0A        Map%3C/* year */ Integer, /* count */ Integer%3E map = new HashMap%3C%3E();%0A        for (int i = 0; i %3C logs.length; i++) %7B%0A            int birth1 = logs%5Bi%5D%5B0%5D;%0A            if (map.containsKey(birth1)) %7B%0A                map.put(birth1, map.getOrDefault(birth1, 0) + 1);%0A                continue;%0A            %7D%0A            map.put(birth1, 1);%0A            for (int j = i - 1; j %3E= 0; j--) %7B%0A                int death2 = logs%5Bj%5D%5B1%5D;%0A                if (birth1 %3C death2) %7B%0A                    map.put(birth1, map.getOrDefault(birth1, 0) + 1);%0A                %7D%0A            %7D%0A        %7D%0A        return Collections.max(map.entrySet(), (a, b) -%3E %7B%0A            int cmp = Integer.compare(a.getValue(),b.getValue()); // by count%0A            if (cmp == 0) %7B%0A                return Integer.compare(b.getKey(), a.getKey()); // by year%0A            %7D%0A            return cmp;%0A        %7D).getKey();
 %0A   

Solve problem 1854