Skip to content

Instantly share code, notes, and snippets.

Set Min[i] equal to Infinity for all of i
Min[0]=0
// Solution 1
For i = 1 to S
For j = 0 to N - 1
If (Vj<=i AND Min[i-Vj]+1<Min[i])
Then Min[i]=Min[i-Vj]+1
// Solution 2
/* Initialize LIS values for all indexes */
for (i = 0; i < n; i++ )
lis[i] = 1;
/* Compute optimized LIS values in bottom up manner */
for (i = 1; i < n; i++ )
for (j = 0; j < i; j++ )
if ( arr[i] > arr[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
If X[m-1] == Y[n-1] then
L(X[0..m-1], Y[0..n-1]) = 1 + L(X[0..m-2], Y[0..n-2])
Else
L(X[0..m-1], Y[0..n-1]) = MAX ( L(X[0..m-2], Y[0..n-1]), L(X[0..m-1], Y[0..n-2])
// Implementation
int L[n+1][m+1];
// Start
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; i++) {
n = input()
a = [input() for _ in xrange(n)]
c, desc_buf = [1]*n, []
for i in xrange(1, n):
if a[i] < a[i-1]:
if not desc_buf:
desc_buf = [i-1]
desc_buf.append(i)
if not i == n-1:
int max_so_far = INT_MIN;
int max_end_here = 0;
for(int i=0; i<size; i++) {
max_end_here += A[i];
max_so_far = MAX(max_end_here, max_so_far);
max_end_here = MAX(0, max_end_here);
}
int maxNonCon = A[0];
for(int i=1; i<size; i++) {
Initialize:
max_so_far = 0
max_ending_here = 0
Loop for each element of the array
(a) max_ending_here = max_ending_here + a[i]
(b) if(max_ending_here < 0)
max_ending_here = 0
(c) if(max_so_far < max_ending_here)
max_so_far = max_ending_here
int maxRight[size-1];
maxRight[size-2] = price[size-1];
for(int i=size-3; i>=0; i--) {
maxRight[i] = MAX(price[i+1], maxRight[i+1]);
}
long long profit = 0;
for(int i=0; i<size-1; i++) {
if(maxRight[i] > price[i]) {
profit += maxRight[i] - price[i];
1) Construct L[m+1][n+1] using the steps discussed in previous post.
2) The value L[m][n] contains length of LCS. Create a character array lcs[] of length equal to the length of lcs plus 1 (one extra to store \0).
2) Traverse the 2D array starting from L[m][n]. Do following for every cell L[i][j]
…..a) If characters (in X and Y) corresponding to L[i][j] are same (Or X[i-1] == Y[j-1]), then include this character as part of LCS.
…..b) Else compare values of L[i-1][j] and L[i][j-1] and go in direction of greater value.
int max = L[n][m];
int i = n;
// We need n+1 rows as the table is consturcted in bottom up manner using
// the base case 0 value case (n = 0)
int table[n+1][m];
// Fill the enteries for 0 value case (n = 0)
for (i=0; i<m; i++)
table[0][i] = 1;
// Fill rest of the table enteries in bottom up manner
for (i = 1; i < n+1; i++)
int M[N+1];
M[0] = 0;
for(int i=0; i<=N; i++) {
if(i < 4) {
M[i] = 1;
} else {
M[i] = M[i-1] + M[i-4];
}
}