LeetCode Daily Challenge#Day 02--Arrays (209,59)
📝 摘要:数组与双指针技巧 本次学习围绕双指针技巧在数组问题中的应用,重点解决了两道LeetCode题目: 209. 长度最小的子数组 使用滑动窗口(双指针变体)在O(n)时间内找到满足和≥target的最短连续子数组 关键:动态调整窗口边界,维护当前和与最小长度 59. 螺旋矩阵II 通过分层填充法生成n×n螺旋矩阵 按圈层循环处理,控制填充方向(右→下→左→上)和边界收缩 核心知识点: 双指
🗓️Day 02 – Array.
Topics: Array II
Problem solved:
#209 - Minimum Size Subarray Sum
#59 - Spiral Matrix II
文章目录
📚Concept Review – Two pointers
Two pointers is a powerful way to reduce time complexity from O(n2) to O(n) when working with linear structures like arrays, strings and linked lists.
When to use
- Sorted array or string. ---- if it mentions sorted array, we should think about two pointers. Such as Two Sum or Squares of a Sorted Array
- Subarray with special conditions (sliding window) ---- sliding window is a special two pointers. so if we want to get a special array or the result of a special array, we can use two pointers. Such as MInimum Size Subarray Sum or Longest Increasing Subsequence
- Traverse, merge or compare two sequences ---- Such as Merge Sorted Array
- Fast and slow pointers (Floyd cycle detection) ---- if we need to detect if there is a cycle in linked list.
- Problems involving shrinking a range from both sides ---- Parlindonme Number
🧩Problem solution – 209. Minimum Size Subarray Sum
💡Problem description
Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.
Example 1:
Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem constraint.
Example 2:
Input: target = 4, nums = [1,4,4]
Output: 1
Example 3:
Input: target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 0
-
Constraints:
-
1 <= target <= 10 9
1 <= nums.length <= 10 5
1 <= nums[i] <= 10 4
🧭Thought Process
This is a good example to use two pointers. If there is a such subarray, we use left and right to represent its beginning and end. At first, first and right are both at the beginning of nums. Then we move the right until the subarray fulfill the requirement. After that, move the left until the subarray fulfill the requirement. Repeat these two steps.
💻Code Implementation
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int start = 0, end = 0;
int len = nums.size();
int newlen = 0, minlen = 1e9, i = 0, sum = 0;
for (i = 0; i < len; i++) {
sum += nums[end];
newlen = end - start + 1;
while (sum >= target) {
newlen = end-start+1;
minlen = min(minlen, newlen);
sum -= nums[start];
start++;
}
end++;
}
return minlen == 1e9 ? 0 : minlen;
}
};
⏱️Complexity Analysis
- Time complexity: O(n)
- Space complexity: O(1)
🧩Problem solution – 59. Spiral Matrix II
💡Problem description
Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.
Example 1:
Input: n = 3
Output: [[1,2,3],[8,9,4],[7,6,5]]
Example 2:
Input: n = 1
Output: [[1]]
-
Constraints:
-
0 <= nums.length <= 100
0 <= nums[i] <= 50
0 <= val <= 100
🧭Thought Process
Just follow the steps that mentioned.
💻Code Implementation
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> ans(n, vector<int>(n, 0));
int x = 0, y = 0;
int loop = n / 2;
int i = 0, j = 0;
int offset = 1, count = 1;
while (loop--) {
i = x;
j = y;
for (j; j < n - offset; j++) {
ans[i][j] = count;
count++;
}
for (i; i < n - offset; i++) {
ans[i][j] = count;
count++;
}
for (; j > y; j--) {
ans[i][j] = count;
count++;
}
for (; i > x; i--) {
ans[i][j] = count;
count++;
}
offset++;
x++;
y++;
}
if (n % 2 != 0) {
ans[n / 2][n / 2] = n * n;
}
return ans;
}
};
⏱️Complexity Analysis
- Time complexity: O(n)
- Space complexity: O(n2)
更多推荐



所有评论(0)