Skip to content
Open
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
101 changes: 100 additions & 1 deletion Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,103 @@
// Any problem you faced while coding this :


// Your code here along with comments explaining your approach
// Your code here along with comments explaining your approach

public class Sample {
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
public void sortColors(int[] nums) {
int low = 0, mid = 0, high = nums.length - 1;
while (mid <= high) {
if (nums[mid] == 0) {
// swap
int temp = nums[low];
nums[low] = nums[mid];
nums[mid] = temp;
mid++;
low++;

} else if (nums[mid] == 1) {
mid++;
} else {
int temp = nums[high];
nums[high] = nums[mid];
nums[mid] = temp;
high--;
}
}
}
// Time Complexity : O(n^2)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes

public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);

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

if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}

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

while (left < right) {
int sum = nums[i] + nums[left] + nums[right];

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

while (left < right && nums[left] == nums[left + 1]) {
left++;
}

while (left < right && nums[right] == nums[right - 1]) {
right--;
}

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

return result;
}

// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
public int maxArea(int[] height) {
int left = 0;
int right = height.length - 1;
int max = 0;

while (left < right) {
int width = right - left;
int h = Math.min(height[left], height[right]);
int area = width * h;

max = Math.max(max, area);

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

return maxArea;
}


}