Skip to content
Open
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
24 changes: 13 additions & 11 deletions best-time-to-buy-and-sell-stock/soobing.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers
  • 설명: 이 코드는 두 포인터(left, right)를 이용하여 최적의 매수와 매도 시점을 찾는 방식으로, 배열 내에서 조건에 따라 포인터를 이동시키는 패턴입니다.

Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// 3rd tried
function maxProfit(prices: number[]): number {
let minPrice = Infinity;
let maxProfit = 0;
for (let i = 0; i < prices.length; i++) {
if (prices[i] < minPrice) {
minPrice = prices[i];
}
let left = 0;
let right = 1;
let max = 0;

if (prices[i] - minPrice > maxProfit) {
maxProfit = prices[i] - minPrice;
}
while(left < right && right < prices.length) {
if(prices[right] - prices[left] > 0) {
max = Math.max(max, prices[right] - prices[left])
} else {
left = right;
}
right++;
}
return maxProfit;
}
return max;
};
40 changes: 16 additions & 24 deletions encode-and-decode-strings/soobing.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: String Manipulation
  • 설명: 이 코드는 문자열을 인코딩하고 디코딩하는 과정에서 문자열 길이와 구분자를 활용하여 문자열을 처리하는 방식으로, 특별한 알고리즘 패턴보다는 문자열 조작에 초점을 맞추고 있습니다.

Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
// 3rd tried
class Solution {
/**
* 문자열 배열을 하나의 문자열로 인코딩합니다.
* @param strs - 문자열 배열
* @returns 인코딩된 하나의 문자열
* @param {string[]} strs
* @returns {string}
*/
encode(strs: string[]): string {
return strs.map((str) => str.length + "#" + str).join("");
encode(strs) {
return strs.map((str) => `${str.length}#${str}`).join('');
}

/**
* 인코딩된 문자열을 원래 문자열 배열로 디코딩합니다.
* @param str - 인코딩된 문자열
* @returns 디코딩된 문자열 배열
* @param {string} str
* @returns {string[]}
*/
decode(str: string): string[] {
const result: string[] = [];

let i = 0;
while (i < str.length) {
let j = i;
while (str[j] !== "#") {
j++;
decode(str) {
const result: string[] = [];
let i = 0;
while(i < str.length) {
const j = str.indexOf('#', i);
const length = Number(str.slice(i, j));
result.push(str.slice(j + 1, j + 1 + length));
i = j + 1 + length;
}

const length = parseInt(str.slice(i, j));
const word = str.slice(j + 1, j + 1 + length);
result.push(word);
i = j + 1 + length;
}

return result;
return result;
}
}
20 changes: 8 additions & 12 deletions group-anagrams/soobing.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set
  • 설명: 이 코드는 문자열을 정렬하여 키로 사용하고, 해시 맵에 그룹화하는 방식으로 동작하여 해시 맵 패턴에 속합니다. 문자열 그룹화를 위해 해시 맵을 활용하는 대표적인 예입니다.

Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
// idea: 배열에 담긴 모든 애들을 다 sorting하면서 sorting된 결과를 key로 바인딩하고 Record<string, string[]> 에 맞게 매핑하여 values들만 리턴하면 될것 같음
// 3rd tried
function groupAnagrams(strs: string[]): string[][] {
const map = new Map<string, string[]>();
const map = new Map();

for (let i = 0; i < strs.length; i++) {
const key = strs[i].split("").sort().join("");
const group = map.get(key);
if (group) {
group.push(strs[i]);
} else {
map.set(key, [strs[i]]);
}
for(const str of strs) {
const key = str.split('').sort().join('');
const value = map.get(key);
map.set(key, value ? [...value, str] : [str])
}
return [...map.values()];
}
return Array.from(map.values());
};
55 changes: 31 additions & 24 deletions implement-trie-prefix-tree/soobing.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Trie
  • 설명: 이 코드는 문자열의 접두사 검색을 위해 트리 구조를 활용하는 Trie 알고리즘을 사용합니다. 효율적인 문자열 검색과 삽입이 가능하게 설계되었습니다.

Original file line number Diff line number Diff line change
@@ -1,46 +1,53 @@
// 3rd tried
class TrieNode {
children: Map<string, TrieNode>;
isEnd: boolean;

constructor() {
this.children = new Map();
this.isEnd = false;
this.children = new Map();
this.isEnd = false
}
}

class Trie {
root: TrieNode;

constructor() {
this.root = new TrieNode();
this.root = new TrieNode();
}

insert(word: string): void {
let node = this.root;
for (const char of word) {
if (!node.children.has(char)) {
node.children.set(char, new TrieNode());
let node = this.root;
for(const ch of word) {
if(!node.children.has(ch)) {
node.children.set(ch, new TrieNode());
}
node = node.children.get(ch)!
}
node = node.children.get(char)!;
}
node.isEnd = true;
node.isEnd = true;
}

search(word: string): boolean {
const node = this._findNode(word);
return node !== null && node.isEnd;
let node = this.root;
for(const ch of word) {
if(!node.children.has(ch)) return false;
node = node.children.get(ch)!;
}
return node.isEnd;
}

startsWith(prefix: string): boolean {
return this._findNode(prefix) !== null;
}

private _findNode(word: string): TrieNode | null {
let node = this.root;
for (const char of word) {
if (!node.children.has(char)) return null;
node = node.children.get(char)!;
}
return node;
let node = this.root;
for(const ch of prefix) {
if(!node.children.has(ch)) return false;
node = node.children.get(ch)!;
}
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)
*/
23 changes: 12 additions & 11 deletions word-break/soobing.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming
  • 설명: 이 코드는 문자열을 부분 문제로 나누어 해결하는 DP 방식을 사용하며, 이전 결과를 활용하여 문자열 분할 가능성을 판단합니다.

Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// 3rd tried
function wordBreak(s: string, wordDict: string[]): boolean {
const dp = new Array(s.length + 1).fill(false);
dp[s.length] = true;
dp[0] = true;

for (let i = s.length - 1; i >= 0; i--) {
for (const word of wordDict) {
if (i + word.length <= s.length && s.slice(i, i + word.length) === word) {
dp[i] = dp[i + word.length];
}

if (dp[i]) break;
for (let i = 1; i <= s.length; i++) {
for (let j = 0; j < i; j++) {
if (dp[j] && wordDict.includes(s.slice(j, i))) {
dp[i] = true;
break;
}
}
}
}
return dp[0];
}

return dp[s.length];
};
Loading