straints 1 ≤ n ≤ 5 × 104 1 ≤ len(c[i]) ≤ 5 There are at most 30 distinct colours in c. Time Limit Your program has to finish running within 4 seconds on any valid input. Sample Input 1 green red red blue red red gre

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

CODE IN PYTHON

Colouring Book

Having spent the last couple of hours grading assignments, I decide it’s time for a break. I take out my favourite colouring book, turn to a random page I haven’t coloured in yet, and lay it on my desk. I then take out all my n crayons and line them up on the desk (it’s a very long desk). The colour of the ith crayon is a string c[i] (e.g. "blue"). Many of the crayons have the same colour. In fact, no matter how many crayons I have, there are at most 30 distinct colours amongst them.

To start colouring, I always take a sublist (see Q1 for a definition) of the crayons laid on the desk and put away the rest (too many options can be overwhelming and can lead to indecision).

I take a look at the line art in front of me and wonder, “How many different colours do I need to make this look great? One? Two? Maybe three?”.

Now you understand my dilemma and are fully aware of my indecision. You kindly decide to help me out by telling me for every number k, if I were to use k distinct colours, what would be the minimum number of crayons I’d need to take?

Filename

Your filename for this question must be q4.py.

Input

The input consists of a single line containing n space-separated names of colours specifying the list c.

Output

Let m be the number of distinct colours in c. Then, output m space-separated integers. The kth of them (1-based) should be the minimum number of crayons I’d need to take so that there are at least k distinct colours amongst them.

Constraints

  • 1 ≤ n ≤ 5 × 104
  • 1 ≤ len(c[i]) ≤ 5
  • There are at most 30 distinct colours in c.

Time Limit

  • Your program has to finish running within 4 seconds on any valid input.

Sample Input 1

green red red blue red red green

Sample Output 1

1 2 4

Sample 1 Explanation

  • If I wanted to use only 1 colour, I could take any single crayon.
  • If I wanted to use 2 distinct colours, I could take a sublist of length 2 such as the sublist red blue.
  • If I wanted to use all three colours, the smallest possible sublist would have length 4. For example, I could take blue red red green.

Sample Input 2

r g z g b b r r g y g g y b

Sample Output 2

1 2 3 5 8

Hints

  • There are several ways to solve this problem, but not all of them are fast enough to pass the biggest test cases within the time limit.
  • For a 100% solution, here’s a starting point (not needed for the 70% solution): as you work through the colours, what if you knew the index of the most recent occurrence of each colourDefinition:
    • sublist of a list a is any list you can obtain by removing some (possibly 0) elements from the start of a and then removing some (possibly 0) elements from the end of it. (It’s like the definition of segment from lecture.)
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

could you provide this in a python code

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Computational Systems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education