Sunday, March 14, 2021

 Given glasses of Juices of J units with capacity C units where no glass is empty, determine the maximum number of juices that can be mixed into one glass. 

 

import java.util.*; 

class Solution { 

    public int solution(int[] J, int[] C) { 

        int result = 1; 

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

            int count  = 1; 

            int max = C[k] - J[k]; 

            if (max == 0) { 

                continue; 

            } 

            int[] sorted = new int[J.length - 1]; 

            int index = 0; 

            for (int i = 0; i < J.length; i++) { 

                if (i != k) { 

                    sorted[index] = J[i]; 

                    index++; 

                } 

            } 

            Arrays.sort(sorted); 

            count = 1; 

            for (int i = 0; i < sorted.length && max > 0; i++) { 

                if (max - sorted[i] >= 0) { 

                    max -= sorted[i]; 

                    count++; 

                } 

            } 

            if (count > result){ 

                result = count; 

            } 

        } 

        return result; 

    } 

} 

Correctness tests

▶simple

Simple tests.✔OK

▶no_pouring

Tests in which it is not possible to pour.✔OK

▶small_cornercases

Tests where N <= 3.✔OK

▶not_biggest_capacity

Tests in which the glass with the biggest capacity is not the one that should be picked as the base.✔OK

▶not_biggest_capacity_left

Tests in which the glass with the largest empty space is not picked as the base.✔OK

▶same_capacity

Tests in which all glasses have the same capacity.✔OK

▶full_glasses

All glasses are full.✔OK

▶almost_empty_glasses

All glasses are almost empty.✔OK

No comments:

Post a Comment