-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
29 lines (25 loc) · 766 Bytes
/
Solution.java
File metadata and controls
29 lines (25 loc) · 766 Bytes
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
import java.util.Scanner;
class Result {
public static int countingValleys(int steps, String path) {
int altitude = 0;
int valleys = 0;
for (int i = 0; i < path.length(); i++) {
if (path.charAt(i) == 'D') altitude--;
else if (path.charAt(i) == 'U') {
if (altitude == -1) valleys++;
altitude++;
}
}
return valleys;
}
}
public class Solution {
private static final Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int steps = scan.nextInt();
scan.nextLine();
String path = scan.nextLine();
scan.close();
System.out.println(Result.countingValleys(steps, path));
}
}