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
Which of the following roles is responsible for creating cloud components and the testing and validation of services?
DanielleElmas [232]

Answer:

The correct answer to the following question will be Option D (Cloud service developer).

Explanation:

  • Developers are usually individuals who layout and build applications or websites such as web or software systems. They would be known as app or web developers in this regard. In the sense that they still build and maintain things, a software developer is quite similar, however-and here's the deciding factor-this is achieved on virtual clouds.
  • It is the responsibility of the cloud service developer to design and build cloud elements and products, as well as to check and validate services.

Therefore, Option D is the right answer.

7 0
3 years ago
Which of these is the longest geologic time frame?
snow_lady [41]
The largest geologic time frame is Eon.
4 0
3 years ago
Read 2 more answers
What's the difference between pseudo code and natural language​
ArbitrLikvidat [17]

Answer: While algorithms are generally written in a natural language or plain English language, pseudocode is written in a format that is similar to the structure of a high-level programming language. Program on the other hand allows us to write a code in a particular programming language.

8 0
2 years ago
Read 2 more answers
Write two lines of code to draw the following circles centered at 600,600. The radius of the smallest circle is 200. There are 5
Bad White [126]

Answer:

canvas.draw_circle((600, 600), 200, 3, "Black")

canvas.draw_circle((600, 600), 250, 3, "Black")

Explanation:

# include < conio.h>

# include < iostream.h>

int canvas.draw_circle,

namespace std

{

canvas.draw_circle((600, 600), 200, 3, "Black")

canvas.draw_circle((600, 600), 250, 3, "Black")

}

return,

7 0
3 years ago
A compound document has
Svet_ta [14]

Answer:

c

Explanation:

more than one application interface

3 0
3 years ago
Other questions:
  • Sulfur content is measured in
    11·1 answer
  • To make IPv4 addresses a little easier for human beings to understand, the 32-bit binary addresses are represented by dotted dec
    9·1 answer
  • Some of the users in your company create and delete so many files that they have problems with fragmented disks. Which of the fo
    5·1 answer
  • What processes can move a solute against its concentration gradient?
    15·1 answer
  • Which of the following tools and techniques shows theimpacts of one decision over another as well as the probability andcost of
    15·1 answer
  • What is the missing line of code? &gt;&gt;&gt; &gt;&gt;&gt; math.sqrt(16) 4.0 &gt;&gt;&gt; math.ceil(5.20) 6
    14·2 answers
  • Help me out on question 29 and 30 please
    5·1 answer
  • The main difference between \f and \r is that \f produces a
    13·1 answer
  • Can you explain the difference between software and hardware? Tell me 3 examples of each one.
    10·1 answer
  • 2. Create 4 riddleson keywords which is related to fire wall.​
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!