You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/2200-2299/2237.Count Positions on Street With Required Brightness/README_EN.md
+43-10
Original file line number
Diff line number
Diff line change
@@ -73,7 +73,17 @@ Positions 0, 1, 2, and 4 meet the requirement so we return 4.
73
73
74
74
<!-- solution:start -->
75
75
76
-
### Solution 1
76
+
### Solution 1: Difference Array
77
+
78
+
To add a value $v$ to a continuous interval $[i, j]$ simultaneously, we can use a difference array.
79
+
80
+
We define an array $\textit{d}$ of length $n + 1$. For each streetlight, we calculate its left boundary $i = \max(0, p - r)$ and right boundary $j = \min(n - 1, p + r)$, then add $1$ to $\textit{d}[i]$ and subtract $1$ from $\textit{d}[j + 1]$.
81
+
82
+
Next, we perform a prefix sum operation on $\textit{d}$. For each position $i$, if the prefix sum of $\textit{d}[i]$ is greater than or equal to $\textit{requirement}[i]$, it means that the position meets the requirement, and we increment the answer by one.
83
+
84
+
Finally, return the answer.
85
+
86
+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of streetlights.
Copy file name to clipboardExpand all lines: solution/2200-2299/2239.Find Closest Number to Zero/README_EN.md
+7-1
Original file line number
Diff line number
Diff line change
@@ -56,7 +56,13 @@ Thus, the closest number to 0 in the array is 1.
56
56
57
57
<!-- solution:start -->
58
58
59
-
### Solution 1
59
+
### Solution 1: One Pass
60
+
61
+
We define a variable $d$ to record the current minimum distance, initially $d=\infty$. Then we traverse the array, for each element $x$, we calculate $y=|x|$. If $y \lt d$ or $y=d$ and $x \gt ans$, we update the answer $ans=x$ and $d=y$.
62
+
63
+
After the traversal, return the answer.
64
+
65
+
Time complexity is $O(n)$, where $n$ is the length of the array. Space complexity is $O(1)$.
0 commit comments