-
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
[환미니니] Week7 문제풀이 #490
[환미니니] Week7 문제풀이 #490
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(n) | ||
|
||
/** | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
var lengthOfLongestSubstring = function(s) { | ||
let maxCount = 0; | ||
const map = new Map() | ||
|
||
let leftIdx = 0; | ||
for (let rightIdx = 0 ; rightIdx < s.length; rightIdx++) { | ||
const char = s[rightIdx] | ||
if (map.has(char) && map.get(char) >= leftIdx) leftIdx = map.get(char) + 1; | ||
map.set(char, rightIdx) | ||
maxCount = Math.max(maxCount, rightIdx - leftIdx + 1) | ||
} | ||
|
||
return maxCount | ||
}; | ||
|
||
|
||
console.log(lengthOfLongestSubstring("abcabcbb")) | ||
console.log(lengthOfLongestSubstring("bbbbb")) | ||
console.log(lengthOfLongestSubstring("pwwkew")) | ||
console.log(lengthOfLongestSubstring("abba")) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// 시간복잡도: O(n * m) | ||
// 공간복잡도: O(n * m) | ||
|
||
const dr = [1,-1,0,0] | ||
const dc = [0,0,-1, 1] | ||
|
||
const isValidMove = (grid, n_row, n_col) => { | ||
return n_row >= 0 && n_row < grid.length && n_col >= 0 && n_col < grid[0].length && grid[n_row][n_col] !== '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. 이렇게 함수를 따로 빼셔서 코드가 엄청 깔끔해진 것 같네요! 저도 다음에 활용해보고 싶네요! |
||
|
||
/** | ||
* @param {character[][]} grid | ||
* @return {number} | ||
*/ | ||
var numIslands = function(grid) { | ||
let islandCount = 0; | ||
|
||
const bfs = (row, col) => { | ||
const que = [[row,col]] | ||
|
||
while (que.length) { | ||
const [row, col] = que.pop() | ||
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. 이 부분을 볼때, 이름과 동작이 맞지 않는것같아요 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. shift로 했을 때 시간이 초과되어서 pop을 사용해서 풀었는데 |
||
|
||
for (let i = 0 ; i < dr.length; i++) { | ||
const n_row = row + dr[i] | ||
const n_col = col + dc[i] | ||
|
||
if (isValidMove(grid, n_row, n_col)) { | ||
que.push([n_row, n_col]) | ||
} | ||
} | ||
|
||
grid[row][col] = '0' | ||
|
||
} | ||
|
||
islandCount += 1 | ||
} | ||
|
||
|
||
for (let row = 0; row < grid.length; row++) { | ||
for (let col = 0; col < grid[row].length; col++) { | ||
if (grid[row][col] !== '0') bfs(row, col) | ||
} | ||
} | ||
|
||
|
||
return islandCount | ||
}; | ||
|
||
console.log(numIslands([["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]])) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(1) | ||
|
||
/** | ||
* 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) { | ||
let curNode = head | ||
let preNode = null | ||
|
||
while (curNode) { | ||
const nextNode = curNode.next | ||
curNode.next = preNode | ||
preNode = curNode | ||
curNode = nextNode | ||
} | ||
|
||
|
||
return preNode | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// 시간복잡도: O((n * m) * (n + m)) | ||
// 공간복잡도: O(n * m) | ||
|
||
/** | ||
* @param {number[][]} matrix | ||
* @return {void} Do not return anything, modify matrix in-place instead. | ||
*/ | ||
var setZeroes = function(matrix) { | ||
const zeroIndexes = [] | ||
|
||
for (let row = 0; row < matrix.length; row++) { | ||
for (let col = 0; col < matrix[row].length; col++) { | ||
if (matrix[row][col] === 0) { | ||
zeroIndexes.push([row,col]) | ||
} | ||
} | ||
} | ||
|
||
while (zeroIndexes.length) { | ||
const [row, col] = zeroIndexes.pop() | ||
|
||
for (let i = 0; i < matrix[0].length; i++) { | ||
matrix[row][i] = 0 | ||
} | ||
|
||
for (let j = 0; j < matrix.length; j++) { | ||
matrix[j][col] = 0 | ||
} | ||
|
||
} | ||
|
||
return matrix | ||
}; | ||
|
||
console.log(setZeroes([ | ||
[1,1,1], | ||
[1,0,1], | ||
[1,1,1]])) | ||
console.log(setZeroes([[0,1,2,0],[3,4,5,2],[1,3,1,5]])) |
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.
중요하지 않은 가독성 관련 의견입니다! 무시하셔도 좋습니다!
이 부분은 핵심 변수인
leftIdx
가 변동되는 굉장히 중요한 부분인데, 길게 원라인으로 되어있어 다른 개발자가 읽기 다소 어렵지 않을까 하는 의견입니다!