Maximum Profit- Memoization,DP,optimality
Date : March 29 2020, 07:55 AM
With these it helps I assume that you would like to solve this one by yourself, so I will give you a hint: this problem has optimal substructure. Imagine that you have both solutions for N-1 coins (without the leftmost one and without the rightmost one). Would it be easy to calculate a solution for N coins then? // solved[L][R] array contains the highest value a player could get
// on a subproblem where L coins are missing from the left
// and R are missing from the right of the original sequence on his move
int solved[N][N] // initialize each element to -1.
int coins[N] // initialize with coin values
int solve(int L, int R) {
if (L+R == N) return 0; // No coins remain
if (solved[L][R] >= 0) return solved[L][R];
int remaining = sum(coins from L to R)
int takeLeft = remaining - solve(L+1, R);
int takeRight = remaining - solve(L, R+1);
int result = max(takeLeft, takeRight);
solved[L][R] = result;
return result;
}
main() {
int alice = solve(0, 0);
int bob = sum(coins) - alice;
}
|
Minimum / maximum required profit on a day
Tag : excel , By : artifex
Date : March 29 2020, 07:55 AM
This might help you The original AGGREGATE function was using its LARGE and SMALL subfunctions to directly return the true numbers. If you need to return a text-based result, AGGREGATE must return the ROW number of the matching value and pass that to an INDEX function. In H2 as a standard formula, =IFERROR(INDEX(B:B, AGGREGATE(15, 6, ROW(B$2:INDEX(B:B, MATCH(1E+99,D:D )))/((A$2:INDEX(A:A, MATCH(1E+99,D:D ))=G2)*
(C$2:INDEX(C:C, MATCH(1E+99,D:D ))=AGGREGATE(14, 6, C$2:INDEX(C:C, MATCH(1E+99,D:D ))/(A$2:INDEX(A:A, MATCH(1E+99,D:D ))=G2), 1))*
(D$2:INDEX(C:C, MATCH(1E+99,D:D ))=AGGREGATE(14, 6, D$2:INDEX(D:D, MATCH(1E+99,D:D ))/(A$2:INDEX(A:A, MATCH(1E+99,D:D ))=G2), 1))), 1))
, "nil")
|
Shortest Path Maximum Profit
Date : March 29 2020, 07:55 AM
Any of those help Instead of storing a single value for each candidate path in Dijkstra's algorithm, store a tuple of (total length, total profit). Consider a path shorter in the relaxation step if the total length is less, or the total lengths are the same but the total profit is higher.
|
maximum profit on selling shares
Date : March 29 2020, 07:55 AM
Any of those help Clearly, for any price we can buy, we would want to sell it at the highest price. Fortunately, we are given that highest price. So, iterating backwards, we know the highest future price seen at any point we visit in our travel "back in time." Python code: def stockmax(prices):
n = len(prices)
highest = prices[n - 1]
m = [0] * n
# Travel back in time,
# deciding whether to buy or not
for i in xrange(n - 2, -1, -1):
# The most profit buying stock at this point
# is what we may have made the next day
# (which is stored in m[i + 1])
# and what we could make if we bought today
m[i] = m[i + 1] + max(
# buy
highest - prices[i],
# don't buy
0
)
# Update the highest "future price"
highest = max(highest, prices[i])
return m[0]
|
Displaying steps to maximum profit
Tag : cpp , By : Daniel Reslie
Date : September 28 2020, 07:00 PM
like below fixes the issue You can simply consider a weighted graph G where a node is a job a node A is linked to a node B if A.endTime < B.startTime weight of edge(A,B) is B.profit (taking the path to B means doing job B) def get_longest_path(node):
if !node.children
return 0
best_all = {
w: weight(node, node.children[0]),
path: [node, get_longest_path(node.children[0])]
}
for node.children as child //starting from 1
best_path_i = get_longest_path(child)
//if we found a path with lower total weight (that is, with maximal profit)
if best_path_i != 0 && best_path_i.weight < best_all.weight
best_all = {
w: weight(node, child),
path:[node, best_path_i]
}
return best_all
get_longest_path(root)
cache = {}
def get_longest_path(node):
if !node.children
return 0
//node.id is jobId
if node.id in cache
return cache[node.id]
best_all = {
w: weight(node,node.children[0]),
path: [node, get_longest_path(node.children[0])]
}
for node.children as child //starting from 1
best_path_i = get_longest_path(child)
//if we found a path with lower total weight (that is, with maximal profit)
if best_path_i != 0 && best_path_i.weight < best_all.weight
best_all = {
w: weight(node, child),
path:[node, best_path_i]
}
cache[node.id] = best_all
return best_all
get_longest_path(root)
|