Find a lower value of a collection, according to one condition

Asked

Viewed 45 times

0

I have two collections: A and B as List<int>, both of the same size N.
In collection A, are the designs.
In B, the offerings by the project.

Illustrating:
projects[5, 1, 4, 5]
offers[8, 7, 6, 9]

Soon,

For the project 5, I have the offers: [8,9]
To the 1: [7]
To the 4: [6]

I would like to return as little value as possible between the offers for each project, following the example illustrated. That in case it will stay 8 + 7 + 6 = 21

Please someone could help me or refer me to another solution(links, references, etc)?

Here is the statement: inserir a descrição da imagem aqui

What I’ve done so far:

internal static long minCost(int numProjects, List<int> projectId, List<int> bid)
    {
        var projectsBids = new Dictionary<int, List<int>>();
        int minValue = 0;

        List<int> projects = projectId.Distinct().ToList();


        if (projects.Count != numProjects)
        {
            return -1;
        }
        
        for (int i = 0; i < projects.Count; i++)
        {
            //Aqui eu preciso adicionar valores que estão para o projeto(projects[i]), mas não sei como
            projectsBids.Add(projects[i], /*aqui*/);

        }

        return minValue;
    }

2 answers

1


Good afternoon Álvaro.

It’s unclear how you’re linking the two collections. Perhaps, in this scenario, the best option is to use a Dictionary<int, List>. This way, you would have as key the project ID and as value the List with offers. This way you can use the Min() instruction to get the lowest bid from that specific project. To be less verbose, you can combine this with Select and have it added at the end, to get the amount you commented. For example:

var projetosEOfertas = new Dictionary<int, List<int>>();

projetosEOfertas.Add(5, new List<int> { 8, 9 });
projetosEOfertas.Add(1, new List<int> { 7 });
projetosEOfertas.Add(4, new List<int> { 6 });

var total = projetosEOfertas.Select(_ => _.Value.Min()).Sum();

If you can send more details of how this link between projects and offers is currently made, we can give you other options.

  • Opa Eduardo. Thank you very much for the answer! I also thought of adding in a Dictonary<int, List<int>>, but I was left with a question of how to add the items in the lists for each key. The link between project and offers is only the I position of each collection, always the two collections have the same size. I thought the project interface 1 to 1-N offers.

  • This is a hacker Hank exercise, which I had to do for an interview, but I couldn’t finish. I would like to resolve, for a next attempt.

  • i added more details. Thanks again!

  • I managed to solve :D! Vlw Eduardo, I used your query!

0

Solved! If anyone needs, follow the code:

internal static long minCost(int numProjects, List<int> projectId, List<int> bid)
    {
        var projectsBids = new Dictionary<int, List<int>>();
        int minValue = 0;

        List<int> projects = projectId.Distinct().ToList();


        if (projects.Count != numProjects)
        {
            return -1;
        }

        for (int i = 0; i < projects.Count; i++)
        {
            projectsBids.Add(projects[i], new List<int> { });
        }

        for (int i = 0; i < projects.Count; i++)
        {
            for (int j = 0; j < projectId.Count; j++)
            {
                if (projectId[j] == projects[i])
                {
                    projectsBids[projects[i]].Add(bid[j]);
                }
            }
        }

        minValue = projectsBids.Select(_ => _.Value.Min()).Sum();

        return minValue;
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.