-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #490 from JEONGHWANMIN/main
[ํ๋ฏธ๋๋] Week7 ๋ฌธ์ ํ์ด
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
longest-substring-without-repeating-characters/hwanmini.js
This file contains 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 @@ | ||
// ์๊ฐ๋ณต์ก๋: 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")) | ||
|
This file contains 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,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' | ||
} | ||
|
||
|
||
/** | ||
* @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() | ||
|
||
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"]])) |
This file contains 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 @@ | ||
// ์๊ฐ๋ณต์ก๋: 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 | ||
}; |
This file contains 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,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]])) |