Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
ff3b408
add readme
oris8 Sep 12, 2024
93d1ee5
217 / Contains Duplicate / easy / 2m 54s
oris8 Sep 24, 2024
b3f3516
268 / Missing Number / easy / 3m 15s
oris8 Sep 24, 2024
a096dc4
121 / Best Time to Buy and Sell Stock / easy / 15m 31s
oris8 Sep 24, 2024
307c972
15 / 3sum / medium / 21m 26s
oris8 Sep 24, 2024
072559b
252 / Meeting Room / easy / 13m 18s
oris8 Sep 24, 2024
e8d4aaa
217 / Contains Duplicate / easy / 2m 54s (๊ฐœ์„ )
oris8 Sep 24, 2024
46e92c5
268 / Missing Number / easy / 3m 15s (๊ฐœ์„ )
oris8 Sep 24, 2024
906778d
Merge branch 'oris8' of https://github.com/forStudyingJavaScript/leetโ€ฆ
oris8 Sep 24, 2024
d2b8ccb
242 / Valid Anagram / easy / 8m 17s
oris8 Oct 1, 2024
13eab5b
191 / Number of 1 Bits / easy / 5m
oris8 Oct 1, 2024
d0c7451
100 / Same Tree / easy / 21m 2s
oris8 Oct 1, 2024
3980088
746 / Min Cost Climbing Stairs / easy / 24m 43s
oris8 Oct 1, 2024
0e0a50c
238 / Product Of Array Except Self / medium / ใ… ใ… 
oris8 Oct 1, 2024
ba97b53
Merge branch 'master' of https://github.com/forStudyingJavaScript/leeโ€ฆ
oris8 Oct 1, 2024
eb03bf6
Merge branch 'master' of https://github.com/forStudyingJavaScript/leeโ€ฆ
oris8 Oct 9, 2024
94e8f36
49 / Group Anagrams / medium / 5m 24s
oris8 Oct 9, 2024
9d8304a
190 / Reverse Bits / easy / 12m 14s
oris8 Oct 9, 2024
bd64249
125 / Valid Palindrome / easy / 4m 3s
oris8 Oct 9, 2024
3cdb5c4
206 / Reverse Linked List / easy / 10m 47s
oris8 Oct 9, 2024
ebe8b2c
322 / Coin Change / medium / 21m 7s
oris8 Oct 9, 2024
8505a8f
Merge branch 'master' of https://github.com/forStudyingJavaScript/leeโ€ฆ
oris8 Oct 15, 2024
0203ee7
1 / Two Sum / easy / 8m 21s
oris8 Oct 15, 2024
3d6bf36
104 / Maximun Depth of Binary Tree / easy / 3m 20
oris8 Oct 15, 2024
ad1a985
141 / Linked List Cycle / easy / 3m 12s
oris8 Oct 15, 2024
dbae9bd
202 / Happy Number / Easy / 7m 10s
oris8 Oct 15, 2024
fc17d7b
20 / Valid Parentheses / easy / 10m 48s
oris8 Oct 15, 2024
0663f7b
Merge branch 'master' of https://github.com/forStudyingJavaScript/leeโ€ฆ
oris8 Oct 23, 2024
020b8cb
338 / Counting Bits / easy
oris8 Oct 23, 2024
76acecb
70 / Climbing Stairs / easy / 8m 19s
oris8 Oct 23, 2024
baeafc5
21 / Merge Two Sorted Lists / easy / 13m
oris8 Oct 23, 2024
05a9eb4
48 / Rotate Image / medium / 14m 21s
oris8 Oct 23, 2024
c514e4d
1046 / Last Stone Weight / easy / 18m 5s
oris8 Oct 23, 2024
dfe2442
Merge branch 'master' of https://github.com/forStudyingJavaScript/leeโ€ฆ
oris8 Nov 5, 2024
a226562
198 / House Robber / medium / 10m 16s
oris8 Nov 5, 2024
46cf7c4
66 / Plus One / easy / 10m 54s
oris8 Nov 5, 2024
6e0bf61
704 / Binary Search / easy / 8m 12s
oris8 Nov 5, 2024
4187edf
703 / Kth Largest Element in a Stream / Easy / 12m 10s
oris8 Nov 5, 2024
5a2f27d
271 / String Encode and Decode / medium / ...
oris8 Nov 5, 2024
9af46da
Merge branch 'master' of https://github.com/forStudyingJavaScript/leeโ€ฆ
oris8 Nov 12, 2024
6baade0
136 / Single Number / easy / 3m 5s
oris8 Nov 12, 2024
a959c5d
973 / K Closest Points to Origin / Medium / 7m 40s
oris8 Nov 12, 2024
3491cea
110 / Balanced Binary Tree / easy / 12m 10s
oris8 Nov 12, 2024
672bf83
155 / Min Stack / midium / 10m 18s
oris8 Nov 12, 2024
42d5b15
54 / Spiral Matrix / medium / 28m 20s
oris8 Nov 12, 2024
ea1d8e7
Merge branch 'master' of https://github.com/forStudyingJavaScript/leeโ€ฆ
oris8 Nov 19, 2024
e401801
128 / Longest Consecutive Sequence / medium / 6m 27s
oris8 Nov 19, 2024
2ff8326
208 / Implement Trie (Prefix Tree) / medium / 13m 30s
oris8 Nov 19, 2024
2e0cf60
208 / Implement Trie (Prefix Tree) / ๊ฐœ์„ 
oris8 Nov 19, 2024
3d7caf7
226 / Invert Binary Tree / easy / 6m 13s
oris8 Nov 19, 2024
189df48
543 / Diameter of Binary Tree / easy / 13m 46s
oris8 Nov 19, 2024
79ad803
371 / Sum of Two Integers / medium / -
oris8 Nov 19, 2024
c3973f1
Merge branch 'master' of https://github.com/forStudyingJavaScript/leeโ€ฆ
oris8 Nov 26, 2024
f91da68
102 / Binary Tree Level Order Traversal / Medium / 12m 8s
oris8 Nov 26, 2024
809d4f3
572 / Subtree of Another Tree / Easy / 23m 12s
oris8 Nov 26, 2024
d9fe54e
213 / House Robber II / Medium / 13m 27s
oris8 Nov 26, 2024
4f08293
207 / Course Schedule / Medium / -
oris8 Nov 26, 2024
fee7432
150 / Evaluate Reverse Polish Notation / Medium / 36m 44s
oris8 Nov 26, 2024
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
66 changes: 66 additions & 0 deletions oris8/102_binary-tree-level-order-traversal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*
* 102 / Binary Tree Level Order Traversal / Medium / 12m 8s
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var levelOrder = function (root) {
if (!root) return [];

const result = [];
const queue = [root];

while (queue.length > 0) {
const levelSize = queue.length;
const currentLevel = [];

for (let i = 0; i < levelSize; i++) {
const node = queue.shift();
currentLevel.push(node.val);

if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}

result.push(currentLevel);
}

return result;
};

// var levelOrder = function(root) {
// const result = [];

// const traverse = (node, level) => {
// if (!node) return;
// if (result.length === level) {
// result.push([]); // ์ƒˆ๋กœ์šด ๋ ˆ๋ฒจ ์ƒ์„ฑ
// }
// result[level].push(node.val); // ํ˜„์žฌ ๋ ˆ๋ฒจ์— ๊ฐ’ ์ถ”๊ฐ€
// traverse(node.left, level + 1); // ์™ผ์ชฝ ์ž์‹ ํƒ์ƒ‰
// traverse(node.right, level + 1); // ์˜ค๋ฅธ์ชฝ ์ž์‹ ํƒ์ƒ‰
// };

// traverse(root, 0);
// return result;
// };
35 changes: 35 additions & 0 deletions oris8/128_longest-consecutive-sequence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
*
* 128. Longest Consecutive Sequence
* Medium
* Topics
* Companies
* Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
*
* You must write an algorithm that runs in O(n) time.
* @param {number[]} nums
* @return {number}
*/
var longestConsecutive = function (nums) {
// ์ค‘๋ณต๋˜๋Š” ์š”์†Œ ํ—ˆ์šฉ, ๋นˆ ๋ฐฐ์—ด ํ—ˆ์šฉ
if (nums.length === 0) return 0;

const set = new Set(nums);
const sortedSet = Array.from(set).sort((a, b) => a - b);

let maxSequenceLength = 1;
let currentSequence = [sortedSet[0]];

for (let i = 1; i < sortedSet.length; i++) {
const last = currentSequence.pop();
if (last + 1 === sortedSet[i]) {
currentSequence.push(last);
currentSequence.push(sortedSet[i]);
} else {
currentSequence = [sortedSet[i]];
}
maxSequenceLength = Math.max(maxSequenceLength, currentSequence.length);
}

return maxSequenceLength;
};
29 changes: 29 additions & 0 deletions oris8/150_evaluate-reverse-polish-notation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
*
* 150 / Evaluate Reverse Polish Notation / Medium / 36m 44s
*
* @param {string[]} tokens
* @return {number}
*/
var evalRPN = function (tokens) {
const operators = {
"+": (a, b) => a + b,
"-": (a, b) => a - b,
"*": (a, b) => a * b,
"/": (a, b) => Math.trunc(a / b),
};

const stack = [];

for (const token of tokens) {
if (operators?.[token]) {
const b = stack.pop();
const a = stack.pop();
stack.push(operators[token](a, b));
} else {
stack.push(+token);
}
}

return stack.pop();
};
50 changes: 50 additions & 0 deletions oris8/207_course-schedule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 207 / Course Schedule / Medium / -
/**
* @param {number} numCourses
* @param {number[][]} prerequisites
* @return {boolean}
*/

/**
* @param {number} numCourses
* @param {number[][]} prerequisites
* @return {boolean}
*/
var canFinish = function (numCourses, prerequisites) {
const graph = new Map();

// ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ
for (let i = 0; i < numCourses; i++) {
graph.set(i, []);
}

for (const [course, prereq] of prerequisites) {
graph.get(prereq).push(course);
}

// ๋ฐฉ๋ฌธ ์ƒํƒœ ๊ด€๋ฆฌ
const visited = new Set(); // ํ˜„์žฌ DFS ๋ฐฉ๋ฌธ ์ค‘์ธ ๋…ธ๋“œ
const completed = new Set(); // ์ด๋ฏธ ์ฒ˜๋ฆฌ ์™„๋ฃŒ๋œ ๋…ธ๋“œ

// ์‚ฌ์ดํด ํƒ์ง€ ํ•จ์ˆ˜
const dfs = (course) => {
if (visited.has(course)) return false; // ์‚ฌ์ดํด ๋ฐœ๊ฒฌ
if (completed.has(course)) return true; // ์ด๋ฏธ ํ™•์ธ๋œ ๊ฒฝ์šฐ

visited.add(course); // ํ˜„์žฌ ๋…ธ๋“œ ๋ฐฉ๋ฌธ ์ค‘
for (const nextCourse of graph.get(course)) {
if (!dfs(nextCourse)) return false; // ๋‹ค์Œ ์ฝ”์Šค์—์„œ ์‚ฌ์ดํด ๋ฐœ๊ฒฌ
}
visited.delete(course); // ํƒ์ƒ‰ ์™„๋ฃŒ ํ›„ ์ œ๊ฑฐ
completed.add(course); // ์™„๋ฃŒ๋œ ๋…ธ๋“œ๋กœ ๊ธฐ๋ก

return true;
};

// ๊ฐ ๊ฐ•์ขŒ์— ๋Œ€ํ•ด DFS ์‹คํ–‰
for (let i = 0; i < numCourses; i++) {
if (!dfs(i)) return false; // ์‚ฌ์ดํด์ด ์กด์žฌํ•˜๋ฉด false ๋ฐ˜ํ™˜
}

return true; // ๋ชจ๋“  ๊ฐ•์ขŒ๋ฅผ ์™„๋ฃŒํ•  ์ˆ˜ ์žˆ์Œ
};
85 changes: 85 additions & 0 deletions oris8/208_implement-trie-prefix-tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* 208. Implement Trie (Prefix Tree)
Medium
Topics
Companies
A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.

Implement the Trie class:

Trie() Initializes the trie object.
void insert(String word) Inserts the string word into the trie.
boolean search(String word) Returns true if the string word is in the trie (i.e., was inserted before), and false otherwise.
boolean startsWith(String prefix) Returns true if there is a previously inserted string word that has the prefix prefix, and false otherwise.

1 <= word.length, prefix.length <= 2000
word and prefix consist only of lowercase English letters.
At most 3 * 104 calls in total will be made to insert, search, and startsWith.
*/

// var Trie = function() {
// node
// };

class TrieNode {
constructor() {
this.children = {};
this.words = new Set();
}
}

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

traverse(word) {
let node = this.root;
for (let char of word) {
node = node.children[char];
if (!node) return null;
}
return node;
}
}

/**
* @param {string} word
* @return {void}
*/
Trie.prototype.insert = function (word) {
let node = this.root;
for (const char of word) {
if (!node.children[char]) {
node.children[char] = new TrieNode();
}
node = node.children[char];
}
node.words.add(word);
};

/**
* @param {string} word
* @return {boolean}
*/
Trie.prototype.search = function (word) {
let node = this.traverse(word);
return !!node && node.words.has(word);
};

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

/**
* 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)
*/
19 changes: 19 additions & 0 deletions oris8/213_house-robber-ii.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 213 / House Robber II / Medium / 13m 27s
/**
* @param {number[]} nums
* @return {number}
*/
var rob = function (nums) {
// T(n) = Math.max(T(n-1), T(n-2) + n)
if (nums.length <= 2) return Math.max(nums[0], nums?.[1] || 0);

const results = [nums[0], Math.max(nums[0], nums[1])];
const not1results = [0, nums[1]];
for (let i = 2; i < nums.length; i++) {
results[i] = Math.max(results[i - 1], results[i - 2] + nums[i]);
not1results[i] = Math.max(not1results[i - 1], not1results[i - 2] + nums[i]);
}

return Math.max(results[nums.length - 2], not1results[nums.length - 1]);
};
21 changes: 21 additions & 0 deletions oris8/226_invert-binary-tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function (root) {
if (root) {
[root.right, root.left] = [invertTree(root.left), invertTree(root.right)];
}
// if (!root) return null;
// [root.left, root.right] = [invertTree(root.right), invertTree(root.left)];

return root;
};
27 changes: 27 additions & 0 deletions oris8/371_sum-of-two-integers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 371. Sum of Two Integers
* Medium
*
* Given two integers a and b, return the sum of the two integers without using the operators + and -.
*
* -1000 <= a, b <= 1000
*
* @param {number} a
* @param {number} b
* @return {number}
*/
var getSum = function (a, b) {
// ๋น„ํŠธ ์—ฐ์‚ฐ ์‚ฌ์šฉ
/**
*
* XOR :๊ฐ ๋น„ํŠธ๋ฅผ ๋น„๊ตํ•˜์—ฌ, ๋‘ ๋น„ํŠธ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅด๋ฉด 1์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ฐ™์œผ๋ฉด 0์„ ๋ฐ˜ํ™˜
* === carry(์˜ฌ๋ฆผ)๋ฅผ ๋ฌด์‹œํ•œ ๋ง์…ˆ๊ณผ ๋™์ผํ•œ ๊ฒฐ๊ณผ
*
* AND :๋‘ ๋น„ํŠธ๊ฐ€ ๋ชจ๋‘ 1์ธ ๊ฒฝ์šฐ
*
* ์ž๋ฆฌ์˜ฌ๋ฆผ์€ ๋‘ ๋น„ํŠธ๊ฐ€ ๋ชจ๋‘ 1์ผ๋•Œ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ AND ์—ฐ์‚ฐ์„ ํ†ตํ•ด์„œ ์ด๋ฅผ ํ™•์ธํ•˜๊ณ , ๋‘ ๋น„ํŠธ๊ฐ€ ๋ชจ๋‘ 1์ผ ๊ฒฝ์šฐ ์ž๋ฆฌ์˜ฌ๋ฆผ ์ˆ˜ํ–‰ (<<)
*
*/

return b === 0 ? a : getSum(a ^ b, (a & b) << 1);
};
44 changes: 44 additions & 0 deletions oris8/543_diameter-of-binary-tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
*
* 543. Diameter of Binary Tree
Easy
Topics
Companies
Given the root of a binary tree, return the length of the diameter of the tree.

The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

The length of a path between two nodes is represented by the number of edges between them.

The number of nodes in the tree is in the range [1, 104].
-100 <= Node.val <= 100

* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var diameterOfBinaryTree = function (root) {
let maxDiameter = 0;
const getBinaryTreeHeight = (node) => {
if (!node) return 0;

// ํ˜„์žฌ ๋…ธ๋“œ๋ถ€ํ„ฐ ๋†’์ด๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ๊ณ„์‚ฐ
const right = getBinaryTreeHeight(node.right);
const left = getBinaryTreeHeight(node.left);

maxDiameter = Math.max(maxDiameter, right + left);

// ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ํฌํ•จํ•˜๊ธฐ ์œ„ํ•ด + 1
return Math.max(right, left) + 1;
};

getBinaryTreeHeight(root);
return maxDiameter;
};
Loading
Loading