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
34 changes: 34 additions & 0 deletions leetcode3/염혜정/1219. Path with Maximum Gold.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// dfs + 백트래킹

class Solution {
int[] dx = {-1, 1, 0, 0};
int[] dy = {0, 0, -1, 1};

public int getMaximumGold(int[][] grid) {
int max = 0;
for (int i = 0; i<grid.length; i++) {
for (int k = 0; k<grid[0].length; k++) {
if (grid[i][k] == 0) continue;
max = Math.max(max, dfs(grid, i, k));
}
}
return max;
}

int dfs(int[][] grid, int x, int y) {
if (x<0 || y<0 || x>=grid.length || y>=grid[0].length
|| grid[x][y] == 0) return 0;

int temp = grid[x][y];
grid[x][y] = 0;

int max = 0;
for (int i = 0; i<4; i++) {
max = Math.max(max, dfs(grid, x+dx[i], y+dy[i]));
}
grid[x][y] = temp;

return max + temp;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 재귀함수
// O(n)

class Solution {
int max;
public int diameterOfBinaryTree(TreeNode root) {
max = 0;
depth(root);
return max;
}

int depth(TreeNode node) {
if (node == null) return 0;

int left = depth(node.left);
int right = depth(node.right);
max = Math.max(max, left + right);

return Math.max(left, right) + 1;
}
}
Loading