Student NL
Student NL

Reputation: 429

Finding the maximum among three integers and knowing which one is chosen

I am currently working on a code that has to find the best solution. First I check whether one of the three is larger than the other two. Hence, there is a maximum that occurs only once. If there are two numbers larger than the third one, but equal to each other, I will have to compare the distance of those two and then the one with the smallest distance is chosen.

The profit functions and distances are calculated outside this method and not that important.

What I have come up with so far is to use a lot of if statements. However, I was wondering whether there would be a more efficient method to do this.

public void bestSolution(List<ROUTE> LS, List<ROUTE> SA, List<ROUTE> RR)
{
    int profitLS = profitRoutes(LS);
    int profitSA = profitRoutes(SA);
    int profitRR = profitRoutes(RR);

    int distanceLS = totalDistance(LS);
    int distanceSA = totalDistance(SA);
    int distanceRR = totalDistance(RR);

    if ((profitLS > profitSA) & (profitLS > profitRR))
    {

    }

}

Upvotes: 0

Views: 91

Answers (4)

Robert Kock
Robert Kock

Reputation: 6008

You could create a TreeSet with the comparison results and select the 'greatest' element.
The comparison result could be something like:

public class ProfitCounter implements Comparable<ProfitCounter>
{
  public ProfitCounter(List<ROUTE> route)
  {
    this.route = route;
    profit = profitRoutes(route);
    distance = totalDistance(route);
  }

  @Override
  public int compareTo(ProfitCounter other)
  {
    int result;
    result = profit - other.profit;
    if (result == 0)
      result = other.distance - distance;
    return (result);
  }

  private List<ROUTE> route;
  private int         profit;
  private int         distance;

} // class ProfitCounter

Upvotes: 1

Ben Barkay
Ben Barkay

Reputation: 5622

static class CalculatedRoute {

    public static CalculatedRoute mostProfitableOf(List<CalculatedRoute> calculatedRoutes) {
        return Collections.max(calculatedRoutes, BY_PROFIT_AND_DISTANCE);
    }

    public static final Comparator<CalculatedRoute> BY_PROFIT_AND_DISTANCE = new Comparator<CalculatedRoute>() {
        @Override
        public int compare(CalculatedRoute o1, CalculatedRoute o2) {
            int cmp = o2.profit - o1.profit;
            if (cmp == 0) {
                cmp = o1.distance - o2.distance;
            }
            return cmp;
        }
    };

    private final List<ROUTE> routeList;
    private final int profit;
    private final int distance;

    public CalculatedRoute(List<ROUTE> routeList, int profit, int distance) {
        this.profit = profit;
        this.distance = distance;
    }
    public List<ROUTE> getRouteList() {
        return routeList;
    }
    public int getProfit() {
        return profit;
    }
    public int getDistance() {
        return distance;
    }
}

public List<ROUTE> mostProfitableOf(List<ROUTE> LS, List<ROUTE> SA, List<ROUTE> RR) {
    return CalculatedRoute.mostProfitableOf(Arrays.asList(
            new CalculatedRoute(LS, profitRoutes(LS), totalDistance(LS)),
            new CalculatedRoute(SA, profitRoutes(SA), totalDistance(SA)),
            new CalculatedRoute(RR, profitRoutes(RR), totalDistance(RR))
    )).getRouteList();
}

Upvotes: 0

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

In case of finding max between three integers -

int mostProfit = Math.max(profitLS, Math.max(profitSA, profitRR));

Considering case - "distance of those two and then the one with the smallest distance is chosen"

class DistanceProfit{
   private int profit;
   private int distance; 
   public DistanceProfit(int profit, int distance){
       this.profit = profit;
       this.distance = distance;
   }
}
...
//create DistanceProfit objects add to list

Collections.sort(distenceProfitList, new Comparator<DistenceProfit>{
    public int compare(DistenceProfit dp1, DistenceProfit dp2){
         if(dp1.getProfit()==dp2.getProfit())
             return dp1.getDistance() - dp2..getDistance();
         return dp1.getProfit() - dp2.getProfit();
    }
});

Upvotes: 1

Dexter
Dexter

Reputation: 1750

Id use something on these lines. Does not limit u to 3 parameters.

public class RouteCalc implements Comparable<RouteCalc> {

    private final List routes;

    public RouteCalc(List routes) {
        this.routes = routes;
    }

    public static int calcProfit(List routes) {

        //use the list to calculate profit
        return 0;
    }

    public static int calcDistance(List routes) {

        //use the list to calculate distance
        return 0;
    }

    @Override
    public int compareTo(@NonNull RouteCalc another) {

        final int profitA = calcProfit(this.routes);
        final int profitB = calcProfit(another.routes);
        //swap parameters to change from ascending to descending and vice-versa
        final int compare = Integer.compare(profitA, profitB);

        //if same profit, compare distance
        if (compare == 0) {

            final int distanceA = calcDistance(this.routes);
            final int distanceB = calcDistance(another.routes);
            return Integer.compare(distanceA, distanceB);
        } else
            return compare;
    }

    //sample usage
    public static void main(String args[]) {

        final List<RouteCalc> allRoutes = new ArrayList<>();
        //add routes
        final RouteCalc bestRoute = Collections.max(allRoutes);
    }
}

Upvotes: 0

Related Questions