All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2501_2600.s2561_rearranging_fruits.Solution Maven / Gradle / Ivy

There is a newer version: 1.35
Show newest version
package g2501_2600.s2561_rearranging_fruits;

// #Hard #Array #Hash_Table #Greedy #2023_08_21_Time_36_ms_(96.81%)_Space_64.6_MB_(12.77%)

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {
    public long minCost(int[] basket1, int[] basket2) {
        int n = basket1.length;
        Map cnt = new HashMap<>();
        for (int i = 0; i < n; ++i) {
            cnt.merge(basket1[i], 1, Integer::sum);
            cnt.merge(basket2[i], -1, Integer::sum);
        }
        int mi = 1 << 30;
        List nums = new ArrayList<>();
        for (var e : cnt.entrySet()) {
            int x = e.getKey();
            int v = e.getValue();
            if (v % 2 != 0) {
                return -1;
            }
            for (int i = Math.abs(v) / 2; i > 0; --i) {
                nums.add(x);
            }
            mi = Math.min(mi, x);
        }
        Collections.sort(nums);
        int m = nums.size();
        long ans = 0;
        for (int i = 0; i < m / 2; ++i) {
            ans += Math.min(nums.get(i), mi * 2);
        }
        return ans;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy