Sunday, December 2, 2012

Topcoder SRM 562 DIV 2, L1: CucumberMarket

// Topcoder SRM 562 DIV 2, L1: CucumberMarket

import java.util.*;

class TopcoderSolution {
    public static void main(String[] args) {
        CucumberMarket obj = new CucumberMarket();
        System.out.println(obj.check(new int[] { 1, 2, 3, 4, 5 },
                10, 3));
    }
}

// change to public before submit
class CucumberMarket {
    public String check(int[] price, int budget, int k) {
        Arrays.sort(price);
        ReversePrimitiveArray(price);
        int sum = 0;
        for (int i = 0; i < k; i++) {
            sum += price[i];
        }
        if (sum <= budget)
            return "YES";
        else
            return "NO";
    }

    public static void ReversePrimitiveArray(int[] a) {
        for (int l = 0, r = a.length - 1; l < r; l++, r--) {
            int temp = a[l];
            a[l] = a[r];
            a[r] = temp;
        }
    }

}

No comments:

Post a Comment