Skip to content

[moonhyeok] Week7 #949

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 4 commits 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
27 changes: 27 additions & 0 deletions longest-substring-without-repeating-characters/mike2ox.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* https://leetcode.com/problems/longest-substring-without-repeating-characters/
* 풀이방법: Sliding Window
*
* 공간 복잡도: O(1)
* 시간 복잡도: O(n)
*/

#include <vector>

class Solution {
public:
int lengthOfLongestSubstring(string s) {
int answer = 0;
vector<int> visit(256, -1);
int i, j;

for (i = 0, j = 0; i < s.size(); i++){
if (visit[s[i]] != -1 && visit[s[i]] >= j && visit[s[i]] < i) {
j = visit[s[i]] + 1;
}
answer = max(answer, i - j + 1);
visit[s[i]] = i;
}
return answer;
}
};
58 changes: 58 additions & 0 deletions number-of-islands/mike2ox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* https://leetcode.com/problems/number-of-islands/
* 풀이방법: BFS를 사용하여 섬의 개수를 구함
*
* 시간복잡도: O(n * m) (n: 그리드의 행, m: 그리드의 열)
* 공간복잡도: O(n * m) (그리드의 모든 요소를 방문)
*/

function numIslands(grid: string[][]): number {
if (!grid.length) return 0; // 그리드가 비어있는 경우

const rows = grid.length;
const cols = grid[0].length;
let islands = 0;

const bfs = (startRow: number, startCol: number) => {
const q: number[][] = [[startRow, startCol]];
grid[startRow][startCol] = "0";

const directions = [
[1, 0],
[-1, 0],
[0, 1],
[0, -1],
]; // 상하좌우

while (q.length) {
const [r, c] = q.shift()!;

for (const [dx, dy] of directions) {
const newR = r + dx;
const newC = c + dy;

if (
newR >= 0 &&
newR < rows &&
newC >= 0 &&
newC < cols &&
grid[newR][newC] === "1"
) {
q.push([newR, newC]);
grid[newR][newC] = "0";
}
}
}
};

for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] === "1") {
islands++;
bfs(r, c);
}
}
}

return islands;
}
41 changes: 41 additions & 0 deletions reverse-linked-list/mike2ox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* https://leetcode.com/problems/reverse-linked-list/
* 풀이방법: 스택을 사용하여 역순으로 노드를 생성
*
* 시간복잡도: O(n) (n: 리스트의 길이)
* 공간복잡도: O(n) (스택에 모든 노드를 저장)
*/

/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

function reverseList(head: ListNode | null): ListNode | null {
if (!head) return null;

const stack: number[] = [];
let result: ListNode | null = null;
let lastNode: ListNode;
while (head) {
stack.push(head.val);
head = head.next;
}
for (let i = stack.length - 1; i >= 0; i--) {
if (!result) {
result = new ListNode(stack[i]);
lastNode = result;
continue;
}
lastNode.next = new ListNode(stack[i]);
lastNode = lastNode.next;
}
return result;
}
Loading