Skip to content

Commit 074c93d

Browse files
Merge pull request #1858 from CodingTestStudy2/염혜정
[염혜정] Day19, Day20
2 parents aafb8ad + 7d8efee commit 074c93d

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// dfs
2+
3+
class Solution {
4+
5+
boolean[][] isVisited;
6+
int[] dr = {-1, 1, 0, 0};
7+
int[] dc = {0, 0, -1, 1};
8+
9+
public int[][] colorBorder(int[][] grid, int row, int col, int color) {
10+
isVisited = new boolean[grid.length][grid[0].length];
11+
12+
List<int[]> borders = new ArrayList<>();
13+
dfs (grid, row, col, grid[row][col], borders);
14+
15+
for (int[] b : borders) {
16+
grid[b[0]][b[1]] = color;
17+
}
18+
19+
return grid;
20+
}
21+
22+
void dfs(int[][] grid, int row, int col, int gridColor, List<int[]> borders) {
23+
isVisited[row][col] = true;
24+
25+
boolean isBorder = false;
26+
for (int i = 0; i<4; i++) {
27+
int nr = row + dr[i];
28+
int nc = col + dc[i];
29+
30+
if (nr<0 || nr>=grid.length || nc<0 || nc>=grid[0].length) { // 테두리
31+
isBorder = true;
32+
continue;
33+
}
34+
35+
if (grid[nr][nc] != gridColor) {
36+
isBorder = true;
37+
continue;
38+
}
39+
40+
if (!isVisited[nr][nc]) {
41+
dfs(grid, nr, nc, gridColor, borders);
42+
}
43+
}
44+
if (isBorder) borders.add(new int[]{row, col});
45+
}
46+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// 97 ~ 122
2+
3+
class Solution {
4+
public String mapWordWeights(String[] words, int[] weights) {
5+
StringBuilder sb = new StringBuilder();
6+
for (String word : words) {
7+
int sum = 0;
8+
for (char c : word.toCharArray()) {
9+
int num = (int) c - 97;
10+
sum += weights[num];
11+
}
12+
int mod = sum % 26;
13+
sb.append((char)('z' - mod));
14+
}
15+
return sb.toString();
16+
}
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public boolean isMiddleElementUnique(int[] nums) {
3+
int middle = (nums.length - 1) / 2;
4+
int cnt = 0;
5+
for (int num : nums) {
6+
if (num == nums[middle]) cnt++;
7+
if (cnt == 2) break;
8+
}
9+
return cnt == 1;
10+
}
11+
}

0 commit comments

Comments
 (0)