-
-
Notifications
You must be signed in to change notification settings - Fork 340
시간/공간 복잡도 테스트 #2520
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
base: main
Are you sure you want to change the base?
시간/공간 복잡도 테스트 #2520
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| const Node = function() { | ||
| this.children = {}; | ||
| this.isEnd = false; | ||
| } | ||
|
Comment on lines
+1
to
+4
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요렇게 하는 게 트라이 구조군요..! 배워갑니다 👍👍👍 |
||
|
|
||
| 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. constraints상 word는 항상 존재하는 것 같습니다만 요 부분은 혹시 모를 안전을 위한 처리겠죠? 👍 |
||
| 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| 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]; | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| 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; | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석