2831. Find the Longest Equal Subarray Medium
1/**
2 * [2831] Find the Longest Equal Subarray
3 *
4 * You are given a 0-indexed integer array nums and an integer k.
5 * A subarray is called equal if all of its elements are equal. Note that the empty subarray is an equal subarray.
6 * Return the length of the longest possible equal subarray after deleting at most k elements from nums.
7 * A subarray is a contiguous, possibly empty sequence of elements within an array.
8 *
9 * <strong class="example">Example 1:
10 *
11 * Input: nums = [1,3,2,3,1,3], k = 3
12 * Output: 3
13 * Explanation: It's optimal to delete the elements at index 2 and index 4.
14 * After deleting them, nums becomes equal to [1, 3, 3, 3].
15 * The longest equal subarray starts at i = 1 and ends at j = 3 with length equal to 3.
16 * It can be proven that no longer equal subarrays can be created.
17 *
18 * <strong class="example">Example 2:
19 *
20 * Input: nums = [1,1,2,2,1,1], k = 2
21 * Output: 4
22 * Explanation: It's optimal to delete the elements at index 2 and index 3.
23 * After deleting them, nums becomes equal to [1, 1, 1, 1].
24 * The array itself is an equal subarray, so the answer is 4.
25 * It can be proven that no longer equal subarrays can be created.
26 *
27 *
28 * Constraints:
29 *
30 * 1 <= nums.length <= 10^5
31 * 1 <= nums[i] <= nums.length
32 * 0 <= k <= nums.length
33 *
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/find-the-longest-equal-subarray/
38// discuss: https://leetcode.com/problems/find-the-longest-equal-subarray/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43 pub fn longest_equal_subarray(nums: Vec<i32>, k: i32) -> i32 {
44 0
45 }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_2831() {
56 }
57}
58
Back
© 2025 bowen.ge All Rights Reserved.