| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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 | | |
| | | 12 | | namespace LeetCode.Algorithms.CountHillsAndValleysInAnArray; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed 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) |
| | 2 | 24 | | { |
| | 2 | 25 | | var count = 0; |
| | | 26 | | |
| | 20 | 27 | | for (var i = 1; i < nums.Length - 1; i++) |
| | 8 | 28 | | { |
| | 8 | 29 | | if (nums[i] == nums[i - 1]) |
| | 3 | 30 | | { |
| | 3 | 31 | | continue; |
| | | 32 | | } |
| | | 33 | | |
| | 5 | 34 | | var left = 0; |
| | | 35 | | |
| | 10 | 36 | | for (var j = i - 1; j >= 0; --j) |
| | 5 | 37 | | { |
| | 5 | 38 | | if (nums[j] > nums[i]) |
| | 3 | 39 | | { |
| | 3 | 40 | | left = 1; |
| | | 41 | | |
| | 3 | 42 | | break; |
| | | 43 | | } |
| | | 44 | | |
| | 2 | 45 | | if (nums[j] >= nums[i]) |
| | 0 | 46 | | { |
| | 0 | 47 | | continue; |
| | | 48 | | } |
| | | 49 | | |
| | 2 | 50 | | left = -1; |
| | | 51 | | |
| | 2 | 52 | | break; |
| | | 53 | | } |
| | | 54 | | |
| | 5 | 55 | | var right = 0; |
| | | 56 | | |
| | 14 | 57 | | for (var j = i + 1; j < nums.Length; ++j) |
| | 7 | 58 | | { |
| | 7 | 59 | | if (nums[j] > nums[i]) |
| | 1 | 60 | | { |
| | 1 | 61 | | right = 1; |
| | | 62 | | |
| | 1 | 63 | | break; |
| | | 64 | | } |
| | | 65 | | |
| | 6 | 66 | | if (nums[j] >= nums[i]) |
| | 2 | 67 | | { |
| | 2 | 68 | | continue; |
| | | 69 | | } |
| | | 70 | | |
| | 4 | 71 | | right = -1; |
| | | 72 | | |
| | 4 | 73 | | break; |
| | | 74 | | } |
| | | 75 | | |
| | 5 | 76 | | if (left == right && left != 0) |
| | 3 | 77 | | { |
| | 3 | 78 | | count++; |
| | 3 | 79 | | } |
| | 5 | 80 | | } |
| | | 81 | | |
| | 2 | 82 | | return count; |
| | 2 | 83 | | } |
| | | 84 | | } |