A company is planning N projects, numbered from 0 to N-1.Completing the K-th project will bring value V[K] to the company.For some projects there may
A company is planning N projects, numbered from 0 to N-1.Completing the K-th project will bring value V[K] to the company.For some projects there may be additional requirements - the L-threquirement states that before starting project B[L], project A[L]should be completed. There are M such requirements.
The company has enough resources for at mosttwo projects to be completed. If two projects are chosen,they will be completed one by one (in sequential order) and thetotal value they bring to the company is the sum of theirindividual values. What is the highest value that a valid choice ofprojects can bring to the company?
Write a function:
class Solution { public int solution(int[] V, int[] A, int[] B);}
that, given array V of N integers and two arrays A and B of Mintegers each, returns the maximum value that the company may gainby completing at most two possible projects.
Examples:
1. Given V = [-3, 5, 7, 2, 3], A = [3, 1] and B = [2, 4], thefunction should return 9. This can be achieved by completingproject 3 (with value 2) first and then project 2 (with value7).
2. Given V = [1, 1, 5], A = [0, 1] and B = [2, 2], the functionshould return 2.
3. Given V = [5, 6, 6, 7, -10] and A = [0, 0, 0, 1, 2, 3] and B= [1, 2, 3, 3, 1, 2], the function should return 5. The projectthat are possible to be completed are 0 and 4. As project 4 wouldbring negative value to the company, it is optimal to do onlyproject 0. The structure of dependencies of projects 1, 2 and 3form a cycle, so none of them can be completed in a valid choice ofprojects.
Write an efficient algorithm for the followingassumptions:
- N is an integer within the range [1..100,000];
- M is an integer within the range [0..100,000];
- each element of array V is an integer within the range[-1,000,000,000..1,000,000,000];
- each element of arrays A and B is an integer within the range[0..N-1];
- a project may not require itself to be completed (A[K] !=B[K]);
- projects’ requirements do not repeat.
Step by Step Solution
3.32 Rating (146 Votes )
There are 3 Steps involved in it
Step: 1
answer The...See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started