-
-
Notifications
You must be signed in to change notification settings - Fork 245
[jdy8739] Week 7 #919
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
Merged
Merged
[jdy8739] Week 7 #919
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2a1025b
reverse-linked-list solution
jdy8739 419d34c
fix: 개행 추가
jdy8739 20470c3
longest-substring-without-repeating-characters solution
jdy8739 c75d8e9
set-matrix-zeroes solution
jdy8739 38f7eac
unique-paths solution
jdy8739 5cfe8c4
unique-paths solutions
jdy8739 d2f737e
number-of-islands solution
jdy8739 edb6386
longest-substring-without-repeating-characters 주석추가
jdy8739 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 hidden or 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,36 @@ | ||
/** | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
var lengthOfLongestSubstring = function(s) { | ||
let start = 0; | ||
let end = 0; | ||
|
||
const set = new Set(); | ||
|
||
let max = 0; | ||
|
||
while (end < s.length) { | ||
const char = s[end]; | ||
|
||
if (set.has(char)) { | ||
set.delete(s[start]); | ||
|
||
start++; | ||
} else { | ||
set.add(char); | ||
|
||
end++; | ||
} | ||
|
||
max = Math.max(max, set.size); | ||
} | ||
|
||
return max; | ||
}; | ||
|
||
// | ||
// | ||
|
||
|
||
|
This file contains hidden or 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,43 @@ | ||
/** | ||
* @param {character[][]} grid | ||
* @return {number} | ||
*/ | ||
var numIslands = function (grid) { | ||
const sink = (row, col) => { | ||
grid[row][col] = '0'; | ||
|
||
const neighbor = [ | ||
[row + 1, col], [row, col + 1], [row - 1, col], [row, col - 1] | ||
].filter(([y, x]) => { | ||
return y >= 0 && x >= 0 && y < grid.length && x < grid[0].length; | ||
}).filter(([y, x]) => { | ||
return grid[y][x] === '1'; | ||
}); | ||
|
||
neighbor.forEach(([y, x]) => { | ||
const el = grid[y][x]; | ||
|
||
if (el === '1') { | ||
sink(y, x); | ||
} | ||
}) | ||
} | ||
|
||
let count = 0; | ||
|
||
for (let i = 0; i < grid.length; i++) { | ||
for (let j = 0; j < grid[0].length; j++) { | ||
|
||
if (grid[i][j] === '1') { | ||
count++; | ||
sink(i, j); | ||
} | ||
} | ||
} | ||
|
||
return count; | ||
}; | ||
|
||
// 시간복잡도 O(2 * m * n) -> m * n 만큼 반복 + 재귀적으로 방문할 수 있는 셀의 수는 총 m * n 개 | ||
// 공간복잡도 O(1) -> 입력 배열을 사용하지 않고 변수만 사용 | ||
|
This file contains hidden or 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,45 @@ | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} head | ||
* @return {ListNode} | ||
*/ | ||
var reverseList = function(head) { | ||
if (head === null) { | ||
return null; | ||
} | ||
|
||
if (head.next === null) { | ||
return head; | ||
} | ||
|
||
const stack = []; | ||
|
||
let nextNode = head; | ||
|
||
while (nextNode) { | ||
stack.push(nextNode); | ||
|
||
nextNode = nextNode.next; | ||
} | ||
|
||
for (let i=stack.length - 1; i>=0; i--) { | ||
if (i === 0) { | ||
stack[i].next = null; | ||
} else { | ||
stack[i].next = stack[i - 1]; | ||
} | ||
} | ||
|
||
return stack[stack.length - 1]; | ||
}; | ||
|
||
// 시간복잡도 O(2n) | ||
|
||
|
This file contains hidden or 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,38 @@ | ||
/** | ||
* @param {number[][]} matrix | ||
* @return {void} Do not return anything, modify matrix in-place instead. | ||
*/ | ||
var setZeroes = function (matrix) { | ||
const coord = []; | ||
|
||
for (let i = 0; i < matrix.length; i++) { | ||
for (let j = 0; j < matrix[i].length; j++) { | ||
|
||
const num = matrix[i][j]; | ||
|
||
if (num === 0) { | ||
coord.push({ y: i, x: j }); | ||
} | ||
} | ||
} | ||
|
||
for (let k = 0; k < coord.length; k++) { | ||
const { y } = coord[k]; | ||
|
||
for (let j = 0; j < matrix[0].length; j++) { | ||
matrix[y][j] = 0; | ||
} | ||
} | ||
|
||
for (let l = 0; l < coord.length; l++) { | ||
const { x } = coord[l]; | ||
|
||
for (let j = 0; j < matrix.length; j++) { | ||
matrix[j][x] = 0; | ||
} | ||
} | ||
}; | ||
|
||
// 시간복잡도 O(n * m) | ||
// 공간복잡도 O(n * m) | ||
|
This file contains hidden or 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,72 @@ | ||
var uniquePaths = function (m, n) { | ||
const cache = new Map(); | ||
|
||
const dfs = (row, col) => { | ||
const cacheKey = `${row}-${col}`; | ||
|
||
if (cache.has(cacheKey)) { | ||
return cache.get(cacheKey); | ||
} | ||
|
||
if (row === m - 1 && col === n - 1) { | ||
return 1; | ||
} | ||
|
||
let count = 0; | ||
|
||
if (row < m - 1) { | ||
count += dfs(row + 1, col); | ||
} | ||
|
||
if (col < n - 1) { | ||
count += dfs(row, col + 1); | ||
} | ||
|
||
cache.set(cacheKey, count); | ||
|
||
return count; | ||
} | ||
|
||
return dfs(0, 0); | ||
}; | ||
|
||
// 시간복잡도 O(m * n) | ||
// 공간복잡도 O(m * n) - 1 (matrix[m][n]에 대한 캐시는 포함되지 않으므로) | ||
|
||
var uniquePaths = function(m, n) { | ||
const matrix = []; | ||
|
||
for (let i=0; i<m; i++) { | ||
const row = new Array(n).fill(1); | ||
matrix.push(row); | ||
} | ||
|
||
for (let j=1; j<matrix.length; j++) { | ||
for (let k=1; k<matrix[0].length; k++) { | ||
matrix[j][k] = matrix[j - 1][k] + matrix[j][k - 1]; | ||
} | ||
} | ||
|
||
return matrix[m - 1][n - 1]; | ||
}; | ||
|
||
// 시간복잡도 O(m * n) | ||
// 공간복잡도 O(m * n) | ||
|
||
var uniquePaths = function(m, n) { | ||
const row = new Array(n).fill(1); | ||
|
||
for (let i=1; i<m; i++) { | ||
let left = 1; | ||
|
||
for (let j=1; j<n; j++) { | ||
row[j] += left; | ||
left = row[j]; | ||
} | ||
} | ||
|
||
return row[n - 1]; | ||
}; | ||
|
||
// 시간복잡도 O(m * n) | ||
// 공간복잡도 (n) |
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.
다른 문제들 시간복잡도 분석도 잘 해주셨는데 누락된 거 같아요!
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.
앗 그러네요! 추가했습니다. 감사합니다 :)