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

[khyo] Week 7 #951

Merged
merged 1 commit 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
28 changes: 28 additions & 0 deletions longest-substring-without-repeating-characters/higeuni.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @param {string} s
* @return {number}
*
* complexity
* time: O(n)
* space: O(n)
*/
var lengthOfLongestSubstring = function(s) {
let set = new Set();
let left = 0;
let answer = 0;

for(let i=0; i<s.length; ++i) {
while(set.has(s[i])){
set.delete(s[left])
if(s[i] === s[left]) {
left ++;
break;
}
left ++;
}
set.add(s[i])
answer = Math.max(answer, i - left + 1)
}
return answer;
};

44 changes: 44 additions & 0 deletions number-of-islands/higeuni.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @param {character[][]} grid
* @return {number}
*
* 접근
* dfs로 접근
*
* 1. 방문한 노드는 0으로 바꿔줘야 함
* 2. 방문한 노드는 방문여부를 잘 체크해야 함
*
* complexity
* time: O(m*n)
* space: O(m*n)
*/
var numIslands = function(grid) {
let answer = 0;
const numRows = grid.length;
const numCols = grid[0].length;

const dr = [-1, 1, 0, 0];
const dc = [0, 0, -1, 1];

const dfs = (row, col) => {
if (row < 0 || col < 0 || row >= numRows || col >= numCols || grid[row][col] === '0') return;

grid[row][col] = '0';

for (let i = 0; i < 4; ++i) {
dfs(row + dr[i], col + dc[i]);
}
};

for (let r = 0; r < numRows; ++r) {
for (let c = 0; c < numCols; ++c) {
if (grid[r][c] === '1') {
dfs(r, c);
answer++;
}
}
}

return answer;
};

25 changes: 25 additions & 0 deletions reverse-linked-list/higeuni.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @param {ListNode} head
* @return {ListNode}
*
* 접근
* ListNode 타입에 대해서 공부하고, 링크드 리스트 개념을 접목해서 문제를 풀이
*
* complexity
* time: O(n)
* space: O(1)
*/
var reverseList = function(head) {
let newList = null
let curNode = null

while(head){
curNode = head.next
head.next = newList
newList = head
head = curNode
}

return newList
};

24 changes: 24 additions & 0 deletions unique-paths/higeuni.js
Copy link
Contributor

Choose a reason for hiding this comment

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

수고하셨습니다! 이미 아주 효율적인 코드이지만, 총 m+n−2번 이동(총 m−1번 아래로 이동하고, n−1번 오른쪽으로 이동) 중에 m - 1 혹은 n - 1 중 적은 쪽을 선택하는 수학 조합 문제로 해석해서 풀면 시간 복잡도를 n으로 개선할 수 있다고 합니다! 저도 신기했던 풀이라 공유드려요. 편안한 연휴 보내세요!

Copy link
Contributor Author

@HiGeuni HiGeuni Jan 25, 2025

Choose a reason for hiding this comment

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

오 좋은 접근이네요! 공유 감사합니다.

좋은 연휴 보내세요~

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @param {number} m
* @param {number} n
* @return {number}
*
* 접근
* dynamic programming으로 접근
* 초기 점화식 : dp[i][j] = dp[i-1][j] + dp[i][j-1]
* 하지만 1차원만 사용해도 되기 때문에 1차원으로 접근
*
* complexity
* time: O(m*n)
* space: O(n)
*/
var uniquePaths = function (m, n) {
const dp = Array(n).fill(1);
for (let i = 1; i < m; ++i) {
for (let j = 1; j < n; ++j) {
dp[j] += dp[j - 1];
}
}
return dp[n - 1];
};

Loading