Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Assignment 1 – Hints and Clarifications

COMP3121/9101 22T1
Released February 15, due March 3

This document provides some hints to help you solve the problems in Assignment 1. You
are not required to follow these hints, and there may be alternate solutions which are
equally correct.
Also included are the clarifications listed in the Assignment 1 FAQ on the Ed forum.
Further clarifications may be added after this document is released.
General clarifications:
• How should I format/explain my algorithms? Is pseudocode required?
Are diagrams/examples allowed?
Explain the steps of your algorithm, and the logic behind these steps (when not
obvious) in plain English if possible. Pseudocode can be used to supplement an
answer, but in many cases it is less clear and harder to mark. Diagrams and
examples are also fine to include, but again as a supplement; your solution should
not rely on them. Any submitted code in any programming language will be ignored.
The solutions to the first set of tutorial exercises will designate some problems
as ’[A]’, meaning that the solution is written to the standard of an assignment
submission. The solutions provided for other problems may be only an outline.
• What data structures and algorithms can I use?
In addition to the new material in this course, you can use any data structure or
algorithm from Lecture 2. If you are using them in their generic form, there is no
need to reprove any of their properties, and you can simply quote the operations
including their time complexity. If you need to modify the structure or algorithm,
you must provide details of the modification.
Note that you should refer to these data structures and algorithms in abstract terms
(e.g. hash table) as opposed to their implementation in any particular programming
language (e.g. HashMap).
• Do the requested time complexities mean expected or worst case?
By default, we mean worst case. We will always explicitly specify if we want the
expected time.
• Is this on the right track? Can you give any additional hints? What
have I missed/done wrong?
Sorry, we can’t comment on these types of questions from individual students.
Hints will be provided halfway between the release date and the due date of each
assignment.

1
1. (20 points) You are playing a level of the latest smash-hit kung-fu video game. The
level is composed of a sequence of n rooms filled with enemies. The hero must
go through all n rooms in order, fighting all the enemies in each room. The level is
completed when the hero exits the final room. For each room i, you know the number
of times ai that the hero will die in that room.
Furthermore, this game has a very interesting death mechanic. The hero starts at age
20, and each time they die, their age is increased by the value of the death counter,
which is the number of times that the hero has died. For example, after the first,
second and third deaths, the hero’s age becomes 21, 23 and 26 respectively. Moreover,
before the hero enters any room, you can reset the death counter to 0, though this
ability may only be used once in the entire level.
Design an algorithm which runs in O(n) time and calculates the minimum age at
which the hero can finish the level.
Clarifications:

• What input is given in an instance of the problem?


The integer n as well as n integers ai .
• Should I store the ai in an array?
You can either refer to the ai directly, or create a structure (such as an array)
to store all of them.
• Are the ai all equal?
Not necessarily.
• Is there any pattern among the ai , e.g. ai = i for all i?
Not necessarily.
• What values do the ai take?
Each of the ai can be any non-negative integer.
• What happens each time the hero dies?
The only effect when the hero dies is that the age is increased. The hero is not
reset to the beginning of the current room, nor are they reset to the beginning
of the level.
• Does resetting the death counter also reset the hero’s age to 20?
No. If the death counter is reset when the hero is age 30, then subsequent deaths
will increase their age to 31, then 33, then 36 and so on.

Hint: For each room i, can you calculate the final age if the death counter is reset
upon entering room i? There are n such calculations to make, so each must be solved
in constant time on average.

Page 2
2. You are given an array A consisting of n integers, each between 1 and M inclusive.
You are guaranteed that no more than k distinct values appear in the array.
You are required to identify which of the values between 1 and M appear in the array,
and for each such value, how many times it appears. Design algorithms which solve
this problem and run in:
(a) (5 points) worst case Θ(n + M ) time;
(b) (5 points) worst case Θ(n log n) time;
(c) (5 points) worst case Θ(n log k) time;
(d) (5 points) expected Θ(n) time.
Clarifications:

• What input is given in an instance of the problem?


The positive integers n, M and k, as well as the array A. You are guaranteed
that k ≤ M and k ≤ n.
• How should I output the answer?
Any format or data structure which fully answers the question is fine.
• Why are the target time complexities specified using Θ(. . .) rather
than O(. . .)?
In most problems in this course, we will accept an algorithm which runs within
the target time complexity or better. If a question asks for an O(n2 ) algorithm,
a linear time algorithm is also acceptable.
In this problem however, we want you to produce algorithms with exactly the
target time complexity. We would not accept a linear time algorithm if the
question asks for Θ(n2 ). You also must not artificially inflate the time complexity
of your algorithm.
• Where the target time complexity does not include all three variables,
can I treat the others as constant? For example, is a Θ(nM log k)
algorithm acceptable for (c)?
No. Where the target time complexity includes only n and k, the complexity
of your algorithm should only depend on n and k, and be independent of M .
There is no relationship guaranteed between these three variables, except that
k ≤ M and k ≤ n.
• Can I create another array of size n?
If you wish to do so, keep in mind that creating and initialising such an array
takes Θ(n) time.

Hint: Consider the various data structures and sorting algorithms presented in Lec-
ture 2.
Hint: For parts (b) and (c), try to develop algorithms which repeat a step n times,
each time taking Θ(log n) or Θ(log k) respectively.
Hint: For part (d), note that expected time complexity (also known as average case)
was only discussed in relation to one data structure and one algorithm.

Page 3
3. (20 points) You are given an n by n matrix of distinct integers. A summit is an
element that is larger than all its neighbours in each of the (up to) 4 directions.
Design an algorithm which runs in O(n log n) time and finds the location of any
summit.
Note that integers in the corners and boundaries can be summits, and there will al-
ways be at least one summit, the maximum element. However, you do not necessarily
have to find this specific summit.
Clarifications:

• What input is given in an instance of the problem?


The integer n and a two-dimensional (n by n) array representing the matrix.
• If there are several summits, do I have to identify all of them?
No. Your task is to identify any one of the (potentially many) summits.

Hint: Starting from an arbitrary element of the matrix with value x, can you find a
path to some peak with value ≥ x?
Hint: Use divide and conquer. How can you reduce the search space to only the left
half or only the right half of the matrix?

4. (20 points) You have n poles of distinct heights spaced in a line, the ith of which is
hi metres tall. You can wire together two poles i < j only if they are both taller than
all poles in between them, that is, for each i < k < j, hk < hi and hk < hj .
Design an algorithm which runs in O(n log n) time and calculates the total number
of pairs of poles that you can wire together.
Clarifications:

• What input is given in an instance of the problem?


The integer n, as well as the n integers hi .
• Can two consecutive poles be wired together?
Yes.
• Can a particular pole be wired to several other poles?
Yes. A pole can be wired to any number of other poles, including poles to both
its left and its right.

Hint: Consider pairs entirely within the left half of the sequences, pairs entirely
within the right half, and pairs crossing the halfway point.

Page 4

You might also like