1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Viktor [21]
3 years ago
8

Greedy Algorithm Design

Computers and Technology
1 answer:
Alik [6]3 years ago
8 0

Answer:

The algorithm is as follows:

1. Start

2. Get the number of items (n)

3. Get the current price of the n items (a1, a2..... an)

4. Get the possible hiked price of the n items (b1, b2..... bn)

5. Calculate the difference between the current and hiked prices for each item i.e. d_i = b_i - a_i

6. Sort the differences in descending order (i.e. from the greatest to the least)

7. Buy items in this order of difference

8. Stop

Explanation:

The algorithm is self-explanatory; however, what it does is that:

It takes a list of the current price of items (say list a)

E.g: a = [100, 150, 160]

Then take a list of the hiked price of the items (say list b)

E.g: b = [110, 180, 165]

Next, it calculates the difference (d) between corresponding prices d_i = b_i - a_i

d = [(110 - 100),(180-150),(165-160)]

d = [10,30,5]

Sort the difference from greatest to lowest (as the difference is sorted, lists a and b are also sorted)

d = [30,10,5]

a = [150, 100, 160]

b = [180, 110, 165]

If there is no hike up to item k, the couple would have saved (i = 1 to d[k-1])

Assume k = 3

The couple would have saved for 2 item

Savings = d[1] + d[2]

Savings = 30 +10

Savings = 40

The saved amount will then be added to the kth item in list a i.e. a[k](in this case k = 3) in order to buy b[k]

Using the assumed value of k

a[k] = a[3]

a[3] = 160

b[3] = 165

Add the saved amount (40) to a[3]

New\ Amount = 40 + 160

New\ Amount = 200

This new amount can then be used to buy b[3] i.e. 165, then they save the change for subsequent items

You might be interested in
A merge is _____.
Aliun [14]
The process of combining information from a variety of sources
7 0
3 years ago
Read 2 more answers
C++ Question: what is in the blanks?
Igoryamba

You didn't specify what the program should output, so there are many possibilities that result in a working program. It *looks* like this was intended:

int x = 24;

int y;

y = x-12;

cout<<y<<endl;

and it will display 12.

8 0
3 years ago
What is a client benefit of partnering with Accenture's Intelligent Platform Services<br> (IPS)?
charle [14.2K]
It’s a bot Bc when you do stuff you can believ it
8 0
2 years ago
Over a TCP connection, suppose host A sends two segments to host B, host B sends an acknowledgement for each segment, the first
irinina [24]

Over a TCP connection, suppose host A sends two segments to host B, host B sends an acknowledgement for each segment, the first acknowledgement is lost, but the second acknowledgement arrives before the timer for the first segment expires is True.

True

<u>Explanation:</u>

In network packet loss is considered as connectivity loss. In this scenario host A send two segment to host B and acknowledgement from host B Is awaiting at host A.

Since first acknowledgement is lost it is marked as packet lost. Since in network packet waiting for acknowledgement is keep continues process and waiting or trying to accept acknowledgement for certain period of time, once period limits cross then it is declared as packet loss.

Meanwhile second comes acknowledged is success. For end user assumes second segments comes first before first segment. But any how first segment expires.

3 0
3 years ago
The theory of continental drift is supported by all of the following EXCEPT
Stels [109]
Climate seems like the correct answer
8 0
3 years ago
Other questions:
  • What is the purpose of a mail merge field?
    9·2 answers
  • If Johanna wants to label the x- and y-axes in Excel she should click Layout, then what?
    12·1 answer
  • Why is the radial gradient type a good choice for the grapes?
    13·2 answers
  • The CSS property of top , bottom , left , or right moves the element the specified distance from the respective edge of the clos
    7·1 answer
  • Adding Web pages helps Web designers ___. Choose all answers that are correct.
    13·1 answer
  • Write a program that inputs a five-digit integer, spearates the integer into its digits and prints them seperated by three space
    6·1 answer
  • Estoy empezando a usar una pagina llamada source filmmaker, no se que apreté y quiero el menú de nuevo (en donde se ponen los mo
    14·1 answer
  • This code --&gt; plt.plot(x,y) is used to draw :
    8·1 answer
  • William brought some data into his Tableau Book, but the data had some null values and incorrect column headers. What did Willia
    15·1 answer
  • you are investigating the use of website and url content filtering to prevent users from visiting certain websites. which benefi
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!