-
-
Notifications
You must be signed in to change notification settings - Fork 338
파일이름 user name과 동일하게 제출하지 않은 부분 수정 #2556
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,14 @@ | ||
| /** | ||
| * 문제 유형 | ||
| * - DP (피보나치) | ||
| * | ||
| * 문제 설명 | ||
| * - 계단을 올라가는 방법의 수를 구하기 | ||
| * | ||
| * 아이디어 | ||
| * 1) 피보나치 수열 활용 | ||
| * - climbStairs(n) = climbStairs(n-1) + climbStairs(n-2) | ||
| */ | ||
| function climbStairsBottomUp(n: number): number { | ||
| function fibonacci(n: number, memo = new Map<number, number>()) { | ||
| if (n === 1) return 1; | ||
| if (n === 2) return 2; | ||
| function climbStairs(n: number): number { | ||
| const memo = new Map<number, number>(); | ||
|
|
||
| function dp (n: number): number { | ||
| if(n <= 1) return 1; | ||
| if(memo.has(n)) return memo.get(n)!; | ||
|
|
||
| if (memo.has(n)) return memo.get(n); | ||
| const result = fibonacci(n - 1, memo) + fibonacci(n - 2, memo); | ||
| memo.set(n, result); | ||
| return result; | ||
| const result = dp(n-1) + dp(n-2); | ||
| memo.set(n, result); | ||
| return result; | ||
| } | ||
| return fibonacci(n); | ||
| } | ||
|
|
||
| function climbStairsTopDown(n: number): number { | ||
| const dp = new Array(n + 1).fill(0); | ||
| dp[1] = 1; | ||
| dp[2] = 2; | ||
|
|
||
| for (let i = 3; i <= n; i++) { | ||
| dp[i] = dp[i - 1] + dp[i - 2]; | ||
| } | ||
| return dp[n]; | ||
| } | ||
| return dp(n); | ||
| }; |
This file was deleted.
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| function containsDuplicate(nums: number[]): boolean { | ||
| const map = new Map(); | ||
| for (let i = 0; i < nums.length; i++) { | ||
| if (map.get(nums[i])) { | ||
| return true; | ||
| } else map.set(nums[i], 1); | ||
| const checkMap = new Map(); | ||
| for(const n of nums) { | ||
| if(checkMap.has(n)) { | ||
| return true; | ||
| } else { | ||
| checkMap.set(n, 1); | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| }; |
This file was deleted.
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,10 @@ | ||
| /** | ||
| * 문제 설명 | ||
| * - 주어진 배열의 시작과 끝이 연결되어 있는 원형이라고 할 경우에, 이웃하는 집은 털 수 없고 최대로 털 수 있는 돈을 구하는 문제 | ||
| * | ||
| * 아이디어 | ||
| * 1) 동적 계획법(Dynamic Programming) 활용 | ||
| * - 원형 구조이므로 첫번째 집과 마지막집은 동시에 털 수 없음 | ||
| * - 따라서 두 가지 경우로 나누어 계산하고, 두 결과 중 더 큰 값을 반환 | ||
| * 2) 선형 배열에 대한 최대 금액 계산 | ||
| * - 매 순회마다 현재 집을 털 경우(prev2 + num)와 털지 않을 경우(prev1) 중 큰 값을 선택 | ||
| * - 최종적으로 prev1에 최대값이 저장 | ||
| */ | ||
| function robLinear(nums: number[]) { | ||
| let prev1 = 0; | ||
| let prev2 = 0; | ||
|
|
||
| for (const num of nums) { | ||
| const temp = prev1; | ||
| prev1 = Math.max(prev2 + num, prev1); | ||
| prev2 = temp; | ||
| } | ||
|
|
||
| return prev1; | ||
| } | ||
|
|
||
| function rob(nums: number[]): number { | ||
| if (nums.length === 1) return nums[0]; | ||
| if (nums.length === 2) return Math.max(nums[0], nums[1]); | ||
|
|
||
| const temp1 = robLinear(nums.slice(0, nums.length - 1)); | ||
| const temp2 = robLinear(nums.slice(1)); | ||
|
|
||
| return Math.max(temp1, temp2); | ||
| } | ||
| const dp = new Array(nums.length); | ||
| dp[0] = nums[0]; | ||
| dp[1] = Math.max(nums[0], nums[1]); | ||
|
|
||
| for(let i = 2; i < nums.length; i++) { | ||
| dp[i] = Math.max(dp[i-1], dp[i-2] + nums[i]); | ||
| } | ||
| return dp[nums.length - 1]; | ||
| }; |
This file was deleted.
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| function maxSubArray(nums: number[]): number { | ||
| let currentSum = nums[0]; | ||
| let maxSum = nums[0]; | ||
| for (let i = 1; i < nums.length; i++) { | ||
| currentSum = Math.max(nums[i], currentSum + nums[i]); | ||
| maxSum = Math.max(maxSum, currentSum); | ||
| let max = nums[0]; | ||
| const dp = new Array(nums.length).fill(0); | ||
| dp[0] = Math.max(0, nums[0]); | ||
|
|
||
| for(let i = 1; i < nums.length; i++) { | ||
| dp[i] = Math.max(dp[i-1] + nums[i], nums[i]); | ||
| max = Math.max(dp[i], max); | ||
| } | ||
| return maxSum; | ||
| } | ||
| return max; | ||
| }; |
This file was deleted.
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,91 +1,9 @@ | ||
| /** | ||
| * 문제 설명 | ||
| * - 2차원 그리드에서 섬의 갯수를 구하는 문제 | ||
| * | ||
| * 조건 | ||
| * - 가로, 세로가 1로 인접해있는 경우 같은 섬으로 간주 | ||
| * | ||
| * 아이디어 | ||
| * - 경로 탐색, 네트워크, 조합 유형이 나오면 '그래프 탐색 알고리즘'을 떠울린다. | ||
| * 1) DFS (재귀) | ||
| * 2) BFS (링크드리스트 or 큐) | ||
| * | ||
| */ | ||
|
|
||
| function numIslands(grid: string[][]): number { | ||
| const rows = grid.length; | ||
| const cols = grid[0].length; | ||
| function hammingWeight(n: number): number { | ||
| let count = 0; | ||
| function dfs(r: number, c: number) { | ||
| if (r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] === "0") return; | ||
|
|
||
| grid[r][c] = "0"; | ||
| dfs(r - 1, c); | ||
| dfs(r + 1, c); | ||
| dfs(r, c - 1); | ||
| dfs(r, c + 1); | ||
| } | ||
|
|
||
| for (let r = 0; r < rows; r++) { | ||
| for (let c = 0; c < cols; c++) { | ||
| if (grid[r][c] === "1") { | ||
| count++; | ||
| dfs(r, c); | ||
| } | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * BFS version | ||
|
|
||
| function numIslands(grid: string[][]): number { | ||
| const rows = grid.length; | ||
| const cols = grid[0].length; | ||
| let count = 0; | ||
|
|
||
| const directions = [ | ||
| [0, 1], | ||
| [1, 0], | ||
| [0, -1], | ||
| [-1, 0], | ||
| ]; | ||
| function bfs(r: number, c: number) { | ||
| const queue = [[r, c]]; | ||
| grid[r][c] = "0"; | ||
|
|
||
| while (queue.length) { | ||
| const [row, col] = queue.shift()!; | ||
|
|
||
| for (const [dr, dc] of directions) { | ||
| const newRow = row + dr; | ||
| const newCol = col + dc; | ||
|
|
||
| if ( | ||
| newRow >= 0 && | ||
| newRow < rows && | ||
| newCol >= 0 && | ||
| newCol < cols && | ||
| grid[newRow][newCol] === "1" | ||
| ) { | ||
| queue.push([newRow, newCol]); | ||
| grid[newRow][newCol] = "0"; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (let r = 0; r < rows; r++) { | ||
| for (let c = 0; c < cols; c++) { | ||
| if (grid[r][c] === "1") { | ||
| count++; | ||
| bfs(r, c); | ||
| for(let i = 31; i >= 0; i--) { | ||
| if((n >> i) & 1) { | ||
| count++; | ||
| } | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| */ | ||
| }; |
This file was deleted.
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,20 @@ | ||
| function productExceptSelf(nums: number[]): number[] { | ||
| const left = Array(nums.length).fill(1); | ||
| const right = Array(nums.length).fill(1); | ||
| const result = Array(nums.length); | ||
| const n = nums.length; | ||
| const left = new Array(n).fill(1); | ||
| const right = new Array(n).fill(1); | ||
| const result = new Array(n).fill(1); | ||
|
|
||
| for (let i = 1; i < nums.length; i++) { | ||
| left[i] = left[i - 1] * nums[i - 1]; | ||
| for (let i = 1; i < n; i++) { | ||
| left[i] = nums[i-1] * left[i - 1]; | ||
| } | ||
|
|
||
| for (let i = nums.length - 2; i >= 0; i--) { | ||
| right[i] = right[i + 1] * nums[i + 1]; | ||
| for (let i = n - 2; i >= 0; i--) { | ||
| right[i] = nums[i+1] * right[i + 1]; | ||
| } | ||
|
|
||
| for (let i = 0; i < nums.length; i++) { | ||
| result[i] = left[i] * right[i]; | ||
| for (let i = 0; i < n; i++) { | ||
| result[i] = left[i] * right[i]; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| }; |
This file was deleted.
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,29 @@ | ||
| // 1. Brute force | ||
| function twoSum(nums: number[], target: number): number[] { | ||
| for (let i = 0; i < nums.length - 1; i++) { | ||
| for (let j = i + 1; j < nums.length; j++) { | ||
| if (nums[i] + nums[j] === target) { | ||
| return [i, j]; | ||
| const map = new Map<number, number[]>(); | ||
| for(let i = 0; i < nums.length; i++) { | ||
| const current = map.get(nums[i]); | ||
| if(current) { | ||
| current.push(i); | ||
| } else { | ||
| map.set(nums[i], [i]); | ||
| } | ||
| } | ||
| } | ||
| return []; | ||
| } | ||
|
|
||
| // 2. Hashmap | ||
| function twoSum(nums: number[], target: number): number[] { | ||
| const map = new Map(); | ||
| for (let i = 0; i < nums.length; i++) { | ||
| map.set(nums[i], i); | ||
| } | ||
| nums.sort((a, b) => a - b); | ||
| let left = 0; | ||
| let right = nums.length - 1; | ||
|
|
||
| for (let i = 0; i < nums.length; i++) { | ||
| const targetIndex = map.get(target - nums[i]); | ||
| if (targetIndex && targetIndex !== i) { | ||
| return [i, targetIndex]; | ||
| } | ||
| while(left < right) { | ||
| if(nums[left] + nums[right] === target) { | ||
| return [map.get(nums[left])?.pop() ?? 0, map.get(nums[right])?.pop() ?? 0]; | ||
| } | ||
|
|
||
| if(nums[left] + nums[right] > target) { | ||
| right--; | ||
| } else { | ||
| left++; | ||
| } | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석