-
Notifications
You must be signed in to change notification settings - Fork 54
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
[환미니니] Week5 문제풀이 #457
Merged
Merged
[환미니니] Week5 문제풀이 #457
Changes from all commits
Commits
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 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,39 @@ | ||
// 시간복잡도 O(n2) | ||
// 공간복잡도 O(k) (k는 결과 개수) | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[][]} | ||
*/ | ||
var threeSum = function(nums) { | ||
nums.sort((a, b) => a - b); | ||
|
||
let result = [] | ||
|
||
for (let i = 0 ; i <= nums.length - 2; i++) { | ||
if (i > 0 && nums[i] === nums[i-1]) continue | ||
|
||
const curNum = nums[i] | ||
let leftIdx = i+1; | ||
let rightIdx = nums.length - 1; | ||
|
||
while (leftIdx < rightIdx) { | ||
const leftNum = nums[leftIdx] | ||
const rightNum = nums[rightIdx] | ||
const threeSum = curNum + leftNum + rightNum | ||
if (threeSum === 0) { | ||
result.push([curNum, leftNum, rightNum]) | ||
while (leftIdx < rightIdx && nums[leftIdx] === nums[leftIdx+1]) leftIdx++ | ||
while (leftIdx < rightIdx && nums[rightIdx] === nums[rightIdx-1]) rightIdx-- | ||
leftIdx++ | ||
rightIdx-- | ||
} else if (threeSum < 0) { | ||
leftIdx = leftIdx + 1 | ||
} else if (threeSum > 0) { | ||
rightIdx = rightIdx - 1 | ||
} | ||
} | ||
|
||
} | ||
|
||
return result | ||
}; |
This file contains 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,23 @@ | ||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(1) | ||
/** | ||
* @param {number[]} prices | ||
* @return {number} | ||
*/ | ||
var maxProfit = function(prices) { | ||
if (!prices || !prices.length) return 0 | ||
|
||
let maxBenefit = 0; | ||
let buyPrice = Infinity; | ||
|
||
for (let i = 0 ; i < prices.length ; i++) { | ||
buyPrice = Math.min(buyPrice, prices[i]); | ||
maxBenefit = Math.max(maxBenefit, prices[i] - buyPrice) | ||
} | ||
|
||
|
||
return maxBenefit | ||
}; | ||
|
||
console.log(maxProfit([7,1,5,3,6,4])) | ||
console.log(maxProfit([7,6,4,3,1])) |
This file contains 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,27 @@ | ||
// n 배열길이, k 단어길이 | ||
// 시간복잡도: O(n * k log k) | ||
// 공간복잡도: O(n * k) | ||
/** | ||
* @param {string[]} strs | ||
* @return {string[][]} | ||
*/ | ||
var groupAnagrams = function(strs) { | ||
const map = new Map() | ||
|
||
for (const word of strs) { | ||
const sortedWord = word.split("").sort().join("") | ||
|
||
if (!map.has(sortedWord)) { | ||
map.set(sortedWord, []) | ||
} | ||
|
||
map.set(sortedWord, [...map.get(sortedWord), word]); | ||
} | ||
|
||
|
||
return [...map.values()] | ||
}; | ||
|
||
console.log(groupAnagrams(["eat","tea","tan","ate","nat","bat"])) | ||
console.log(groupAnagrams([""])) | ||
console.log(groupAnagrams(["a"])) |
This file contains 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,60 @@ | ||
// 시간복잡도 | ||
// insert: O(L), search: O(L), startsWith: O(L) | ||
// 공간복잡도: O(N * L) | ||
|
||
var TrieNode = function(){ | ||
this.children = {}; | ||
this.endOfWord = false; | ||
} | ||
|
||
var Trie = function() { | ||
this.root = new TrieNode(); | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {void} | ||
*/ | ||
Trie.prototype.insert = function(word) { | ||
let curNode = this.root; | ||
for (let i = 0; i < word.length; i++) { | ||
const char = word[i]; | ||
if (!curNode.children[char]) { | ||
curNode.children[char] = new TrieNode(); | ||
} | ||
curNode = curNode.children[char]; | ||
} | ||
curNode.endOfWord = true; | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
Trie.prototype.search = function(word) { | ||
let curNode = this.root; | ||
for (let i = 0; i < word.length; i++) { | ||
const char = word[i]; | ||
if (!curNode.children[char]) { | ||
return false; | ||
} | ||
curNode = curNode.children[char]; | ||
} | ||
return curNode.endOfWord; | ||
}; | ||
|
||
/** | ||
* @param {string} prefix | ||
* @return {boolean} | ||
*/ | ||
Trie.prototype.startsWith = function(prefix) { | ||
let curNode = this.root; | ||
for (let i = 0; i < prefix.length; i++) { | ||
const char = prefix[i]; | ||
if (!curNode.children[char]) { | ||
return false; | ||
} | ||
curNode = curNode.children[char]; | ||
} | ||
return true; | ||
}; |
This file contains 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,27 @@ | ||
// 시간복잡도: O(n * m * n) | ||
// 공간복잡도: O(n) | ||
/** | ||
* @param {string} s | ||
* @param {string[]} wordDict | ||
* @return {boolean} | ||
*/ | ||
var wordBreak = function(s, wordDict) { | ||
const wordChecks = Array.from({length: s.length}).fill(false) | ||
const dp = [true, ...wordChecks] | ||
|
||
for (let i = 0 ; i <= s.length; i++) { | ||
for (let j = 0; j < wordDict.length; j++) { | ||
const dict = wordDict[j] | ||
const dictLen = dict.length | ||
|
||
const checkWord = s.slice(i - dictLen, i) | ||
if (dp[i - dictLen] && checkWord === dict) { | ||
dp[i] = true | ||
} | ||
} | ||
} | ||
|
||
return dp[dp.length - 1] | ||
}; | ||
|
||
console.log(wordBreak("leetcode", ["leet","code"])) |
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.
아 전에 리뷰할 때 놓쳤는데 공간복잡도가
O(N * L)
인 이유를 알 수 있을까요? (머지 이후 말씀드려 죄송합니다 ㅠㅠ)