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
71 changes: 71 additions & 0 deletions binary-tree-level-order-traversal/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

// ----- [Solution 1]
// Time Complexity: O(n)
// Space Complexity: O(n)
function levelOrder(root: TreeNode | null): number[][] {
if (!root) return [];

const answer: number[][] = [];

const queue: [TreeNode, number][] = [];
let pointer = 0;

queue.push([root, 0]);

while (pointer < queue.length) {
const [currNode, level] = queue[pointer++];

if (!answer[level]) answer[level] = [];

answer[level].push(currNode.val);

if (currNode.left) queue.push([currNode.left, level + 1]);
if (currNode.right) queue.push([currNode.right, level + 1]);
}

return answer;
}

// ----- [Solution 2]
// Time Complexity: O(n)
// Space Complexity: O(n)
function levelOrder(root: TreeNode | null): number[][] {
if (!root) return [];

const answer: number[][] = [];

const queue: TreeNode[] = [];
queue.push(root);

let pointer = 0;

while (pointer < queue.length) {
const levelSize = queue.length - pointer;
const currentLevel: number[] = [];

for (let i = 0; i < levelSize; i++) {
const currNode = queue[pointer++];
currentLevel.push(currNode.val);

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

answer.push(currentLevel);
}

return answer;
}
11 changes: 11 additions & 0 deletions counting-bits/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Time Complexity: O(n)
// Space Complexity: O(n)
function countBits(n: number): number[] {
const ans: number[] = Array.from({ length: n + 1 }, () => 0);

for (let i = 1; i <= n; i++) {
ans[i] = ans[i >> 1] + (i & 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

비트연산자를 이용하셨네요. 이게 정석인 것 같은데 저는 이진수로 바꿔서 1 개수를 카운팅해서 풀어서..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

비트 연산자에 익숙하지 않다면 이진수로 바꿔서 1의 갯수를 카운팅하는 방법도 좋은 방법이라고 생각합니다!

}

return ans;
}
26 changes: 26 additions & 0 deletions house-robber-ii/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Time Complexity: O(n)
// Space Complexity: O(1)
function rob(nums: number[]): number {
const n = nums.length;

if (n === 1) return nums[0];
if (n === 2) return Math.max(nums[0], nums[1]);

const getLinearRob = (start: number, end: number) => {
let prev2 = 0;
let prev1 = 0;

for (let i = start; i <= end; i++) {
const current = Math.max(prev1, prev2 + nums[i]);
prev2 = prev1;
prev1 = current;
}

return prev1;
};

let case1 = getLinearRob(0, n - 2);
let case2 = getLinearRob(1, n - 1);

return Math.max(case1, case2);
}
46 changes: 46 additions & 0 deletions meeting-rooms-ii/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Interval } from "../home/lib/index";
/**
* Definition of Interval:
* export class Interval {
* start :number;
* end :number;
* constructor(start :number, end :number) {
* this.start = start;
* this.end = end;
* }
* }
*/
import { MinHeap } from "@datastructures-js/heap";

export class Solution {
/**
* @param intervals: an array of meeting time intervals
* @return: the minimum number of conference rooms required
*/
// Time Complexity: O(n log n)
// Space Complexity: O(n)
minMeetingRooms(intervals: Interval[]): number {
intervals.sort((a, b) => a.start - b.start);

const minHeap = new MinHeap();
const n = intervals.length;

let maximumRooms = 0;
for (let i = 0; i < n; i++) {
const { start, end } = intervals[i];

if (minHeap.isEmpty()) {
minHeap.insert(end);
maximumRooms = Math.max(maximumRooms, minHeap.size());
continue;
}

if (start >= minHeap.root()) {
minHeap.pop();
}
minHeap.insert(end);
maximumRooms = Math.max(maximumRooms, minHeap.size());
}
return maximumRooms;
}
}
176 changes: 176 additions & 0 deletions word-search-ii/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// 84ms
class TrieNode {
children: Map<string, TrieNode>;
isEndOf: boolean;
word: string | null;

constructor() {
this.children = new Map();
this.word = null;
}
}
class Trie {
root: TrieNode;

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

insert(word: string) {
let current = this.root;

for (const char of word) {
if (!current.children.has(char)) {
current.children.set(char, new TrieNode());
}
current = current.children.get(char);
}

current.word = word;
}
}

function findWords(board: string[][], words: string[]): string[] {
const m = board.length;
const n = board[0].length;

const dictionary = new Trie();
for (const word of words) {
dictionary.insert(word);
}

const results: string[] = [];

const dfs = (row: number, col: number, trieNode: TrieNode) => {
if (trieNode.word !== null) {
results.push(trieNode.word);
trieNode.word = null;
}

const directions = [
[-1, 0],
[1, 0],
[0, -1],
[0, 1],
];

for (const [dr, dc] of directions) {
const [nr, nc] = [row + dr, col + dc];

if (nr >= 0 && nr < m && nc >= 0 && nc < n) {
if (trieNode.children.has(board[nr][nc])) {
const char = board[nr][nc];
const childNode = trieNode.children.get(char);

if (board[nr][nc] !== "#") {
board[nr][nc] = "#";
dfs(nr, nc, trieNode.children.get(char));
board[nr][nc] = char;
}

if (childNode.children.size === 0 && childNode.word === null) {
trieNode.children.delete(char);
}
}
}
}
};

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
const firstChar = board[i][j];
if (dictionary.root.children.has(firstChar)) {
board[i][j] = "#";
dfs(i, j, dictionary.root.children.get(firstChar));
board[i][j] = firstChar;
}
}
}

return results;
}

// 2312ms
class TrieNode {
children: Map<string, TrieNode>;
word: string | null;

constructor() {
this.children = new Map();
this.word = null;
}
}
class Trie {
root: TrieNode;

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

insert(word: string) {
let current = this.root;

for (const char of word) {
if (!current.children.has(char)) {
current.children.set(char, new TrieNode());
}
current = current.children.get(char)!;
}

current.word = word;
}
}

function findWords(board: string[][], words: string[]): string[] {
const m = board.length;
const n = board[0].length;

const dictionary = new Trie();
for (const word of words) {
dictionary.insert(word);
}

const results: string[] = [];

const dfs = (row: number, col: number, trieNode: TrieNode, path: string) => {
if (trieNode.word === path) {
results.push(path);
trieNode.word = null;
}

const directions = [
[-1, 0],
[1, 0],
[0, -1],
[0, 1],
];

for (const [dr, dc] of directions) {
const [nr, nc] = [row + dr, col + dc];

if (nr >= 0 && nr < m && nc >= 0 && nc < n) {
if (trieNode.children.has(board[nr][nc])) {
if (!isVisited.has(`${nr},${nc}`)) {
isVisited.add(`${nr},${nc}`);
dfs(nr, nc, trieNode.children.get(board[nr][nc])!, path + board[nr][nc]);
isVisited.delete(`${nr},${nc}`);
}
}
}
}
};

const isVisited = new Set<string>(); // 'r,c'
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
const firstChar = board[i][j];
if (dictionary.root.children.has(firstChar)) {
isVisited.add(`${i},${j}`);
dfs(i, j, dictionary.root.children.get(firstChar)!, firstChar);
isVisited.delete(`${i},${j}`);
}
}
}

return results;
}