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
37 changes: 37 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
1// 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
/* we can count the number of 0s,1s and 2s and just set them in the array which works but that would be pass through the array twice
I have proceeded with two point approach along with an additional pointer as marker for tracking the mid value.
*/
class Solution {
public void sortColors(int[] nums) {
//two pointer
int l=0, mid =0;//left for tracking 0s and mid for tracking 1s
int r = nums.length-1;// right for tracking 2s
while(mid<=r){
if(nums[mid]==2){
swap(nums, mid, r);//move the 2s to right
r--;//reduce right by 1 since we know 2 is in the end
}
else if(nums[mid]==0){
swap(nums, l, mid);//move 0 to the front
l++;//move left since we know 0 is in the front
mid++;
}
else{
mid++;//since the number is 1 do nothing as 1 is in mid
}
}
}

public void swap(int []nums, int x, int y){
int temp = nums[x];
nums[x]=nums[y];
nums[y]=temp;
}
}
50 changes: 50 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Time Complexity :O(n2)
// 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
/*
we can implement by selecting a pivot element and sum other two elements which would bring total sum to 0
the other two elements can be identified wither by
1. two sum problem - this will need a additional hashset of triplets to avoid duplicate triplets
time - O(n2)
space - O(n)
2. binary search
time - O(n2logn)
3.we can use two pointers
time- O(n2)
space - O(1)
*/
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result= new ArrayList<>();
Arrays.sort(nums);// sort because we want to implement two pointer
for(int i=0;i<nums.length;i++){
if(nums[i]>0) break;//we dont want to iterate if one element all the elements after this is >0 because sum is >0
if(i>0 && nums[i]==nums[i-1]) continue;//remove external duplicate triplets
int l=i+1;
int r=nums.length-1;
//two pointer for remaining elements after the pivot
while(l<r){
if(nums[l]+nums[r]+nums[i]==0){
List<Integer> li= Arrays.asList(nums[l],nums[r],nums[i]);
result.add(li);
l++;
r--;
//remove interval duplicate elements
while(l<r && nums[l]==nums[l-1]) l++;
while(l<r && nums[r]==nums[r+1]) r--;
}
else if(nums[l]+nums[r]+nums[i]<0){
l++;
}
else{
r--;
}
}
}
return result;
}
}
31 changes: 31 additions & 0 deletions Problem3.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
/* use two pointer approach to find tha maximum area of the rectangle
height being value and width being difference between indexes
time-O(n)
space- O(1)*/

class Solution {
public int maxArea(int[] height) {
int l=0;
int m=0;
int a=0;
int r=height.length-1;
while(l<r){
//find maximum area
a=Math.max(Math.min(height[l], height[r])*(r-l),a);
if(height[l]<height[r]){
l++;
}
else{
r--;
}
}
return a;
}
}
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.