Posts

Showing posts from October, 2024

1671 - Minimum Number of Removals to Make Mountain Array

You may recall that an array  arr  is a  mountain array  if and only if: arr.length >= 3 There exists some index  i  ( 0-indexed ) with  0 < i < arr.length - 1  such that: arr[0] < arr[1] < ... < arr[i - 1] < arr[i] arr[i] > arr[i + 1] > ... > arr[arr.length - 1] Given an integer array  nums ​​​, return  the  minimum  number of elements to remove to make  nums ​​​   a  mountain array .   Example 1: Input: nums = [1,3,1] Output: 0 Explanation: The array itself is a mountain array so we do not need to remove any elements. Example 2: Input: nums = [2,1,1,5,6,2,3,1] Output: 3 Explanation: One solution is to remove the elements at indices 0, 1, and 5, making the array nums = [1,5,6,3,1].   Constraints: 3 <= nums.length <= 1000 1 <= nums[i] <= 10 9 It is guaranteed that you can make a mountain array out of  nums . C++ class Solution { public: int m...

2684. Maximum Number of Moves in a Grid

Image
You are given a  0-indexed   m x n  matrix  grid  consisting of  positive  integers. You can start at  any  cell in the first column of the matrix, and traverse the grid in the following way: From a cell  (row, col) , you can move to any of the cells:  (row - 1, col + 1) ,  (row, col + 1)  and  (row + 1, col + 1)  such that the value of the cell you move to, should be  strictly  bigger than the value of the current cell. Return  the  maximum  number of  moves  that you can perform.   Example 1: Input: grid = [[2,4,3,5],[5,4,9,3],[3,4,2,11],[10,9,13,15]] Output: 3 Explanation: We can start at the cell (0, 0) and make the following moves: - (0, 0) -> (0, 1). - (0, 1) -> (1, 2). - (1, 2) -> (2, 3). It can be shown that it is the maximum number of moves that can be made. Example 2: Input: grid = [[3,2,4],[2,1,9],[1,1,7]] Output: 0 Explanation: Starting from any ...

2501. Longest Square Streak in an Array

You are given an integer array  nums . A subsequence of  nums  is called a  square streak  if: The length of the subsequence is at least  2 , and after  sorting the subsequence, each element (except the first element) is the  square  of the previous number. Return  the length of the  longest square streak  in  nums , or return  -1  if there is no  square streak . A  subsequence  is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.   Example 1: Input: nums = [4,3,6,16,8,2] Output: 3 Explanation: Choose the subsequence [4,16,2]. After sorting it, it becomes [2,4,16]. - 4 = 2 * 2. - 16 = 4 * 4. Therefore, [4,16,2] is a square streak. It can be shown that every subsequence of length 4 is not a square streak. Example 2: Input: nums = [2,3,5,6,7] Output: -1 Explanation: There is no square streak in nums so retu...