2420. Find All Good Indices LeetCode Solution
In this guide, you will get 2420. Find All Good Indices LeetCode Solution with the best time and space complexity. The solution to Find All Good Indices problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find All Good Indices solution in C++
- Find All Good Indices solution in Java
- Find All Good Indices solution in Python
- Additional Resources
Problem Statement of Find All Good Indices
You are given a 0-indexed integer array nums of size n and a positive integer k.
We call an index i in the range k <= i < n – k good if the following conditions are satisfied:
The k elements that are just before the index i are in non-increasing order.
The k elements that are just after the index i are in non-decreasing order.
Return an array of all good indices sorted in increasing order.
Example 1:
Input: nums = [2,1,1,1,3,4,1], k = 2
Output: [2,3]
Explanation: There are two good indices in the array:
– Index 2. The subarray [2,1] is in non-increasing order, and the subarray [1,3] is in non-decreasing order.
– Index 3. The subarray [1,1] is in non-increasing order, and the subarray [3,4] is in non-decreasing order.
Note that the index 4 is not good because [4,1] is not non-decreasing.
Example 2:
Input: nums = [2,1,1,2], k = 2
Output: []
Explanation: There are no good indices in this array.
Constraints:
n == nums.length
3 <= n <= 105
1 <= nums[i] <= 106
1 <= k <= n / 2
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2420. Find All Good Indices LeetCode Solution in C++
class Solution {
public:
// Same as 2100. Find Good Days to Rob the Bank
vector<int> goodIndices(vector<int>& nums, int k) {
const int n = nums.size();
vector<int> ans;
// dec[i] := 1 + the number of continuous decreasing numbers before i
vector<int> dec(n, 1);
// inc[i] := 1 + the number of continuous increasing numbers after i
vector<int> inc(n, 1);
for (int i = 1; i < n; ++i)
if (nums[i - 1] >= nums[i])
dec[i] = dec[i - 1] + 1;
for (int i = n - 2; i >= 0; --i)
if (nums[i] <= nums[i + 1])
inc[i] = inc[i + 1] + 1;
for (int i = k; i < n - k; ++i)
if (dec[i - 1] >= k && inc[i + 1] >= k)
ans.push_back(i);
return ans;
}
};
/* code provided by PROGIEZ */
2420. Find All Good Indices LeetCode Solution in Java
class Solution {
// Same as 2100. Find Good Days to Rob the Bank
public List<Integer> goodIndices(int[] nums, int k) {
final int n = nums.length;
List<Integer> ans = new ArrayList<>();
// dec[i] := 1 + the number of continuous decreasing numbers before i
int[] dec = new int[n];
// inc[i] := 1 + the number of continuous increasing numbers after i
int[] inc = new int[n];
Arrays.fill(dec, 1);
Arrays.fill(inc, 1);
for (int i = 1; i < n; ++i)
if (nums[i - 1] >= nums[i])
dec[i] = dec[i - 1] + 1;
for (int i = n - 2; i >= 0; --i)
if (nums[i] <= nums[i + 1])
inc[i] = inc[i + 1] + 1;
for (int i = k; i < n - k; ++i)
if (dec[i - 1] >= k && inc[i + 1] >= k)
ans.add(i);
return ans;
}
}
// code provided by PROGIEZ
2420. Find All Good Indices LeetCode Solution in Python
class Solution:
# Same as 2100. Find Good Days to Rob the Bank
def goodIndices(self, nums: list[int], k: int) -> list[int]:
n = len(nums)
dec = [1] * n # 1 + the number of continuous decreasing numbers before i
inc = [1] * n # 1 + the number of continuous increasing numbers after i
for i in range(1, n):
if nums[i - 1] >= nums[i]:
dec[i] = dec[i - 1] + 1
for i in range(n - 2, -1, -1):
if nums[i] <= nums[i + 1]:
inc[i] = inc[i + 1] + 1
return [i for i in range(k, n - k)
if dec[i - 1] >= k and inc[i + 1] >= k]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.