-
-
Notifications
You must be signed in to change notification settings - Fork 339
[hwi-middle] WEEK 07 solutions #2549
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| class Solution { | ||
| public: | ||
| int lengthOfLongestSubstring(string s) { | ||
| int n = s.size(); | ||
| unordered_map<char, int> m; // 현재 윈도우 내에 있는 문자 - 인덱스 | ||
| int maxLen = 0; | ||
| int l = 0; | ||
| for (int r = 0; r < n; ++r) | ||
| { | ||
| if (m.contains(s[r])) // 중복된 문자를 찾은 경우 | ||
| { | ||
| l = max(m[s[r]] + 1, l); // 해당 문자가 l보다 뒤에 있다면 l을 거기로 옮김 | ||
| } | ||
|
|
||
| m[s[r]] = r; // s[r]의 위치 업데이트 | ||
|
|
||
| maxLen = max(maxLen, r - l + 1); | ||
| } | ||
|
|
||
| return maxLen; | ||
| } | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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,49 @@ | ||
| class Solution { | ||
| public: | ||
| int numIslands(vector<vector<char>>& grid) { | ||
| // BFS 기반 풀이 | ||
| int r = grid.size(); | ||
| int c = grid[0].size(); | ||
|
|
||
| int cnt = 0; | ||
| for (int i = 0; i < r; ++i) | ||
| { | ||
| for (int j = 0; j < c; ++j) | ||
| { | ||
| if (grid[i][j] == '0') | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| cnt++; | ||
| queue<pair<int, int>> q; | ||
| q.push({i, j}); | ||
| grid[i][j] = '0'; // 방문 여부를 저장하기 위해 grid를 직접 수정 | ||
|
|
||
| while (!q.empty()) | ||
| { | ||
| int x, y; | ||
| tie(x, y) = q.front(); | ||
| q.pop(); | ||
|
|
||
| int dx[] = { 0, 1, 0, -1 }; | ||
| int dy[] = { 1, 0, -1, 0 }; | ||
|
|
||
| for (int dir = 0; dir < 4; ++dir) | ||
| { | ||
| int nx = x + dx[dir]; | ||
| int ny = y + dy[dir]; | ||
|
|
||
| if (nx < 0 || nx >= r || ny < 0 || ny >= c) continue; | ||
| if (grid[nx][ny] == '0') continue; | ||
|
|
||
| grid[nx][ny] = '0'; | ||
| q.push({nx, ny}); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return cnt; | ||
| } | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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,28 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * struct ListNode { | ||
| * int val; | ||
| * ListNode *next; | ||
| * ListNode() : val(0), next(nullptr) {} | ||
| * ListNode(int x) : val(x), next(nullptr) {} | ||
| * ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
| * }; | ||
| */ | ||
| class Solution { | ||
| public: | ||
| ListNode* reverseList(ListNode* head) { | ||
| ListNode* prev = nullptr; | ||
| ListNode* cur = head; | ||
|
|
||
| // next를 이전 노드로 설정해나가며 루프 | ||
| while (cur != nullptr) | ||
| { | ||
| ListNode* tmp = cur->next; | ||
| cur->next = prev; | ||
| prev = cur; | ||
| cur = tmp; | ||
| } | ||
|
|
||
| return prev; | ||
| } | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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,108 @@ | ||
| // 풀이 1 | ||
| class Solution { | ||
| public: | ||
| void setZeroes(vector<vector<int>>& matrix) { | ||
| int m = matrix.size(); | ||
| int n = matrix[0].size(); | ||
| vector<pair<int, int>> v; | ||
| v.reserve(m * n); | ||
|
|
||
| // 0인 칸을 별도의 공간에 마킹 | ||
| for (int i = 0; i < m; ++i) | ||
| { | ||
| for (int j = 0; j < n; ++j) | ||
| { | ||
| if (matrix[i][j] == 0) | ||
| { | ||
| v.push_back({i, j}); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 마킹이 끝난 후 해당 칸들을 확인하며 행과 열을 채움 | ||
| for (auto coord : v) | ||
| { | ||
| int x = coord.first; | ||
| int y = coord.second; | ||
|
|
||
| // 행 | ||
| for (int i = 0; i < n; ++i) | ||
| { | ||
| matrix[x][i] = 0; | ||
| } | ||
|
|
||
| // 열 | ||
| for (int i = 0; i < m; ++i) | ||
| { | ||
| matrix[i][y] = 0; | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // 풀이 2 | ||
| class Solution { | ||
| public: | ||
| void setZeroes(vector<vector<int>>& matrix) { | ||
| int r = matrix.size(); | ||
| int c = matrix[0].size(); | ||
|
|
||
| // 핵심 아이디어 | ||
| // - 각 행과 열에는 반드시 첫 번째 요소는 존재한다. | ||
| // - 이를 활용하여 각 행과 열의 첫 번째 요소를 마킹용으로 사용한다. | ||
| // - matrix[i][0]: i번째 행 전체를 0으로 만들어야 한다는 의미 | ||
| // - matrix[0][j]: j번째 열 전체를 0으로 만들어야 한다는 의미 | ||
|
|
||
| // 엣지 케이스 | ||
| // - matrix[0][0]: 0번째 행인지, 0번째 열인지 구분할 수 없음 | ||
|
|
||
| // 해결 방법 | ||
| // - matrix[0][0]은 0번째 행을 의미하도록 약속 | ||
| // - 0번째 열은 별도의 플래그를 둠 | ||
|
|
||
| bool isFirstColZero = false; | ||
| for (int i = 0; i < r; ++i) | ||
| { | ||
| if (matrix[i][0] == 0) | ||
| { | ||
| isFirstColZero = true; | ||
| } | ||
|
|
||
| for (int j = 1; j < c; ++j) | ||
| { | ||
| if (matrix[i][j] == 0) | ||
| { | ||
| matrix[i][0] = 0; | ||
| matrix[0][j] = 0; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (int i = 1; i < r; ++i) | ||
| { | ||
| for (int j = 1; j < c; ++j) | ||
| { | ||
| if (matrix[i][0] == 0 || matrix[0][j] == 0) | ||
| { | ||
| matrix[i][j] = 0; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (matrix[0][0] == 0) | ||
| { | ||
| for (int j = 0; j < c; j++) | ||
| { | ||
| matrix[0][j] = 0; | ||
| } | ||
| } | ||
|
|
||
| if (isFirstColZero) | ||
| { | ||
| for (int i = 0; i < r; i++) | ||
| { | ||
| matrix[i][0] = 0; | ||
| } | ||
| } | ||
| } | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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,20 @@ | ||
| class Solution { | ||
| public: | ||
| int uniquePaths(int m, int n) { | ||
| // 결과 구하기: C(m + n - 2, m - 1) | ||
|
|
||
| int k = min(m - 1, n - 1); // C(n, k) = C(n, n - k) | ||
| return combination(m + n - 2, k); | ||
| } | ||
|
|
||
| int combination(int n, int k) | ||
| { | ||
| long long res = 1; | ||
| for (int i = 1; i <= k; ++i) | ||
| { | ||
| res = res * (n - i + 1) / i; | ||
| } | ||
|
|
||
| return res; | ||
| } | ||
| }; |
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.
🏷️ 알고리즘 패턴 분석