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 Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
75. Sort Colors
Ran on leetcode: Yes
TC: O(n) SC: O(1)
Use 3 pointers, low and mid on first element of array and high on last element of array, traverse mid through array and if element on mid is 0 swap it with low and move low and mid pointer, if element is 2 swap between mid and high and move high pointer and if element is 1 it is already in correct position and move the mid pointer
*/
class Solution {
public void sortColors(int[] nums) {
int low = 0, mid = 0, high = nums.length - 1; // Divide nums array in 3 sections

while(mid <= high){
if(nums[mid] == 0){ // Move the element with 0 value to low section
swap(nums, low, mid); // swap the elements
mid++; // Move mid, cauz the element is already at correct position, because we have seen it already
low++; // Move low pointer, to place next 0 value element
} else if(nums[mid] == 1){ // Correct position, move mid
mid++;
} else if(nums[mid] == 2){ // Move the element with 2 value to high section
swap(nums, high, mid); // swap the elements
high--; // Move high pointer, to place next 2 value element
}
}

}

private void swap(int[] nums, int i, int j){ // swap elements on index i and j
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
57 changes: 57 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
15. 3Sum
Ran on leetcode: Yes
TC: O(n^2) SC: O(1)
Sort the array, fix one pointer and apply two sum on the subarray to find the target. In two sum if the sum > 0 then move the second pointer to get lower value and if sum < 0 move the first pointer to get higher value item. Increment fixed pointer and apply two sum again.
*/
class Solution {
public List<List<Integer>> threeSum(int[] nums) {

List<List<Integer>> result = new ArrayList<>();

// Sort, to use two pointers to find the target
Arrays.sort(nums);

// Fix first pointer and two pointers on rest of array
int low = 1;
int high = nums.length - 1;

for (int i = 0; i < nums.length; i++) { // Fix one element
if (i != 0 && nums[i] == nums[i - 1]){
continue;
}
if(nums[i] > 0) break; // Early termination if the element is positive, cauz the other two elements will be also positive and never sum to 0
low = i + 1; // First pointer on the subarray
high = nums.length - 1; // Second pointer on last element
while (low < high) { // Find the target based on rest of the subarray
int sum = nums[low] + nums[high] + nums[i];
if (sum == 0) { // If target is found add to result and move both pointers
result.add(List.of(
nums[low],
nums[high],
nums[i]));
low++;
high--;
while (low < high && nums[high] == nums[high + 1]) { // Skip duplicates
high--;
}
while (low < high && nums[low] == nums[low - 1]) { // Skip duplicates
low++;
}
} else if (sum > 0) {
high--; // If sum is more than target, move pointer to get lower value
while (low < high && nums[high] == nums[high + 1]) { // Skip duplicates
high--;
}
} else {
low++; // If sum is less than target, move pointer to get higher value
while (low < high && nums[low] == nums[low - 1]) { // Skip duplicates
low++;
}
}
}
}

return result;
}
}
30 changes: 30 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
11. Container With Most Water
Ran on leetcode: Yes
TC: O(n) SC: O(1)
Start with max width, low pointer on first element and high pointer on last element, if height on low pointer is smaller than on high pointer move the low pointer and vice versa, since height is the limiting factor and we already have the max width. At each point calculate area and update max if necessary.
*/
class Solution {
public int maxArea(int[] height) {
// Set to max width
int low = 0, high = height.length - 1;

int area = Integer.MIN_VALUE;

while(low < high){

int width = high - low; // Find the distance
int minHeight = Math.min(height[low], height[high]); // Find the limiting height
area = Math.max(area, width * minHeight); // Store area, if it is new max

if(height[low] < height[high]){ // height on low index is limiting
low++;
} else { // height on high index is limiting
high--;
}

}

return area;
}
}