Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions leetcode2/1easy/김대원/Q892.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public int surfaceArea(int[][] grid) {
int total = 0;
int n = grid.length;
int m = grid[0].length;
Queue<int[]> 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;
}
}
59 changes: 59 additions & 0 deletions leetcode2/2medium/김대원/Q117.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Queue<Node>> 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);
}
}