LeetCode 2237: Count Positions on Street With Required Brightness
Problem Description
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) :
Solutions
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;
}
}
Loading editor...