Data Structures & AlgorithmsHardTCS Digital · Infosys SP
Daily prices are [15, 17, 19, 13, 20, 17, 38, 38, 40]. What is the maximum profit with at most ONE buy and one later sell, and with UNLIMITED non-overlapping transactions?
- A. 27 and 33
- B. 27 and 27
- C. 34 and 34
- D. 27 and 34
Correct answer: D. 27 and 34
One transaction: track the minimum price so far and the best difference (27) — max − min fails when the minimum comes after the maximum. Unlimited: add every positive day-to-day rise (34).
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Using a sliding window with a last-seen index map, what is the length of the longest substring of "dbdcccdcbaddad" with all characters…
- Two strings are isomorphic if characters can be replaced one-to-one to turn one into the other. Version A checks both directions; version…
- Swap every two adjacent nodes of a linked list by relinking (not by swapping values). What is printed? python def swappairs(h): d = p =…
- Count distinct values in every window of size k with a frequency map. What is printed? python from collections import Counter a, k = [3…
- A min-stack keeps a second stack of running minimums. What is printed? python st, mn, out = [], [], [] for op in [7, 5, 7, 5, 5, "pop"…
- A hash-map two-sum returns the first index pair (i, j), i < j, found while scanning left to right. What is printed? python def twosum(a…