Skip to content

[anniemon78] Week5 #871

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

Merged
merged 4 commits into from
Jan 12, 2025
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
18 changes: 18 additions & 0 deletions best-time-to-buy-and-sell-stock/anniemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* 시간 복잡도: prices.length만큼 순회하므로 O(n)
* 공간 복잡도: 상수 크기의 변수만 사용하므로 O(1)
*/
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function (prices) {
let maxProfit = 0;
let min = prices[0];

for (let i = 0; i < prices.length; i++) {
maxProfit = Math.max(maxProfit, prices[i] - min);
min = Math.min(min, prices[i]);
}
return maxProfit;
};
37 changes: 37 additions & 0 deletions encode-and-decode-strings/anniemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution {
/**
* 시간 복잡도: strs의 길이만큼 순회하므로, O(n)
* 공간 복잡도: 결괏값 제외 추가 변수 사용 없으므로 O(1)
*/
/**
* @param {string[]} strs
* @returns {string}
*/
encode(strs) {
return strs.reduce((acc, cur) => acc+ `${cur.length}` + '!' + cur, '');
}

/**
* 시간 복잡도: str의 길이만큼 순회하므로 str의 길이가 m이면, O(m)
* 공간 복잡도: 결괏값 제외 상수 크기 변수만 사용하므로, O(1)
*/
/**
* @param {string} str
* @returns {string[]}
*/
decode(str) {
const res = [];
let i = 0;
while (i < str.length) {
let j = i;
while (str[j] !== '!') {
j++;
}
const len = Number(str.slice(i, j));
const s = str.slice(j + 1, j + 1 + len);
res.push(s);
i = j + 1 + len;
}
return res;
}
}
23 changes: 23 additions & 0 deletions group-anagrams/anniemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 시간 복잡도:
* 정렬 작업은 각 문자열의 길이가 m일 때 O(m logm)이고, 총 strs의 길이만큼 수행되므로
* 시간 복잡도는 O(n * mlogm)
* 공간 복잡도:
* Map 키는 최대 길이 m인 문자열 strs.length개이다.
* 따라서 공간 복잡도는 O(n * m)
*/
/**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function(strs) {
const map = new Map();
for(const s of strs) {
const key = s.split('').sort().join('');
if(!map.has(key)) {
map.set(key, [])
}
map.get(key).push(s);
}
return Array.from(map.values());
};
61 changes: 61 additions & 0 deletions implement-trie-prefix-tree/anniemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* 시간 복잡도: 주어진 문자열의 길이만큼 순회하므로, O(n)
* 공간 복잡도: 삽입된 모든 문자열의 길이만큼 노드가 만들어지므로, 이를 m이라고 하면 O(m)
*/
var Trie = function() {
this.isEnd = false;
this.children = {};
};

/**
* @param {string} word
* @return {void}
*/
Trie.prototype.insert = function(word) {
let cur = this.children;
for(const c of word) {
if(!cur[c]) {
cur[c] = { isEnd: false, children: {} };
}
cur = cur[c];
}
cur.isEnd = true;
};

/**
* @param {string} word
* @return {boolean}
*/
Trie.prototype.search = function(word) {
let cur = this.children;
for(const c of word) {
if(!cur[c]) {
return false;
}
cur = cur[c]
}
return cur.isEnd;
};

/**
* @param {string} prefix
* @return {boolean}
*/
Trie.prototype.startsWith = function(prefix) {
let cur = this.children;
for(const c of prefix) {
if(!cur[c]){
return false;
}
cur = cur[c];
}
return true;
};

/**
* Your Trie object will be instantiated and called as such:
* var obj = new Trie()
* obj.insert(word)
* var param_2 = obj.search(word)
* var param_3 = obj.startsWith(prefix)
*/
Loading