Skip to content
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

[영우] Week7 Solutions #497

Merged
merged 8 commits into from
Sep 26, 2024
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/yeonguchoe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
int lengthOfLongestSubstring(string s) {

int current_max = 0;
int pivot = 0;
int checker = 0;

int current_length = 0;
while (pivot < s.size()) {
set<char> included;
included.insert(s[pivot]);
checker = pivot + 1;
while (checker < s.size() and
included.find(s[checker]) == included.end()) {
included.insert(s[checker]);
checker += 1;
}
current_length = checker - pivot;
current_max = max(current_max, current_length);
pivot += 1;
}
return current_max;
}
// 시간 복잡도: O(n)
// 공간 복잡도: O(n)
};
68 changes: 68 additions & 0 deletions number-of-islands/yeonguchoe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int count = 0;

int row_size = grid.size();
int column_size = grid[0].size();

queue<pair<int, int>> q;

for (int i = 0; i < row_size; i++) {
for (int j = 0; j < column_size; j++) {
if (grid[i][j] == '1') {
LookAround(i, j, grid, q, row_size, column_size);
count += 1;
}
}
}
return count;
}

// BFS
void LookAround(int& x, int& y, vector<vector<char>>& grid,
queue<pair<int, int>>& q, int& row_size, int& column_size) {
pair<int, int> center_location{x, y};
q.push(center_location);

grid[x][y] = '0';

while (!q.empty()) {
pair<int, int> selected_location = q.front();
q.pop();

int selected_x = selected_location.first;
int selected_y = selected_location.second;

// 선택된 cell에서 왼쪽
if (selected_x - 1 >= 0 &&
grid[selected_x - 1][selected_y] == '1') {
q.push({selected_x - 1, selected_y});
grid[selected_x - 1][selected_y] = '0';
}

// 선택된 cell에서 오른쪽
if (selected_x + 1 < row_size &&
grid[selected_x + 1][selected_y] == '1') {
q.push({selected_x + 1, selected_y});
grid[selected_x + 1][selected_y] = '0';
}

// 선택된 cell에서 위쪽
if (selected_y - 1 >= 0 &&
grid[selected_x][selected_y - 1] == '1') {
q.push({selected_x, selected_y - 1});
grid[selected_x][selected_y - 1] = '0';
}

// 선택된 cell에서 아래쪽
if (selected_y + 1 < column_size &&
grid[selected_x][selected_y + 1] == '1') {
q.push({selected_x, selected_y + 1});
grid[selected_x][selected_y + 1] = '0';
}
}
}
// 시간 복잡도: O(row*column)
// 공간 복잡도: O(row*column)
};
25 changes: 25 additions & 0 deletions reverse-linked-list/yeonguchoe.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head) {

struct ListNode* ptr1 = NULL;
struct ListNode* ptr2 = head;
struct ListNode* ptr3 = NULL;

while (ptr2 != NULL) {
ptr3 = ptr2->next;
ptr2->next = ptr1;

// 앞으로 전진
ptr1 = ptr2;
ptr2 = ptr3;
}
return ptr1;
}
// 시간 복잡도: O(노드 개수)
// 공간 복잡도: O(1)
53 changes: 53 additions & 0 deletions set-matrix-zeroes/yeonguchoe.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
void setZeroes(int** matrix, int matrixSize, int* matrixColSize) {

int row_size = matrixSize;
int column_size = matrixColSize[0];

bool zero_on_first_row = false;
bool zero_on_first_column = false;

// 첫번째 row에 0이 존재하는지 확인
for (int i = 0; i < column_size; i++) {
if (matrix[0][i] == 0) {
zero_on_first_row = true;
}
}
// 첫번째 column에 0이 존재하는지 확인
for (int i = 0; i < row_size; i++) {
if (matrix[i][0] == 0) {
zero_on_first_column = true;
}
}

// 전체 돌면서 0 발견시 첫번째 row, column에 0으로 표시
for (int i = 0; i < row_size; i++) {
for (int j = 0; j < column_size; j++) {
if (matrix[i][j] == 0) {
matrix[0][j] = 0;
matrix[i][0] = 0;
}
}
}

for (int i = 1; i < row_size; i++) {
for (int j = 1; j < column_size; j++) {
if (matrix[0][j] == 0 || matrix[i][0] == 0) {
matrix[i][j] = 0;
}
}
}

if (zero_on_first_row) {
for (int i = 0; i < column_size; i++) {
matrix[0][i] = 0;
}
}

if (zero_on_first_column) {
for (int i = 0; i < row_size; i++) {
matrix[i][0] = 0;
}
}
}
// 시간 복잡도: O(matrix 크기)
// 공간 복잡도: O(1)
14 changes: 14 additions & 0 deletions unique-paths/yeonguchoe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int combination(int n, int r) {
int k = n - r > r ? r : n - r;
unsigned long long result = 1;
for (int i = 0; i < k; i++) {
result = result * (n - i) / (i + 1);
}
return result;
}
int uniquePaths(int m, int n) { return combination(m - 1 + n - 1, m - 1); }
// 시간 복잡도: O(min(m,n))
// 공간 복잡도: O(1)
};