-
-
Notifications
You must be signed in to change notification settings - Fork 195
[byol-han] WEEK 07 solutions #1461
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b6c9cdc
reverse linked list solution
byol-han ab8be5f
reverse linked list solution with explanation
byol-han 09d04d5
longest-substring-without-repeating-characters
byol-han 2766d3d
number-of-islands solution
byol-han 8da3261
unique paths solution
byol-han d3a4622
set matrix zeroes solution
byol-han File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
longest-substring-without-repeating-characters/byol-han.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
var lengthOfLongestSubstring = function (s) { | ||
let longestS = []; | ||
let result = 0; | ||
for (let char of s) { | ||
if (!longestS.includes(char)) { | ||
longestS.push(char); | ||
} else { | ||
result = Math.max(result, longestS.length); | ||
longestS = longestS.slice(longestS.indexOf(char) + 1); | ||
longestS.push(char); | ||
} | ||
} | ||
return Math.max(result, longestS.length); | ||
}; | ||
|
||
/* | ||
Sliding Window | ||
Sliding Window는 문자열이나 배열에서 연속된 부분(subarray/substring)을 다룰 때 아주 유용한 알고리즘 기법 | ||
고정되거나 유동적인 “창(window)”을 좌우로 움직이며 문제를 해결하는 방식 | ||
|
||
슬라이딩 윈도우 핵심 아이디어 | ||
1. 두 포인터 사용: left, right | ||
2. 조건을 만족하는 윈도우 유지 | ||
3. 조건이 깨지면 left를 이동 | ||
4. 조건을 만족하면 결과 업데이트 | ||
*/ | ||
var lengthOfLongestSubstring = function (s) { | ||
let set = new Set(); | ||
let left = 0; | ||
let maxLen = 0; | ||
|
||
for (let right = 0; right < s.length; right++) { | ||
while (set.has(s[right])) { | ||
set.delete(s[left]); // 중복 문자 제거 | ||
left++; // 왼쪽 포인터 이동 | ||
} | ||
set.add(s[right]); // 현재 문자 추가 | ||
maxLen = Math.max(maxLen, right - left + 1); // 최대 길이 업데이트 | ||
} | ||
|
||
return maxLen; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* https://leetcode.com/problems/number-of-islands/ | ||
* @param {character[][]} grid | ||
* @return {number} | ||
*/ | ||
var numIslands = function (grid) { | ||
if (!grid || grid.length === 0) return 0; | ||
|
||
const rows = grid.length; | ||
const cols = grid[0].length; | ||
let count = 0; | ||
|
||
const dfs = (r, c) => { | ||
// 경계 밖이거나 물인 경우 리턴 | ||
if (r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] === "0") { | ||
return; | ||
} | ||
|
||
// 방문 표시 (육지를 물로 바꿈) | ||
grid[r][c] = "0"; | ||
|
||
// 상하좌우 탐색 | ||
dfs(r - 1, c); // 위 | ||
dfs(r + 1, c); // 아래 | ||
dfs(r, c - 1); // 왼쪽 | ||
dfs(r, c + 1); // 오른쪽 | ||
}; | ||
|
||
for (let r = 0; r < rows; r++) { | ||
for (let c = 0; c < cols; c++) { | ||
if (grid[r][c] === "1") { | ||
count++; | ||
dfs(r, c); // 해당 섬 전체 방문 처리 | ||
} | ||
} | ||
} | ||
|
||
return count; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* https://leetcode.com/problems/reverse-linked-list/ | ||
* @param {ListNode} head | ||
* @return {ListNode} | ||
*/ | ||
var reverseList = function (head) { | ||
let prev = null; | ||
let current = head; | ||
|
||
while (current) { | ||
const next = current.next; // 다음 노드 기억 | ||
current.next = prev; // 현재 노드가 이전 노드를 가리키도록 변경 | ||
prev = current; // prev를 현재 노드로 이동 | ||
current = next; // current를 다음 노드로 이동 | ||
} | ||
|
||
return prev; // prev는 새로운 head | ||
}; | ||
|
||
/* | ||
*** linked list *** | ||
리스트의 각 노드가 다음 노드를 가리키는 포인터를 가지고 있는 자료구조 | ||
|
||
리스트의 첫 번째 노드를 head라고 하고, head.next는 두 번째 노드, head.next.next는 세 번째 노드... | ||
이런식으로 노드들을 순차적으로 접근할 수 있는 자료구조를 '연결 리스트(linked list)'라고 함 | ||
|
||
reverseList(head)에서 head는 리스트 전체의 진입점. | ||
head 하나만 알고 있어도, .next를 따라가면서 전체 노드들을 순차적으로 접근할 수 있기 때문에 리스트 전체를 다룰 수 있음 | ||
|
||
*/ |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 공부 더 해야함 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* https://leetcode.com/problems/set-matrix-zeroes/ | ||
* @param {number[][]} matrix | ||
* @return {void} Do not return anything, modify matrix in-place instead. | ||
*/ | ||
var setZeroes = function (matrix) { | ||
const m = matrix.length; | ||
const n = matrix[0].length; | ||
|
||
let firstRowHasZero = false; | ||
let firstColHasZero = false; | ||
|
||
// 1. 첫 번째 열에 0이 있는지 확인 | ||
for (let i = 0; i < m; i++) { | ||
if (matrix[i][0] === 0) { | ||
firstColHasZero = true; | ||
break; | ||
} | ||
} | ||
|
||
// 2. 첫 번째 행에 0이 있는지 확인 | ||
for (let j = 0; j < n; j++) { | ||
if (matrix[0][j] === 0) { | ||
firstRowHasZero = true; | ||
break; | ||
} | ||
} | ||
|
||
// 3. 나머지 matrix에서 0을 찾고, 첫 행/열에 마킹 | ||
for (let i = 1; i < m; i++) { | ||
for (let j = 1; j < n; j++) { | ||
if (matrix[i][j] === 0) { | ||
matrix[i][0] = 0; // i번째 행에 0 마킹 | ||
matrix[0][j] = 0; // j번째 열에 0 마킹 | ||
} | ||
} | ||
} | ||
|
||
// 4. 마킹을 기준으로 행과 열을 0으로 변경 | ||
for (let i = 1; i < m; i++) { | ||
for (let j = 1; j < n; j++) { | ||
if (matrix[i][0] === 0 || matrix[0][j] === 0) { | ||
matrix[i][j] = 0; | ||
} | ||
} | ||
} | ||
|
||
// 5. 첫 번째 행을 0으로 설정 | ||
if (firstRowHasZero) { | ||
for (let j = 0; j < n; j++) { | ||
matrix[0][j] = 0; | ||
} | ||
} | ||
|
||
// 6. 첫 번째 열을 0으로 설정 | ||
if (firstColHasZero) { | ||
for (let i = 0; i < m; i++) { | ||
matrix[i][0] = 0; | ||
} | ||
} | ||
}; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 우선 문제풀이 제출 - 공부 더 해야함 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* https://leetcode.com/problems/unique-paths/ | ||
* @param {number} m | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var uniquePaths = function (m, n) { | ||
// 2차원 배열 생성 (m행 n열), 모든 값을 1로 초기화 | ||
const dp = Array.from({ length: m }, () => Array(n).fill(1)); | ||
|
||
// (1,1)부터 시작해서 경로 수 계산 | ||
for (let i = 1; i < m; i++) { | ||
for (let j = 1; j < n; j++) { | ||
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; | ||
} | ||
} | ||
|
||
// 오른쪽 아래 모서리의 값이 정답 | ||
return dp[m - 1][n - 1]; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 간당간당하게 브루트포스로 풀었는데, 슬라이딩 윈도우라는 방식으로 풀면 더 효율적으로 풀 수 있네요. 배워갑니다!