-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #951 from HiGeuni/main
[khyo] Week 7
- Loading branch information
Showing
4 changed files
with
121 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,28 @@ | ||
/** | ||
* @param {string} s | ||
* @return {number} | ||
* | ||
* complexity | ||
* time: O(n) | ||
* space: O(n) | ||
*/ | ||
var lengthOfLongestSubstring = function(s) { | ||
let set = new Set(); | ||
let left = 0; | ||
let answer = 0; | ||
|
||
for(let i=0; i<s.length; ++i) { | ||
while(set.has(s[i])){ | ||
set.delete(s[left]) | ||
if(s[i] === s[left]) { | ||
left ++; | ||
break; | ||
} | ||
left ++; | ||
} | ||
set.add(s[i]) | ||
answer = Math.max(answer, i - left + 1) | ||
} | ||
return answer; | ||
}; | ||
|
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,44 @@ | ||
/** | ||
* @param {character[][]} grid | ||
* @return {number} | ||
* | ||
* 접근 | ||
* dfs로 접근 | ||
* | ||
* 1. 방문한 노드는 0으로 바꿔줘야 함 | ||
* 2. 방문한 노드는 방문여부를 잘 체크해야 함 | ||
* | ||
* complexity | ||
* time: O(m*n) | ||
* space: O(m*n) | ||
*/ | ||
var numIslands = function(grid) { | ||
let answer = 0; | ||
const numRows = grid.length; | ||
const numCols = grid[0].length; | ||
|
||
const dr = [-1, 1, 0, 0]; | ||
const dc = [0, 0, -1, 1]; | ||
|
||
const dfs = (row, col) => { | ||
if (row < 0 || col < 0 || row >= numRows || col >= numCols || grid[row][col] === '0') return; | ||
|
||
grid[row][col] = '0'; | ||
|
||
for (let i = 0; i < 4; ++i) { | ||
dfs(row + dr[i], col + dc[i]); | ||
} | ||
}; | ||
|
||
for (let r = 0; r < numRows; ++r) { | ||
for (let c = 0; c < numCols; ++c) { | ||
if (grid[r][c] === '1') { | ||
dfs(r, c); | ||
answer++; | ||
} | ||
} | ||
} | ||
|
||
return answer; | ||
}; | ||
|
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,25 @@ | ||
/** | ||
* @param {ListNode} head | ||
* @return {ListNode} | ||
* | ||
* 접근 | ||
* ListNode 타입에 대해서 공부하고, 링크드 리스트 개념을 접목해서 문제를 풀이 | ||
* | ||
* complexity | ||
* time: O(n) | ||
* space: O(1) | ||
*/ | ||
var reverseList = function(head) { | ||
let newList = null | ||
let curNode = null | ||
|
||
while(head){ | ||
curNode = head.next | ||
head.next = newList | ||
newList = head | ||
head = curNode | ||
} | ||
|
||
return newList | ||
}; | ||
|
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,24 @@ | ||
/** | ||
* @param {number} m | ||
* @param {number} n | ||
* @return {number} | ||
* | ||
* 접근 | ||
* dynamic programming으로 접근 | ||
* 초기 점화식 : dp[i][j] = dp[i-1][j] + dp[i][j-1] | ||
* 하지만 1차원만 사용해도 되기 때문에 1차원으로 접근 | ||
* | ||
* complexity | ||
* time: O(m*n) | ||
* space: O(n) | ||
*/ | ||
var uniquePaths = function (m, n) { | ||
const dp = Array(n).fill(1); | ||
for (let i = 1; i < m; ++i) { | ||
for (let j = 1; j < n; ++j) { | ||
dp[j] += dp[j - 1]; | ||
} | ||
} | ||
return dp[n - 1]; | ||
}; | ||
|