-
-
Notifications
You must be signed in to change notification settings - Fork 200
[uraflower] WEEK 07 solutions #1473
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4c1d721
[ PS ] : Reverse Linked List
uraflower 054c2d9
[ PS ] : Longest Substring Without Repeating Characters
uraflower a739c2e
[ PS ] : Number of Islands
uraflower f46290a
[ PS ] : Unique Paths
uraflower 23cfd49
[ PS ] : Set Matrix Zeroes
uraflower 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
28 changes: 28 additions & 0 deletions
28
longest-substring-without-repeating-characters/uraflower.js
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,28 @@ | ||
/** | ||
* 문자열에서 중복 문자 없는 가장 긴 부분 문자열의 길이를 반환하는 함수 | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
const lengthOfLongestSubstring = function(s) { | ||
let start = 0; | ||
let end = 0; | ||
|
||
const set = new Set(); | ||
let maxSize = 0; | ||
|
||
while (end < s.length) { | ||
while (set.has(s[end])) { | ||
set.delete(s[start]); | ||
start++; | ||
} | ||
|
||
set.add(s[end]); | ||
maxSize = Math.max(maxSize, set.size); | ||
end++; | ||
} | ||
|
||
return maxSize; | ||
}; | ||
|
||
// 시간복잡도: O(n) (최대 end로 n번, start로 n번 이동하므로 2n만큼 소요) | ||
// 공간복잡도: O(n) |
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,46 @@ | ||
/** | ||
* 주어진 2차원 격자에서 섬의 개수를 반환하는 함수 | ||
* 시간복잡도: O(m * n) (모든 원소를 순회해야 함) | ||
* 공간복잡도: O(m * n) (bfs의 공간복잡도에 따름) | ||
* @param {character[][]} grid | ||
* @return {number} | ||
*/ | ||
const numIslands = function (grid) { | ||
let num = 0; | ||
|
||
for (let r = 0; r < grid.length; r++) { | ||
for (let c = 0; c < grid[0].length; c++) { | ||
if (grid[r][c] === '1') { | ||
bfs(grid, r, c); | ||
num += 1; | ||
} | ||
} | ||
} | ||
|
||
return num; | ||
}; | ||
|
||
// grid의 주어진 좌표에서 bfs를 수행해 방문했음을 표시 | ||
// 시간복잡도: O(m * n) (최악의 경우 모든 원소 순회) | ||
// 공간복잡도: O(m * n) (최악의 경우 queue에 모든 원소 저장) | ||
function bfs(grid, x, y) { | ||
const queue = [[x, y]]; | ||
const dx = [0, 0, 1, -1]; | ||
const dy = [1, -1, 0, 0]; | ||
const rows = grid.length; | ||
const cols = grid[0].length; | ||
|
||
while (queue.length) { | ||
const [r, c] = queue.shift(); | ||
|
||
for (let i = 0; i < 4; i++) { | ||
const nr = r + dx[i]; | ||
const nc = c + dy[i]; | ||
|
||
if (0 <= nr && nr < rows && 0 <= nc && nc < cols && grid[nr][nc] === '1') { | ||
queue.push([nr, nc]); | ||
grid[nr][nc] = '0'; | ||
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. 원본 배열을 변경하는 방식으로 푸셨군요! |
||
} | ||
} | ||
} | ||
} |
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,48 @@ | ||
function ListNode(val, next) { | ||
this.val = (val === undefined ? 0 : val) | ||
this.next = (next === undefined ? null : next) | ||
} | ||
|
||
// 첫 번째 시도 | ||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(n) | ||
/** | ||
* 단방향 연결 리스트를 reverse하여 반환하는 함수 | ||
* @param {ListNode} head | ||
* @return {ListNode} | ||
*/ | ||
const reverseList = function (head) { | ||
const newHead = new ListNode(); | ||
|
||
function _reverseList(head) { | ||
if (!head) { | ||
return newHead; | ||
} | ||
|
||
const reversedHead = _reverseList(head.next); | ||
reversedHead.next = new ListNode(head.val, null); | ||
|
||
return reversedHead.next; | ||
} | ||
|
||
_reverseList(head); | ||
return newHead.next; | ||
}; | ||
|
||
|
||
// 두 번째 시도 | ||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(1) | ||
const reverseList = function (head) { | ||
let current = head; | ||
let prev = null; | ||
|
||
while (current) { | ||
const next = current.next; | ||
current.next = prev; | ||
prev = current; | ||
current = next; | ||
} | ||
|
||
return prev; | ||
}; |
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,30 @@ | ||
/** | ||
* 주어진 격자에서 원소가 0인 행과 열의 값을 0으로 수정하는 함수 | ||
* @param {number[][]} matrix | ||
* @return {void} Do not return anything, modify matrix in-place instead. | ||
*/ | ||
const setZeroes = function (matrix) { | ||
const m = matrix.length; | ||
const n = matrix[0].length; | ||
const rows = new Set(); | ||
const cols = new Set(); | ||
|
||
for (let r = 0; r < m; r++) { | ||
for (let c = 0; c < n; c++) { | ||
if (matrix[r][c] === 0) { | ||
rows.add(r); | ||
cols.add(c); | ||
} | ||
} | ||
} | ||
|
||
rows.forEach((row) => matrix[row] = Array(n).fill(0)); | ||
cols.forEach((col) => { | ||
for (let r = 0; r < m; r++) { | ||
matrix[r][col] = 0; | ||
} | ||
}); | ||
}; | ||
|
||
// 시간복잡도: O(m * n) | ||
// 공간복잡도: O(m + n) |
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 @@ | ||
/** | ||
* (0,0)에서 (m,n)에 도달할 수 있는 방법의 수를 반환하는 함수 | ||
* @param {number} m | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
const uniquePaths = function (m, n) { | ||
const grid = Array.from({length: m}, () => Array(n).fill(0)); | ||
let r = 0; | ||
let c = 0; | ||
const queue = [[r, c]]; | ||
grid[r][c] = 1; | ||
|
||
while (queue.length) { | ||
const [x, y] = queue.shift(); | ||
|
||
if (x === m - 1 && y === n - 1) { | ||
continue; | ||
} | ||
|
||
if (0 <= x + 1 && x + 1 < m) { | ||
if (grid[x+1][y] === 0) queue.push([x + 1, y]); | ||
grid[x+1][y] += grid[x][y]; | ||
} | ||
|
||
if (0 <= y + 1 && y + 1 < n) { | ||
if (grid[x][y+1] === 0) queue.push([x, y + 1]); | ||
grid[x][y+1] += grid[x][y]; | ||
} | ||
} | ||
|
||
return grid[m-1][n-1]; | ||
}; | ||
|
||
// 시간복잡도: O(m * n) | ||
// 공간복잡도: O(m * n) |
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.
Set을 사용해서 길이를 체크하는 로직으로 풀이하셨네요 👍
Map을 사용하면 while문을 if문으로도 바꿀 수 있을 거 같네요!