File tree Expand file tree Collapse file tree 3 files changed +138
-0
lines changed Expand file tree Collapse file tree 3 files changed +138
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ ํ์ด :
3+ grid๋ฅผ ์ํํ๋ฉด์ '1'์ ๋ง๋๋ฉด ์ํ์ข์ฐ๋ฅผ dfs๋ฅผ ํตํด ์ด๋ฏธ ํ์ํ ๋
์ด๋ผ๋ ๋ป์ผ๋ก '$'๋ก ๋ณ๊ฒฝํ๊ณ
4+ ์๋ก์ด ๋
์ ๋ง๋ ๋๋ง๋ค cnt ์ฆ๊ฐ
5+
6+ grid ํฌ๊ธฐ M * N
7+
8+ TC : O(M * N)
9+ dfs ํธ์ถ์ grid์ ํฌ๊ธฐ์ ๋น๋ก
10+
11+ SC : O(M * N)
12+ dfs ํธ์ถ ์คํ๋ grid์ ํฌ๊ธฐ์ ๋น๋ก๋ก
13+ */
14+
15+ #include < vector>
16+ using namesapce std;
17+
18+ class Solution {
19+ public:
20+ int numIslands (vector<vector<char >>& grid) {
21+ int cnt = 0 ;
22+
23+ for (int i = 0 ; i < grid.size (); i++) {
24+ for (int j = 0 ; j < grid[0 ].size (); j++) {
25+ if (grid[i][j] == ' 1' ) {
26+ dfs (i, j, grid);
27+ cnt++;
28+ }
29+ }
30+ }
31+ return (cnt);
32+ }
33+
34+ void dfs (int row, int col, vector<vector<char >>& grid) {
35+ if (row < 0 || row >= grid.size () || col < 0 || col >= grid[0 ].size () || grid[row][col] != ' 1' )
36+ return ;
37+ grid[row][col] = ' $' ;
38+ dfs (row + 1 , col, grid);
39+ dfs (row - 1 , col, grid);
40+ dfs (row, col + 1 , grid);
41+ dfs (row, col - 1 , grid);
42+ }
43+ };
Original file line number Diff line number Diff line change 1+ /*
2+ ํ์ด :
3+ row0๊ณผ col0 ์ ํด๋น ์ค์ด 0์ด ๋๋์ง ์งํ๋ก ์ฌ์ฉํด์ ๊ณต๊ฐ๋ณต์ก๋ O(1)์ ๋ฌ์ฑํ ๊ฒ์ด๋ค
4+
5+ 1. row0๊ณผ col0 ์ค์ 0์ด ์กด์ฌํ๋์ง ๋ฏธ๋ฆฌ ํ์ธํ๊ณ ๋ณ์์ ์ ์ฅ
6+
7+ 2. matrix๋ฅผ ์ํ(์ฒซํ, ์ฒซ์ด ์ ์ธ)ํ๋ฉด์ 0์ธ ์นธ์ ๋ง๋๋ฉด ํด๋น ์ ๋ณด๋ฅผ ์ฒซํ, ์ฒซ์ด์ ์ ์ฅ
8+
9+ 3. ์ ์ฅ๋ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก matrix ์
๋ฐ์ดํธ
10+
11+ 4. ๋ณ์์ ๋ด์๋ ์ฒซํ, ์ฒซ์ด์ 0์ด ์กด์ฌํ๋๊ฐ ๋ฐํ์ผ๋ก ์
๋ฐ์ดํธ
12+
13+ matrix ํฌ๊ธฐ : M * N
14+
15+ TC : O(M * N)
16+
17+ SC : O(1)
18+ */
19+
20+ #include < vector>
21+ using namespace std ;
22+
23+ class Solution {
24+ public:
25+ void setZeroes (vector<vector<int >>& matrix) {
26+ bool firstRowZero = false , firstColZero = false ;
27+
28+ int nRows = matrix.size ();
29+ int nCols = matrix[0 ].size ();
30+
31+ for (int i = 0 ; i < nRows; i++)
32+ if (matrix[i][0 ] == 0 )
33+ firstColZero = true ;
34+
35+ for (int j = 0 ; j < nCols; j++)
36+ if (matrix[0 ][j] == 0 )
37+ firstRowZero = true ;
38+
39+ for (int i = 1 ; i <nRows; i++) {
40+ for (int j = 1 ; j < nCols; j++) {
41+ if (matrix[i][j] == 0 ) {
42+ matrix[i][0 ] = 0 ;
43+ matrix[0 ][j] = 0 ;
44+ }
45+ }
46+ }
47+
48+ for (int i = 1 ; i < nRows; i++) {
49+ for (int j = 1 ; j < nCols; j++)
50+ if (matrix[i][0 ] == 0 || matrix[0 ][j] == 0 )
51+ matrix[i][j] = 0 ;
52+ }
53+
54+ if (firstRowZero) {
55+ for (int i = 0 ; i < nCols; i++)
56+ matrix[0 ][i] = 0 ;
57+ }
58+
59+ if (firstColZero) {
60+ for (int i = 0 ; i < nRows; i++)
61+ matrix[i][0 ] = 0 ;
62+ }
63+ }
64+ };
Original file line number Diff line number Diff line change 1+ /*
2+ ํ์ด :
3+ ํน์ ์นธ์ ์ค๋ ๋ฐฉ๋ฒ์ ์ผ์ชฝ์์ ์ค๊ฑฐ๋ ์์์ ์ค๊ฑฐ๋ ๋ ์ค ํ๋์ด๋ฏ๋ก ๋ ๊ณณ์ unique path ๊ฐ์๋ฅผ ๋ํ๋ฉด ๊ตฌํ ์ ์๋ค
4+ ํ row ๋ง๋ค๊ณ ๋๋ฒ์จฐ ์ค๋ถํฐ ๊ทธ ์ด์ ๊ฐ(์์์ ์ค๋ ๊ฐ์)๊ณผ ๊ทธ ์ index์ ๊ฐ(์ผ์ชฝ์์ ์ค๋ ๊ฐ์)๋ฅผ ๋ํ๋ฉด์ ๋ฐ๋ณตํ๋ค
5+ ์ต์ข
์ ์ผ๋ก row์ ๊ฐ์ฅ ์ค๋ฅธ์ชฝ ๊ฐ return
6+
7+ rowํฌ๊ธฐ = M, col ํฌ๊ธฐ = N
8+
9+ TC : O(M * N)
10+ ๋ชจ๋ ์นธ์ ๋ํด ์ํ
11+
12+ SC : O(N)
13+ ํ row๋งํผ ๋ฉ๋ชจ๋ฆฌ ํ ๋น
14+ */
15+
16+ #include < vector>
17+ using namespace std ;
18+
19+ class Solution {
20+ public:
21+ int uniquePaths (int m, int n) {
22+ vector<int > row (n, 1 );
23+
24+ for (int i = 1 ; i < m; i++) {
25+ for (int j = 1 ; j < n; j++) {
26+ row[j] = row[j] + row[j - 1 ];
27+ }
28+ }
29+ return row[n - 1 ];
30+ }
31+ };
You canโt perform that action at this time.
0 commit comments