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
73 changes: 73 additions & 0 deletions leetcode153.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 8 15:37:19 2026

@author: rishigoswamy

Problem:
----------
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/

Approach:
----------
The array is originally sorted in ascending order and then rotated at
an unknown pivot, which causes the minimum element to be the rotation
point.

If the array is already sorted (nums[low] < nums[high]), the first
element is the minimum and can be returned immediately.

Otherwise, we use binary search to locate the pivot by checking the
relationship between nums[mid] and its neighboring elements.
If nums[mid] is smaller than nums[mid - 1], it is the minimum;
if nums[mid + 1] is smaller than nums[mid], then nums[mid + 1] is the
minimum.

Based on whether nums[mid] lies in the left sorted portion or the right
rotated portion, we discard one half of the search space and continue.

Time Complexity:
----------------
O(log n), where n is the number of elements in the array.

Space Complexity:
-----------------
O(1), since the search is performed in-place using constant extra space.

Did this code successfully run on Leetcode:
------------------------------------------
Yes

Any problem you faced while coding this:
---------------------------------------
Carefully handling boundary conditions when accessing nums[mid - 1]
and nums[mid + 1] to avoid index out-of-range errors.

"""


class Solution:
def findMin(self, nums) -> int:
low = 0
high = len(nums) - 1

if low == high:
return nums[0]

if nums[low] < nums[high]:
return nums[low]

while low <= high:
mid = (low + high)//2

if nums[mid] < nums[mid-1]:
return nums[mid]
if nums[mid+1] < nums[mid]:
return nums[mid+1]

if nums[mid] > nums[low]:
low = mid + 1
else:
high = mid - 1

81 changes: 81 additions & 0 deletions leetcode162.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 8 15:43:49 2026

@author: rishigoswamy

Problem:
----------
https://leetcode.com/problems/find-peak-element/

Approach:
----------
A peak element is defined as an element that is strictly greater than
its neighboring elements, and the problem guarantees that a peak
always exists.

We use binary search to efficiently find a peak by comparing the
middle element with its neighbors.
If the middle element is greater than both neighbors, it is a peak
and can be returned immediately.

If the left neighbor is greater than the middle element, a peak must
exist in the left half of the array; otherwise, a peak must exist in
the right half.
By discarding half of the search space at each step, we maintain
logarithmic time complexity.

Special boundary checks are performed for the first and last elements
to safely handle edge cases.

Time Complexity:
----------------
O(log n), where n is the number of elements in the array.

Space Complexity:
-----------------
O(1), since the search is performed in-place using constant extra space.

Did this code successfully run on Leetcode:
------------------------------------------
Yes

Any problem you faced while coding this:
---------------------------------------
Handling boundary conditions for the first and last indices and
correctly deciding which half of the array to discard during the
binary search.


"""
import List

class Solution:
def findPeakElement(self, nums: List[int]) -> int:
low = 0
high = len(nums) - 1

if high == low:
return 0

while low <= high:

mid = (low+high)//2

if mid == 0 and nums[mid+1] < nums[mid]:
return mid

elif mid == len(nums) - 1 and nums[mid-1] < nums[mid]:
return mid

elif nums[mid-1] < nums[mid] and nums[mid+1] < nums[mid]:
return mid

if mid > 0 and nums[mid] < nums[mid-1]:
high = mid - 1

elif mid < len(nums):
low = mid + 1


84 changes: 84 additions & 0 deletions leetcode34.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 8 15:39:30 2026

@author: rishigoswamy

Problem:
----------
https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/

Approach:
----------
Since the array is sorted in ascending order, binary search can be used
to locate the first and last occurrences of the target efficiently.

We perform two separate binary searches:
- The first search finds the leftmost (first) position of the target by
continuing the search on the left half even after the target is found.
- The second search finds the rightmost (last) position of the target by
continuing the search on the right half after the target is found.

Each search carefully checks boundary conditions to ensure the correct
extreme index is identified.

Time Complexity:
----------------
First binary search : O(log n)
Second binary search : O(log n)
Overall : O(log n)

Space Complexity:
-----------------
O(1), since only constant extra space is used.

Did this code successfully run on Leetcode:
------------------------------------------
Yes

Any problem you faced while coding this:
---------------------------------------
Handling edge cases where the target appears at the beginning or end
of the array, and ensuring both searches terminate correctly when the
target does not exist.

"""

class Solution:
def searchRange(self, nums, target):
low = 0
high = len(nums) - 1
mid = (low + high)//2
first, second = -1, -1

while low <= high:
mid = (low + high)//2
if nums[mid] == target:
if mid == 0 or nums[mid-1] != nums[mid]:
first = mid
break
else:
high = mid - 1
elif nums[mid] > target:
high = mid - 1
else:
low = mid + 1


low = 0
high = len(nums) - 1
while low <= high:
mid = (low + high)//2
if nums[mid] == target:
if mid == len(nums)-1 or nums[mid+1] != target:
second = mid
break
else:
low = mid + 1
elif nums[mid] > target:
high = mid - 1
else:
low = mid + 1

return [first, second]