Skip to content

[hsskey] Week 06 Solutions #1441

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
May 10, 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
21 changes: 21 additions & 0 deletions container-with-most-water/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function(height) {
let result = 0
let l = 0;
let r = height.length - 1;

while(l < r) {
const area = (r - l) * Math.min(height[l], height[r]);
result = Math.max(result, area);

if(height[l] < height[r]) {
l += 1;
} else {
r -= 1;
}
}
return result
};
47 changes: 47 additions & 0 deletions design-add-and-search-words-data-structure/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class TrieNode {
constructor() {
this.children = {};
this.word = false;
}
}

class WordDictionary {
constructor() {
this.root = new TrieNode();
}

addWord(word) {
let cur = this.root;
for (let c of word) {
if (!cur.children[c]) {
cur.children[c] = new TrieNode();
}
cur = cur.children[c];
}
cur.word = true;
}

search(word) {
const dfs = (j, root) => {
let cur = root;
for (let i = j; i < word.length; i++) {
const c = word[i];
if (c === '.') {
for (let child of Object.values(cur.children)) {
if (dfs(i + 1, child)) {
return true;
}
}
return false;
} else {
if (!cur.children[c]) {
return false;
}
cur = cur.children[c];
}
}
return cur.word;
};
return dfs(0, this.root);
}
}
16 changes: 16 additions & 0 deletions longest-increasing-subsequence/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @param {number[]} nums
* @return {number}
*/
var lengthOfLIS = function(nums) {
const lis = new Array(nums.length).fill(1);

for(let i = nums.length - 2; i >= 0; i--) {
for(let j = i + 1; j < nums.length; j++) {
if(nums[i] < nums[j]) {
lis[i] = Math.max(lis[i], 1 + lis[j])
}
}
}
return Math.max(...lis)
};
41 changes: 41 additions & 0 deletions spiral-matrix/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function(matrix) {
if (matrix.length === 0) return [];

const res = [];
let left = 0, right = matrix[0].length;
let top = 0, bottom = matrix.length;

while (left < right && top < bottom) {
// 상단 행 왼쪽 → 오른쪽
for (let i = left; i < right; i++) {
res.push(matrix[top][i]);
}
top += 1;

// 오른쪽 열 위 → 아래
for (let i = top; i < bottom; i++) {
res.push(matrix[i][right - 1]);
}
right -= 1;

if (!(left < right && top < bottom)) break;

// 하단 행 오른쪽 → 왼쪽
for (let i = right - 1; i >= left; i--) {
res.push(matrix[bottom - 1][i]);
}
bottom -= 1;

// 왼쪽 열 아래 → 위
for (let i = bottom - 1; i >= top; i--) {
res.push(matrix[i][left]);
}
left += 1;
}

return res;
};
25 changes: 25 additions & 0 deletions valid-parentheses/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
const stack = []

const pair = {
')' : '(',
'}' : '{',
']' : '['
}

for (let item of s) {
// 여는 괄호
if(item === '(' || item === '{' || item === '[') {
stack.push(item)
// stack길이가 0 or 닫는 괄호 케이스
} else if (stack.length === 0 || stack.pop() !== pair[item]) {
return false
}
}

return stack.length === 0
};