LeetCode 2237: Count Positions on Street With Required Brightness Solution
Master LeetCode problem 2237 (Count Positions on Street With Required Brightness), 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.
2237. Count Positions on Street With Required Brightness
Problem Explanation
Explanation:
To solve this problem, we can iterate over the positions on the street and calculate the brightness at each position based on the lamps' positions and their brightness. If the brightness at a position is equal to the required brightness, we increment a counter. Finally, we return the counter as the result.
- Iterate over the positions from 0 to n-1.
- For each position, calculate the brightness by summing up the brightness of all lamps at that position.
- If the calculated brightness is equal to the required brightness, increment the counter.
- Return the counter as the result.
Time complexity: O(n * m) where n is the number of positions on the street and m is the number of lamps. Space complexity: O(1) :
Solution Code
class Solution {
public int countPositions(int[] lamps, int[] brightness, int requiredBrightness) {
int n = brightness.length;
int m = lamps.length;
int count = 0;
for (int i = 0; i < n; i++) {
int totalBrightness = 0;
for (int j = 0; j < m; j++) {
if (lamps[j] == i) {
totalBrightness += brightness[j];
}
}
if (totalBrightness == requiredBrightness) {
count++;
}
}
return count;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2237 (Count Positions on Street With Required Brightness)?
This page provides optimized solutions for LeetCode problem 2237 (Count Positions on Street With Required Brightness) 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 2237 (Count Positions on Street With Required Brightness)?
The time complexity for LeetCode 2237 (Count Positions on Street With Required Brightness) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2237 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2237 in Java, C++, or Python.