diff --git "a/leetcode3/\354\227\274\355\230\234\354\240\225/1219. Path with Maximum Gold.java" "b/leetcode3/\354\227\274\355\230\234\354\240\225/1219. Path with Maximum Gold.java" new file mode 100644 index 00000000..701ca698 --- /dev/null +++ "b/leetcode3/\354\227\274\355\230\234\354\240\225/1219. Path with Maximum Gold.java" @@ -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 || 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; + } + +} diff --git "a/leetcode3/\354\227\274\355\230\234\354\240\225/Code Testcase Testcase Test Result 543. Diameter of Binary Tree.java" "b/leetcode3/\354\227\274\355\230\234\354\240\225/Code Testcase Testcase Test Result 543. Diameter of Binary Tree.java" new file mode 100644 index 00000000..75fdd08f --- /dev/null +++ "b/leetcode3/\354\227\274\355\230\234\354\240\225/Code Testcase Testcase Test Result 543. Diameter of Binary Tree.java" @@ -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; + } +}