-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeaks.java
More file actions
61 lines (55 loc) · 1.88 KB
/
Peaks.java
File metadata and controls
61 lines (55 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package array;
import java.util.Scanner;
public class Peaks {
public static int solution1(int len, int[][] inputArray) {
int result = 0;
for (int i = 1; i <= len; i++) {
for (int j = 1; j <= len; j++) {
if (inputArray[i][j] > inputArray[i - 1][j] && inputArray[i][j] > inputArray[i][j - 1] && inputArray[i][j] > inputArray[i + 1][j] && inputArray[i][j] > inputArray[i][j + 1]) {
result++;
}
}
}
return result;
}
public static int solution2(int len, int[][] inputArray) {
int result = 0;
int[] dx = {-1, 0, 1, 0};
int[] dy = {0, 1, 0, -1};
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
boolean isPeaks = true;
for (int x = 0; x < 4; x++) {
int nx = i - dx[x];
int ny = j - dy[x];
if (nx >= 0 && ny >= 0 && nx < len && ny < len && inputArray[i][j] < inputArray[nx][ny]) {
isPeaks = false;
break;
}
}
if (isPeaks) {
result++;
}
}
}
return result;
}
public void main() {
Scanner in=new Scanner(System.in);
int len = in.nextInt();
int[][] inputArray = new int[len+2][len+2];
// solution 1 적용
// for (int i = 1; i <= len; i++) {
// for (int j = 1; j <= len; j++) {
// inputArray[i][j] = in.nextInt();
// }
// }
// System.out.println(solution1(len, inputArray));
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
inputArray[i][j] = in.nextInt();
}
}
System.out.println(solution2(len, inputArray));
}
}