Skip to content
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/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* 주어진 prices에서 가장 큰 prices[j] - prices[i] (i < j) 를 반환하는 함수
* @param {number[]} prices
* @return {number}
*/
const maxProfit = function(prices) {
let min = prices[0];
let profit = 0;

for (const price of prices) {
min = Math.min(min, price);
profit = Math.max(profit, price - min);
}

return profit;
};
// 시간복잡도: O(n)
// 공간복잡도: O(1)
16 changes: 16 additions & 0 deletions encode-and-decode-strings/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const encode = function (strs) {
const separator = '\\';
return strs.join(separator);
}


const decode = function (str) {
const separator = '\\';
return str.split(separator);
}

// 문제가 너무 별로다
// 문제에서 제시하고 있는 해답도 별로다
// 이모지같은 걸 구분자로 쓰거나, length를 앞에 넣어서 구별하거나 해도 사실 제대로 암호화했다고 말할 수 없음
// 그런 걸 정답으로 제공할 거면 이런 문제를 왜 내는 거여
// 이 문제가 Blind 75에 속해 있는 이유가 뭘까...?!
Copy link
Contributor

Choose a reason for hiding this comment

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

저도 공감합니다..!

32 changes: 32 additions & 0 deletions group-anagrams/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 애너그램끼리 묶어서 반환하는 함수
* @param {string[]} strs
* @return {string[][]}
*/
const groupAnagrams = function(strs) {
// 풀이 1
// 시간복잡도: O(n*s) (n: strs.length, s: str.length)
// 공간복잡도: O(n)
function groupManually () {
const groups = {};

strs.forEach((str) => {
const key = [...str].sort().join('');
if (!groups[key]) groups[key] = [];
groups[key].push(str);
});

return Object.values(groups);
}

// 풀이 2
// 시간복잡도: O(n*s) (n: strs.length, s: str.length)
// 공간복잡도: O(n)
function groupByAnagram() {
const result = Object.groupBy(strs, (str) => [...str].sort().join(''));
return Object.values(result);
}

return groupByAnagram();
};

65 changes: 65 additions & 0 deletions implement-trie-prefix-tree/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const Node = function (value) {
this.value = value;
this.children = {};
this.data = null;
}

const Trie = function () {
this.root = new Node(null);
};

/**
* @param {string} word
* @return {void}
*/
Trie.prototype.insert = function (word) {
let parent = this.root;

for (let i = 0; i < word.length; i++) {
if (!parent.children[word[i]]) {
parent.children[word[i]] = new Node(word[i]);
}
parent = parent.children[word[i]];
}

parent.data = word;
};

/**
* @param {string} word
* @return {boolean}
*/
Trie.prototype.search = function (word) {
let parent = this.root;
let i = 0;

while (i < word.length && parent.children[word[i]]) {
parent = parent.children[word[i]];
i += 1;
}

return parent.data === word;
};

/**
* @param {string} prefix
* @return {boolean}
*/
Trie.prototype.startsWith = function (prefix) {
let parent = this.root;

for (let char of prefix) {
if (!parent.children[char]) return false;
parent = parent.children[char];
}

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)
*/
30 changes: 30 additions & 0 deletions word-break/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @param {string} s
* @param {string[]} wordDict
* @return {boolean}
*/
const wordBreak = function (s, wordDict) {
const dp = Array(s.length);
const dict = new Set(wordDict);

function recurse(start) {
if (start === s.length) return true;
if (dp[start] !== undefined) return dp[start];

for (let end = start + 1; end <= s.length; end++) {
const substr = s.slice(start, end);
if (dict.has(substr) && recurse(end)) {
dp[start] = true;
return true;
}
}

dp[start] = false;
return false;
}

return recurse(0);
};

// 시간복잡도: O(n^2) (n: s.length. n번 재귀 & 최대 n번 슬라이싱)
// 공간복잡도: O(n + m) (m: wordDict.length)