Skip to content

[hsskey] Week 07 Solutions #1468

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

Merged
merged 5 commits into from
May 16, 2025
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
20 changes: 20 additions & 0 deletions longest-substring-without-repeating-characters/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
const charSet = new Set();
let l = 0;
let res = 0;

for (let r = 0; r < s.length; r++) {
while (charSet.has(s[r])) {
charSet.delete(s[l]);
l += 1;
}
charSet.add(s[r]);
res = Math.max(res, r - l + 1);
}

return res;
};
56 changes: 56 additions & 0 deletions number-of-islands/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @param {character[][]} grid
* @return {number}
*/
var numIslands = function(grid) {
if (!grid.length) return 0;

const rows = grid.length;
const cols = grid[0].length;
const visit = new Set();
let islands = 0;

const bfs = (r, c) => {
const queue = [];
queue.push([r, c]);
visit.add(`${r},${c}`);

while (queue.length) {
const [row, col] = queue.shift();
const directions = [
[1, 0],
[-1, 0],
[0, 1],
[0, -1],
];

for (const [dr, dc] of directions) {
const newRow = row + dr;
const newCol = col + dc;

if (
newRow >= 0 &&
newRow < rows &&
newCol >= 0 &&
newCol < cols &&
grid[newRow][newCol] === '1' &&
!visit.has(`${newRow},${newCol}`)
) {
queue.push([newRow, newCol]);
visit.add(`${newRow},${newCol}`);
}
}
}
};

for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] === '1' && !visit.has(`${r},${c}`)) {
bfs(r, c);
islands += 1;
}
}
}

return islands;
};
25 changes: 25 additions & 0 deletions reverse-linked-list/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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) {
if (head === null) return null;

let newHead = head;
if (head.next !== null) {
newHead = reverseList(head.next);

head.next.next = head;
}

head.next = null;

return newHead;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

작성하신 코드를 보니 재귀적으로 문제를 깔끔하게 해결하신 것 같아요! 👍
저는 아직까지도 재귀함수로 접근하는 방식이 어렵게 느껴지는데, 저도 좀 더 노력해야 할 것 같다는 생각을 해봅니다.

42 changes: 42 additions & 0 deletions set-matrix-zeroes/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const setZeroes = (matrix) => {
const ROWS = matrix.length;
const COLS = matrix[0].length;
let rowZero = false;

// 1. 어떤 행과 열이 0이 되어야 하는지 기록
for (let r = 0; r < ROWS; r++) {
for (let c = 0; c < COLS; c++) {
if (matrix[r][c] === 0) {
matrix[0][c] = 0;
if (r > 0) {
matrix[r][0] = 0;
} else {
rowZero = true;
}
}
}
}

// 2. 첫 번째 행과 첫 번째 열을 제외한 나머지 처리
for (let r = 1; r < ROWS; r++) {
for (let c = 1; c < COLS; c++) {
if (matrix[0][c] === 0 || matrix[r][0] === 0) {
matrix[r][c] = 0;
}
}
}

// 3. 첫 번째 열 처리
if (matrix[0][0] === 0) {
for (let r = 0; r < ROWS; r++) {
matrix[r][0] = 0;
}
}

// 4. 첫 번째 행 처리
if (rowZero) {
for (let c = 0; c < COLS; c++) {
matrix[0][c] = 0;
}
}
};
20 changes: 20 additions & 0 deletions unique-paths/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {number} m
* @param {number} n
* @return {number}
*/
var uniquePaths = function(m, n) {
let row = new Array(n).fill(1);

for (let i = 0; i < m - 1; i++) {
const newRow = new Array(n).fill(1);

for (let j = n - 2; j >= 0; j--) {
newRow[j] = newRow[j + 1] + row[j];
}

row = newRow;
}

return row[0];
};