Skip to content

Commit be3b1fa

Browse files
authored
Merge pull request #490 from JEONGHWANMIN/main
[ํ™˜๋ฏธ๋‹ˆ๋‹ˆ] Week7 ๋ฌธ์ œํ’€์ด
2 parents f09a6df + 5b1c79d commit be3b1fa

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
3+
4+
/**
5+
* @param {string} s
6+
* @return {number}
7+
*/
8+
var lengthOfLongestSubstring = function(s) {
9+
let maxCount = 0;
10+
const map = new Map()
11+
12+
let leftIdx = 0;
13+
for (let rightIdx = 0 ; rightIdx < s.length; rightIdx++) {
14+
const char = s[rightIdx]
15+
if (map.has(char) && map.get(char) >= leftIdx) leftIdx = map.get(char) + 1;
16+
map.set(char, rightIdx)
17+
maxCount = Math.max(maxCount, rightIdx - leftIdx + 1)
18+
}
19+
20+
return maxCount
21+
};
22+
23+
24+
console.log(lengthOfLongestSubstring("abcabcbb"))
25+
console.log(lengthOfLongestSubstring("bbbbb"))
26+
console.log(lengthOfLongestSubstring("pwwkew"))
27+
console.log(lengthOfLongestSubstring("abba"))
28+

โ€Žnumber-of-islands/hwanmini.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n * m)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n * m)
3+
4+
const dr = [1,-1,0,0]
5+
const dc = [0,0,-1, 1]
6+
7+
const isValidMove = (grid, n_row, n_col) => {
8+
return n_row >= 0 && n_row < grid.length && n_col >= 0 && n_col < grid[0].length && grid[n_row][n_col] !== '0'
9+
}
10+
11+
12+
/**
13+
* @param {character[][]} grid
14+
* @return {number}
15+
*/
16+
var numIslands = function(grid) {
17+
let islandCount = 0;
18+
19+
const bfs = (row, col) => {
20+
const que = [[row,col]]
21+
22+
while (que.length) {
23+
const [row, col] = que.pop()
24+
25+
for (let i = 0 ; i < dr.length; i++) {
26+
const n_row = row + dr[i]
27+
const n_col = col + dc[i]
28+
29+
if (isValidMove(grid, n_row, n_col)) {
30+
que.push([n_row, n_col])
31+
}
32+
}
33+
34+
grid[row][col] = '0'
35+
36+
}
37+
38+
islandCount += 1
39+
}
40+
41+
42+
for (let row = 0; row < grid.length; row++) {
43+
for (let col = 0; col < grid[row].length; col++) {
44+
if (grid[row][col] !== '0') bfs(row, col)
45+
}
46+
}
47+
48+
49+
return islandCount
50+
};
51+
52+
console.log(numIslands([["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]))

โ€Žreverse-linked-list/hwanmini.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
3+
4+
/**
5+
* Definition for singly-linked list.
6+
* function ListNode(val, next) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
*/
11+
/**
12+
* @param {ListNode} head
13+
* @return {ListNode}
14+
*/
15+
var reverseList = function(head) {
16+
let curNode = head
17+
let preNode = null
18+
19+
while (curNode) {
20+
const nextNode = curNode.next
21+
curNode.next = preNode
22+
preNode = curNode
23+
curNode = nextNode
24+
}
25+
26+
27+
return preNode
28+
};

โ€Žset-matrix-zeroes/hwamini.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O((n * m) * (n + m))
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n * m)
3+
4+
/**
5+
* @param {number[][]} matrix
6+
* @return {void} Do not return anything, modify matrix in-place instead.
7+
*/
8+
var setZeroes = function(matrix) {
9+
const zeroIndexes = []
10+
11+
for (let row = 0; row < matrix.length; row++) {
12+
for (let col = 0; col < matrix[row].length; col++) {
13+
if (matrix[row][col] === 0) {
14+
zeroIndexes.push([row,col])
15+
}
16+
}
17+
}
18+
19+
while (zeroIndexes.length) {
20+
const [row, col] = zeroIndexes.pop()
21+
22+
for (let i = 0; i < matrix[0].length; i++) {
23+
matrix[row][i] = 0
24+
}
25+
26+
for (let j = 0; j < matrix.length; j++) {
27+
matrix[j][col] = 0
28+
}
29+
30+
}
31+
32+
return matrix
33+
};
34+
35+
console.log(setZeroes([
36+
[1,1,1],
37+
[1,0,1],
38+
[1,1,1]]))
39+
console.log(setZeroes([[0,1,2,0],[3,4,5,2],[1,3,1,5]]))

0 commit comments

Comments
ย (0)