-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountingValleys.java
63 lines (49 loc) · 1.8 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
56
57
58
59
60
61
62
63
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.*;
public class CountingValleys{
private static int placementFunction(char movement,int placement){
if (movement == 'U'){
return placement+1;
}
return placement-1;
}
private static int valleyFunction(ArrayList<Character> tup, int placement){
int new_placement = placementFunction(tup.get(1), placement);
boolean prior_low_level = (placement < 0);
boolean sealevel = (new_placement == 0);
if (sealevel && prior_low_level){
return 1;
}
return 0;
}
public static int countingValleys(int n, char[] s) {
int valley = 0;
int plment = placementFunction(s[0], 0);
ArrayList<Character> tup = new ArrayList<Character>();
for (int i = 1; i < n; i++) {
tup.add(0, s[i - 1]);
tup.add(1, s[i]);
valley += valleyFunction(tup, plment);
plment = placementFunction(s[i], plment);
}
return valley;
}
public static void main(String[] args) throws Exception {
File file = new File("input.txt");
// Create a buffer to read the file
BufferedReader br = new BufferedReader(new FileReader(file));
String str;
Vector<String> storage = new Vector<String>();
// While str = read next line isn't null, store it to the storage vector
while ((str = br.readLine()) != null) {
storage.add(str);
}
// storage.get(0) -> size of array; storage.get(1) -> the array
int n = Integer.parseInt(storage.get(0));
char[] s = storage.get(1).toCharArray();
System.out.println(countingValleys(n, s));
br.close();
}
}