ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import bisect
def lis(a):
t = []
for x in a:
i = bisect.bisect_left(t, x)
if i == len(t):
t.append(x)
else:
t[i] = x
return len(t)
print(lis([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]))- A. 5
- B. 3
- C. 4
- D. 6
Correct answer: C. 4
The list t holds the smallest possible tail for each strictly increasing length, and bisect_left finds where each value goes. Its final length 4 equals the longest strictly increasing subsequence, such as 1, 4, 5, 9.
More Python Programming questions
- A list a is sorted in ascending order and may contain duplicates. Using bisectleft and bisectright from the bisect module, which…
- A Python list q holds n items. A loop repeatedly does x = q.pop(0) until q is empty. What is the total time cost of the loop in big-O terms?
- What is printed? python def rob(a): p, c = 0, 0 for x in a: p, c = c, max(c, p + x) return c print(rob([2, 7, 9, 3, 1]), rob([5]), rob([]))
- What is printed? python def ed(a, b): prev = list(range(len(b) + 1)) for i in range(1, len(a) + 1): cur = [i] + [0] len(b) for j in…
- What is printed? python def minlen(a, t): best = float('inf') s = 0 l = 0 for r, x in enumerate(a): s += x while s >= t: best = min(best…
- What is printed? python import heapq def kthlargest(a, k): h = [] for x in a: heapq.heappush(h, x) if len(h) > k: heapq.heappop(h) return…