-
-
Notifications
You must be signed in to change notification settings - Fork 341
시간/공간 복잡도 테스트 #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
Open
lkhoony
wants to merge
2
commits into
DaleStudy:main
Choose a base branch
from
lkhoony:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
시간/공간 복잡도 테스트 #2520
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
|
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. 🏷️ 알고리즘 패턴 분석
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. 🏷️ 알고리즘 패턴 분석
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🏷️ 알고리즘 패턴 분석