Skip to content

Commit 027de11

Browse files
authored
Merge pull request #497 from YeonguChoe/main
[영우] Week7 Solutions
2 parents d94393f + 78b9cff commit 027de11

File tree

5 files changed

+187
-0
lines changed

5 files changed

+187
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int lengthOfLongestSubstring(string s) {
4+
5+
int current_max = 0;
6+
int pivot = 0;
7+
int checker = 0;
8+
9+
int current_length = 0;
10+
while (pivot < s.size()) {
11+
set<char> included;
12+
included.insert(s[pivot]);
13+
checker = pivot + 1;
14+
while (checker < s.size() and
15+
included.find(s[checker]) == included.end()) {
16+
included.insert(s[checker]);
17+
checker += 1;
18+
}
19+
current_length = checker - pivot;
20+
current_max = max(current_max, current_length);
21+
pivot += 1;
22+
}
23+
return current_max;
24+
}
25+
// 시간 복잡도: O(n)
26+
// 공간 복잡도: O(n)
27+
};

number-of-islands/yeonguchoe.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
class Solution {
2+
public:
3+
int numIslands(vector<vector<char>>& grid) {
4+
int count = 0;
5+
6+
int row_size = grid.size();
7+
int column_size = grid[0].size();
8+
9+
queue<pair<int, int>> q;
10+
11+
for (int i = 0; i < row_size; i++) {
12+
for (int j = 0; j < column_size; j++) {
13+
if (grid[i][j] == '1') {
14+
LookAround(i, j, grid, q, row_size, column_size);
15+
count += 1;
16+
}
17+
}
18+
}
19+
return count;
20+
}
21+
22+
// BFS
23+
void LookAround(int& x, int& y, vector<vector<char>>& grid,
24+
queue<pair<int, int>>& q, int& row_size, int& column_size) {
25+
pair<int, int> center_location{x, y};
26+
q.push(center_location);
27+
28+
grid[x][y] = '0';
29+
30+
while (!q.empty()) {
31+
pair<int, int> selected_location = q.front();
32+
q.pop();
33+
34+
int selected_x = selected_location.first;
35+
int selected_y = selected_location.second;
36+
37+
// 선택된 cell에서 왼쪽
38+
if (selected_x - 1 >= 0 &&
39+
grid[selected_x - 1][selected_y] == '1') {
40+
q.push({selected_x - 1, selected_y});
41+
grid[selected_x - 1][selected_y] = '0';
42+
}
43+
44+
// 선택된 cell에서 오른쪽
45+
if (selected_x + 1 < row_size &&
46+
grid[selected_x + 1][selected_y] == '1') {
47+
q.push({selected_x + 1, selected_y});
48+
grid[selected_x + 1][selected_y] = '0';
49+
}
50+
51+
// 선택된 cell에서 위쪽
52+
if (selected_y - 1 >= 0 &&
53+
grid[selected_x][selected_y - 1] == '1') {
54+
q.push({selected_x, selected_y - 1});
55+
grid[selected_x][selected_y - 1] = '0';
56+
}
57+
58+
// 선택된 cell에서 아래쪽
59+
if (selected_y + 1 < column_size &&
60+
grid[selected_x][selected_y + 1] == '1') {
61+
q.push({selected_x, selected_y + 1});
62+
grid[selected_x][selected_y + 1] = '0';
63+
}
64+
}
65+
}
66+
// 시간 복잡도: O(row*column)
67+
// 공간 복잡도: O(row*column)
68+
};

reverse-linked-list/yeonguchoe.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* struct ListNode *next;
6+
* };
7+
*/
8+
struct ListNode* reverseList(struct ListNode* head) {
9+
10+
struct ListNode* ptr1 = NULL;
11+
struct ListNode* ptr2 = head;
12+
struct ListNode* ptr3 = NULL;
13+
14+
while (ptr2 != NULL) {
15+
ptr3 = ptr2->next;
16+
ptr2->next = ptr1;
17+
18+
// 앞으로 전진
19+
ptr1 = ptr2;
20+
ptr2 = ptr3;
21+
}
22+
return ptr1;
23+
}
24+
// 시간 복잡도: O(노드 개수)
25+
// 공간 복잡도: O(1)

set-matrix-zeroes/yeonguchoe.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
void setZeroes(int** matrix, int matrixSize, int* matrixColSize) {
2+
3+
int row_size = matrixSize;
4+
int column_size = matrixColSize[0];
5+
6+
bool zero_on_first_row = false;
7+
bool zero_on_first_column = false;
8+
9+
// 첫번째 row에 0이 존재하는지 확인
10+
for (int i = 0; i < column_size; i++) {
11+
if (matrix[0][i] == 0) {
12+
zero_on_first_row = true;
13+
}
14+
}
15+
// 첫번째 column에 0이 존재하는지 확인
16+
for (int i = 0; i < row_size; i++) {
17+
if (matrix[i][0] == 0) {
18+
zero_on_first_column = true;
19+
}
20+
}
21+
22+
// 전체 돌면서 0 발견시 첫번째 row, column에 0으로 표시
23+
for (int i = 0; i < row_size; i++) {
24+
for (int j = 0; j < column_size; j++) {
25+
if (matrix[i][j] == 0) {
26+
matrix[0][j] = 0;
27+
matrix[i][0] = 0;
28+
}
29+
}
30+
}
31+
32+
for (int i = 1; i < row_size; i++) {
33+
for (int j = 1; j < column_size; j++) {
34+
if (matrix[0][j] == 0 || matrix[i][0] == 0) {
35+
matrix[i][j] = 0;
36+
}
37+
}
38+
}
39+
40+
if (zero_on_first_row) {
41+
for (int i = 0; i < column_size; i++) {
42+
matrix[0][i] = 0;
43+
}
44+
}
45+
46+
if (zero_on_first_column) {
47+
for (int i = 0; i < row_size; i++) {
48+
matrix[i][0] = 0;
49+
}
50+
}
51+
}
52+
// 시간 복잡도: O(matrix 크기)
53+
// 공간 복잡도: O(1)

unique-paths/yeonguchoe.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int combination(int n, int r) {
4+
int k = n - r > r ? r : n - r;
5+
unsigned long long result = 1;
6+
for (int i = 0; i < k; i++) {
7+
result = result * (n - i) / (i + 1);
8+
}
9+
return result;
10+
}
11+
int uniquePaths(int m, int n) { return combination(m - 1 + n - 1, m - 1); }
12+
// 시간 복잡도: O(min(m,n))
13+
// 공간 복잡도: O(1)
14+
};

0 commit comments

Comments
 (0)