Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
27 changes: 27 additions & 0 deletions container-with-most-water/byol-han.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* https://leetcode.com/problems/container-with-most-water/
* @param {number[]} height
* @return {number}
*/
var maxArea = function (height) {
let left = 0;
let right = height.length - 1;
let maxWater = 0;

while (left < right) {
let width = right - left; // 현재 너비는 두 포인터 사이 거리
let minHeight = Math.min(height[left], height[right]); // 현재 높이는 두 선 중 더 낮은 값 (물이 넘치지 않기 위해 낮은 선 기준)
let area = width * minHeight; // 현재 구간이 담을 수 있는 물의 양 계산

maxWater = Math.max(maxWater, area); // 최대 물 저장량 갱신
Comment on lines +14 to +16
Copy link
Contributor

Choose a reason for hiding this comment

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

area변수가 있어 가독성이 좋지만, 생략하고 Math.max(maxWater, width * minHeight) 이렇게 하는 방법도 괜찮을 것 같아요. 주석이 상세히 적혀있어 이해하기 쉬웠습니다.


// 낮은 쪽 포인터를 이동시켜서 더 큰 높이를 찾음
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}

return maxWater;
};
23 changes: 23 additions & 0 deletions longest-increasing-subsequence/byol-han.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* https://leetcode.com/problems/longest-increasing-subsequence/
* @param {number[]} nums
* @return {number}
*/
var lengthOfLIS = function (nums) {
if (nums.length === 0) return 0;

const dp = new Array(nums.length).fill(1); // 최소 길이는 1 (자기 자신만 포함)

for (let i = 1; i < nums.length; i++) {
// 현재 숫자 이전의 숫자들과 비교
for (let j = 0; j < i; j++) {
// 증가하는 순서인지 확인
if (nums[i] > nums[j]) {
// 증가하는 subsequence가 발견되면 길이 갱신
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}

return Math.max(...dp);
};
22 changes: 22 additions & 0 deletions valid-parentheses/byol-han.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function isValid(s) {
const stack = [];
const bracketMap = {
")": "(",
"}": "{",
"]": "[",
};
Comment on lines +3 to +7
Copy link
Contributor

Choose a reason for hiding this comment

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

딕셔너리를 활용한 방법이 굉장히 깔끔하네요!


for (let char of s) {
if (char === "(" || char === "{" || char === "[") {
stack.push(char);
} else {
// 닫는 괄호가 나왔을 때 스택이 비었거나 짝이 안 맞는 경우
if (stack.pop() !== bracketMap[char]) {
return false;
}
}
}

// 모든 괄호가 짝지어졌는지 확인
return stack.length === 0;
}