<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Algorithms Q&amp;A - Recent questions and answers in Dynamic Programming</title>
<link>https://notexponential.com/qa/dynamic-programming</link>
<description>Powered by Question2Answer</description>
<item>
<title>Answered: Minimizing Weight of a Linear Partition</title>
<link>https://notexponential.com/382/minimizing-weight-of-a-linear-partition?show=953#a953</link>
<description>&lt;p&gt;To minimize the weight of the linear k-partition of the given array A[1:n], you can use dynamic programming. Define a 2D array DP where DP[i][j] represents the minimum weight of the linear j-partition for the subarray A[1:i]. The recurrence relation can be expressed as follows:&lt;/p&gt;&lt;p&gt;DP[i][j] = min{1 &amp;lt; x &amp;lt; i} { DP[x][j-1] + sum(A[x+1:i]) }]&lt;/p&gt;&lt;p&gt;Here, sum(A[x+1:i]) represents the sum of elements in the subarray A[x+1:i].&lt;/p&gt;&lt;p&gt;The base case is DP[i][1] = sum(A[1:i]) since the minimum weight of a linear 1-partition is the sum of the entire array.&lt;/p&gt;&lt;p&gt;The final answer will be in DP[n][k], as it represents the minimum weight of the linear k-partition for the entire array A.&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DP[i][j] = min(DP[i][j], DP[x][j - 1] + prefix_sums[i] - prefix_sums[x])&lt;/p&gt;&lt;p&gt;&amp;nbsp;return DP[n][k]&lt;/p&gt;&lt;p&gt;&lt;strong&gt;code:&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;def linear_k_partition(A, k):&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; n = len(A)&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; prefix_sums = [0] * (n + 1)&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; for i in range(1, n + 1):&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; prefix_sums[i] = prefix_sums[i - 1] + A[i - 1]&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; DP = [[float(&#039;inf&#039;)] * (k + 1) for _ in range(n + 1)]&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;for i in range(1, n + 1):&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DP[i][1] = prefix_sums[i]&lt;/p&gt;&lt;p&gt;&amp;nbsp;for j in range(2, k + 1):&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for i in range(1, n + 1):&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for x in range(i):&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DP[i][j] = min(DP[i][j], DP[x][j - 1] + prefix_sums[i] - prefix_sums[x])&lt;/p&gt;&lt;p&gt;&amp;nbsp; return DP[n][k]&lt;/p&gt;&lt;p&gt;A = [4, 8, 7, 6, 5, 1, 2, 3]&lt;/p&gt;&lt;p&gt;k = 3&lt;/p&gt;&lt;p&gt;result = linear_k_partition(A, k)&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;print(result)&lt;/p&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/382/minimizing-weight-of-a-linear-partition?show=953#a953</guid>
<pubDate>Sat, 16 Dec 2023 18:39:46 +0000</pubDate>
</item>
<item>
<title>Answered: DNA sequences</title>
<link>https://notexponential.com/515/dna-sequences?show=947#a947</link>
<description>&lt;p&gt;&lt;strong&gt;Notation: &lt;/strong&gt;LCS(i,j) represents the length of maximum common subsequence of the respective strings A(0...i), B(0....j)&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Optimality:&lt;/strong&gt; To get the Longest Common Subsequence (LCS) at position (i)(j)&amp;nbsp;&lt;/p&gt;&lt;p&gt;when the final characters of two strings, A&amp;nbsp;and B, do not match (A(i)&amp;nbsp;is not equal to B(i)), take the maximum of two values: LCS(i)(j)&amp;nbsp;=&amp;nbsp;MAX(LCS(i-1)(j), LCS(i)(j-1)).&amp;nbsp;&lt;/p&gt;&lt;p&gt;if A(i) equal to B(i) then LCS(i)(j) =&amp;nbsp;1+ LCS(i-1)(j-1)&lt;/p&gt;&lt;p&gt;This illustrates the notion that the LCS is affected by the LCS values derived by removing the final character from each string when the last characters are not identical.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Recurrence Relation: &lt;/strong&gt;if A(i)!=B(i) then LCS(i, j) =max( LCS(i-1,j) ,(i, j-1)&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if A(i)=B(j) then LCS(i, j) = 1 + LCS(i-1,j-1)&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Algorithm:&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;def find_lcs(A, B):&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; dp = [[0] * (len(B) + 1) for _ in range(len(A) + 1)]&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; for i in range(1, len(A) + 1):&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for j in range(1, len(B) + 1):&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dp[i][j] = dp[i-1][j-1] + 1 if A[i-1] == B[j-1] else max(dp[i-1][j], dp[i][j-1])&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; lcs = &#039;&#039;.join([s1[i-1] for i, j in zip(range(len(A), 0, -1), range(len(B), 0, -1)) if dp[i][j] !=&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dp[i-1][j] and dp[i][j] != dp[i][j-1]])&lt;/p&gt;&lt;p&gt;&amp;nbsp;return lcs&lt;/p&gt;&lt;p&gt;s1 =&amp;nbsp;&lt;span style=&quot;font-family:Helvetica,Arial,sans-serif; font-size:14px&quot;&gt;&quot;ACTGACT&quot;&lt;/span&gt;&lt;span style=&quot;font-family:Helvetica,Arial,sans-serif; font-size:14px&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;s2 =&amp;nbsp;&lt;span style=&quot;font-family:Helvetica,Arial,sans-serif; font-size:14px&quot;&gt;&quot;CAAGCATA&quot;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;result = find_lcs(s1, s2)&lt;/p&gt;&lt;p&gt;print(result)&amp;nbsp;&amp;nbsp;&lt;/p&gt;&lt;p&gt;As we are storing the results of sub-problem in the array of size n1*n2 so the time and space complexity of this problem is O(n1*n2)&lt;/p&gt;&lt;div&gt;&lt;/div&gt;</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/515/dna-sequences?show=947#a947</guid>
<pubDate>Sat, 16 Dec 2023 17:55:42 +0000</pubDate>
</item>
<item>
<title>Answered: “Teleportation” across astro-haunted galaxies</title>
<link>https://notexponential.com/173/teleportation-across-astro-haunted-galaxies?show=938#a938</link>
<description>We observe that the problem is similar to all pairs shortest path problem, but in addition to that, we have the constraint that we can go through at most m astro-haunted galaxies. We model this constraint also in our notation. &lt;br /&gt;
&lt;br /&gt;
Notation Let D(i,j,k) denote the cost of the shortest path from i to j using at most k astro-haunted galaxies. &lt;br /&gt;
&lt;br /&gt;
Recursive Formulation Recursive formulation on D(i,j,k) can be written on the variable k and by deciding on the last astro-haunted galaxy on the path from i to j . D(i,j,k) = min { D(i,j,k-1), min (1≤z≤n | A[z]=1 ) {D(i,z,k-1) + D(z,j,0)} } We observe that A[z] = 1 constraint specifies that the galaxy z is astro haunted.&lt;br /&gt;
&lt;br /&gt;
Further, by taking the minimum with D(i,j,k-1) we satisfy the constraint of at most k astro-haunted galaxies, while still avoiding pitfall of leaving this value undefined in case there is no astro haunted galaxy. &lt;br /&gt;
&lt;br /&gt;
Base Case D(i,j,0) can be solved simply by using All Pairs Shortest Path and by eliminating all astro-haunted galaxies. &lt;br /&gt;
&lt;br /&gt;
Algorithm The algorithm can easily be written in terms of for loops for each of the indices in the notation of D(i,j,k) . As is often observed, the index of the main recursive formulation usually forms the outermost loop.&lt;br /&gt;
&lt;br /&gt;
// Step 1: Base Case &lt;br /&gt;
&lt;br /&gt;
Calculate D[i][j][0] as All Pairs Shortest path by ignoring the astro haunted galaxies. &lt;br /&gt;
&lt;br /&gt;
// Step 2: Inductive Step &lt;br /&gt;
&lt;br /&gt;
for k = 1 to m &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;for i = 1 to n &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;for j = 1 to n &lt;br /&gt;
&lt;br /&gt;
Calculate D[i][j][k] = min {D([i][j][k-1], D[i][z][k-1] + D[z][j][0] for all z such that A[z] = 1}&lt;br /&gt;
&lt;br /&gt;
Time Complexity &lt;br /&gt;
&lt;br /&gt;
Time Complexity of base case: O(n^3 ) from the all pairs shortest path problem. Each calculation of D(i,j,k) takes O(n) time. Further, there are kn^2 entries in the dynamic programming table. Therefore, the total time Complexity of recursive portion: O(k n^3) Therefore, the total time complexity is O(k n^3 ).</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/173/teleportation-across-astro-haunted-galaxies?show=938#a938</guid>
<pubDate>Wed, 13 Dec 2023 20:43:14 +0000</pubDate>
</item>
<item>
<title>Answered: Barbie&#039;s Array of Diamonds</title>
<link>https://notexponential.com/497/barbies-array-of-diamonds?show=935#a935</link>
<description>&lt;p&gt;&lt;strong&gt;Notation:&lt;/strong&gt;&amp;nbsp;We shall call the list&amp;nbsp;diamonds[i], where i ranges from 0 to n, be the list of all of Barbie&#039;s diamonds. We also have corresponding tuple (shiny_i, weight_i) values for each i-th diamond in diamonds[i]. Let D(i) be the maximum number of diamonds in line for the i-th diamond.&lt;br&gt;&lt;br&gt;&lt;strong&gt;Optimal Substructure&lt;/strong&gt;: The maximum number of diamonds in line for the i-th diamond depends on the maximum number of diamonds in line for a smaller index diamond.&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;strong&gt;Recurrence Relation&lt;/strong&gt;: D(i) = 1 + max(D(j))&amp;nbsp;for j &amp;lt; i where shiny_j &amp;lt; shiny_i and weight_j &amp;lt; weight_i; or D(i) = 1&amp;nbsp;if no such j exists.&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;strong&gt;Algorithm:&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;```&lt;br&gt;def barbie(diamonds):&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;n = len(diamonds)&lt;br&gt;&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;# Initialize D(i) array, which is the DP table. Initially, each diamond forms a line with itself, therefore = 1. When D is filled, we look for max(D) as the solution for max number of diamonds in a line.&amp;nbsp;&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;D = [1] * n&lt;br&gt;&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;# Sort diamonds by shiny value&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;diamonds = sorted(diamonds, key=lambda x: x[0]) # According to wiki, Timsort worst case is O(n log n)&lt;br&gt;&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;# Fill DP table. Start at index 1.&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;for i in range(1, n):&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for j in range(i):&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;shiny_j, shiny_i = diamonds[j][0], diamonds[i][0]&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;weight_j, weight_i = diamonds[j][1], diamonds[j][1]&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (shiny_j &amp;lt; shiny_i) and (weight_j &amp;lt; weight_i):&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; D[i]&amp;nbsp;= max(D[i], D[j] + 1) # Is&amp;nbsp;the current line greater regardless or is there a valid diamond to add to the line?&lt;br&gt;&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;# Return max number of diamonds in a line found.&lt;br&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;return max(D)&lt;br&gt;&lt;br&gt;diamonds = [(5, 10), (6, 12), (4, 8), (7, 15), (3, 5)]&lt;br&gt;barbie(diamonds) # Answer is 5 diamonds in a line. If you sort the diamonds list by shiny value, the LIS according to weight is 5.&lt;br&gt;```&lt;br&gt;&lt;br&gt;Time complexity:&amp;nbsp;solution runs in O(n^2 * log n)&lt;/p&gt;</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/497/barbies-array-of-diamonds?show=935#a935</guid>
<pubDate>Wed, 13 Dec 2023 05:26:01 +0000</pubDate>
</item>
<item>
<title>Answered: Maximize value of coins from k stacks, of n coins each</title>
<link>https://notexponential.com/633/maximize-value-of-coins-from-k-stacks-of-n-coins-each?show=928#a928</link>
<description>Notation: Let MCV(V, i, j) denote the maximum coin value that we can obtain given the list of coins V[i..j] . &amp;nbsp;Further, let S(V,i,j) denote the sum of the values of all the coins in V[i..j] . &lt;br /&gt;
Recursive Formulation: We observe that once the first player pockets the first or the last coin, the opponent realizes the maximum value from the remaining list. Therefore, the value realized by the first player is the sum of the list minus the value realized by the opponent. &amp;nbsp;Therefore, we can write MCV(V,i,j) as: &lt;br /&gt;
MCV(V,i,j)= &amp;nbsp;ma x {S(V,i,j) – MCV(V – v i ), S(V,i,j) MCV(V – v j )} &lt;br /&gt;
This can further be written as: MCV(V,i,j)=S(V,i,j) – mi n {MCV(V – v i ), MCV(V – v j ) } &lt;br /&gt;
The base case of the recurrence can be set as MCV(φ) = 0 , or equivalently that the value that can be realized by a single element list is the value of that element.&lt;br /&gt;
Algorithm: This algorithm works like many other algorithms that need to “grow” the range of the array. &amp;nbsp;Specifically, the iterations can work as follows:&lt;br /&gt;
Notation: Let MCV(V, i, j) denote the maximum coin value that we can obtain given the list of coins V[i..j] . &amp;nbsp;Further, let S(V,i,j) denote the sum of the values of all the coins in V[i..j] . &lt;br /&gt;
Recursive Formulation: We observe that once the first player pockets the first or the last coin, the opponent realizes the maximum value from the remaining list. Therefore, the value realized by the first player is the sum of the list minus the value realized by the opponent. &amp;nbsp;Therefore, we can write MCV(V,i,j) as: &lt;br /&gt;
MCV(V,i,j)= &amp;nbsp;ma x {S(V,i,j) – MCV(V – v i ), S(V,i,j) MCV(V – v j )} &lt;br /&gt;
This can further be written as: MCV(V,i,j)=S(V,i,j) – mi n {MCV(V – v i ), MCV(V – v j ) } &lt;br /&gt;
The base case of the recurrence can be set as MCV(φ) = 0 , or equivalently that the value that can be realized by a single element list is the value of that element.&lt;br /&gt;
Algorithm: This algorithm works like many other algorithms that need to “grow” the range of the array. &amp;nbsp;Specifically, the iterations can work as follows:&lt;br /&gt;
double[][] S = new double[n][n];&lt;br /&gt;
&lt;br /&gt;
double[][] MCV = new double[n][n];&lt;br /&gt;
&lt;br /&gt;
// Initialize S[i,i] = V[i]&lt;br /&gt;
&lt;br /&gt;
// Initialize MCV[i,i] = V[i]&lt;br /&gt;
&lt;br /&gt;
for i=0 to n-2&lt;br /&gt;
&lt;br /&gt;
for j=i+1 to n-1&lt;br /&gt;
&lt;br /&gt;
S[i,j]=S[i,j-1]+V[j]&lt;br /&gt;
&lt;br /&gt;
for k=1 to n-1&lt;br /&gt;
&lt;br /&gt;
for i=0 to n-2&lt;br /&gt;
&lt;br /&gt;
MCV[i,i+k] = S[i,i+k] - min{MCV[i+1,i+k], MCV[i,i+k-1]}</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/633/maximize-value-of-coins-from-k-stacks-of-n-coins-each?show=928#a928</guid>
<pubDate>Tue, 12 Dec 2023 22:59:40 +0000</pubDate>
</item>
<item>
<title>Answered: Selecting lowest cost hotel rooms, with no adjacent rooms selected</title>
<link>https://notexponential.com/772/selecting-lowest-cost-hotel-rooms-adjacent-rooms-selected?show=895#a895</link>
<description>&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-family:Helvetica,Arial,sans-serif; font-size:14px&quot;&gt;Notation: Suppose S(i,j) is the smallest cost of selecting j hotel rooms from 1..i hotel rooms&lt;/span&gt;&lt;/li&gt;&lt;li&gt;The principle of optimality holds i.e., it has optimal sub-structure&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-family:Helvetica,Arial,sans-serif; font-size:14px&quot;&gt;&amp;nbsp;Base Case S(1,1) = c[1]&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-family:Helvetica,Arial,sans-serif; font-size:14px&quot;&gt;We need to find S(n,k).&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-family:Helvetica,Arial,sans-serif; font-size:14px&quot;&gt;Recursive Formulation: S(i,j) = min { S(i-1,j), S(i-2,j-1) + c[i] }&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;span style=&quot;font-family:Helvetica,Arial,sans-serif; font-size:14px&quot;&gt;Algorithm:&lt;/span&gt;&lt;br style=&quot;font-family: Helvetica, Arial, sans-serif; font-size: 14px;&quot;&gt;&lt;br style=&quot;font-family: Helvetica, Arial, sans-serif; font-size: 14px;&quot;&gt;&lt;span style=&quot;font-family:Helvetica,Arial,sans-serif; font-size:14px&quot;&gt;for i = 1 to n&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family:Helvetica,Arial,sans-serif; font-size:14px&quot;&gt;&amp;nbsp; for j = 1 to k&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family:Helvetica,Arial,sans-serif; font-size:14px&quot;&gt;&amp;nbsp; &amp;nbsp; S(i,j) = min { S(i-1,j), S(i-2,j-1) + c[i] }&lt;/span&gt;&lt;br style=&quot;font-family: Helvetica, Arial, sans-serif; font-size: 14px;&quot;&gt;&lt;br style=&quot;font-family: Helvetica, Arial, sans-serif; font-size: 14px;&quot;&gt;&lt;span style=&quot;font-family:Helvetica,Arial,sans-serif; font-size:14px&quot;&gt;Time Complexity= O(nk)&lt;/span&gt;&lt;/p&gt;</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/772/selecting-lowest-cost-hotel-rooms-adjacent-rooms-selected?show=895#a895</guid>
<pubDate>Sun, 10 Dec 2023 22:28:21 +0000</pubDate>
</item>
<item>
<title>Answered: &quot;SpicyBitter&quot; Sequence of Sauces</title>
<link>https://notexponential.com/710/spicybitter-sequence-of-sauces?show=748#a748</link>
<description>&lt;p&gt;Let us tackle it by sorting the list of sauces by the characteristics of Spiciness and Bitterness and apply the Longest increasing sub-sequence problem.&amp;nbsp;We can consider a sauce&amp;nbsp;to be greater when &lt;u&gt;BOTH&lt;/u&gt;&amp;nbsp;spiciness and bitterness is strictly greater than another sauce.&lt;/p&gt;&lt;p&gt;The LISA algorithm is as follows:&lt;/p&gt;&lt;p&gt;Let A[1...n] represents the sauce options and let B(i) represents the size of the longest strictly increasing subsequence that ends at position i.&lt;/p&gt;&lt;p&gt;That sequence must include A[i], thus, each B(i) is, at least, 1. We can conclude, we can&lt;/p&gt;&lt;p&gt;write the recurrence as:&lt;/p&gt;&lt;p&gt;B(1)=1&lt;br&gt;B(i) = max {B(j)}+1, such that j &amp;lt; i&amp;nbsp;and A[j] &amp;lt;&amp;nbsp;A[i]&lt;/p&gt;&lt;p&gt;We observe that to calculate each L(i), we need O(n) time, to examine the A[j] values against A[i], for all j &amp;lt; i.&lt;br&gt;Thus, we can calculate all B(i) values in O(n^2) time, then find the largest one in O(n) time. Therefore, overall complexity is O(n^2). Even though, we can try to make it better by finding the largest value by updating B (i) in the recurrence.&lt;/p&gt;</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/710/spicybitter-sequence-of-sauces?show=748#a748</guid>
<pubDate>Thu, 02 Jul 2020 10:34:50 +0000</pubDate>
</item>
<item>
<title>Answered: Love (Skip) thy neighbor</title>
<link>https://notexponential.com/498/love-skip-thy-neighbor?show=722#a722</link>
<description>&lt;p class=&quot;p1&quot; style=&quot;color: rgb(0, 0, 0); font-family: Times; font-size: medium;&quot;&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif&quot;&gt;&lt;span style=&quot;caret-color:#000000; font-size:small&quot;&gt;Using S(j)&amp;nbsp;represent the maximum sum of the points that can be achieved by first j numbers&lt;/span&gt;&lt;span style=&quot;caret-color:#000000; font-size:small&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class=&quot;p1&quot; style=&quot;color: rgb(0, 0, 0); font-family: Times; font-size: medium;&quot;&gt;S(j) can be defined recursively as follows:&lt;/p&gt;&lt;p class=&quot;p1&quot; style=&quot;color: rgb(0, 0, 0); font-family: Times; font-size: medium;&quot;&gt;s(j)=max{s(j-1),s(j-2)+pv[j]}&lt;/p&gt;&lt;p class=&quot;p1&quot; style=&quot;color: rgb(0, 0, 0); font-family: Times; font-size: medium;&quot;&gt;The two arguments in the max function correspond to the cases where we select (j – 1)-th element and&amp;nbsp; then&amp;nbsp;j-th element, or we select j-th element and&amp;nbsp;&amp;nbsp;(j – 1)-th element. As if S(j) is optimal, then the sub-solutions S(j-1) and S(j-2) must be optimal as well.&lt;/p&gt;&lt;p class=&quot;p1&quot; style=&quot;color: rgb(0, 0, 0); font-family: Times; font-size: medium;&quot;&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif&quot;&gt;&lt;span style=&quot;caret-color:#000000; font-size:small&quot;&gt;the persudo&amp;nbsp;code is&lt;/span&gt;&lt;span style=&quot;caret-color:#000000; font-size:small&quot;&gt;&amp;nbsp;&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p class=&quot;p1&quot; style=&quot;color: rgb(0, 0, 0); font-family: Times; font-size: medium;&quot;&gt;S&lt;span class=&quot;s1&quot;&gt;[&lt;/span&gt;1&lt;span class=&quot;s1&quot;&gt;]&lt;/span&gt;&amp;nbsp;= a&lt;span class=&quot;s1&quot;&gt;[&lt;/span&gt;1&lt;span class=&quot;s1&quot;&gt;]&lt;/span&gt;&lt;/p&gt;&lt;p class=&quot;p1&quot; style=&quot;color: rgb(0, 0, 0); font-family: Times; font-size: medium;&quot;&gt;S&lt;span class=&quot;s1&quot;&gt;[&lt;/span&gt;2&lt;span class=&quot;s1&quot;&gt;]&lt;/span&gt;&amp;nbsp;= max{pv&lt;span class=&quot;s1&quot;&gt;[&lt;/span&gt;1&lt;span class=&quot;s1&quot;&gt;],&lt;/span&gt;&amp;nbsp;pv&lt;span class=&quot;s1&quot;&gt;[&lt;/span&gt;2&lt;span class=&quot;s1&quot;&gt;]&lt;/span&gt;}&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p class=&quot;p1&quot; style=&quot;color: rgb(0, 0, 0); font-family: Times; font-size: medium;&quot;&gt;for j = 3 to n&lt;/p&gt;&lt;p class=&quot;p1&quot; style=&quot;color: rgb(0, 0, 0); font-family: Times; font-size: medium;&quot;&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;S&lt;span class=&quot;s1&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;s1&quot;&gt;]&lt;/span&gt;&amp;nbsp;= max {S&lt;span class=&quot;s1&quot;&gt;[&lt;/span&gt;j-1&lt;span class=&quot;s1&quot;&gt;],&lt;/span&gt;&amp;nbsp;S&lt;span class=&quot;s1&quot;&gt;[&lt;/span&gt;j-2&lt;span class=&quot;s1&quot;&gt;]&lt;/span&gt;+pv&lt;span class=&quot;s1&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;s1&quot;&gt;]&lt;/span&gt;&amp;nbsp;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p class=&quot;p1&quot; style=&quot;color: rgb(0, 0, 0); font-family: Times; font-size: medium;&quot;&gt;&lt;/p&gt;&lt;p class=&quot;p1&quot; style=&quot;color: rgb(0, 0, 0); font-family: Times; font-size: medium;&quot;&gt;&amp;nbsp;The time complexity is O(n).&lt;/p&gt;</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/498/love-skip-thy-neighbor?show=722#a722</guid>
<pubDate>Tue, 23 Jun 2020 21:15:55 +0000</pubDate>
</item>
<item>
<title>Answered: Gold is a precious metal</title>
<link>https://notexponential.com/623/gold-is-a-precious-metal?show=688#a688</link>
<description>&lt;p style=&quot;margin-top: 0px; font-family: Helvetica, Arial, sans-serif; font-size: 14px;&quot;&gt;&lt;span style=&quot;font-size:14px&quot;&gt;&lt;span style=&quot;font-family:Times New Roman,Times,serif&quot;&gt;Notation:&amp;nbsp;let f[i][v] means&amp;nbsp;the max value for the total i choice along&amp;nbsp;with the total v length.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;margin-top: 0px; font-family: Helvetica, Arial, sans-serif; font-size: 14px;&quot;&gt;&lt;span style=&quot;font-size:14px&quot;&gt;&lt;span style=&quot;font-family:Times New Roman,Times,serif&quot;&gt;Recursion: &amp;nbsp;&lt;span style=&quot;color:#ff0000&quot;&gt;f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;margin-top: 0px; font-size: 14px;&quot;&gt;&lt;span style=&quot;color:null&quot;&gt;&lt;span style=&quot;font-family:Times New Roman, Times, serif&quot;&gt;Algorithm:&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;margin-top: 0px; margin-bottom: 0px; font-size: 12px; color: rgb(0, 0, 0); font-family: &amp;quot;Courier New&amp;quot; !important;&quot;&gt;&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;line-height:1.5 !important&quot;&gt; main()
{
    &lt;/span&gt;&lt;span style=&quot;color:#008000; line-height:1.5 !important&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color:#008000; line-height:1.5 !important&quot;&gt;int m = 120;
    &lt;/span&gt;&lt;span style=&quot;color:#008000; line-height:1.5 !important&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color:#008000; line-height:1.5 !important&quot;&gt;int n = 5;
    &lt;/span&gt;&lt;span style=&quot;color:#008000; line-height:1.5 !important&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color:#008000; line-height:1.5 !important&quot;&gt;vector&amp;lt;int&amp;gt; w = { 0, 40, 50, 70, 40, 20 };
    &lt;/span&gt;&lt;span style=&quot;color:#008000; line-height:1.5 !important&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color:#008000; line-height:1.5 !important&quot;&gt;vector&amp;lt;int&amp;gt; v = { 0, 10, 25, 40, 20, 10 };&lt;/span&gt;

    &lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;int&lt;/span&gt; m, n;    
    &lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;while&lt;/span&gt; (cin &amp;gt;&amp;gt; m &amp;gt;&amp;gt;&lt;span style=&quot;line-height:1.5 !important&quot;&gt; n)
    {
        vector&lt;/span&gt;&amp;lt;&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;int&lt;/span&gt;&amp;gt; w(n + &lt;span style=&quot;color:#800080; line-height:1.5 !important&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color:#800080; line-height:1.5 !important&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;line-height:1.5 !important&quot;&gt;);
        vector&lt;/span&gt;&amp;lt;&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;int&lt;/span&gt;&amp;gt; v(n + &lt;span style=&quot;color:#800080; line-height:1.5 !important&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color:#800080; line-height:1.5 !important&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;line-height:1.5 !important&quot;&gt;);
        &lt;/span&gt;&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;int&lt;/span&gt; i = &lt;span style=&quot;color:#800080; line-height:1.5 !important&quot;&gt;1&lt;/span&gt;; i &amp;lt;= n; i++&lt;span style=&quot;line-height:1.5 !important&quot;&gt;)
        {
            &lt;/span&gt;&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;line-height:1.5 !important&quot;&gt; tmp;
            cin &lt;/span&gt;&amp;gt;&amp;gt;&lt;span style=&quot;line-height:1.5 !important&quot;&gt; tmp;
            w[i] &lt;/span&gt;=&lt;span style=&quot;line-height:1.5 !important&quot;&gt; tmp;
        }
        &lt;/span&gt;&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;int&lt;/span&gt; i = &lt;span style=&quot;color:#800080; line-height:1.5 !important&quot;&gt;1&lt;/span&gt;; i &amp;lt;= n; i++&lt;span style=&quot;line-height:1.5 !important&quot;&gt;)
        {
            &lt;/span&gt;&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;line-height:1.5 !important&quot;&gt; tmp;
            cin &lt;/span&gt;&amp;gt;&amp;gt;&lt;span style=&quot;line-height:1.5 !important&quot;&gt; tmp;
            v[i] &lt;/span&gt;=&lt;span style=&quot;line-height:1.5 !important&quot;&gt; tmp;
        }
        vector&lt;/span&gt;&amp;lt; vector&amp;lt;&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;int&lt;/span&gt;&amp;gt; &amp;gt; vec(n + &lt;span style=&quot;color:#800080; line-height:1.5 !important&quot;&gt;1&lt;/span&gt;, vector&amp;lt;&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;int&lt;/span&gt;&amp;gt;(m + &lt;span style=&quot;color:#800080; line-height:1.5 !important&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color:#800080; line-height:1.5 !important&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;line-height:1.5 !important&quot;&gt;));
        &lt;/span&gt;&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;int&lt;/span&gt; i = &lt;span style=&quot;color:#800080; line-height:1.5 !important&quot;&gt;1&lt;/span&gt;; i &amp;lt;= n; i++&lt;span style=&quot;line-height:1.5 !important&quot;&gt;)
        {
            &lt;/span&gt;&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;int&lt;/span&gt; j = &lt;span style=&quot;color:#800080; line-height:1.5 !important&quot;&gt;1&lt;/span&gt;; j &amp;lt;= m; j++&lt;span style=&quot;line-height:1.5 !important&quot;&gt;)
            {
                &lt;/span&gt;&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;if&lt;/span&gt; (w[i] &amp;gt;&lt;span style=&quot;line-height:1.5 !important&quot;&gt; j)
                    vec[i][j] &lt;/span&gt;= vec[i - &lt;span style=&quot;color:#800080; line-height:1.5 !important&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;line-height:1.5 !important&quot;&gt;][j];
                &lt;/span&gt;&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;line-height:1.5 !important&quot;&gt;
                {
                    &lt;/span&gt;&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;int&lt;/span&gt; tmp1 = v[i] + vec[i - &lt;span style=&quot;color:#800080; line-height:1.5 !important&quot;&gt;1&lt;/span&gt;][j -&lt;span style=&quot;line-height:1.5 !important&quot;&gt; w[i]];
                    &lt;/span&gt;&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;int&lt;/span&gt; tmp2 = vec[i - &lt;span style=&quot;color:#800080; line-height:1.5 !important&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;line-height:1.5 !important&quot;&gt;][j];
                    vec[i][j] &lt;/span&gt;= tmp1 &amp;gt; tmp2 ?&lt;span style=&quot;line-height:1.5 !important&quot;&gt; tmp1 : tmp2;
                }
            }
        }
        &lt;/span&gt;&lt;span style=&quot;color:#0000ff; line-height:1.5 !important&quot;&gt;double&lt;/span&gt; val = vec[n][m] * &lt;span style=&quot;color:#800080; line-height:1.5 !important&quot;&gt;0.1&lt;/span&gt;&lt;span style=&quot;line-height:1.5 !important&quot;&gt;;
        cout &lt;/span&gt;&amp;lt;&amp;lt; val &amp;lt;&amp;lt;&lt;span style=&quot;line-height:1.5 !important&quot;&gt; endl;
    }

    system(&lt;/span&gt;&lt;span style=&quot;color:#800000; line-height:1.5 !important&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#800000; line-height:1.5 !important&quot;&gt;pause&lt;/span&gt;&lt;span style=&quot;color:#800000; line-height:1.5 !important&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;line-height:1.5 !important&quot;&gt;);
}&lt;/span&gt;
&lt;/pre&gt;</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/623/gold-is-a-precious-metal?show=688#a688</guid>
<pubDate>Wed, 08 May 2019 16:43:10 +0000</pubDate>
</item>
<item>
<title>Answered: If you are happy and you know it, jump up high!</title>
<link>https://notexponential.com/502/if-you-are-happy-and-you-know-it-jump-up-high?show=676#a676</link>
<description>A simple retreat, you only have three actions to reach the final state</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/502/if-you-are-happy-and-you-know-it-jump-up-high?show=676#a676</guid>
<pubDate>Thu, 02 May 2019 06:10:51 +0000</pubDate>
</item>
<item>
<title>Answered: Around the block party planning</title>
<link>https://notexponential.com/499/around-the-block-party-planning?show=672#a672</link>
<description>You must traverse, so it is impossible to be better than O(n)</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/499/around-the-block-party-planning?show=672#a672</guid>
<pubDate>Thu, 02 May 2019 05:55:52 +0000</pubDate>
</item>
<item>
<title>Answered: Maximum Value Contiguous Subregion</title>
<link>https://notexponential.com/476/maximum-value-contiguous-subregion?show=665#a665</link>
<description>First judge what kind of rectangle I want to get. If the negative numbers are the majority, the rectangle is very small. On the contrary, this rectangle is very large. Choose from the sum of the rectangle. The worst case is the negative number is not the majority but very small or otherwise. But divide into two situations will quicker in most case.&lt;br /&gt;
&lt;br /&gt;
If it is a large rectangle, I will shrink from the outside to the inside, as long as the sum of the reduced circles is negative, continue to shrink. If it is positive, check the negative number in the circle and the size of the circle. If it is still regular, Otherwise continue until the confirmation is correct, going back to the best known result.&lt;br /&gt;
&lt;br /&gt;
If it is a small rectangle, it expands from the largest positive number, and each positive number expands to the best result when the total is negative, until the traversal of all positive possibilities.</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/476/maximum-value-contiguous-subregion?show=665#a665</guid>
<pubDate>Thu, 02 May 2019 04:55:43 +0000</pubDate>
</item>
<item>
<title>Answered: Cost Minimizing Ice Cream Shop</title>
<link>https://notexponential.com/493/cost-minimizing-ice-cream-shop?show=656#a656</link>
<description>In fact, it is far more complicated than your model. The shipped goods are not necessarily consumed in one day. The consumption of the same volume of ice cream is not linear.</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/493/cost-minimizing-ice-cream-shop?show=656#a656</guid>
<pubDate>Thu, 02 May 2019 02:49:47 +0000</pubDate>
</item>
<item>
<title>Answered: Make change using smallest number of coins of given denominations</title>
<link>https://notexponential.com/390/make-change-using-smallest-number-coins-given-denominations?show=588#a588</link>
<description>&lt;p&gt;Would this be a greedy approach? You are taking the optimum number of higher value coins in each iteration&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Notation:&amp;nbsp; &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;M = value to make change for&lt;/p&gt;&lt;p&gt;C = the number of coins used&lt;/p&gt;&lt;p&gt;V = coin denominations, V[i] gives the value for i-th coin&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i = k to 1&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while M &amp;gt;= V[i]&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; C += 1&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; M -= V[i]&lt;/p&gt;</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/390/make-change-using-smallest-number-coins-given-denominations?show=588#a588</guid>
<pubDate>Thu, 12 Jul 2018 03:38:15 +0000</pubDate>
</item>
<item>
<title>Answered: 1-D k-means clustering</title>
<link>https://notexponential.com/531/1-d-k-means-clustering?show=561#a561</link>
<description>Sorry, I just found that I can not prove the optimility of this solution. Maybe It is not an optimal &amp;nbsp;solution.</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/531/1-d-k-means-clustering?show=561#a561</guid>
<pubDate>Wed, 09 May 2018 02:41:33 +0000</pubDate>
</item>
<item>
<title>Answered: Canoeing on the cheap</title>
<link>https://notexponential.com/500/canoeing-on-the-cheap?show=533#a533</link>
<description>I guess this is a example of All Pair Shartest Path Dynamic Programming algorithm where we have to return the Shortest Path from 1 to N</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/500/canoeing-on-the-cheap?show=533#a533</guid>
<pubDate>Thu, 08 Mar 2018 19:32:33 +0000</pubDate>
</item>
<item>
<title>Answered: Calculate g(1000) given the following definition of g function..</title>
<link>https://notexponential.com/460/calculate-1000-given-the-following-definition-of-function?show=483#a483</link>
<description>This problem is very similar to the fib by using dynamic programming. &lt;br /&gt;
&lt;br /&gt;
The solution is using tabulation. &lt;br /&gt;
&lt;br /&gt;
memo=[1]*1001&lt;br /&gt;
memo[0]=0&lt;br /&gt;
def g(n):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if n&amp;lt;=0:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return memo[0]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;elif n&amp;lt;=10:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return memo[n]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for i in range(11,n+1):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;memo[i]=memo[i-1]+3*memo[i-3]+7*memo[i-10]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return memo[n]&lt;br /&gt;
&lt;br /&gt;
print(g(1000))</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/460/calculate-1000-given-the-following-definition-of-function?show=483#a483</guid>
<pubDate>Sun, 22 Oct 2017 00:28:23 +0000</pubDate>
</item>
<item>
<title>Answered: Maximum Value But Limited Neighbors</title>
<link>https://notexponential.com/466/maximum-value-but-limited-neighbors?show=473#a473</link>
<description>&lt;h3&gt;Notation&lt;/h3&gt;&lt;p&gt;MVBLN(i,m): Maximum value sum that can be achieved from the array A[1..i], using adjacent 1s at most m times.&lt;/p&gt;&lt;h3&gt;Recurrence Relation&lt;/h3&gt;&lt;p&gt;MVBLN(i,m) = max{MVBLN(i-1,m), MVBLN(i-1,m-1) + A[i], MVBLN(i-2,m) + A[i]}&lt;/p&gt;&lt;p&gt;Base Case: MVBLN(i,0) can be solved separately, using a separate problem, that we can call maximum value no neighbors.&amp;nbsp; MVNN(i) = max{MVNN(i-1), MVNN(i-2) + A[i]}. MVNN(0) = 0.&amp;nbsp; MVNN(1) = a[1].&amp;nbsp; MVNN(2) = max{a[1], a[2]}.&lt;/p&gt;&lt;h3&gt;Optimality&lt;/h3&gt;&lt;p&gt;MVBLN(i,m) is either an extension of MVBLN(i-1,m-1) that includes A[i], or extends MVBLN(i-2,m) that includes A[i], or something that uses MVBLN(i-1,m) and just ignores A[i].&lt;/p&gt;&lt;h2&gt;&lt;/h2&gt;</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/466/maximum-value-but-limited-neighbors?show=473#a473</guid>
<pubDate>Tue, 18 Jul 2017 21:02:06 +0000</pubDate>
</item>
<item>
<title>Answered: Magical eggs and tiny floors</title>
<link>https://notexponential.com/180/magical-eggs-and-tiny-floors?show=463#a463</link>
<description>&lt;p&gt;We can use dynamic programming to solve this problem.&lt;/p&gt;&lt;p&gt;Notation: M[i][j]: The minimized number of throws when we have i eggs and j floors to test. We also have the base case: M[n][1]=1, M[1][n]=n.( Note: If we just have 1 floor, we only have to throw&amp;nbsp;one time; If we have 1 egg and n floor to try, we can start at the first floor and we may have n throws in the worst case.)&lt;/p&gt;&lt;p&gt;Optimality: We can throw the egg from k floor at first. If the egg is broken, we will test the remaining (j-k) floors in the worst case; if the egg is not broken, we will test the remaining (k-1) floors in the worst case.&lt;/p&gt;&lt;p&gt;Recurrence relation: M[i][j] = min{max{M[i-1][k-1],M[i][j-k]} + 1} &amp;nbsp;( Note: We use the max at here because we need to consider the worst case of them. )&lt;/p&gt;&lt;p&gt;Algorithm:&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family:helvetica,arial,sans-serif; font-size:14px&quot;&gt;for i = 1 to m&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;font-family:helvetica,arial,sans-serif; font-size:14px&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for j = 1 to n&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;font-family:helvetica,arial,sans-serif; font-size:14px&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for k = 1 to n&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;font-family:helvetica,arial,sans-serif; font-size:14px&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size:14px&quot;&gt;M[i][j] = min{max{M[i-1][k-1],M[i][j-k]} + 1}&amp;nbsp; &lt;/span&gt;&lt;/p&gt;</description>
<category>Dynamic Programming</category>
<guid isPermaLink="true">https://notexponential.com/180/magical-eggs-and-tiny-floors?show=463#a463</guid>
<pubDate>Mon, 19 Jun 2017 18:26:49 +0000</pubDate>
</item>
</channel>
</rss>