Skip to content

[byol-han] WEEK 02 solutions #1223

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 7 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
36 changes: 36 additions & 0 deletions 3sum/byol-han.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function (nums) {
// 오름차순 정렬
nums.sort((a, b) => a - b);
const result = [];

for (let i = 0; i < nums.length - 2; i++) {
// 중복된 숫자는 스킵
if (i > 0 && nums[i] === nums[i - 1]) continue;

let left = i + 1;
let right = nums.length - 1;

while (left < right) {
const sum = nums[i] + nums[left] + nums[right];

if (sum < 0) {
left++;
} else if (sum > 0) {
right--;
} else {
result.push([nums[i], nums[left], nums[right]]);
// 중복된 left,right 값 스킵
while (left < right && nums[left] === nums[left + 1]) left++;
while (left < right && nums[right] === nums[right - 1]) right--;

left++;
right--;
}
}
}
return result;
};
17 changes: 17 additions & 0 deletions climbing-stairs/byol-han.js
Copy link
Contributor

Choose a reason for hiding this comment

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

DP로 푸는건 생각 못하고있었는데 훨씬 간단하게 풀 수 있는 문제였군요....

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @param {number} n
* @return {number}
*/
var climbStairs = function (n) {
if (n <= 2) return n;

let first = 1;
let second = 2;

for (let i = 3; i <= n; i++) {
let count = first + second;
first = second;
second = count;
}
return second;
};
41 changes: 41 additions & 0 deletions product-of-array-except-self/byol-han.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function (nums) {
// 1.
// 각 인덱스에서 자기 자신 제외한 배열 만든 뒤 곱셈 수행 → 시간복잡도 O(n²)
// (중첩 루프로 인해 시간복잡도 O(n²), 큰 입력에서는 시간 초과 발생)
let result = [];
for (let i = 0; i < nums.length; i++) {
const productNums = [...nums.slice(0, i), ...nums.slice(i + 1)];

let product = 1;
for (let j = 0; j < productNums.length; j++) {
product *= productNums[j];
}
result.push(product);
}
return result;

// 2.
const n = nums.length;
// 정답 배열을 1로 초기화 (곱셈에 영향을 주지 않도록)
const answer = new Array(n).fill(1);

// 왼쪽 누적 곱 계산
let left = 1;
for (let i = 0; i < n; i++) {
answer[i] = left;
left *= nums[i];
}

// 오른쪽 누적 곱 계산
let right = 1;
for (let i = n - 1; i >= 0; i--) {
answer[i] *= right;
right *= nums[i];
}

return answer;
};
20 changes: 20 additions & 0 deletions valid-anagram/byol-han.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function (s, t) {
if (s.length !== t.length) return false;

// 문자열 t를 배열로 변환해서 문자 제거할 수 있게 함
let tArr = t.split("");

for (let i = 0; i < s.length; i++) {
let index = tArr.indexOf(s[i]); // s[i]가 tArr에 있는지 확인
if (index === -1) {
return false;
}
tArr.splice(index, 1);
}
return true;
};
38 changes: 38 additions & 0 deletions validate-binary-search-tree/byol-han.js
Copy link
Contributor Author

Choose a reason for hiding this comment

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

BST(Binary Search Tree) 잘 이해못함. 더 공부해야함.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 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) {
function helper(node, lower = -Infinity, upper = Infinity) {
if (!node) return true;

const val = node.val;

// 현재 노드가 범위를 벗어나면 false
if (val <= lower || val >= upper) {
return false;
}

// 오른쪽 서브트리: 최소값은 현재 노드 값
if (!helper(node.right, val, upper)) {
return false;
}

// 왼쪽 서브트리: 최대값은 현재 노드 값
if (!helper(node.left, lower, val)) {
return false;
}

return true;
}

return helper(root);
};