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
67 changes: 67 additions & 0 deletions first-last.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Time Complexity : O(logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :yes
// Any problem you faced while coding this :None

// Your code here along with comments explaining your approach in three sentences only
//find start, end of the index by doing binary searches separately
// do binary search to find the target


class Solution {
public int[] searchRange(int[] nums, int target) {
int low = 0;
int high = nums.length - 1;

while (low <= high) {
int mid = low + (high - low) / 2;


if (nums[mid] == target) {
int start = findStart(nums, target, low, high);
int end = findEnd(nums, target, low, high);
return new int[]{start, end};
} else if (nums[mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}

return new int[]{-1, -1};
}

private int findStart(int[] nums, int target, int low, int high) {
int pos = -1;

while (low <= high) {
int mid = low + (high - low) / 2;

if (nums[mid] >= target) {
if (nums[mid] == target) pos = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}

return pos;
}

private int findEnd(int[] nums, int target, int low, int high) {
int pos = -1;

while (low <= high) {
int mid = low + (high - low) / 2;

if (nums[mid] <= target) {
if (nums[mid] == target) pos = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}

return pos;
}
}
46 changes: 46 additions & 0 deletions first-last.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Time Complexity : O(logn)
# Space Complexity :O(1)
# Did this code successfully run on Leetcode :yes
# Any problem you faced while coding this :None

# Your code here along with comments explaining your approach in three sentences only
#find start, end of the index by doing binary searches separately
# do binary search to find the target

class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
low = 0
high = len(nums)-1
start = self.findStart(nums, low, high, target)
if start == -1:
return [-1,-1]
end = self.findEnd(nums, low,high,target)
return [start,end]
def findStart(self, nums, low, high, target):
while low <= high:
mid = (low + high)#2
if nums[mid] == target:
if mid == 0 or nums[mid-1] < nums[mid]:
return mid
else:
high = mid -1
elif nums[mid] > target:
high = mid -1
else:
low = mid + 1
return -1
def findEnd(self, nums, low, high, target):
while low <= high:
mid = (low + high)#2
if nums[mid] == target:
if mid == len(nums)-1 or nums[mid] < nums[mid+1]:
return mid
else:
low = mid + 1
elif nums[mid] > target:
high = mid -1
else:
low = mid + 1
return -1


33 changes: 33 additions & 0 deletions min-element.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Time Complexity : O(logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :yes
// Any problem you faced while coding this :None

// Your code here along with comments explaining your approach in three sentences only
// do binary search to find min element
// the main condition is if mid < mid-1 and mid < mid + 1
// check which side is sorted and move the opposite side of the sorted part as the min lie there

class Solution {
public int findMin(int[] nums) {
int n = nums.length-1;
int low = 0;
int high = n;
while (low<= high){
if (nums[low]<= nums[high]){
return nums[low];
}
int mid = low + (high-low)/2;
if((mid == 0 || nums[mid] < nums[mid-1]) && (mid == n || nums[mid] < nums[mid+1])){
return nums[mid];
}
else if (nums[low] <= nums[mid]){
low = mid + 1;
}else{
high = mid -1;
}
}
return 77777;

}
}
28 changes: 28 additions & 0 deletions min-element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Time Complexity : O(logn)
# Space Complexity :O(1)
# Did this code successfully run on Leetcode :yes
# Any problem you faced while coding this :None

# Your code here along with comments explaining your approach in three sentences only
# do binary search to find min element
# the main condition is if mid < mid-1 and mid < mid + 1
# check which side is sorted and move the opposite side of the sorted part as the min lie there

class Solution:
def findMin(self, nums: List[int]) -> int:
n = len(nums)-1
low = 0
high = n
while (low<= high):
if (nums[low]<= nums[high]):
return nums[low]
mid = (high+low)#2
if((mid == 0 or nums[mid] <= nums[mid-1])and(mid == n or nums[mid] <= nums[mid+1])):
return nums[mid]
elif (nums[low] <= nums[mid]):
low = mid + 1
else:
high = mid -1
return 77777


28 changes: 28 additions & 0 deletions peak-element.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Time Complexity : O(logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :yes
// Any problem you faced while coding this :None

// Your code here along with comments explaining your approach in three sentences only
//do binary search and move towards the side where the highest element lies
//if mid is the highest element then return mid

class Solution {
public int findPeakElement(int[] nums) {
int low = 0;
int high = nums.length-1;
while (low <= high){
int mid = low + (high-low)/2;
if ((mid== 0 || nums[mid]> nums[mid -1]) && (mid == nums.length-1 || nums[mid]> nums[mid + 1])){
return mid;
}
else if (nums[mid + 1] > nums[mid]){
low = mid + 1;
}else{
high = mid -1;
}
}
return -1;

}
}
25 changes: 25 additions & 0 deletions peak-element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

# Time Complexity : O(logn)
# Space Complexity :O(1)
# Did this code successfully run on Leetcode :yes
# Any problem you faced while coding this :None

# Your code here along with comments explaining your approach in three sentences only
#do binary search and move towards the side where the highest element lies
#if mid is the highest element then return mid

class Solution:
def findPeakElement(self, nums: List[int]) -> int:
low = 0
high = len(nums)-1
while (low <= high):
mid = low + (high-low)#2
if ((mid== 0 or nums[mid]> nums[mid -1]) and (mid == len(nums)-1 or nums[mid]> nums[mid + 1])):
return mid
elif (nums[mid + 1] > nums[mid]):
low = mid + 1
else:
high = mid -1
return -1