Algorithm
-
238. Product of Array Except SelfAlgorithm/java tip 2021. 3. 24. 00:55
leetcode.com/problems/product-of-array-except-self/ class Solution { public int[] productExceptSelf(int[] nums) { int multiple = 1; boolean isZero = false; int zeroCnt = 0; for(int num : nums) { if(num != 0) { multiple *= num; } if(num == 0) { isZero = true; zeroCnt++; } } int[] ans = new int[nums.length]; if(isZero) { if(zeroCnt > 1) { return ans; } else { for(int i=0; i
-
347. Top K Frequent ElementsAlgorithm/java tip 2021. 3. 24. 00:42
leetcode.com/problems/top-k-frequent-elements/ class Solution { /** 1. priority queue 2. array and sort */ public int[] topKFrequent(int[] nums, int k) { // {num : cnt} Map map = new HashMap(); for(int num : nums) { if(map.containsKey(num)) { map.put(num, map.get(num) + 1); } else { map.put(num, 1); } } PriorityQueue pq = new PriorityQueue(k, (a, b) -> b.cnt - a.cnt); for(Map.Entry e : map.entry..
-
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..