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
Using the Internet, search and discuss why naming conventions are important and why programmers should consistently follow them.
LiRa [457]

Answer:

Answered below

Explanation:

Some variable naming conventions include;

1) Variable should begin with either a letter or an underscore.

2) Variables having multiple words should have the first letter of every word after the first word, capitalized. This is the camelCase style.

3) variables should not be named after any of the inbuilt keywords except on special operations to override the original function of such keyword.

4) variable names are case-sensitive.

The importance of following these conventions is to maintain readability and consistency of code. Failure to follow these conventions may lead to chaotic codes, bugs and inefficient performance.

7 0
3 years ago
What does the W in WAN represent
Ilya [14]
WAN stands for Wide Area Network.
7 0
4 years ago
Read 2 more answers
What does If you’re not paying for the product, you are the product.” mean
pochemuha

Answer:

it means your free

Explanation:

7 0
4 years ago
Consider the following code snippet: String[] data = { "abc", "def", "ghi", "jkl" }; String [] data2; In Java 6 and later, which
nikklg [1K]

Answer:

String[] data2 = Arrays.copyOf(data, 4); is the statement which copies the data array to the data2 array in java 6 .

Explanation:

The Arrays.copyOf() function copies the data from first array to another array in java .We pass the two argument in this function  first argument is the name of first array and second the length of first array .

Following are the program in java  

import java.util.Arrays; // import package  

public class Main

{

public static void main(String[] args) // main function

{

String[] data = { "abc", "def", "ghi", "jkl" }; // string declaration

// printing the array data1

 System.out.println("before copy new array:");

 for (int k = 0; k < data.length; k++)  

     {

 System.out.println(data[k]);

 }

String[] data2 = Arrays.copyOf(data, 4);

 // printing the array data2

     System.out.println("after copy new array:");

     for (int k = 0; k < data2.length; k++)  

     {

        System.out.println(data2[k]);

     }}}

Output:

before copy new array:

abc

def

ghi

jkl

after copy new array:

abc

def

ghi

jkl

6 0
4 years ago
What is another name for Mineral Spirits? What is it used for?
OlgaM077 [116]

Answer:

White spirit (UK) or mineral spirits (US, Canada), also known as mineral turpentine (AU/NZ), turpentine substitute, and petroleum spirits, is a petroleum-derived clear liquid used as a common organic solvent in painting.

8 0
3 years ago
Other questions:
  • Shania has started a new job as an app developer. Her first task was to make an old app designed for Android available on other
    6·1 answer
  • What can you do to stop a computer from repeatedly restarting in a continuous loop?
    13·1 answer
  • In Python,The sum of the elements in a tuple can be recusively calculated as follows:The sum of the elements in a tuple of size
    5·1 answer
  • A(n) ____ instruction might look like a meaningless string of 0s and 1s, but it actually represents specific operations and stor
    9·1 answer
  • Entering the search criteria "B?" would yield which of the following results?
    6·1 answer
  • 1.) Florida has ____________ roads that are designated as part of the National Highway System.
    12·1 answer
  • What is professionalism and unprofessionalism in the work place?​
    10·1 answer
  • How to assign some object key values to other name react const.
    7·1 answer
  • An array of integers can be assigned to a memory address in the ...
    10·1 answer
  • Traceroute would be best suited for which of the following tasks?
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!