Sei sulla pagina 1di 1

Algorithms: CSE 202 — Additional problems along the lines of

homework I problems

Problem 1: Skylines
In a video game, a city’s buildings are rectangles along an axis, specified as Bi = (si , fi , hi ), for
1 ≤ i ≤ n, for a building starting at x = si and going to x = fi at height hi . When seen from a
distance, the graphics is supposed to plot the skyline, which at each vertical point x has height the
height of the tallest building Bi with si ≤ x ≤ fi . Give an algorithm to plot the skyline, as a series
of points connected by line segments. Assume n is a power of 2.

Problem 2: Tree isomorphism


Give an efficient algorithm to decide, given two rooted binary trees T and T 0 , whether T and T 0
are isomorphic as rooted trees.

Problem 3: Computing mode


The mode of a set of numbers is the number that occurs most frequently in the set. The set
(4,6,2,4,3,1) has a mode of 4.
1. Give an efficient and correct algorithm to compute the mode of a set of n numbers;
2. Suppose we know that there is an (unknown) element that occurs bn/2c + 1 times in the set.
Give a worst-case linear-time algorithm to find the mode. For partial credit, your algorithm
may run in expected linear time.

Problem 4: k-th smallest element


You are given two sorted lists of size m and n. Give an O(log m + log n) time algorithm for
computing the k’th smallest element in the union of the two lists.

Problem 5: Finding the missing integer


An array A[1..n] contains all the integers from 0 to n except one. It would be easy to determine
the missing integer in O(n) time by using an auxiliary array B[0..n] to record which numbers appear
in A. In this problem, we cannot access an entire integer in A with a single operation. The elements
of A are represented in binary, and the only operation we can use to access them is “fetch the jth
bit of A[i],” which takes constant time.
Show that if we use only this operation, we can still determine the missing integer in O(n) time.

Potrebbero piacerti anche