Skip to content
Open
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
67 changes: 67 additions & 0 deletions design-add-and-search-words-data-structure/lkhoony.js
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Trie, Backtracking
  • 설명: 이 코드는 트라이 자료구조를 이용해 단어를 저장하고 검색하며, '.' 와일드카드 문자에 대해 백트래킹으로 탐색합니다. 트라이 구조와 재귀적 탐색이 결합된 패턴입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const Node = function() {
this.children = {};
this.isEnd = false;
}

var WordDictionary = function() {
this.root = new Node();
};

/**
* @param {string} word
* @return {void}
*/
WordDictionary.prototype.addWord = function(word) {
let currentNode = this.root;

for (let char of word) {
if (!currentNode.children[char]) {
currentNode.children[char] = new Node();
}
currentNode = currentNode.children[char];
}

currentNode.isEnd = true;
};

/**
* @param {string} word
* @return {boolean}
*/
WordDictionary.prototype.search = function(word) {
if (word === undefined) return false;
return this._search(this.root, word, 0);
};

WordDictionary.prototype._search = function(node, word, index) {
if (index === word.length) {
return node.isEnd;
}

const char = word[index];

if (char !== '.') {
const child = node.children[char];
return child ? this._search(child, word, index + 1) : false;
}

for (const key in node.children) {
if (this._search(node.children[key], word, index + 1)) {
return true;
}
}

return false;
};

/**
* Your WordDictionary object will be instantiated and called as such:
* var obj = new WordDictionary()
* obj.addWord(word)
* var param_2 = obj.search(word)
*/

// n: 단어수, m: 단어 길이
// addWord: 시간 복잡도 O(m)
// search: 시간 복잡도 O(m)
// 공간 복잡도 O(n * m)
43 changes: 43 additions & 0 deletions find-minimum-in-rotated-sorted-array/lkhoony.js
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Binary Search
  • 설명: 이 코드는 회전된 정렬 배열에서 최소값을 찾기 위해 이진 탐색 방식을 사용하며, 중간값과 끝값을 비교하여 범위를 좁혀가는 방식입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// math의 min을 이용하는 방법
// tc: O(n^4)
// sc: 잘 몰랐는데 모든 요소를 함수 인자로 풀어 콜스택에 올린다 하여 O(n)이 된다고 함..
// (대용량의 배열 시 maximum exceed 에러가 날 수 있음)
const findMin_use_math_min = function (nums) {
return Math.min(...nums);
};

// 메서드를 사용하지 않은 풀이.
// tc: O(n^3)
// sc: O(1)
const findMin_naive = function (nums) {
let min = nums[0];

for (let i = 1; i < nums.length; i++) {
if (nums[i] <= min) {
min = nums[i];
break;
}
}

return min;
};

// 시간복잡도를 문제의 요구사항에 맞도록 줄여본 풀이
// tc: O(n*logn)
// sc: O(1)
const findMin = function (nums) {
let left = 0,
right = nums.length - 1;

while (left < right) {
let mid = Math.floor((left + right) / 2);

if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}

return nums[left];
};
38 changes: 38 additions & 0 deletions graph-valid-tree/lkhoony.js
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: DFS
  • 설명: 이 코드는 그래프의 연결성과 사이클 유무를 DFS로 탐색하여 판단하는 방식입니다. 재귀적 탐색을 통해 방문 여부를 체크하며, 그래프의 유효성을 검증합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export class Solution {
/**
* @param {number} n - number of nodes
* @param {number[][]} edges - undirected edges
* @return {boolean}
*/
validTree(n, edges) {
if (n === 0) return true;

// 인접 리스트 생성
const adj = {};
for (let i = 0; i < n; i++) {
adj[i] = [];
}
for (const [n1, n2] of edges) {
adj[n1].push(n2);
adj[n2].push(n1);
}

const visit = new Set();

const dfs = (i, prev) => {
if (visit.has(i)) return false;

visit.add(i);

for (const j of adj[i]) {
if (j === prev) continue;
if (!dfs(j, i)) return false;
}

return true;
};

return dfs(0, -1) && visit.size === n;
}
}

Loading