You are given an array of integers nums
and an integer k
. Your task is to find the subarray of length k
that has the largest lexicographical order. Return this subarray as a new array.
Key constraints:
k
.
Lexicographical order means the subarray that would appear last if all possible subarrays of length k
were sorted like words in a dictionary.
At first glance, you might consider checking every possible subarray of length k
and comparing them to find the largest. However, this would be inefficient, especially for large arrays.
Instead, we realize that for each position in nums
where a subarray of length k
can start, we can compare the subarray starting at that position to the current largest found so far. Because subarrays are contiguous, and we want the one with the largest lexicographical order, we can take advantage of direct comparison between slices.
The key insight is that comparing two arrays lexicographically is similar to comparing words: you look at the first index where they differ, and the one with the larger value at that position is greater. Therefore, we can keep a variable to store the current "best" subarray and update it as we scan through nums
.
We can solve this problem efficiently by scanning through all possible starting indices for subarrays of length k
and keeping track of the largest one found so far.
max_subarray
) to store the current largest subarray found. You can start by taking the first subarray of length k
.
i
from 0
to len(nums) - k
:
nums[i:i+k]
.max_subarray
using lexicographical comparison. In most languages, comparing arrays directly works as expected.max_subarray
to this subarray.max_subarray
.
This approach is efficient because:
n - k + 1
subarrays.k
steps.
Input: nums = [1, 4, 5, 2, 3], k = 3
All possible subarrays of length 3:
max_subarray = [1, 4, 5]
.max_subarray
to [4, 5, 2].max_subarray
to [5, 2, 3].
Result: The largest subarray of length 3 is [5, 2, 3]
.
class Solution:
def largestSubarray(self, nums, k):
max_subarray = nums[:k]
for i in range(1, len(nums) - k + 1):
current = nums[i:i+k]
if current > max_subarray:
max_subarray = current
return max_subarray
class Solution {
public:
vector<int> largestSubarray(vector<int>& nums, int k) {
vector<int> max_subarray(nums.begin(), nums.begin() + k);
for (int i = 1; i <= nums.size() - k; ++i) {
vector<int> current(nums.begin() + i, nums.begin() + i + k);
if (current > max_subarray) {
max_subarray = current;
}
}
return max_subarray;
}
};
class Solution {
public int[] largestSubarray(int[] nums, int k) {
int n = nums.length;
int start = 0;
for (int i = 1; i <= n - k; ++i) {
for (int j = 0; j < k; ++j) {
if (nums[i + j] > nums[start + j]) {
start = i;
break;
} else if (nums[i + j] < nums[start + j]) {
break;
}
}
}
int[] res = new int[k];
System.arraycopy(nums, start, res, 0, k);
return res;
}
}
var largestSubarray = function(nums, k) {
let maxSubarray = nums.slice(0, k);
for (let i = 1; i <= nums.length - k; i++) {
let current = nums.slice(i, i + k);
for (let j = 0; j < k; j++) {
if (current[j] > maxSubarray[j]) {
maxSubarray = current;
break;
} else if (current[j] < maxSubarray[j]) {
break;
}
}
}
return maxSubarray;
};
Brute-force approach:
k
(there are n - k + 1
of them).O((n-k+1) \cdot k \log(n-k+1))
time and O((n-k+1) \cdot k)
space.n - k + 1
starting positions.k
elements.O((n - k + 1) \cdot k)
.O(k)
for the result.This is efficient enough for typical constraints in coding interviews and LeetCode problems.
To solve the "Largest Subarray Length K" problem, we scan through all possible subarrays of length k
and keep track of the one with the largest lexicographical order. The key insight is that comparing arrays lexicographically is efficient and can be done as we iterate, without storing all subarrays. This yields a clean and efficient solution that works well for both small and large inputs.