Skip to content

[호돌이] Week5 #876

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
21 changes: 21 additions & 0 deletions best-time-to-buy-and-sell-stock/yeeZinu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function (prices) {
// 초기 값
let buy = prices[0];
// 차액
let diff = 0;

for (let i = 1; i < prices.length; i++) {
// 구매가보다 현재가격이 더 싸면 구매가로 변경
if (buy > prices[i]) {
buy = prices[i];
}
// 차액 계산, 누가더 큰지 비교
diff = Math.max(diff, (prices[i] - buy));

}
return diff
};
23 changes: 23 additions & 0 deletions group-anagrams/yeeZinu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function (strs) {
// 정답 객체
let ans = {};

for (let s of strs) {
// strs 배열에서 받아온 s를 하나씩 쪼개서 정렬, 다시 하나로 뭉침
let key = s.split('').sort().join('');

// 만약 정답 객체에 현재 단어가 없다?
if (!ans[key]) {
// 해당 값을 빈 배열로 초기화
ans[key] = [];
}
// 해당 값 배열에 초기 단어 추가.
ans[key].push(s);
}
// 객체에 값을 추가한 것이기 때문에 Object.value()를 사용해서 열거 가능한 배열로 리턴
return Object.values(ans);
};
82 changes: 82 additions & 0 deletions implement-trie-prefix-tree/yeeZinu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// trieNode 클래스 선언
// child = {}, end = false 로 초기화
class TrieNode {
constructor(child = {}, end = false) {
this.child = child;
this.end = end;
}
}

// Trie함수의 root는 TrieNode의 객체
var Trie = function() {
this.root = new TrieNode();
};

/**
* @param {string} word
* @return {void}
*/
// 단어 삽입
Trie.prototype.insert = function(word) {
// 현재 = 최상단으로 초기화
let current = this.root;

// 단어를 반복하면서 없으면 TrieNode에 추가
for (const char of word) {
if (!current.child[char]) {
current.child[char] = new TrieNode();
}
current = current.child[char];
}

// 반복이 끝나면 end true
current.end = true;
};

/**
* @param {string} word
* @return {boolean}
*/
// 단어 탐색
Trie.prototype.search = function(word) {
// 현재위치 = 최상단으로
let current = this.root;

// 반복하면서 단어찾기
for(const char of word) {
if(current.child[char]) {
current = current.child[char];
}
else {
return false;
}
}
return current.end;

};

/**
* @param {string} prefix
* @return {boolean}
*/
Trie.prototype.startsWith = function(prefix) {
let current = this.root;
for (const char of prefix) {
if (current.child[char]) {
current = current.child[char];
}
else {
return false;
}
}

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