Skip to content

Commit ea9b6b4

Browse files
authored
Merge pull request #457 from JEONGHWANMIN/main
[ํ™˜๋ฏธ๋‹ˆ๋‹ˆ] Week5 ๋ฌธ์ œํ’€์ด
2 parents ec67f28 + 37ed176 commit ea9b6b4

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed

โ€Ž3sum/hwanmini.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„ O(n2)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„ O(k) (k๋Š” ๊ฒฐ๊ณผ ๊ฐœ์ˆ˜)
3+
/**
4+
* @param {number[]} nums
5+
* @return {number[][]}
6+
*/
7+
var threeSum = function(nums) {
8+
nums.sort((a, b) => a - b);
9+
10+
let result = []
11+
12+
for (let i = 0 ; i <= nums.length - 2; i++) {
13+
if (i > 0 && nums[i] === nums[i-1]) continue
14+
15+
const curNum = nums[i]
16+
let leftIdx = i+1;
17+
let rightIdx = nums.length - 1;
18+
19+
while (leftIdx < rightIdx) {
20+
const leftNum = nums[leftIdx]
21+
const rightNum = nums[rightIdx]
22+
const threeSum = curNum + leftNum + rightNum
23+
if (threeSum === 0) {
24+
result.push([curNum, leftNum, rightNum])
25+
while (leftIdx < rightIdx && nums[leftIdx] === nums[leftIdx+1]) leftIdx++
26+
while (leftIdx < rightIdx && nums[rightIdx] === nums[rightIdx-1]) rightIdx--
27+
leftIdx++
28+
rightIdx--
29+
} else if (threeSum < 0) {
30+
leftIdx = leftIdx + 1
31+
} else if (threeSum > 0) {
32+
rightIdx = rightIdx - 1
33+
}
34+
}
35+
36+
}
37+
38+
return result
39+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
3+
/**
4+
* @param {number[]} prices
5+
* @return {number}
6+
*/
7+
var maxProfit = function(prices) {
8+
if (!prices || !prices.length) return 0
9+
10+
let maxBenefit = 0;
11+
let buyPrice = Infinity;
12+
13+
for (let i = 0 ; i < prices.length ; i++) {
14+
buyPrice = Math.min(buyPrice, prices[i]);
15+
maxBenefit = Math.max(maxBenefit, prices[i] - buyPrice)
16+
}
17+
18+
19+
return maxBenefit
20+
};
21+
22+
console.log(maxProfit([7,1,5,3,6,4]))
23+
console.log(maxProfit([7,6,4,3,1]))

โ€Žgroup-anagrams/hwanmini.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// n ๋ฐฐ์—ด๊ธธ์ด, k ๋‹จ์–ด๊ธธ์ด
2+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n * k log k)
3+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n * k)
4+
/**
5+
* @param {string[]} strs
6+
* @return {string[][]}
7+
*/
8+
var groupAnagrams = function(strs) {
9+
const map = new Map()
10+
11+
for (const word of strs) {
12+
const sortedWord = word.split("").sort().join("")
13+
14+
if (!map.has(sortedWord)) {
15+
map.set(sortedWord, [])
16+
}
17+
18+
map.set(sortedWord, [...map.get(sortedWord), word]);
19+
}
20+
21+
22+
return [...map.values()]
23+
};
24+
25+
console.log(groupAnagrams(["eat","tea","tan","ate","nat","bat"]))
26+
console.log(groupAnagrams([""]))
27+
console.log(groupAnagrams(["a"]))
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„
2+
// insert: O(L), search: O(L), startsWith: O(L)
3+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(N * L)
4+
5+
var TrieNode = function(){
6+
this.children = {};
7+
this.endOfWord = false;
8+
}
9+
10+
var Trie = function() {
11+
this.root = new TrieNode();
12+
};
13+
14+
/**
15+
* @param {string} word
16+
* @return {void}
17+
*/
18+
Trie.prototype.insert = function(word) {
19+
let curNode = this.root;
20+
for (let i = 0; i < word.length; i++) {
21+
const char = word[i];
22+
if (!curNode.children[char]) {
23+
curNode.children[char] = new TrieNode();
24+
}
25+
curNode = curNode.children[char];
26+
}
27+
curNode.endOfWord = true;
28+
};
29+
30+
/**
31+
* @param {string} word
32+
* @return {boolean}
33+
*/
34+
Trie.prototype.search = function(word) {
35+
let curNode = this.root;
36+
for (let i = 0; i < word.length; i++) {
37+
const char = word[i];
38+
if (!curNode.children[char]) {
39+
return false;
40+
}
41+
curNode = curNode.children[char];
42+
}
43+
return curNode.endOfWord;
44+
};
45+
46+
/**
47+
* @param {string} prefix
48+
* @return {boolean}
49+
*/
50+
Trie.prototype.startsWith = function(prefix) {
51+
let curNode = this.root;
52+
for (let i = 0; i < prefix.length; i++) {
53+
const char = prefix[i];
54+
if (!curNode.children[char]) {
55+
return false;
56+
}
57+
curNode = curNode.children[char];
58+
}
59+
return true;
60+
};

โ€Žword-break/hwanmini.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n * m * n)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
3+
/**
4+
* @param {string} s
5+
* @param {string[]} wordDict
6+
* @return {boolean}
7+
*/
8+
var wordBreak = function(s, wordDict) {
9+
const wordChecks = Array.from({length: s.length}).fill(false)
10+
const dp = [true, ...wordChecks]
11+
12+
for (let i = 0 ; i <= s.length; i++) {
13+
for (let j = 0; j < wordDict.length; j++) {
14+
const dict = wordDict[j]
15+
const dictLen = dict.length
16+
17+
const checkWord = s.slice(i - dictLen, i)
18+
if (dp[i - dictLen] && checkWord === dict) {
19+
dp[i] = true
20+
}
21+
}
22+
}
23+
24+
return dp[dp.length - 1]
25+
};
26+
27+
console.log(wordBreak("leetcode", ["leet","code"]))

0 commit comments

Comments
ย (0)