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
31 changes: 31 additions & 0 deletions Problem34.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach
class Solution {
public void sortColors(int[] nums) {
int low = 0; int mid = 0; int high = nums.length-1;

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

private void swap(int[] nums, int x, int y) {
int temp = nums[x];
nums[x] = nums[y];
nums[y] = temp;
}
}
38 changes: 38 additions & 0 deletions Problem35.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Time Complexity : O(n^2)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
List<List<Integer>> result = new ArrayList<>();

for (int i = 0; i < n; i++) {
if (i != 0 && nums[i] == nums[i-1]) continue;
if (nums[0] > 0) break;

int low = i+1; int high = n-1;

while(low < high) {
int sum = nums[i] + nums[low] + nums[high];
if (sum == 0) {
result.add(Arrays.asList(nums[i], nums[low], nums[high]));
low++;
high--;
while(low < high && nums[low] == nums[low-1]) low++;
while(low < high && nums[high] == nums[high+1]) high--;
} else if (sum > 0) {
high--;
} else {
low++;
}
}
}

return result;
}
}
32 changes: 32 additions & 0 deletions Problem36.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


// Your code here along with comments explaining your approach
class Solution {
public int maxArea(int[] height) {
int n = height.length;

int low = 0; int high = n-1;
int area = 0;

while(low < high) {
int h = 0;
int w = high - low;

if (height[low] < height[high]) {
h = height[low];
low++;
} else {
h = height[high];
high--;
}

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

return area;
}
}
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.