Don't know why the following code pass all tests?Pos and Neg pq


  • 1
    F

    Just try my intuition and it passed all tests:

    public class Solution {
        public int minTransfers(int[][] trans) {
            Map<Integer, Integer> net = new HashMap<>();
            for(int i = 0; i < trans.length; i++){
                net.put(trans[i][0], net.getOrDefault(trans[i][0], 0) - trans[i][2]);
                net.put(trans[i][1], net.getOrDefault(trans[i][1], 0) + trans[i][2]);
            }
            int res = 0;
            PriorityQueue<Integer> pos = new PriorityQueue<>(Collections.reverseOrder());
            PriorityQueue<Integer> neg = new PriorityQueue<>();
            for(int i : net.values()){
                if(i > 0) pos.offer(i);
                if(i < 0) neg.offer(i);
            }
            
            while(!pos.isEmpty() && !neg.isEmpty()){
                res++;
                int temp = pos.poll() + neg.poll();
                if(temp > 0) pos.offer(temp);
                if(temp < 0) neg.offer(temp);
            }
            return res;
        }
    }
    

  • 0

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


  • 0
    P

    I got the same idea with you, but failed with this case:
    [[10,11,6],[12,13,7],[14,15,2],[14,16,2],[14,17,2],[14,18,2]]


Log in to reply
 

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