leetcode

Leetcode problem solutions
git clone https://git.sinitax.com/sinitax/leetcode
Log | Files | Refs | sfeed.txt

3254-find-the-power-of-k-size-subarrays-i.rs (2388B)


      1/// 3254. Find the Power of K-Size Subarrays I (Medium)
      2///
      3/// You are given an array of integers nums of length n and a positive integer
      4/// k.
      5///
      6/// The *power* of an array is defined as:
      7///
      8///   * Its *maximum* element if all of its elements are *consecutive* and
      9///   *sorted* in *ascending* order.
     10///   * -1 otherwise.
     11///
     12/// You need to find the *power* of all subarrays of nums of size k.
     13///
     14/// Return an integer array results of size n - k + 1, where results[i] is the
     15/// power of nums[i..(i + k - 1)].
     16///
     17/// *Example 1:*
     18///
     19///     *Input:* nums = [1,2,3,4,3,2,5], k = 3
     20///
     21///   *Output:* [3,4,-1,-1,-1]
     22///
     23///   *Explanation:*
     24///
     25///   There are 5 subarrays of nums of size 3:
     26///
     27///     * [1, 2, 3] with the maximum element 3.
     28///     * [2, 3, 4] with the maximum element 4.
     29///     * [3, 4, 3] whose elements are *not* consecutive.
     30///     * [4, 3, 2] whose elements are *not* sorted.
     31///     * [3, 2, 5] whose elements are *not* consecutive.
     32///
     33/// *Example 2:*
     34///
     35///     *Input:* nums = [2,2,2,2,2], k = 4
     36///
     37///   *Output:* [-1,-1]
     38///
     39/// *Example 3:*
     40///
     41///     *Input:* nums = [3,2,3,2,3,2], k = 2
     42///
     43///   *Output:* [-1,3,-1,3,-1]
     44///
     45/// *Constraints:*
     46///
     47///   * 1 <= n == nums.length <= 500
     48///   * 1 <= nums[i] <= 105
     49///   * 1 <= k <= n
     50///
     51use leetcode::*;
     52
     53struct Solution {}
     54
     55impl Solution {
     56    pub fn results_array(nums: Vec<i32>, k: i32) -> Vec<i32> {
     57        let k = k as usize;
     58        let mut start = 0usize;
     59        let mut answer = vec![0; nums.len() - k + 1];
     60        for i in 0..nums.len() {
     61            if i > 0 && nums[i] != nums[i - 1] + 1 {
     62                start = i;
     63            }
     64            if i + 1 >= start + k {
     65                answer[i + 1 - k] = nums[i];
     66            } else if i + 1 >= k {
     67                answer[i + 1 - k] = -1;
     68            }
     69        }
     70        answer
     71    }
     72}
     73
     74pub fn main() {
     75    println!("{:?}", Solution::results_array(arg_into(1), arg_into(2)));
     76}
     77
     78#[cfg(test)]
     79mod tests {
     80    use crate::Solution;
     81    use leetcode::vi;
     82
     83    #[test]
     84    fn test() {
     85        assert_eq!(
     86            Solution::results_array(vi("[1,2,3,4,3,2,5]"), 3),
     87            vi("[3,4,-1,-1,-1]")
     88        );
     89        assert_eq!(Solution::results_array(vi("[2,2,2,2,2]"), 4), vi("[-1,-1]"));
     90        assert_eq!(
     91            Solution::results_array(vi("[3,2,3,2,3,2]"), 2),
     92            vi("[-1,3,-1,3,-1]")
     93        );
     94    }
     95}