From bd67c2f1a69361a0cb16fb489ab1b2a96ae98df4 Mon Sep 17 00:00:00 2001 From: dae won Date: Mon, 19 May 2025 10:30:18 +0900 Subject: [PATCH 1/2] Day16 --- .../Q117.java" | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 "leetcode2/2medium/\352\271\200\353\214\200\354\233\220/Q117.java" diff --git "a/leetcode2/2medium/\352\271\200\353\214\200\354\233\220/Q117.java" "b/leetcode2/2medium/\352\271\200\353\214\200\354\233\220/Q117.java" new file mode 100644 index 00000000..42b3b439 --- /dev/null +++ "b/leetcode2/2medium/\352\271\200\353\214\200\354\233\220/Q117.java" @@ -0,0 +1,59 @@ +/* +// Definition for a Node. +class Node { + public int val; + public Node left; + public Node right; + public Node next; + + public Node() {} + + public Node(int _val) { + val = _val; + } + + public Node(int _val, Node _left, Node _right, Node _next) { + val = _val; + left = _left; + right = _right; + next = _next; + } +}; +*/ + +class Solution { + + static Map> map; + + public Node connect(Node root) { + map = new HashMap<>(); + searchDepth(0, root); + return helper(0, root); + } + + private Node helper(int depth, Node root) { + if (root == null) return null; + if (!map.get(depth).isEmpty()) { + Node n = map.get(depth).poll(); + root.next = n; + } + + helper(depth + 1, root.left); + helper(depth + 1, root.right); + + return root; + } + + private void searchDepth(int depth, Node root) { + if (root == null) return; + + if (!map.containsKey(depth)) { + map.put(depth, new ArrayDeque<>()); + } else { + map.get(depth).add(root); + } + + searchDepth(depth + 1, root.left); + searchDepth(depth + 1, root.right); + } +} \ No newline at end of file From f5f3aaa35d70382b28a63ee60acd4c5dc70cfb48 Mon Sep 17 00:00:00 2001 From: dae won Date: Mon, 19 May 2025 10:47:25 +0900 Subject: [PATCH 2/2] Day16 --- .../Q892.java" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 "leetcode2/1easy/\352\271\200\353\214\200\354\233\220/Q892.java" diff --git "a/leetcode2/1easy/\352\271\200\353\214\200\354\233\220/Q892.java" "b/leetcode2/1easy/\352\271\200\353\214\200\354\233\220/Q892.java" new file mode 100644 index 00000000..5f611688 --- /dev/null +++ "b/leetcode2/1easy/\352\271\200\353\214\200\354\233\220/Q892.java" @@ -0,0 +1,35 @@ +class Solution { + public int surfaceArea(int[][] grid) { + int total = 0; + int n = grid.length; + int m = grid[0].length; + Queue pq = new PriorityQueue<>((a, b) -> b[2] - a[2]); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + total += grid[i][j] * 4 + 2; + pq.offer(new int[] {i, j, grid[i][j]}); + } + } + + int gap = 0; + int[] dx = {-1, 0, 1, 0}; + int[] dy = {0, 1, 0, -1}; + while (!pq.isEmpty()) { + int[] now = pq.poll(); + int x = now[0]; + int y = now[1]; + int val = now[2]; + + for (int i = 0; i < 4; i++) { + int nx = x + dx[i]; + int ny = y + dy[i]; + + if (nx >= 0 && nx < n && ny >= 0 && ny < m) { + if (val > grid[nx][ny]) gap += (val - grid[nx][ny]); + } + } + } + + return total - gap; + } +} \ No newline at end of file