본문 바로가기

algorithm7

[Algorithm][Graph][Java] 문제 풀이 #29 - 542. 01 Matrix 문제 링크 https://leetcode.com/problems/01-matrix/ 문제 풀이 public int[][] updateMatrix(int[][] mat) { int row = mat.length; int col = mat[0].length; for (int i = 0; i 0) mat[i][j] = Math.min(mat[i][j], mat[i - 1][j] + 1); if (j > 0) mat[i][j] = Math.min(mat[i][j], mat[i][j - 1] + 1); } } for (int.. 2022. 8. 31.
[Algorithm][Graph][Java] 문제 풀이 #28 - 733. Flood Fill 문제 링크 https://leetcode.com/problems/flood-fill/ 내 코드 int startPixel = 0; public int[][] floodFill(int[][] image, int sr, int sc, int color) { if(!isInArrayIndex(image, sr, sc)) return image; startPixel = image[sr][sc]; recursion(image, sr, sc, color); return image; } private void recursion(int[][] image, int sr, int sc, int color) { if(!isInArrayIndex(image, sr, sc)) return; else if(image[sr][sc.. 2022. 8. 29.
[Algorithm][Tree][Java] 문제 풀이 #26 - 102. Binary Tree Level Order Traversal 문제 링크 https://leetcode.com/problems/binary-tree-level-order-traversal/ 내 코드 List resultList = new ArrayList(); public List levelOrder(TreeNode root) { List list = new ArrayList(); list.add(root); if(root != null) recursion(list); return resultList; } private void recursion(List list) { if(list.size() == 0) return; List childList = new ArrayList(); List result = new ArrayList(); for(TreeNode node.. 2022. 8. 22.
[Algorithm][Tree][Java] 문제 풀이 #25 - 124. Binary Tree Maximum Path Sum 문제 링크 https://leetcode.com/problems/binary-tree-maximum-path-sum/ 내 풀이 int max = Integer.MIN_VALUE; public int maxPathSum(TreeNode root) { if(root.left == null && root.right == null) return root.val; recursion(root); return max; } private void recursion(TreeNode node) { if(node == null) return; if(node.left == null && node.right == null) return; int leftVal = node.left != null ? node.left.val : .. 2022. 8. 21.