diff --git "a/leetcode3/\352\263\275\355\230\204\354\247\200/1039. Minimum Score Triangulation of Polygon" "b/leetcode3/\352\263\275\355\230\204\354\247\200/1039. Minimum Score Triangulation of Polygon" new file mode 100644 index 00000000..fd5c7dec --- /dev/null +++ "b/leetcode3/\352\263\275\355\230\204\354\247\200/1039. Minimum Score Triangulation of Polygon" @@ -0,0 +1,29 @@ +class Solution { + public int minScoreTriangulation(int[] values) { + int n = values.length; // 꼭짓점의 위치를 나타내는 인덱스 값 + int[][] dp = new int[n][n]; // 꼭짓점 위치 + + // 구간 길이 len (최소 3개의 꼭짓점이 필요하므로 2부터 시작) + for (int len = 2; len < n; len++) { + for (int i = 0; i + len < n; i++) { + int j = i + len; + dp[i][j] = Integer.MAX_VALUE; + + for (int k = i + 1; k < j; k++) { + // 대각선의 위치와 한 삼각형의 세 개의 꼭짓점에 위치한 숫자들의 곱을 더하기 + int score = dp[i][k] + dp[k][j] + values[i] * values[k] * values[j]; + + // 특정 위치의 값이 목표 점수보다 클 때 + if (score < dp[i][j]) { + dp[i][j] = score; + } + } + } + } + + // 결과값 출력 + return dp[0][n - 1]; + } +} + +// 시간 복잡도 : O(n^3) => 삼중 반복문을 사용 diff --git "a/leetcode3/\352\263\275\355\230\204\354\247\200/1080. Insufficient Nodes in Root to Leaf Paths" "b/leetcode3/\352\263\275\355\230\204\354\247\200/1080. Insufficient Nodes in Root to Leaf Paths" new file mode 100644 index 00000000..f0f6a3be --- /dev/null +++ "b/leetcode3/\352\263\275\355\230\204\354\247\200/1080. Insufficient Nodes in Root to Leaf Paths" @@ -0,0 +1,38 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ + +// 부모 노드로부터 상속한 특정 노드와의 합 값이 목표 값보다 미만일 때, 부모 노드를 제외한, 자식 노드들을 제거하는 방식 +class Solution { + public TreeNode sufficientSubset(TreeNode root, int limit) { + if (root == null) { + return null; + } + + // 리프 노드인 경우 : 현재 노드의 값이 남은 limit보다 작으면 삭제 (null 반환) + if (root.left == null && root.right == null) { + return root.val < limit ? null : root; + } + + // 리프 노드가 아닌 경우 : 좌/우 서브트리 재귀 탐색 (limit에서 현재 노드 값 차감) + root.left = sufficientSubset(root.left, limit - root.val); + root.right = sufficientSubset(root.right, limit - root.val); + + // 탐색 후 좌/우 자식이 모두 삭제되었다면 현재 노드도 불충분한 노드이므로 삭제 + return (root.left == null && root.right == null) ? null : root; + } +} + +// 시간 복잡도 : O(n) => 노드를 한 개씩 조회