Error Question?


  • 2
    W

    '''
    public class Solution {
    public int minTransfers(int[][] transactions) {
    int count = transactions.length;
    if (count == 0)
    return 0;

        Map<Integer, Integer> consult = new HashMap<Integer, Integer>();
    
        for(int i = 0; i < count; i++) {
            int left = transactions[i][0];
            int right = transactions[i][1];
            int z = transactions[i][2];
    
            consult.put(left, consult.getOrDefault(left, 0) + z);
            consult.put(right, consult.getOrDefault(right, 0) - z);
        }
        // 所有人账目归一之后处理
        // 恰好的账目抵消掉
        Queue<Integer> posi = new PriorityQueue<>(20, (o1, o2) -> o2 - o1);
        Queue<Integer> nega = new PriorityQueue<>(20, (o1, o2) -> o2 - o1);
    
        List<Integer> tmp = new ArrayList<>();
        System.out.println(consult.toString());
        for(int i: consult.values()) {
            if (i > 0) {
                posi.add(i);
            }
            else if (i < 0)
                tmp.add(-i);
        }
    
        int res = 0;
    
      //  System.out.println(tmp.toString());
       // System.out.println(posi.toString());
        //System.out.println(posi.toString());
        for(int i: tmp) {
           if(posi.contains(i)) {
               res++;
               posi.remove(Integer.valueOf(i));
            } else
                nega.add(i);
        }
    
        //  System.out.println(nega.toString());
       // System.out.println(posi.toString());
        //System.out.println(res);
    
        while (!nega.isEmpty() && !posi.isEmpty()) {
            int diff = posi.peek() - nega.peek();
            posi.poll();
            nega.poll();
            res++;
            if(diff > 0) {
                if (nega.contains(diff)) {
                    nega.remove(Integer.valueOf(diff));
                    res++;
                } else
                    posi.add(diff);
                } else if (diff < 0) {
                if (posi.contains(-diff)) {
                    posi.remove(Integer.valueOf(-diff));
                    res++;
                } else
                    nega.add(-diff);
            }
            //System.out.println(nega.toString());
            //System.out.println(posi.toString());
          //  System.out.println(res);
        }
        //System.out.println(nega.toString());
        //System.out.println(posi.toString());
        System.out.println(res);
        return res;
    }
    
    public int minTransfers2(int[][] transactions) {
        Map<Integer, Long> bal = new HashMap<>();
        for(int[] tr: transactions){
            if(!bal.containsKey(tr[0])) bal.put(tr[0], 0L);
            if(!bal.containsKey(tr[1])) bal.put(tr[1], 0L);
            bal.put(tr[0], bal.get(tr[0])+(long)tr[2]);
            bal.put(tr[1], bal.get(tr[1])-(long)tr[2]);
        }
        System.out.println(bal.toString());
        TreeMap<Long, Integer> balancemap = new TreeMap<>();
        for(int person: bal.keySet()){
            long cur = bal.get(person);
            if(cur!=0L){
                balancemap.put(cur, balancemap.getOrDefault(cur, 0)+1);
            }
        }
        int payment = 0;
        while(!balancemap.isEmpty()){
            long lo = balancemap.firstKey();
            long hi = balancemap.lastKey();
            long sum = lo+hi;
    
            if(balancemap.get(lo)==1){
                balancemap.remove(lo);
            }
            else{
                balancemap.put(lo, balancemap.get(lo)-1);
            }
            if(balancemap.get(hi)==1){
                balancemap.remove(hi);
            }
            else{
                balancemap.put(hi, balancemap.get(hi)-1);
            }
    
            if(sum!=0){
                balancemap.put(sum, balancemap.getOrDefault(sum, 0)+1);
            }
            payment++;
        }
        System.out.println(payment);
        return payment;
    
    }
    
    public static void main(String[] args) {
        int[][] trs = {{6,29,18},{1,22,84},{4,25,16},{7,22,16},{5,27,79},{7,27,60},{3,28,95},{4,20,95},{7,25,99},{1,29,25},{9,21,87},{4,26,39},{5,23,67},{6,22,68},{3,29,94},{8,24,66},{6,28,33},{9,28,85},{8,28,28},{3,21,25},{7,26,3},{3,22,89},{6,28,58},{6,28,45},{1,29,3},{7,21,95},{0,25,14},{8,27,37},{5,21,98},{9,23,57},{8,24,21},{8,25,78},{2,23,15},{3,24,50},{1,26,66},{6,21,38},{6,28,30},{8,29,49},{3,20,86},{4,22,91},{1,21,18},{1,26,22},{5,25,75},{5,28,48},{2,26,83},{3,28,3},{2,27,76},{7,22,73},{8,27,97},{2,26,49},{0,20,83},{7,23,47},{9,29,94},{2,27,43},{5,25,50},{7,29,76},{1,20,26},{9,28,77},{1,29,63},{9,26,55},{1,25,17},{3,28,37}};
        int[][] trs2 = {{8,23,20},{3,24,78},{4,20,37},{0,29,66},{2,29,2},{0,20,23},{0,22,65},{5,24,34},{0,27,6},{6,21,16},{1,26,2},{4,21,73},{8,27,64},{6,27,39},{5,25,15},{5,23,28},{8,25,53},{6,27,98},{0,25,92},{5,28,91},{8,21,75},{1,25,39},{1,22,55},{1,25,14},{4,26,70},{6,29,30},{6,26,11},{1,28,68},{1,26,13},{7,21,4},{3,29,77},{0,26,93},{7,20,39},{5,22,91},{9,27,80},{1,23,71},{6,29,27},{8,26,95},{8,29,24},{7,25,70},{1,29,17},{9,29,98},{6,22,26},{1,24,74},{0,25,33},{0,24,68},{8,25,91},{8,23,36},{1,29,25},{8,27,82},{4,24,14}};
        new Solution().minTransfers(trs2);
        new Solution().minTransfers2(trs2);
    }
    

    }

    '''

    I compared my answer(first one) with the No.1
    I think some wrong with this question!


  • 0

    @WaterH We've just rejudged the submissions again and this solution will get Wrong answer. Please let me know if you still find incorrect solution getting Accepted.


  • 0

    @1337c0d3r

    In the example 1, This is wrong, I think...

    "Two transactions are needed. One way to settle the debt is person #1 pays person #0 and #2 $5 each."

    It should be #0 and #2 pay #1 $5 each.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.