-
230. Kth Smallest Element in a BSTAlgorithm/java tip 2021. 3. 24. 00:25
leetcode.com/problems/kth-smallest-element-in-a-bst/submissions/ /** * 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; * } * } */ // inorder with counting class Solution ..
-
198. House RobberAlgorithm/java tip 2021. 3. 23. 23:53
leetcode.com/problems/house-robber/ House Robber - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com class Solution { // dp public int rob(int[] nums) { int[][] dp = new int[2][nums.length]; dp[0][0] = nums[0]; dp[1][0] = 0; for(int i=1; i
-
1387. Sort Integers by The Power ValueAlgorithm/java tip 2021. 3. 21. 00:12
leetcode.com/problems/sort-integers-by-the-power-value/ Sort Integers by The Power Value - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com class Solution { /* even -> x /= 2; odd -> x = 3*x + 1 return step count lo - - - hi sort power value asc same - asc 12, 13, 14, 15 9, 9, 17, 1..
-
1557. Minimum Number of Vertices to Reach All NodesAlgorithm/java tip 2021. 3. 20. 23:53
leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes/ /* edges[i] = [from, to] n : node count // strategy 1. bfs */ class Solution { public List findSmallestSetOfVertices(int n, List edges) { Queue q = new LinkedList(); boolean[] visit = new boolean[n]; List ans = new ArrayList(); for(int i=0; i
-
797. All Paths From Source to TargetAlgorithm/java tip 2021. 3. 20. 23:20
leetcode.com/problems/all-paths-from-source-to-target/ class Solution { /* - node i direct to nodes in graph[i] - find all paths from node 0 to node n-1 //strategy 1. bfs > q 2. dfs > st //process 1. input 2. process mat[i][j] = 1 -> node i -> node j graph[0] = [1, 2] graph[1] = [3] graph[2] = [3] graph[3] = [] */ int start, end, len; boolean[] visit; List ans = new ArrayList(); public List allP..
-
98. Validate Binary Search TreeAlgorithm/java tip 2021. 3. 14. 17:32
leetcode.com/problems/validate-binary-search-tree/ Validate Binary Search Tree - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com class Solution { public boolean isValidBST(TreeNode root) { return isValidBST(root, false, false, 0, 0); } public boolean isValidBST(TreeNode root, boole..
-
21. Merge Two Sorted ListsAlgorithm/java tip 2021. 3. 13. 16:29
leetcode.com/problems/merge-two-sorted-lists/ Merge Two Sorted Lists - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode ret = null; if(l1 == null) return l2; if(l2 == null) return l1; if(l1.val < l2...
-
530. Minimum Absolute Difference in BSTAlgorithm/java tip 2021. 3. 13. 15:41
leetcode.com/problems/minimum-absolute-difference-in-bst/ Minimum Absolute Difference in BST - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com /* Tree의 value들 간에 차이가 가장 작은 값 Tree 순회 - prenoder : self, left, right - inorder : left, self, right - postorder : left, right, self 이진트리의 성..