Skip to content

[lhc0506] WEEK 02 solutions #1237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions 3sum/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
const result = [];
const numsMap = {};

nums.forEach((num, index) => {
numsMap[num] = (numsMap[num] || 0) + 1;

});

for (let i = 0; i < nums.length - 1; i++) {
for (let j = i + 1; j < nums.length; j++) {
const a = nums[i];
const b = nums[j];
const targetNum = -(a + b);

if (!numsMap[targetNum]) {
continue;
}

if ((targetNum === a || targetNum === b) && numsMap[targetNum] === 1) {
continue;
}

if (targetNum === a && targetNum === b && numsMap[targetNum] <= 2) {
continue;
}

const triplet = [a, b, targetNum].sort((x, y) => x - y);
const key = triplet.join(',');

if (seen.has(key)) {
continue;
}

seen.add(key);

result.push(triplet);
}
}

return result;
};


//시간 복잡도 O(n²) , 공간 복잡도 O(n²) 라고 생각했는데 Time Limit Exceeded 에러 발생
// 추후 다시 풀어볼 예정
17 changes: 17 additions & 0 deletions climbing-stairs/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @param {number} n
* @return {number}
*/
var climbStairs = function(n) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var를 이용해서 함수를 정의하고 있는데, var는 재선언, 재할당을 허용하니까 let이나 const가 더 좋을 것 같아요. 물론 리트코드에서 기본적으로 제시하는 코드가 var 함수명으로 되어 있긴 하지만요..! 저는 const로 바꿔서 사용하고 있습니다.

let preStairs = 0;
let curStairs = 1;


for (let i = 0; i < n; i++) {
let sum = preStairs + curStairs;
preStairs = curStairs;
curStairs = sum;
}

return curStairs;
};
54 changes: 54 additions & 0 deletions product-of-array-except-self/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

// 풀이 1
// /**
// * @param {number[]} nums
// * @return {number[]}
// */
// var productExceptSelf = function(nums) {
// let hasZero = false;
// const allProductNums = nums.reduce((acc, cur) => {
// if (cur === 0) {
// if (!hasZero) {
// hasZero = true;
// return acc;
// }

// return 0;
// }
// return acc * cur;
// }, 1);

// return nums.map(num => {
// if (num === 0) {
// return allProductNums;
// }

// return hasZero ? 0 : allProductNums / num;
// });
// };

//풀이 2
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function(nums) {
const result = Array(nums.length).fill(1);

let beforeProductNum = 1;
for (let i = 0; i < nums.length; i++) {
result[i] *= beforeProductNum;
beforeProductNum *= nums[i];
}

let afterProductNum = 1;
for (let i = nums.length - 1; i >= 0; i--) {
result[i] *= afterProductNum;
afterProductNum *= nums[i];
}

return result;
};


//두 풀이 모두 시간 복잡도 O(n), 공간 복잡도는 O(n)
26 changes: 26 additions & 0 deletions valid-anagram/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function(s, t) {
if (s.length !== t.length) {
return false;
}

const charCountMap = {};

for (let char of s) {
charCountMap[char] = (charCountMap[char] || 0) + 1;
}

for (let char of t) {
if (!charCountMap[char]) {
return false;
}

charCountMap[char] -= 1;
}

return true;
};
24 changes: 24 additions & 0 deletions validate-binary-search-tree/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isValidBST = function(root) {
const dfs = (node, min, max) => {
if (!node) return true;
if (!((node.val > min) && (node.val < max))) return false;

return dfs(node.left, min, node.val) && dfs(node.right, node.val, max);
}

return dfs(root, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
};

// 시간 복잡도 O(n) , 공간 복잡도 최악의 경우 O(n)