forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* λ°°μ΄μμ κ°μ₯ μμ μ μ°ΎκΈ° ( μ μ½ : μκ° λ³΅μ‘λ O(log n) ) | ||
* @param {number[]} nums νμ λ μ λ°°μ΄ | ||
* | ||
* μκ° λ³΅μ‘λ: O(log n) | ||
* - μ΄λΆ νμμ μ¬μ©νμ¬ μ΅μκ°μ νμ | ||
* | ||
* κ³΅κ° λ³΅μ‘λ: O(1) | ||
*/ | ||
function findMin(nums: number[]): number { | ||
let left = 0, right = nums.length - 1; | ||
|
||
while (left <= right) { | ||
let mid = Math.floor((left + right) / 2); | ||
|
||
// μ λ ¬μ΄ λ§κ°μ§ κ²½μ° | ||
if (nums[mid] < nums[mid-1]) return nums[mid]; | ||
|
||
// left, right λ²μ μ€μ¬λκ°κΈ° | ||
if (nums[0] < nums[mid]){ | ||
left = mid + 1; | ||
} else { | ||
right = mid - 1; | ||
} | ||
} | ||
|
||
// νμ νμλ μ°Ύμ§ λͺ»ν κ²½μ° νμ λμ§ μμ κ²½μ° | ||
return nums[0]; | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* class ListNode { | ||
* val: number | ||
* next: ListNode | null | ||
* constructor(val?: number, next?: ListNode | null) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
* } | ||
*/ | ||
|
||
/** | ||
* μ°κ²° 리μ€νΈμΈκ° μννλμ§ μ¬λΆ νμΈ | ||
* @param {ListNode} head - ListNode | ||
* @returns {boolean} - μν μ¬λΆ | ||
* | ||
* μκ° λ³΅μ‘λ: O(n) | ||
* | ||
* κ³΅κ° λ³΅μ‘λ: O(n) | ||
* - λ Έλμ κ°μλ§νΌ Setμ μ μ₯ | ||
*/ | ||
function hasCycle(head: ListNode | null): boolean { | ||
const set = new Set(); | ||
|
||
while (head) { | ||
if (set.has(head)) { | ||
return true; | ||
} | ||
|
||
set.add(head); | ||
head = head.next; | ||
} | ||
|
||
return false; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* subArray μ€ μ΅λ κ³±μ ꡬνλ ν¨μ | ||
* @param {number[]} nums - μ«μ λ°°μ΄ | ||
* @returns {number} - subArray μ€ μ΅λ κ³± | ||
* | ||
* @description μμμ κ³±μ΄ μ‘΄μ¬ ν μ μκΈ° λλ¬Έμ, μ΅λ κ°κ³Ό μ΅μκ°μ κ°±μ νλ©°, κ²°κ³Ό κ°μ κ°±μ . | ||
* | ||
* μκ° λ³΅μ‘λ : O(n) | ||
* - nums λ°°μ΄ 1ν μν | ||
* | ||
* κ³΅κ° λ³΅μ‘λ : O(1) | ||
*/ | ||
function maxProduct(nums: number[]): number { | ||
let max = nums[0]; | ||
let min = nums[0]; | ||
let result = nums[0]; | ||
|
||
// 첫 λ²μ§Έ μμλ₯Ό μ μΈν λͺ¨λ μμλ₯Ό νμ | ||
for (let i = 1; i < nums.length; i++) { | ||
let current = nums[i] | ||
|
||
// νμ¬ κ°, μ΄μ μ΅λ κ³±κ³Όμ κ³±, μ΄μ μ΅μ κ³±κ³Όμ κ³± μ€ μ΅λ/μ΅μ κ°±μ | ||
const cases = [current * max, current * min, current]; | ||
|
||
max = Math.max(...cases); | ||
min = Math.min(...cases); | ||
result = Math.max(result, max); | ||
} | ||
|
||
return result; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* κΉμ΄ μ°μ νμ(DFS)μ μ¬μ©νμ¬ νΉμ λ°λ€μμ μ¬λΌκ° μ μλ μ’νμ μ μ₯ | ||
* @param i νμ¬ μμΉμ ν (row) | ||
* @param j νμ¬ μμΉμ μ΄ (column) | ||
* @param visited λ°©λ¬Έν μ’νλ₯Ό μ μ₯νλ Set (λ°λ€μμ λλ¬ν μ μλ μμΉλ₯Ό μ μ₯) | ||
* @param heights λμ΄ μ λ³΄κ° λ΄κΈ΄ 2μ°¨μ λ°°μ΄ | ||
* | ||
* μκ° λ³΅μ‘λ: O(m Γ n) | ||
* - κ° μ μ μ΅λ ν λ² λ°©λ¬Ένλ©°, μ΄ m Γ nκ°μ μ μ νμν¨ | ||
* | ||
* κ³΅κ° λ³΅μ‘λ: O(m Γ n) | ||
* - `visited` Setμ μ΅λ m Γ nκ°μ μ’νλ₯Ό μ μ₯ κ°λ₯ | ||
* - μ¬κ· νΈμΆ μ€νμ κΉμ΄λ O(m + n) (μ΅μ μ κ²½μ° κ°μ₯ κΈ΄ κ²½λ‘λ₯Ό λ°λΌ νμ) | ||
*/ | ||
function dfs(i: number, j: number, visited: Set<string>, heights: number[][]) { | ||
if (visited.has(`${i},${j}`)) return; | ||
|
||
visited.add(`${i},${j}`); | ||
|
||
for (const [di, dj] of [[-1, 0], [1, 0], [0, -1], [0, 1]]) { | ||
const newI = i + di; | ||
const newJ = j + dj; | ||
|
||
if ( | ||
newI >= 0 && newI < heights.length && | ||
newJ >= 0 && newJ < heights[0].length && | ||
heights[newI][newJ] >= heights[i][j] | ||
) { | ||
dfs(newI, newJ, visited, heights); | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* λ λ°λ€ λͺ¨λ λλ¬ν μ μλ μ’νλ₯Ό μ°Ύλ ν¨μ | ||
* | ||
* @param heights 2μ°¨μ λ°°μ΄λ‘ μ΄λ£¨μ΄μ§ μ§νμ λμ΄ μ 보 | ||
* @returns λ λ°λ€ λͺ¨λ λλ¬ν μ μλ μ’ν λ°°μ΄ | ||
* | ||
* μκ° λ³΅μ‘λ: O(m Γ n) | ||
* - ννμ λ° λμμμμ κ°κ° DFS μν β O(m Γ n) | ||
* - κ²°κ³Όλ₯Ό μ°Ύλ μ΄μ€ 루ν β O(m Γ n) | ||
* | ||
* κ³΅κ° λ³΅μ‘λ: O(m Γ n) | ||
* - `pacificSet`κ³Ό `atlanticSet`μ μ΅λ O(m Γ n)κ°μ μ’ν μ μ₯ | ||
* | ||
*/ | ||
function pacificAtlantic(heights: number[][]): number[][] { | ||
if (!heights || heights.length === 0 || heights[0].length === 0) return []; | ||
|
||
const rows = heights.length; | ||
const cols = heights[0].length; | ||
|
||
const pacificSet = new Set<string>(); | ||
const atlanticSet = new Set<string>(); | ||
|
||
// ννμ(μΌμͺ½, μμͺ½)μμ μΆλ°νλ DFS | ||
for (let i = 0; i < rows; i++) dfs(i, 0, pacificSet, heights); | ||
for (let j = 0; j < cols; j++) dfs(0, j, pacificSet, heights); | ||
|
||
// λμμ(μ€λ₯Έμͺ½, μλμͺ½)μμ μΆλ°νλ DFS | ||
for (let i = 0; i < rows; i++) dfs(i, cols - 1, atlanticSet, heights); | ||
for (let j = 0; j < cols; j++) dfs(rows - 1, j, atlanticSet, heights); | ||
|
||
const result: number[][] = []; | ||
for (let i = 0; i < rows; i++) { | ||
for (let j = 0; j < cols; j++) { | ||
if (pacificSet.has(`${i},${j}`) && atlanticSet.has(`${i},${j}`)) { | ||
result.push([i, j]); | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
}; | ||
|