-
-
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
Open
byol-han
wants to merge
4
commits into
DaleStudy:main
Choose a base branch
from
byol-han:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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를 따라가면서 전체 노드들을 순차적으로 접근할 수 있기 때문에 리스트 전체를 다룰 수 있음 | ||
|
||
*/ |
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.
저는 간당간당하게 브루트포스로 풀었는데, 슬라이딩 윈도우라는 방식으로 풀면 더 효율적으로 풀 수 있네요. 배워갑니다!