ProgrammingVery hardCAT · MBA placement
What is printed?
What is printed?
def cool(p):
hold, sold, rest = float('-inf'), 0, 0
for x in p:
hold, sold, rest = max(hold, rest - x), hold + x, max(rest, sold)
return max(sold, rest)
print(cool([1, 2, 3, 0, 2]))- A. 2
- B. 5
- C. 4
- D. 3
Correct answer: D. 3
Three states track holding, just sold and resting, and a sale must be followed by a rest day. The best plan buys at 1, sells at 2, rests, buys at 0 and sells at 2, for a total profit of 3.
More Python Programming questions
- What is printed? python import bisect def inv(a): s, c = [], 0 for x in a: c += len(s) - bisect.bisectright(s, x) bisect.insort(s, x)…
- What is printed? python import bisect a = [10, 20, 20, 30] print([bisect.bisectleft(a, x) for x in (20, 25, 5, 40)])
- What is printed? python from functools import lrucache @lrucache(maxsize=None) def grid(r, c): if r == 0 or c == 0: return 1 return grid(r…
- What is printed? python def cancomplete(gas, cost): total = tank = start = 0 for i in range(len(gas)): d = gas[i] - cost[i] total += d…
- What is printed? python x = 0b101100 print(x & (x - 1), x & -x, bin(x).count('1'))
- What is printed? python M = 1000000007 print(pow(2, -1, 7), pow(3, 4, 5), (pow(10, 9, M) 2) % M)