Skip to content

Commit 17c09df

Browse files
committed
#258 #273 #283 solution
1 parent 394febb commit 17c09df

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

โ€Žnumber-of-islands/sungjinwi.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
};

โ€Žset-matrix-zeroes/sungjinwi.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
};

โ€Žunique-paths/sungjinwi.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
};

0 commit comments

Comments
ย (0)