< Summary

Information
Class: LeetCode.Algorithms.CountHillsAndValleysInAnArray.CountHillsAndValleysInAnArrayBruteForce
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountHillsAndValleysInAnArray\CountHillsAndValleysInAnArrayBruteForce.cs
Line coverage
94%
Covered lines: 36
Uncovered lines: 2
Coverable lines: 38
Total lines: 84
Line coverage: 94.7%
Branch coverage
95%
Covered branches: 19
Total branches: 20
Branch coverage: 95%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CountHillValley(...)95%202094.73%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountHillsAndValleysInAnArray\CountHillsAndValleysInAnArrayBruteForce.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.CountHillsAndValleysInAnArray;
 13
 14/// <inheritdoc />
 15public class CountHillsAndValleysInAnArrayBruteForce : ICountHillsAndValleysInAnArray
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n^2)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <returns></returns>
 23    public int CountHillValley(int[] nums)
 224    {
 225        var count = 0;
 26
 2027        for (var i = 1; i < nums.Length - 1; i++)
 828        {
 829            if (nums[i] == nums[i - 1])
 330            {
 331                continue;
 32            }
 33
 534            var left = 0;
 35
 1036            for (var j = i - 1; j >= 0; --j)
 537            {
 538                if (nums[j] > nums[i])
 339                {
 340                    left = 1;
 41
 342                    break;
 43                }
 44
 245                if (nums[j] >= nums[i])
 046                {
 047                    continue;
 48                }
 49
 250                left = -1;
 51
 252                break;
 53            }
 54
 555            var right = 0;
 56
 1457            for (var j = i + 1; j < nums.Length; ++j)
 758            {
 759                if (nums[j] > nums[i])
 160                {
 161                    right = 1;
 62
 163                    break;
 64                }
 65
 666                if (nums[j] >= nums[i])
 267                {
 268                    continue;
 69                }
 70
 471                right = -1;
 72
 473                break;
 74            }
 75
 576            if (left == right && left != 0)
 377            {
 378                count++;
 379            }
 580        }
 81
 282        return count;
 283    }
 84}

Methods/Properties

CountHillValley(System.Int32[])