977. Squares of a Sorted Array
Problem Link on LeetCode
Approach: Two Pointers Technique Since the array may contain negative numbers, we use two pointers to compare the squares of the elements from both ends. The left pointer starts at the beginning of the array, and the right pointer starts at the end. The larger square is placed at the current end of the result array.
Note: Proper way to declare arrays in Java:
int[] result = new int[size];
Java Implementation:
class Solution {
public int[] sortedSquares(int[] nums) {
int length = nums.length;
int[] result = new int[length];
int left = 0, right = length - 1;
int index = length - 1;
while (index >= 0) {
int leftSquare = nums[left] * nums[left];
int rightSquare = nums[right] * nums[right];
if (leftSquare > rightSquare) {
result[index] = leftSquare;
left++;
} else {
result[index] = rightSquare;
right--;
}
index--;
}
return result;
}
}
Complexity:
- Time: O(n)
- Space: O(n)
209. Minimum Size Subarray Sum
Problem Link on LeetCode
Approach: Sliding Window (Two Pointers) Use a for loop to move the right pointer and a while loop to adjust the left pointer. This creates a window where we accumulate the sum of elements and adjust the window size to find the minimum length subarray that meets the sum condition.
Notes:
- Use a while loop for the left pointer to ensure multiple adjustments if needed.
- Remember to subtract the element at the left pointer from the sum before moving the pointer.
- Use
Integer.MAX_VALUEandMath.min()correct.
Java Implementation:
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int left = 0, sum = 0, minLength = Integer.MAX_VALUE;
for (int right = 0; right < nums.length; right++) {
sum += nums[right];
while (sum >= target) {
minLength = Math.min(minLength, right - left + 1);
sum -= nums[left];
left++;
}
}
return minLength == Integer.MAX_VALUE ? 0 : minLength;
}
}
Complexiyt:
- Time: O(n) (each element is visited at most twice)
- Space: O(1)
59. Spiral Matrix II
Problem Link on LeetCode
Approach: Matrix Construction with Loop Invariant The matrix is filled layer by layer. Each layer consists of four edges, and each edge is filled with a consistent strategy (left-closed, right-open interval). For odd-sized matrices, the center element is filled separately after the main loop.
Notes:
- Handle the case where
nis odd separately. - Update the offset correctly after each loop iteration.
Java Implementation:
class Solution {
public int[][] generateMatrix(int n) {
int[][] matrix = new int[n][n];
int startRow = 0, startCol = 0;
int offset = 1;
int value = 1;
while (value < n * n) {
// Top row
for (int col = startCol; col < n - offset; col++) {
matrix[startRow][col] = value++;
}
// Right column
for (int row = startRow; row < n - offset; row++) {
matrix[row][n - offset] = value++;
}
// Bottom row
for (int col = n - offset; col > startCol; col--) {
matrix[n - offset][col] = value++;
}
// Left column
for (int row = n - offset; row > startRow; row--) {
matrix[row][startCol] = value++;
}
startRow++;
startCol++;
offset++;
}
if (n % 2 == 1) {
matrix[startRow][startCol] = value;
}
return matrix;
}
}
Complexity:
- Time: O(n²)
- Space: O(n²)