-
Notifications
You must be signed in to change notification settings - Fork 126
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
[khyo] Week 7 #951
Merged
Merged
[khyo] Week 7 #951
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]; | ||
}; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
수고하셨습니다! 이미 아주 효율적인 코드이지만, 총 m+n−2번 이동(총 m−1번 아래로 이동하고, n−1번 오른쪽으로 이동) 중에 m - 1 혹은 n - 1 중 적은 쪽을 선택하는 수학 조합 문제로 해석해서 풀면 시간 복잡도를 n으로 개선할 수 있다고 합니다! 저도 신기했던 풀이라 공유드려요. 편안한 연휴 보내세요!
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.
오 좋은 접근이네요! 공유 감사합니다.
좋은 연휴 보내세요~