Answers for "counting valleys hackerrank solution"

0

counting valleys hackerrank solution javascript

'use strict';
 
const processData = input => {
    const lines = input.split('n');
    const steps = lines[1];
    let valleys = 0;
    let seaLevel = 0;
 
    for(let i = 0; i < steps.length; i++) {
        let c = steps.charAt(i);
        if(c == 'D') {
            seaLevel -= 1;
        } else {
            seaLevel += 1;
        }
        if(seaLevel == 0 && c == 'U') {
            valleys += 1;
        }
    }
    console.log(valleys);
 
};
 
process.stdin.resume();
process.stdin.setEncoding("ascii");
 
let _input = "";
process.stdin.on("data", input => _input += input);
process.stdin.on("end", () => processData(_input));
Posted by: Guest on August-31-2020
0

counting valleys hackerrank solution in c++

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n,updown = 0,res = 0;
    char c;
    cin >> n;
    
    for(int i = 0; i++ < n;){
        cin >> c;
        if(c == 'U')updown++;
        else updown--;
        if(updown == 0 && c == 'U')res++;
    }
    cout << res << endl;
    return 0;
}
Posted by: Guest on June-26-2020
0

Counting Valleys

# HackerRank.com "Counting Valleys" problem
def countingValleys(n, s):
    total_valley = 0
    location = 0

    for i in range(n):

        if s[i] == 'D':
            if location == 0:
                total_valley = total_valley + 1
            location = location - 1
			continue
        elif s[i] == 'U':
            location = location + 1

    return total_valley
Posted by: Guest on June-29-2020

Code answers related to "counting valleys hackerrank solution"

Python Answers by Framework

Browse Popular Code Answers by Language