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
33 changes: 33 additions & 0 deletions 3SumProblem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<>();
List<Integer> list = new ArrayList<>();
for(int i=0;i<nums.length-2;++i){
if(i!=0 && nums[i]==nums[i-1]) continue;
if(nums[i]>0) continue;
int target = nums[i];
int left = i+1;
int right = nums.length-1;
while(left<right){
int sum = nums[left]+nums[right]+target;
if(sum==0){
result.add(Arrays.asList(nums[left],nums[right],target));
left++;
right--;
while(left<right && nums[left]==nums[left-1]){
left++;
}
while(left<right && nums[right]==nums[right+1]){
right--;
}
}else if(sum>0){
right--;
}else{
left++;
}
}
}
return result;
}
}
19 changes: 19 additions & 0 deletions ContainerWaterProblem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int maxArea(int[] height) {
int left = 0;
int right = height.length-1;
int max = 0;
while(left<right){
int width = right-left;
int heightOfLines = Math.min(height[left],height[right]);
int area = width * heightOfLines;
max = Math.max(max,area);
if(height[left]<height[right]){
left++;
}else{
right--;
}
}
return max;
}
}
27 changes: 26 additions & 1 deletion Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,29 @@
// 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

class Solution {
public void sortColors(int[] nums) {
if(nums.length==1) return;
int i = 0;
int mid = 0;
int k = nums.length-1;
while(mid<=k){
if(nums[mid]==2){
swap(nums,mid,k);
k--;
}else if(nums[mid]==0){
swap(nums,mid,i);
i++; mid++;
}else{
mid++;
}
}
}
void swap(int[] nums,int i,int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}