Skip to content

[jdy8739] Week 7 #919

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 8 commits into from
Jan 25, 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
33 changes: 33 additions & 0 deletions longest-substring-without-repeating-characters/jdy8739.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
let start = 0;
let end = 0;

const set = new Set();

let max = 0;

while (end < s.length) {
const char = s[end];

if (set.has(char)) {
set.delete(s[start]);

start++;
} else {
set.add(char);

end++;
}

max = Math.max(max, set.size);
}

return max;
};

// 시간복잡도 O(2n) -> 최대 2n번 반복
// 공간복잡도 O(n) -> 최대 s의 길이만큼 공간을 사용
43 changes: 43 additions & 0 deletions number-of-islands/jdy8739.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @param {character[][]} grid
* @return {number}
*/
var numIslands = function (grid) {
const sink = (row, col) => {
grid[row][col] = '0';

const neighbor = [
[row + 1, col], [row, col + 1], [row - 1, col], [row, col - 1]
].filter(([y, x]) => {
return y >= 0 && x >= 0 && y < grid.length && x < grid[0].length;
}).filter(([y, x]) => {
return grid[y][x] === '1';
});

neighbor.forEach(([y, x]) => {
const el = grid[y][x];

if (el === '1') {
sink(y, x);
}
})
}

let count = 0;

for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[0].length; j++) {

if (grid[i][j] === '1') {
count++;
sink(i, j);
}
}
}

return count;
};

// 시간복잡도 O(2 * m * n) -> m * n 만큼 반복 + 재귀적으로 방문할 수 있는 셀의 수는 총 m * n 개
// 공간복잡도 O(1) -> 입력 배열을 사용하지 않고 변수만 사용

45 changes: 45 additions & 0 deletions reverse-linked-list/jdy8739.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

/**
* 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;
}

if (head.next === null) {
return head;
}

const stack = [];

let nextNode = head;

while (nextNode) {
stack.push(nextNode);

nextNode = nextNode.next;
}

for (let i=stack.length - 1; i>=0; i--) {
if (i === 0) {
stack[i].next = null;
} else {
stack[i].next = stack[i - 1];
}
}

return stack[stack.length - 1];
};

// 시간복잡도 O(2n)


38 changes: 38 additions & 0 deletions set-matrix-zeroes/jdy8739.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var setZeroes = function (matrix) {
const coord = [];

for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {

const num = matrix[i][j];

if (num === 0) {
coord.push({ y: i, x: j });
}
}
}

for (let k = 0; k < coord.length; k++) {
const { y } = coord[k];

for (let j = 0; j < matrix[0].length; j++) {
matrix[y][j] = 0;
}
}

for (let l = 0; l < coord.length; l++) {
const { x } = coord[l];

for (let j = 0; j < matrix.length; j++) {
matrix[j][x] = 0;
}
}
};

// 시간복잡도 O(n * m)
// 공간복잡도 O(n * m)

72 changes: 72 additions & 0 deletions unique-paths/jdy8739.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var uniquePaths = function (m, n) {
const cache = new Map();

const dfs = (row, col) => {
const cacheKey = `${row}-${col}`;

if (cache.has(cacheKey)) {
return cache.get(cacheKey);
}

if (row === m - 1 && col === n - 1) {
return 1;
}

let count = 0;

if (row < m - 1) {
count += dfs(row + 1, col);
}

if (col < n - 1) {
count += dfs(row, col + 1);
}

cache.set(cacheKey, count);

return count;
}

return dfs(0, 0);
};

// 시간복잡도 O(m * n)
// 공간복잡도 O(m * n) - 1 (matrix[m][n]에 대한 캐시는 포함되지 않으므로)

var uniquePaths = function(m, n) {
const matrix = [];

for (let i=0; i<m; i++) {
const row = new Array(n).fill(1);
matrix.push(row);
}

for (let j=1; j<matrix.length; j++) {
for (let k=1; k<matrix[0].length; k++) {
matrix[j][k] = matrix[j - 1][k] + matrix[j][k - 1];
}
}

return matrix[m - 1][n - 1];
};

// 시간복잡도 O(m * n)
// 공간복잡도 O(m * n)

var uniquePaths = function(m, n) {
const row = new Array(n).fill(1);

for (let i=1; i<m; i++) {
let left = 1;

for (let j=1; j<n; j++) {
row[j] += left;
left = row[j];
}
}

return row[n - 1];
};

// 시간복잡도 O(m * n)
// 공간복잡도 (n)
Loading