Skip to content

[krokerdile] Week 06 solutions #1446

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 13, 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/krokerdile.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 left = 0;
let right = height.length - 1;
let max = 0;

while(right > left){
if(height[left] >= height[right]){
max = Math.max(max, height[right] * (right - left));
right = right -1;
}
else{
max = Math.max(max, height[left] * (right - left));
left = left +1;
}
}
return max;
};
53 changes: 53 additions & 0 deletions design-add-and-search-words-data-structure/krokerdile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class TrieNode {
constructor() {
this.children = {}; // 문자 -> TrieNode 매핑
this.isEnd = false; // 단어의 끝 표시
}
}

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

/**
* 단어를 트라이에 추가
* Time Complexity: O(L) // L = word.length
* Space Complexity: O(L) // 새로운 노드 최대 L개 추가 가능
*/
addWord(word) {
let node = this.root;
for (const char of word) {
if (!node.children[char]) {
node.children[char] = new TrieNode();
}
node = node.children[char];
}
node.isEnd = true;
}

/**
* 단어 또는 패턴 검색
* Time Complexity: O(26^D * L) // D = '.' 개수 (최대 2), L = word.length
* Space Complexity: O(L) // DFS 재귀 호출 스택 깊이
*/
search(word) {
const dfs = (index, node) => {
for (let i = index; i < word.length; i++) {
const char = word[i];
if (char === '.') {
for (const child of Object.values(node.children)) {
if (dfs(i + 1, child)) return true;
}
return false;
} else {
if (!node.children[char]) return false;
node = node.children[char];
}
}
return node.isEnd;
};

return dfs(0, this.root);
}
}
23 changes: 23 additions & 0 deletions longest-increasing-subsequence/krokerdile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Time: O(n log n), Space: O(n)
function lengthOfLIS(nums) {
const tails = [];

for (const num of nums) {
let left = 0, right = tails.length;

// 이진 탐색: tails에서 num이 들어갈 최소 위치 찾기
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (tails[mid] < num) {
left = mid + 1;
} else {
right = mid;
}
}

// left는 삽입 위치
tails[left] = num;
}

return tails.length;
}
42 changes: 42 additions & 0 deletions spiral-matrix/krokerdile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Time: O(m * n)
// Space: O(1) + output array
function spiralOrder(matrix) {
const result = [];

let top = 0;
let bottom = matrix.length - 1;
let left = 0;
let right = matrix[0].length - 1;

while (top <= bottom && left <= right) {
// 1. 왼 → 오
for (let i = left; i <= right; i++) {
result.push(matrix[top][i]);
}
top++;

// 2. 위 → 아래
for (let i = top; i <= bottom; i++) {
result.push(matrix[i][right]);
}
right--;

// 3. 오 → 왼
if (top <= bottom) {
for (let i = right; i >= left; i--) {
result.push(matrix[bottom][i]);
}
bottom--;
}

// 4. 아래 → 위
if (left <= right) {
for (let i = bottom; i >= top; i--) {
result.push(matrix[i][left]);
}
left++;
}
}

return result;
}
31 changes: 31 additions & 0 deletions valid-parentheses/krokerdile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
let ch = {};
ch[')'] = '(';
ch['}'] = '{';
ch[']'] = '[';

let list = s.split('');
let stack = [];

list.forEach((ele)=>{
if(stack.length == 0){
stack.push(ele);
}else{
let len = stack.length;
if(stack[len-1] == ch[ele]){
stack.pop();
}else{
stack.push(ele);
}
}
})
if(stack.length == 0){
return true;
}else{
return false;
}
};