Skip to content

[uarflower] WEEK 06 solutions #1424

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 11, 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
28 changes: 28 additions & 0 deletions container-with-most-water/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 주어진 배열에서 (두 원소 중 작은 값) * (두 원소의 인덱스 차이)의 최댓값을 반환하는 함수
* @param {number[]} height
* @return {number}
*/
const maxArea = function(height) {
let left = 0;
let right = height.length - 1;
let max = 0;

while (left < right) {
const w = right - left;
const h = Math.min(height[left], height[right]);

max = Math.max(max, w * h);

if (height[left] <= height[right]) {
left++;
} else {
right--;
}
}

return max;
};

// 시간복잡도: O(n)
// 공간복잡도: O(1)
59 changes: 59 additions & 0 deletions design-add-and-search-words-data-structure/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class TrieNode {
constructor(value) {
this.value = value;
this.children = {};
this.end = false;
}
}

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

/**
* 시간복잡도: O(w) (w: word.length)
* 공간복잡도: O(w)
* @param {string} word
* @return {void}
*/
addWord(word) {
let current = this.root;

for (const char of word) {
if (!current.children[char]) {
const child = new TrieNode(char);
current.children[char] = child;
}
current = current.children[char];
}

current.end = true;
};

/**
* 시간복잡도: O(k * w) (k: children의 길이로 최대 26, w: word.length)
* 공간복잡도: O(w)
* @param {string} word
* @return {boolean}
*/
search(word) {
return this.#search(this.root, word, 0);
};

#search(node, word, idx) {
if (!node) return false;
if (idx >= word.length) return node.end;

if (word[idx] === '.') {
for (const current of Object.values(node.children)) {
if (this.#search(current, word, idx + 1)) {
return true;
}
}
return false;
} else {
return this.#search(node.children[word[idx]], word, idx + 1);
}
}
};
20 changes: 20 additions & 0 deletions longest-increasing-subsequence/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 주어진 배열에서 가장 긴 증가하는 수열의 길이를 반환하는 함수
* @param {number[]} nums
* @return {number}
*/
const lengthOfLIS = function (nums) {
const dp = Array(nums.length).fill(1);

for (let i = 0; i < nums.length; i++) {
for (let j = 0; j < i; j++) {
if (nums[j] < nums[i])
dp[i] = Math.max(dp[j] + 1, dp[i]);
}
}

return Math.max(...dp);
};

// 시간복잡도: O(n^2)
// 공간복잡도: O(n)
33 changes: 33 additions & 0 deletions spiral-matrix/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 주어진 행렬을 나선형(우-하-좌-상)으로 순회한 결과를 반환하는 함수
* @param {number[][]} matrix
* @return {number[]}
*/
const spiralOrder = function (matrix) {
const rows = matrix.length;
const cols = matrix[0].length;
let r = 0;
let c = 0;
let dr = 0; // 0, 1, 0, -1
let dc = 1; // 1, 0, -1, 0

const output = [];

for (let i = 0; i < rows * cols; i++) {
output.push(matrix[r][c]);
matrix[r][c] = null;

// 방향을 전환해야 하는 경우
if (!(0 <= r + dr && r + dr < rows && 0 <= c + dc && c + dc < cols) || matrix[r + dr][c + dc] === null) {
[dr, dc] = [dc, -dr];
}

r += dr;
c += dc;
}

return output;
};

// 시간복잡도: O(r * c)
// 공간복잡도: O(1)
30 changes: 30 additions & 0 deletions valid-parentheses/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 주어진 문자열의 괄호 쌍이 알맞는지 반환하는 함수
* @param {string} s
* @return {boolean}
*/
const isValid = function (s) {
const stack = [];
const pairs = {
'(': ')',
'{': '}',
'[': ']',
}

for (const bracket of s) {
if (bracket in pairs) {
stack.push(bracket);
continue;
}

const popped = stack.pop();
if (bracket !== pairs[popped]) {
return false;
}
}

return stack.length === 0;
};

// 시간복잡도: O(n)
// 공간복잡도: O(n)