목록Algorithm/leetcode (15)
에러 안나게 해주세요
var isCousins = function(root, x, y) { const queue = [root]; while (queue.length) { const size = queue.length; let foundX = false; let foundY = false; // iterate through one level for (let i = 0; i < size; i++) { const node = queue.shift(); console.log(queue); // check if children are x and y if (node.left && node.right) { if ( (node.left.val === x && node.right.val === y) || (node.left.val === ..
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center). var isSymmetric = function(root) { if (root == null) return true; return symmetryChecker(root.left, root.right); }; function symmetryChecker(left, right) { if (left == null && right == null) return true; // If both sub trees are empty if (left == null || right == null) return false; // If..
솔루션을 보고도 아직 잘 이해가 안됨. -_-;; 답답 var numSquares = function(n) { const dp = [0]; for (let i = 1; i
기본적인 dp 계산식으로 풀 수 있는 문제, 저번에는 더 어려웠던 거 같은데.. 눈이 침침해서 자야겠다. var climbStairs = function(n) { if (n == 1) return 1; let dp = new Array( n + 1); dp[1] = 1; dp[2] = 2; for(let i = 3; i
못푼 문제. 다시 풀어봐야 함. index 0 ~ end까지 더한 값을 리턴 후 (범위[j] - 리턴값[i-1])로 구하던데, 왜 이런건지 잘 모르겠음.. 노트에 한땀한땀 써가면서 되짚어봤는데 답은 나오는데, 원리를 이해를 못한거 같다. function NumArray(nums) { this.sums = []; var sum = 0; for (var i = 0; i 0 ? this.sums[i - 1] : 0); };
var maxProfit = function(prices) { var min = Number.MAX_SAFE_INTEGER; var max = 0; for(let i = 0; i < prices.length; i+=1){ min = Math.min(min, prices[i]); // console.log('최소값',min); max = Math.max(max, prices[i] - min); // console.log('최댓값',max); } return max; }; 저번 주는 포스팅을 안해서 반성 중.. 좀 더 사고를 길러야할 듯 2번 케이스에서 막혀서 한참 고민하다 답을 봄.
var isSubsequence = function(s, t) { let count = 0; let bool = true; let num = 0; let prev = 0; for(let i = 0; i < s.length; i++){ bool = t.includes(s[i]); if(bool === false) { return false; } num = t.indexOf(s[i]); if(num < prev){ return false; } prev = num; console.log(num); } return true; }; 틀린 답변.. 1시간 정도 풀다가 모르겠어서 문제를 봤다. 내가 막힌 테스트 케이스는 'aaaaaa' / 'aabbbb' 히든 케이스가 틀릴 때 마다 계속 코드를 다시 쳐야해서 맞는 ..
var minCostClimbingStairs = function(cost) { if(cost.length === 0) return 0; if(cost.length === 2) return Math.min(cost[0], cost[1]); let First = cost[1]; let Two = cost[0]; for(let i = 2; i < cost.length; i++){ const current = cost[i] + Math.min(First,Two); Two = First; First = current; } return Math.min(First,Two); }; DP 문제 이전 포스팅 문제보다 먼저 풀었는데, 엊그제 해놓고 포스팅을 안했다. 하는 김에 SUBMIT도 같이.. 2시간 동안 DP의 대..