Skip to content
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

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

중요하지 않은 가독성 관련 의견입니다! 무시하셔도 좋습니다!

이 부분은 핵심 변수인 leftIdx가 변동되는 굉장히 중요한 부분인데, 길게 원라인으로 되어있어 다른 개발자가 읽기 다소 어렵지 않을까 하는 의견입니다!

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'
}

Copy link
Contributor

Choose a reason for hiding this comment

The 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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분을 볼때, 이름과 동작이 맞지 않는것같아요
queue를 의도하신것같은데, 실제 동작은 stack으로 하고있는 것 같습니다.
결과적으로 bfs라는 함수도 dfs로 동작하고있는것으로 보여요

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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"]]))
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]]))