From 2eac0141f67a1cf443ad19a4e65a436324efc030 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Fri, 16 May 2025 06:09:07 +0900 Subject: [PATCH 1/4] #223 reverse-linked-list solution --- reverse-linked-list/sungjinwi.cpp | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 reverse-linked-list/sungjinwi.cpp diff --git a/reverse-linked-list/sungjinwi.cpp b/reverse-linked-list/sungjinwi.cpp new file mode 100644 index 000000000..8163a1f45 --- /dev/null +++ b/reverse-linked-list/sungjinwi.cpp @@ -0,0 +1,62 @@ +/* + 풀이 : + linked-list를 순회하며 stack에 차례대로 push하고 다 넣은 후 + pop하면서 역순으로 연결해준다 + + 노드의 개수 : N + + TC : O(N) + 총 2회 while 반복문 O(N + N) + + SC : O(N) + 스택은 노드의 개수에 비례 +*/ + +/** + * 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) {} + * }; + */ + +#include +using namespace std; + +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) { + stack st; + ListNode dummy; + ListNode* prv = &dummy; + ListNode* cur; + + while (head) + { + st.push(head); + head = head->next; + } + + while (!st.empty()) + { + cur = st.top(); + prv->next = cur; + prv = cur; + st.pop(); + } + prv->next = nullptr; + + return dummy.next; + } + }; From 85fb4cfd94f6a56e7accbf1df1da955624dc094b Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Fri, 16 May 2025 19:07:02 +0900 Subject: [PATCH 2/4] #243 solution --- .../sungjinwi.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 longest-substring-without-repeating-characters/sungjinwi.cpp diff --git a/longest-substring-without-repeating-characters/sungjinwi.cpp b/longest-substring-without-repeating-characters/sungjinwi.cpp new file mode 100644 index 000000000..24a0c66a2 --- /dev/null +++ b/longest-substring-without-repeating-characters/sungjinwi.cpp @@ -0,0 +1,40 @@ +/* + 풀이 : + 슬라이딩 윈도우 기법을 사용해서 풀이 + right가 현재까지 문자열에 포함되지 않는 문자면 right의 문자를 set에 추가 후 right 증가, 문자열길이를 늘리고 최대길이와 비교해서 업데이트 + 이미 문자열에 포함되는 문자면 left의 문자를 set에서 제거 후 left 증가 + + 문자열 길이 : N + + TC : O(N) + 문자열 전체에 대해 1회 순회 + + SC : O(N) + 해시테이블 unordered_set의 크기는 문자열 길이에 비례례 +*/ + +#include + +class Solution { +public: + int lengthOfLongestSubstring(string s) { + int ans = 0; + int left = 0, right = 0; + unordered_set lookup; + + while (right < s.size()) + { + if (lookup.find(s[right]) == lookup.end()) { + ans = max(ans, right - left + 1); + lookup.insert(s[right]); + right++; + } + else { + lookup.erase(s[left]); + left++; + } + } + + return ans; + } +}; From 394febb8ce3506a29d6a3fd54bd370c249e58184 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Fri, 16 May 2025 19:14:38 +0900 Subject: [PATCH 3/4] #243 additional obtimization --- .../sungjinwi.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/longest-substring-without-repeating-characters/sungjinwi.cpp b/longest-substring-without-repeating-characters/sungjinwi.cpp index 24a0c66a2..ef9ed9c16 100644 --- a/longest-substring-without-repeating-characters/sungjinwi.cpp +++ b/longest-substring-without-repeating-characters/sungjinwi.cpp @@ -10,7 +10,12 @@ 문자열 전체에 대해 1회 순회 SC : O(N) - 해시테이블 unordered_set의 크기는 문자열 길이에 비례례 + 해시테이블 unordered_set의 크기는 문자열 길이에 비례 + + 추가적인 최적화 방법 : + int lastIdx[256] 선언하고 아스키코드 위치에 마지막으로 해당 문자가 나온 인덱스를 저장한다 + ex) a가 10번쨰 인덱스에서 나오면 lastIdx['a'] = 10; + 나중에 중복되는 문자를 만나는 경우 left를 1씩 전진시키는 것이 아니라 중복된 문자의 마지막 바로 다음으로 left를 업데이트 */ #include From 17c09df89fb4acf6b470f90041c24c4571ceb240 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Sat, 17 May 2025 18:46:06 +0900 Subject: [PATCH 4/4] #258 #273 #283 solution --- number-of-islands/sungjinwi.cpp | 43 ++++++++++++++++++++++ set-matrix-zeroes/sungjinwi.cpp | 64 +++++++++++++++++++++++++++++++++ unique-paths/sungjinwi.cpp | 31 ++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 number-of-islands/sungjinwi.cpp create mode 100644 set-matrix-zeroes/sungjinwi.cpp create mode 100644 unique-paths/sungjinwi.cpp diff --git a/number-of-islands/sungjinwi.cpp b/number-of-islands/sungjinwi.cpp new file mode 100644 index 000000000..7995f63b9 --- /dev/null +++ b/number-of-islands/sungjinwi.cpp @@ -0,0 +1,43 @@ +/* + 풀이 : + grid를 순회하면서 '1'을 만나면 상하좌우를 dfs를 통해 이미 탐색한 땅이라는 뜻으로 '$'로 변경하고 + 새로운 땅을 만날때마다 cnt 증가 + + grid 크기 M * N + + TC : O(M * N) + dfs 호출은 grid의 크기에 비례 + + SC : O(M * N) + dfs 호출 스택도 grid의 크기에 비례례 +*/ + +#include +using namesapce std; + +class Solution { +public: + int numIslands(vector>& grid) { + int cnt = 0; + + for (int i = 0; i < grid.size(); i++) { + for (int j = 0; j < grid[0].size(); j++) { + if (grid[i][j] == '1') { + dfs(i, j, grid); + cnt++; + } + } + } + return (cnt); + } + + void dfs(int row, int col, vector>& grid) { + if (row < 0 || row >= grid.size() || col < 0 || col >= grid[0].size() || grid[row][col] != '1') + return ; + grid[row][col] = '$'; + dfs(row + 1, col, grid); + dfs(row - 1, col, grid); + dfs(row, col + 1, grid); + dfs(row, col - 1, grid); + } +}; diff --git a/set-matrix-zeroes/sungjinwi.cpp b/set-matrix-zeroes/sungjinwi.cpp new file mode 100644 index 000000000..8a5bdcb59 --- /dev/null +++ b/set-matrix-zeroes/sungjinwi.cpp @@ -0,0 +1,64 @@ +/* + 풀이 : + row0과 col0 을 해당 줄이 0이 되는지 지표로 사용해서 공간복잡도 O(1)을 달성할 것이다 + + 1. row0과 col0 중에 0이 존재하는지 미리 확인하고 변수에 저장 + + 2. matrix를 순회(첫행, 첫열 제외)하면서 0인 칸을 만나면 해당 정보를 첫행, 첫열에 저장 + + 3. 저장된 정보를 바탕으로 matrix 업데이트 + + 4. 변수에 담았던 첫행, 첫열에 0이 존재하는가 바탕으로 업데이트 + + matrix 크기 : M * N + + TC : O(M * N) + + SC : O(1) +*/ + +#include +using namespace std; + +class Solution { +public: + void setZeroes(vector>& matrix) { + bool firstRowZero = false, firstColZero = false; + + int nRows = matrix.size(); + int nCols = matrix[0].size(); + + for (int i = 0; i < nRows; i++) + if (matrix[i][0] == 0) + firstColZero = true; + + for (int j = 0; j < nCols; j++) + if (matrix[0][j] == 0) + firstRowZero = true; + + for (int i = 1; i +using namespace std; + +class Solution { +public: + int uniquePaths(int m, int n) { + vector row(n, 1); + + for (int i = 1; i < m; i++) { + for (int j = 1; j < n; j++) { + row[j] = row[j] + row[j - 1]; + } + } + return row[n - 1]; + } +};