Similar Problems

Similar Problems not available

Brightest Position On Street - Leetcode Solution

Companies:

LeetCode:  Brightest Position On Street Leetcode Solution

Difficulty: Medium

Topics: prefix-sum array  

Problem Statement:

Given an array representing the brightness of each street light on a street, find the brightest position on the street.

Example:

Input: [4, 2, 8, 3, 6, 9] Output: 5 Explanation: The 5th position has the brightest light with brightness level 9.

Solution:

To solve this problem, we need to find the maximum brightness level and then return its index in the array.

We can do this by iterating over the input array and keeping track of the maximum brightness level and its index. We can initialize the maximum brightness level and its index to the first element in the array. Then, we can loop through the rest of the elements in the array and check if the current element has a greater brightness level than the maximum brightness level we have seen so far. If it does, we update the maximum brightness level and its index.

Once we have iterated over all the elements in the array, we should have the index of the brightest position. We can then return this index.

Here is the Python code for the solution:

def brightest_position_on_street(lights): max_brightness = lights[0] max_brightness_index = 0 for i in range(1, len(lights)): if lights[i] > max_brightness: max_brightness = lights[i] max_brightness_index = i return max_brightness_index

The time complexity of this solution is O(n) because we are iterating over the input array once.

Overall, this problem is not difficult and can be solved with a simple linear scan of the input array.

Brightest Position On Street Solution Code

1