-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountingValleys.java
55 lines (53 loc) · 1.4 KB
/
CountingValleys.java
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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcases = sc.nextInt();
String path = sc.next();
int count =0;
List<Integer> list = new ArrayList<Integer😠);
list.add(0);
for(char c: path.toCharArray()){
if(c == 'D'){
count-=1;
list.add(-1);
}else if(c=='U'){
count+=1;
list.add(1);
}
if(count == 0){
list.add(0);
}
}
int count1=0;
int result = 0;
boolean isSeaLevel = false;
boolean valleyStart = false;
for(Integer i :list){
if(i==0){
isSeaLevel = true;
valleyStart = false;
count1=0;
}
if(i==1 && isSeaLevel){
valleyStart =false;
isSeaLevel = false;
}
if(i == -1 && isSeaLevel){
valleyStart = true;
isSeaLevel = false;
}
if(valleyStart) {
count1 += i;
if (count1 == 0) {
result++;
}
}
}
System.out.println(result);
}
}