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: content/6_Advanced/Lagrange.mdx
+42-44
Original file line number
Diff line number
Diff line change
@@ -25,17 +25,17 @@ frequency: 1
25
25
26
26
## Lagrangian Relaxation
27
27
28
-
Lagrangian Relaxation involves transforming a constraint on a variable into a cost $\lambda$ and binary searching for the optimal $\lambda$.
28
+
Lagrangian Relaxation involves transforming a constraint on a variable into a cost $\lambda$ and binary searching for the optimal $\lambda$.
29
29
30
30
<FocusProblemproblem="sample" />
31
31
32
32
The problem gives us a length $N$ ($1 \le N \le 3 \cdot 10^5$) array of integers in the range $[-10^9,10^9]$. We are given some $K$ ($1 \le K \le N$) and are asked to choose at most $K$ disjoint subarrays such that the sum of elements included in a subarray is maximized.
33
33
34
34
### Intuition
35
35
36
-
The main bottleneck of any dynamic programming solution to this problem is having to store the number of subarrays we have created so far.
36
+
The main bottleneck of any dynamic programming solution to this problem is having to store the number of subarrays we have created so far.
37
37
38
-
Let's try to find a way around this. Instead of storing the number of subarrays we have created so far, we assign a penalty of $\lambda$ for creating a new subarray (i.e. everytime we create a subarray we penalize our sum by $\lambda$).
38
+
Let's try to find a way around this. Instead of storing the number of subarrays we have created so far, we assign a penalty of $\lambda$ for creating a new subarray (i.e. everytime we create a subarray we penalize our sum by $\lambda$).
39
39
40
40
This leads us to the sub-problem of finding the maximal sum and number of subarrays used if creating a new subarray costs $\lambda$. We can solve this in $\mathcal{O}(N)$ time with dynamic programming.
41
41
@@ -72,7 +72,7 @@ This idea almost works but there are still some very important caveats and condi
72
72
73
73
### Geometry
74
74
75
-
Let $f(x)$ be the maximal sum if we use at most $x$ subarrays. We want to find $f(K)$.
75
+
Let $f(x)$ be the maximal sum if we use at most $x$ subarrays. We want to find $f(K)$.
76
76
77
77
The first condition is that $f(x)$ **must be concave or convex**. Since $f(x)$ is increasing in this problem, the means that we need $f(x)$ to be concave: $f(x) - f(x - 1) \ge f(x + 1) - f(x)$. In other words, this means that the more subarrays we add, the less we increase our answer by. We can intuitively see that this is true.
78
78
@@ -103,7 +103,7 @@ Here is where the fact that $f(x)$ is concave comes in. Because the slope is non
103
103
104
104
Let $v(\lambda)$ be the optimal maximal achievable sum with $\lambda$ penalty and $c(\lambda)$ be the number of subarrays used to achieve $v(\lambda)$ (note that if there are multiple such possibilities, we set $c$ to be the **minimal** number of subarrays to achieve $v$). These values can be calculated in $\mathcal{O}(N)$ time using the dynamic programming approach described above.
105
105
106
-
When we assign the penalty of $\lambda$, we are trying to find the maximal sum if creating a subarray reduces our sum by $\lambda$. In other words, **we are trying to find the maximum of $f(x) - \lambda x$**.
106
+
When we assign the penalty of $\lambda$, we are trying to find the maximal sum if creating a subarray reduces our sum by $\lambda$. In other words, **we are trying to find the maximum of $f(x) - \lambda x$**.
107
107
108
108
Without loss of generality, suppose there exists a slope equal to $\lambda$. Given the shape of $f(x) - \lambda x$, we know that $f(x) - \lambda x$ will be maximized at the points where $\lambda$ is equal to the slope of $f(x)$ (these points are red in the graph above). This means that $c(\lambda)$ will be the point at which $\lambda$ is equal to the slope of $f(x)$ (if there are multiple such points, then $c(\lambda)$ will be the leftmost one).
109
109
@@ -113,57 +113,55 @@ We binary search for $\lambda$ and find the highest $\lambda$ such that $c(\lamb
113
113
114
114
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{\sumA[i]})$ time.
0 commit comments