Skip to content

Commit 9f601ed

Browse files
authoredMar 22, 2024
Update Lagrange.mdx
1 parent c49c270 commit 9f601ed

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed
 

‎content/6_Advanced/Lagrange.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Given the shape of $f(x) - \lambda x$, we know that $f(x) - \lambda x$ will be m
110110

111111
Now we know exactly what $\lambda$ represents: $\lambda$ is the slope and $c(\lambda)$ is the rightmost $x$ at which the slope of $f(x)$ is still greater or equal to $\lambda$.
112112

113-
We binary search for $\lambda$ and find the highest $\lambda$ such that $c(\lambda) \le K$. Let the optimal value be $\lambda_{\texttt{opt}}$. Then our answer is $v(\lambda_{\texttt{opt}}) + \lambda_{\texttt{opt}} K$. Note that this works even if $c(\lambda_{\texttt{opt}}) \neq K$ since $c(\lambda_{\texttt{opt}})$ and $K$ will be on the same line with slope $\lambda_{\texttt{opt}}$ in that case.
113+
We binary search for $\lambda$ and find the first $\lambda$ such that $c(\lambda) \ge K$. Let the optimal value be $\lambda_{\texttt{opt}}$. Then our answer is $v(\lambda_{\texttt{opt}}) + \lambda_{\texttt{opt}} K$. Note that this works even if $c(\lambda_{\texttt{opt}}) \neq K$ since $c(\lambda_{\texttt{opt}})$ and $K$ will be on the same line with slope $\lambda_{\texttt{opt}}$ in that case.
114114

115115
Because calculating $v(\lambda)$ and $c(\lambda)$ with the dynamic programming solution described above will take $\mathcal{O}(N)$ time, this solution runs in $\mathcal{O}(N\log{\sum A[i]})$ time.
116116

0 commit comments

Comments
 (0)