Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No, just need to handle mid pointer properly.


// Your code here along with comments explaining your approach in three sentences only
// I use three pointers low, mid, high to place 0s to the left and 2s to the right.
// If nums[mid] is 0, swap with low and move both; if it is 2, swap with high and move high only.
// If nums[mid] is 1, just move mid, and this finishes sorting in one pass.

class Solution {
public void sortColors(int[] nums) {

int low = 0, mid = 0;
int high = nums.length - 1;

while (mid <= high) {

if (nums[mid] == 0) {
swap(nums, low, mid);
low++;
mid++;
}
else if (nums[mid] == 2) {
swap(nums, mid, high);
high--;
}
else {
mid++;
}
}
}

private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
52 changes: 52 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Time Complexity : O(n^2)
// Space Complexity : O(1) (excluding output list)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Avoiding duplicates is the main tricky part.


// Your code here along with comments explaining your approach in three sentences only
// First I sort the array, then fix one number and use two pointers to find pairs that make sum zero.
// When the sum is too small I move left pointer, when too large I move right pointer, and when zero I store the triplet.
// I skip duplicates for i, left, and right so output does not repeat.

import java.util.*;

class Solution {
public List<List<Integer>> threeSum(int[] nums) {

Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();

for (int i = 0; i < nums.length; i++) {

if (i > 0 && nums[i] == nums[i - 1]) continue; // skip duplicate i

int left = i + 1;
int right = nums.length - 1;

while (left < right) {

int sum = nums[i] + nums[left] + nums[right];

if (sum == 0) {
res.add(Arrays.asList(nums[i], nums[left], nums[right]));
left++;
right--;

// skip duplicates on left
while (left < right && nums[left] == nums[left - 1]) left++;

// skip duplicates on right
while (left < right && nums[right] == nums[right + 1]) right--;

} else if (sum < 0) {
left++;
} else {
right--;
}
}
}

return res;
}
}
32 changes: 32 additions & 0 deletions problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No, just need to move the smaller height pointer.


// Your code here along with comments explaining your approach in three sentences only
// I use two pointers at start and end and calculate area using width and minimum height.
// To possibly get a bigger area, I always move the pointer with smaller height because that is the limiting factor.
// I keep updating max area until pointers cross.

class Solution {
public int maxArea(int[] height) {

int left = 0;
int right = height.length - 1;
int max = 0;

while (left < right) {

int h = Math.min(height[left], height[right]);
int w = right - left;

max = Math.max(max, h * w);

if (height[left] < height[right]) left++;
else right--;
}

return max;
}
}