Skip to content

[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
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions longest-substring-without-repeating-characters/byol-han.js
Copy link
Contributor

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,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;
};
39 changes: 39 additions & 0 deletions number-of-islands/byol-han.js
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;
};
37 changes: 37 additions & 0 deletions reverse-linked-list/byol-han.js
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를 따라가면서 전체 노드들을 순차적으로 접근할 수 있기 때문에 리스트 전체를 다룰 수 있음

*/