LeetCode 2021: Brightest Position on Street Solution

Master LeetCode problem 2021 (Brightest Position on Street), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2021. Brightest Position on Street

Problem Explanation

Explanation:

To solve this problem, we can use a binary search approach. We start by defining a function to check if a given position is the brightest on the street. We then perform a binary search to find the position with the maximum brightness. We can compare the brightness at a given position with its neighbors to decide whether to move left or right in the search space.

Algorithm:

  1. Define a function isBrightest to check if a given position is the brightest on the street.
  2. Initialize left and right pointers to 0 and n-1 respectively.
  3. Perform a binary search:
    • Calculate the mid position mid as (left + right) / 2.
    • Check if mid is the brightest position using the isBrightest function.
    • If mid is the brightest, return mid.
    • Otherwise, update left or right based on the brightness comparison with neighbors.
  4. Return the brightest position found.

Time Complexity:

The binary search approach has a time complexity of O(log n) where n is the size of the street.

Space Complexity:

The space complexity is O(1) as we are using constant extra space.

:

Solution Code

class Solution {
    public int brightestPosition(int[] street) {
        int left = 0, right = street.length - 1;
        
        while (left < right) {
            int mid = left + (right - left) / 2;
            
            if (isBrightest(street, mid)) {
                return mid;
            } else {
                if (brightness(street, mid) < brightness(street, mid + 1)) {
                    left = mid + 1;
                } else {
                    right = mid;
                }
            }
        }
        
        return left;
    }
    
    private boolean isBrightest(int[] street, int pos) {
        return brightness(street, pos) > brightness(street, pos - 1) && brightness(street, pos) > brightness(street, pos + 1);
    }
    
    private int brightness(int[] street, int pos) {
        if (pos < 0 || pos >= street.length) {
            return 0;
        }
        return street[pos];
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2021 (Brightest Position on Street)?

This page provides optimized solutions for LeetCode problem 2021 (Brightest Position on Street) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2021 (Brightest Position on Street)?

The time complexity for LeetCode 2021 (Brightest Position on Street) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2021 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2021 in Java, C++, or Python.

Back to LeetCode Solutions