Counting Valleys

  • + 0 comments
    func countingValleys(steps int32, path string) int32 {
        stepsCount := 0
        valleys := 0
        seaLevel := 0
        for _, char := range path {
            if char == 'U' {
                stepsCount++
            } else if char == 'D' {
                if stepsCount == seaLevel {
                    valleys++
                }
                stepsCount--
            }
        }
    
        return int32(valleys)
    
    }