From 35e7b5b1dd0aacb503da1cd66b233f80436f0291 Mon Sep 17 00:00:00 2001 From: kamalelhaddad Date: Sat, 23 Oct 2021 14:47:29 +0200 Subject: [PATCH] Update Solution.cs less coding --- .../Counting Valleys/Solution.cs | 36 +++++++------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/Algorithms/Implementation/Counting Valleys/Solution.cs b/Algorithms/Implementation/Counting Valleys/Solution.cs index 0ecb8f6..94e2923 100644 --- a/Algorithms/Implementation/Counting Valleys/Solution.cs +++ b/Algorithms/Implementation/Counting Valleys/Solution.cs @@ -30,33 +30,21 @@ class Solution static void Main(String[] args) { var seaLevel = 0; - var valleyCount = 0; + var valleysCount = 0; var totalNumberOfSteps = int.Parse(Console.ReadLine()); var garyStepRecord = Console.ReadLine().ToArray(); - var isValleyActive = false; - for (int i = 0; i < totalNumberOfSteps; i++) - { - if (garyStepRecord[i] == 'U') - { - seaLevel++; + foreach (var c in garyStepRecord) { + if (c == 'U') { + seaLevel++; + } + else { + seaLevel--; + if (seaLevel == -1) { + valleysCount++; + } + } } - else - { - seaLevel--; - } - - if (!isValleyActive && seaLevel < 0) - { - isValleyActive = true; - } - - if (isValleyActive && seaLevel == 0) - { - valleyCount++; - isValleyActive = false; - } - } - Console.WriteLine(valleyCount); + Console.WriteLine(valleysCount); } }