|
| 1 | +package medium300; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Comparator; |
| 5 | +import java.util.List; |
| 6 | +import java.util.stream.Collectors; |
| 7 | + |
| 8 | +public class FeelSafe { |
| 9 | + |
| 10 | + int[][] dp = new int[1001][1001]; |
| 11 | + |
| 12 | + class Canal { |
| 13 | + int x1, y1, x2, y2; |
| 14 | + |
| 15 | + public Canal(int x1, int y1, int x2, int y2) { |
| 16 | + this.x1 = x1; |
| 17 | + this.y1 = y1; |
| 18 | + this.x2 = x2; |
| 19 | + this.y2 = y2; |
| 20 | + } |
| 21 | + |
| 22 | + int getArea() { |
| 23 | + return (y2 - y1) * (x2 - x1); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + boolean inside(Canal c1, Canal c2) { |
| 28 | + return c2.x1 > c1.x1 && c2.y1 > c1.y1 && c2.x2 < c1.x2 && c2.y2 < c1.y2; |
| 29 | + } |
| 30 | + |
| 31 | + public int maximumCanalCount(int[] x1, int[] y1, int[] x2, int[] y2) { |
| 32 | + |
| 33 | + Canal[] c = new Canal[x1.length]; |
| 34 | + |
| 35 | + for (int i = 0; i < x1.length; i++) |
| 36 | + c[i] = new Canal(Math.min(x1[i], x2[i]), |
| 37 | + Math.min(y1[i], y2[i]), |
| 38 | + Math.max(x1[i], x2[i]), |
| 39 | + Math.max(y1[i], y2[i])); |
| 40 | + |
| 41 | + List<Canal> list = Arrays.stream(c).sorted(Comparator.comparing(Canal::getArea)).collect(Collectors.toList()); |
| 42 | + |
| 43 | + int[] dp = new int[51]; |
| 44 | + int rez = 0; |
| 45 | + for (int i = 0; i < x1.length; i++) |
| 46 | + for (int j = 0; j < i; j++) { |
| 47 | + if (inside(list.get(i), list.get(j))) { |
| 48 | + dp[i] = Math.max(dp[i], dp[j] + 1); |
| 49 | + rez = Math.max(rez, dp[i]); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return rez + 1; |
| 54 | + } |
| 55 | + |
| 56 | + public static void main(String[] args) { |
| 57 | + FeelSafe f = new FeelSafe(); |
| 58 | + System.out.println(f.maximumCanalCount(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, |
| 59 | + new int[] { 20, 19, 18, 17, 16, 15, 14, 13, 12, 11 }, |
| 60 | + new int[] { 20, 19, 18, 17, 16, 15, 14, 13, 12, 11 }, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })); |
| 61 | + } |
| 62 | +} |
0 commit comments