Skip to content

Commit

Permalink
Merge pull request #490 from JEONGHWANMIN/main
Browse files Browse the repository at this point in the history
[ํ™˜๋ฏธ๋‹ˆ๋‹ˆ] Week7 ๋ฌธ์ œํ’€์ด
  • Loading branch information
SamTheKorean authored Sep 30, 2024
2 parents f09a6df + 5b1c79d commit be3b1fa
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
28 changes: 28 additions & 0 deletions longest-substring-without-repeating-characters/hwanmini.js
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"))

52 changes: 52 additions & 0 deletions number-of-islands/hwanmini.js
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"]]))
28 changes: 28 additions & 0 deletions reverse-linked-list/hwanmini.js
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
};
39 changes: 39 additions & 0 deletions set-matrix-zeroes/hwamini.js
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]]))

0 comments on commit be3b1fa

Please sign in to comment.