-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathSolution.js
36 lines (32 loc) · 1.1 KB
/
Solution.js
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
//Problem: https://www.hackerrank.com/challenges/counting-valleys
//JavaScript
/*
Initial Thoughts:
Iterate over the ups and downs keeping track of
your distance from sea level and whenever you make
the transition to below increase a value and repeat
that for each time you make the transition
Time Complexity: O(n) //We need to iterate over the whole hike
Space Complexity: O(1) //We do no use any dynamically sized variables
*/
function processData(input) {
var level = 0, vallies = 0;
var slopes = input.split("\n", 2)[1];
for (var i in slopes) {
level = (slopes.charAt(i) == 'U') ? level + 1 : level - 1; //Increase/decrease sea level
if (level === 0 && slopes.charAt(i) === 'U') { //Check for transition of sea level
vallies++;
}
}
console.log(vallies);
}
////////// BOILERPLATE PLATE CODE \\\\\\\\\\
process.stdin.resume();
process.stdin.setEncoding("ascii");
var _input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});