Skip to content

Completed Binary-Search-2 - #2371

Open
pranati05 wants to merge 3 commits into
super30admin:masterfrom
pranati05:master
Open

pranati05 wants to merge 3 commits into
super30admin:masterfrom
pranati05:master

Conversation

@pranati05

Copy link
Copy Markdown

No description provided.

@pranati05 pranati05 changed the title Completed Design-2 Completed Binary-Search-2 Sep 9, 2026
@super30admin

Copy link
Copy Markdown
Owner

Find First and Last Position of Element in Sorted Array (first_and_last_position_in_array.py)

Strengths:

  • Your binary search solution correctly implements the two-pass approach to find the first and last positions.
  • The boundary checks (mid == low and mid == high) are good defensive programming to avoid index out-of-bounds errors.
  • The early termination check (nums[0] > target or nums[-1] < target) is a nice optimization.

Areas for Improvement:

  1. Remove the O(N) solution: The first solution using linear search violates the problem's O(log n) requirement. It should be removed entirely from your submission.
  2. Single Solution class: Never define the same class twice in one file. This is confusing and bad practice. Keep only the binary search version.
  3. Update comments: Your header comment says "Time Complexity: O(logN)" but having an O(N) solution in the same file contradicts this. Make sure your comments accurately reflect the solution.
  4. Consider edge cases: Test with arrays like [1] where target is 1 to ensure your boundary conditions work correctly.
  5. Code organization: Consider combining both binary searches into a single helper function with a parameter to indicate whether to find the first or last position, which would reduce code duplication.

VERDICT: PASS


Find Minimum in Rotated Sorted Array (Minimum_in_rotated_sorted_array.py)

Strengths:

  • Your binary search solution correctly implements the algorithm to find the minimum in O(log n) time.
  • The logic matches the reference solution - checking if mid is the minimum by comparing with neighbors, and deciding which half to search.
  • Good use of comments to explain the approach.

Areas for improvement:

  1. File structure: You have two Solution classes in the same file. In Python, the second class definition will override the first. You should only include the binary search solution.
  2. Remove the linear search solution: Since the problem requires O(log n) time complexity, the linear search solution is not appropriate. Remove it from your submission.
  3. Clean up comments: Your initial comments mention binary search, but then you provide a linear search solution. Make sure your comments accurately reflect your final solution.
  4. Edge case handling: Consider what happens when nums is empty. Your current code returns None, but the problem constraints state n >= 1, so this check might be unnecessary.

VERDICT: PASS


Find Peak Element (Peak_Element.py)

Strengths:

  1. Your binary search approach correctly identifies peak elements using the same logic as the reference solution.
  2. The time and space complexity match the required O(log N) and O(1).
  3. You provided multiple approaches showing different ways to solve the problem.

Areas for Improvement:

  1. File Organization: Having three different Solution class definitions in one file is problematic. Only the last one will be executed by LeetCode. Remove the unused solutions and keep only the O(log N) binary search approach.
  2. Comment Style: Use Python-style comments (#) instead of C++-style (//). Your file mixes both styles.
  3. Unnecessary Code: The if not nums: return None check is unnecessary since the problem guarantees a non-empty array.
  4. Edge Case Handling: Consider what happens when nums has only one element - your code handles it correctly since mid == 0 and mid == len(nums) - 1 would both be True.
  5. Code Clarity: The condition if mid == len(nums)-1 or nums[mid] < nums[mid+1] is a bit confusing. A cleaner approach would be to always check nums[mid] < nums[mid+1] (since we know mid < n in the loop, but we need to ensure mid+1 is valid). Actually, since low <= high and high = len(nums) - 1, mid can be at most len(nums) - 1, so nums[mid+1] access could be out of bounds. Your guard mid == len(nums)-1 prevents this, but it's worth noting.

VERDICT: PASS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants