Skip to content
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 1 commit into from
Sep 14, 2024
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
39 changes: 39 additions & 0 deletions 3sum/hwanmini.js
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
};
23 changes: 23 additions & 0 deletions best-time-to-buy-and-sell-stock/hwanmini.js
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]))
27 changes: 27 additions & 0 deletions group-anagrams/hwanmini.js
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"]))
60 changes: 60 additions & 0 deletions implement-trie-prefix-tree/hwanmini.js
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

아 전에 리뷰할 때 놓쳤는데 공간복잡도가 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;
};
27 changes: 27 additions & 0 deletions word-break/hwanmini.js
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"]))