diff --git "a/leetcode3/\354\227\274\355\230\234\354\240\225/1094. Car Pooling.java" "b/leetcode3/\354\227\274\355\230\234\354\240\225/1094. Car Pooling.java" new file mode 100644 index 00000000..ffff8e9b --- /dev/null +++ "b/leetcode3/\354\227\274\355\230\234\354\240\225/1094. Car Pooling.java" @@ -0,0 +1,29 @@ +// 커스텀 정렬 +// O(n log n) + +class Solution { + public boolean carPooling(int[][] trips, int capacity) { + int[][] events = new int[trips.length*2][2]; + int i = 0; + for (int[] trip : trips) { + events[i] = new int[]{trip[1], trip[0]}; + i++; + events[i] = new int[]{trip[2], -trip[0]}; + i++; + } + + Arrays.sort(events, (a, b) -> { + // 승차 + 하차 - + // 위치 오름차순, 위치가 같으면 승객 오름차순 + if (a[0] == b[0]) return Integer.compare(a[1], b[1]); + return Integer.compare(a[0], b[0]); + }); + + int now = 0; + for (int[] event : events) { + now += event[1]; + if (now > capacity) return false; + } + return true; + } +} diff --git "a/leetcode3/\354\227\274\355\230\234\354\240\225/4061. Minimum Queen Moves to Reach Target.java" "b/leetcode3/\354\227\274\355\230\234\354\240\225/4061. Minimum Queen Moves to Reach Target.java" new file mode 100644 index 00000000..f2014048 --- /dev/null +++ "b/leetcode3/\354\227\274\355\230\234\354\240\225/4061. Minimum Queen Moves to Reach Target.java" @@ -0,0 +1,12 @@ +// O(1) + +class Solution { + public int minQueenMoves(int[] source, int[] target) { + int x = Math.abs(source[0]-target[0]); + int y = Math.abs(source[1]-target[1]); + + if (x == 0 && y == 0) return 0; + else if (x == 0 || y == 0 || x == y) return 1; + else return 2; + } +}