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
26 changes: 26 additions & 0 deletions FindPeakElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Time Complexity: O(log n)
// Space Complexity: O(1)

// 1: To find the peak element, we need to compare the mid element with its left and right elements
// 2: We will try to expand the range of search to the area where the element is larger
// 3: L#15 covers cases where mid is the first or last element
class Solution {
public int findPeakElement(int[] nums) {
int n = nums.length;
int low = 0;
int high = n - 1;

while (low <= high) {
int mid = low + (high - low) / 2;
if ((mid == 0 || nums[mid] > nums[mid - 1])
&& (mid == n - 1 || nums[mid] > nums[mid + 1])) {
return mid;
} else if (nums[mid] < nums[mid + 1]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
}
58 changes: 58 additions & 0 deletions FirstLastPositionInSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Time Complexity: O(log n)
// Space Complexity: O(1)

// 1: We perform binary search twice to find both the first and last index
// 2: To find the first index, we move the high pointer to mid-1 and then continue binary search as usual
// 3: To find the last index, the move the low pointer to mid+1 before continuing binary search
class Solution {
public int[] searchRange(int[] nums, int target) {
int size = nums.length - 1;
int first = binarySearchFirst(nums, target, 0, size);

if (first == -1) return new int[]{-1, -1};

int last = binarySearchLast(nums, target, first, nums.length - 1);

return new int[]{first, last};
}

private int binarySearchFirst(int[] nums, int target, int low, int high) {

while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[mid] == target) {
if (mid == 0 || nums[mid - 1] != target) {
return mid;
} else {
high = mid - 1;
}
} else if (nums[mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}

return -1;
}

private int binarySearchLast(int[] nums, int target, int low, int high) {

while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[mid] == target) {
if (mid == size || nums[mid + 1] != target) {
return mid;
} else {
low = mid + 1;
}
} else if (nums[mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}

return -1;
}
}
25 changes: 25 additions & 0 deletions MinimumInRotatedSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Time Complexity: O(log n)
// Space Complexity: O(1)

// 1: We use Binary Search to find the minimum integer
// 2: We maintain a minimum value that is recalculated each time we change the range of the array
// 3: Once the condition on L#12 no longer holds true, we return the minimum value found
class Solution {
public int findMin(int[] nums) {
int low = 0;
int high = nums.length - 1;
int min = Integer.MAX_VALUE;
while(low <= high){
int mid = (low + high)/2;
if(nums[low] <= nums[mid]){
min = Math.min(min, nums[low]);
low = mid + 1;
}
else{
min = Math.min(min, nums[mid]);
high = mid - 1;
}
}
return min;
}
}