Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

Implement the Greedy Algorithm discussed for the problem below, in
C++.

Using C++, implement the given problem below. (Greedy Algorithm)

Given an array of size n that has the following specifications:
1.Each element in the array contains either a policeman or a thief.
2.Each policeman can catch only one thief.
3.A policeman cannot catch a thief who is more than K units away from the policeman. We need to find the maximum number of thieves that can be caught. 

Examples:

Input arr[] = {'P', 'T', 'T', 'P', 'T'}, k = 1.
Output: 2.
Here maximum 2 thieves can be caught, first policeman catches first thief and second police- man can catch either second or third thief.
Input = arr[] = {'T', 'T', 'P', 'P', 'T', 'P'}, k = 2.
Output: 3.
Input arr[] = {'P', 'T', 'P', 'T', 'T', 'P'}, k = 3.
Output: 3.

 

Copy the code. Show the algorithm use. Don't copy code from other sites.

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education