Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
36 changes: 36 additions & 0 deletions longest-substring-without-repeating-characters/jdy8739.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @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;
};

//
Copy link
Contributor

Choose a reason for hiding this comment

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

다른 문제들 시간복잡도 분석도 잘 해주셨는데 누락된 거 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

앗 그러네요! 추가했습니다. 감사합니다 :)

//



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