-
-
Notifications
You must be signed in to change notification settings - Fork 246
[hyer0705] WEEK 07 solutions #1879
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
Changes from all commits
643ea3e
eb2596c
46ac718
fcababd
5b0bed6
780e017
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,19 @@ | ||
function lengthOfLongestSubstring(s: string): number { | ||
let maxLen = 0; | ||
|
||
const used = new Set<string>(); | ||
let windowStart = 0; | ||
|
||
for (let windowEnd = 0; windowEnd < s.length; windowEnd++) { | ||
const currentCh = s[windowEnd]; | ||
while (used.has(currentCh)) { | ||
used.delete(s[windowStart]); | ||
windowStart++; | ||
} | ||
|
||
used.add(currentCh); | ||
maxLen = Math.max(maxLen, windowEnd - windowStart + 1); | ||
} | ||
|
||
return maxLen; | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,52 @@ | ||||||||||||
function numIslands(grid: string[][]): number { | ||||||||||||
const m = grid.length; | ||||||||||||
const n = grid[0].length; | ||||||||||||
|
||||||||||||
const LAND = "1"; | ||||||||||||
const WATER = "0"; | ||||||||||||
|
||||||||||||
const visited: boolean[][] = Array.from({ length: m }, () => Array(n).fill(false)); | ||||||||||||
|
||||||||||||
const isValid = (row: number, col: number): boolean => row >= 0 && row < m && col >= 0 && col < n; | ||||||||||||
|
||||||||||||
const bfs = (row: number, col: number): void => { | ||||||||||||
const directions = [ | ||||||||||||
[0, 1], | ||||||||||||
[0, -1], | ||||||||||||
[1, 0], | ||||||||||||
[-1, 0], | ||||||||||||
]; | ||||||||||||
|
||||||||||||
// [row, col][] | ||||||||||||
const queue: number[][] = []; | ||||||||||||
|
||||||||||||
visited[row][col] = true; | ||||||||||||
queue.push([row, col]); | ||||||||||||
|
||||||||||||
while (queue.length > 0) { | ||||||||||||
const [cx, cy] = queue.shift()!; | ||||||||||||
Comment on lines
+26
to
+27
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.
Suggested change
그리고 혹시 const n = 100000;
let arr;
let start, end;
let x;
arr = Array(n).fill(1);
x = 0;
start = Date.now();
for (let i = 0; i < n; i++) {
x += arr.shift();
}
end = Date.now();
console.log('shift:', end - start, 'ms, x:', x);
arr = Array(n).fill(1);
y = 0;
start = Date.now();
let front = 0;
for (let i = 0; i < n; i++) {
y += arr[front++]
}
end = Date.now();
console.log('front:', end - start, 'ms, y:', y);
// shift: 3273 ms, x: 100000
// front: 2 ms, y: 100000 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. 긴 코멘트 감사합니다! 제 코드에서 개선할 점도 알게 되고 저도 고민해볼 거리가 생긴거 같네요! 질문 남기신 것 중에 제가 답변해드릴 수 있는 부분은 아래와 같이 정리를 해봤습니다. 평소에 그냥 Array.prototype.shift() 메서드를 쓰는거에 익숙해서 일단은 풀어서 정답이 나오면 바로 넘어가서 이런 코드가 남겨진거 같네요! 다음엔 말씀해주신 방법을 사용해서 문제를 풀이해보도록 하겠습니다.
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. 오.. 그랬군요. 반대로 Bracket으로 Property access 할 때는 |
||||||||||||
|
||||||||||||
for (const [dx, dy] of directions) { | ||||||||||||
const [nx, ny] = [cx + dx, cy + dy]; | ||||||||||||
|
||||||||||||
if (isValid(nx, ny) && !visited[nx][ny] && grid[nx][ny] === LAND) { | ||||||||||||
visited[nx][ny] = true; | ||||||||||||
queue.push([nx, ny]); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
}; | ||||||||||||
|
||||||||||||
let island = 0; | ||||||||||||
|
||||||||||||
for (let i = 0; i < m; i++) { | ||||||||||||
for (let j = 0; j < n; j++) { | ||||||||||||
if (grid[i][j] === LAND && !visited[i][j]) { | ||||||||||||
island++; | ||||||||||||
bfs(i, j); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
return island; | ||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* class ListNode { | ||
* val: number | ||
* next: ListNode | null | ||
* constructor(val?: number, next?: ListNode | null) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
* } | ||
*/ | ||
|
||
// using iterative | ||
function reverseList(head: ListNode | null): ListNode | null { | ||
if (!head) return null; | ||
|
||
let prev: ListNode | null = null; | ||
let current = head; | ||
|
||
while (current) { | ||
const temp: ListNode | null = current.next; | ||
current.next = prev; | ||
prev = current; | ||
current = temp; | ||
} | ||
|
||
return prev; | ||
} | ||
|
||
// using recursive | ||
function reverseList(head: ListNode | null): ListNode | null { | ||
if (!head || !head.next) return head; | ||
|
||
const newHead = reverseList(head.next); | ||
|
||
head.next.next = head; | ||
head.next = null; | ||
|
||
return newHead; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// mark first row, first col - 0ms | ||
/** | ||
Do not return anything, modify matrix in-place instead. | ||
*/ | ||
function setZeroes(matrix: number[][]): void { | ||
const m = matrix.length; | ||
const n = matrix[0].length; | ||
|
||
let isFirstColZero = false; | ||
let isFirstRowZero = false; | ||
for (let i = 0; i < m; i++) { | ||
for (let j = 0; j < n; j++) { | ||
if (matrix[i][j] === 0) { | ||
if (!isFirstRowZero && i === 0) isFirstRowZero = true; | ||
if (!isFirstColZero && j === 0) isFirstColZero = true; | ||
matrix[i][0] = 0; | ||
matrix[0][j] = 0; | ||
} | ||
} | ||
} | ||
|
||
for (let i = 1; i < m; i++) { | ||
for (let j = 1; j < n; j++) { | ||
if (matrix[i][0] === 0 || matrix[0][j] === 0) { | ||
matrix[i][j] = 0; | ||
} | ||
} | ||
} | ||
|
||
if (isFirstRowZero) { | ||
for (let j = 0; j < n; j++) { | ||
matrix[0][j] = 0; | ||
} | ||
} | ||
if (isFirstColZero) { | ||
for (let i = 0; i < m; i++) { | ||
matrix[i][0] = 0; | ||
} | ||
} | ||
} | ||
|
||
// using set - 4ms | ||
/** | ||
Do not return anything, modify matrix in-place instead. | ||
*/ | ||
function setZeroes(matrix: number[][]): void { | ||
const m = matrix.length; | ||
const n = matrix[0].length; | ||
|
||
// `${row},${col}` | ||
const coordinates = new Set<string>(); | ||
|
||
for (let i = 0; i < m; i++) { | ||
for (let j = 0; j < n; j++) { | ||
if (matrix[i][j] === 0) { | ||
coordinates.add(`${i},${j}`); | ||
} | ||
} | ||
} | ||
|
||
for (const coordinate of coordinates) { | ||
const [x, y] = coordinate.split(","); | ||
for (let j = 0; j < n; j++) { | ||
matrix[x][j] = 0; | ||
} | ||
for (let i = 0; i < m; i++) { | ||
matrix[i][y] = 0; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function uniquePaths(m: number, n: number): number { | ||
const dp: number[][] = Array.from({ length: m }, () => Array(n).fill(1)); | ||
|
||
for (let i = 1; i < m; i++) { | ||
for (let j = 1; j < n; j++) { | ||
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; | ||
} | ||
} | ||
|
||
return dp[m - 1][n - 1]; | ||
} |
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.
첨언할 게 없게 잘 짜셔서 이거라도 써봅니다.
maxLen
을0
대신-1
로 초기화하면 다음과 같이 덧셈 연산 반복을 줄일 수 있겠어요: