Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1.  
  2. public class BankTransactions {
  3.     public static void main(String[] args) {
  4.         int n = 3;
  5.         ListNode node = new ListNode(5);
  6.         ListNode start = node;
  7.         node.next = new ListNode(0);
  8.         node.next.next = new ListNode(-5);
  9.         node.next.next.next = start;
  10.  
  11.         ListNode node2 = new ListNode(-1);
  12.         ListNode start2 = node2;
  13.         node2.next = new ListNode(0);
  14.         node2.next.next = new ListNode(1);
  15.         node2.next.next.next = new ListNode(0);
  16.         node2.next.next.next.next = start2;
  17.  
  18.         ListNode node3 = new ListNode(1);
  19.         ListNode start3 = node3;
  20.         node3.next = new ListNode(2);
  21.         node3.next.next = new ListNode(3);
  22.         node3.next.next.next = new ListNode(-6);
  23.         node3.next.next.next.next = start3;
  24.  
  25.         ListNode node4 = new ListNode(1);
  26.         ListNode start4 = node4;
  27.         node4.next = new ListNode(0);
  28.         node4.next.next = new ListNode(0);
  29.         node4.next.next.next = new ListNode(0);
  30.         node4.next.next.next.next = new ListNode(-1);
  31.         node4.next.next.next.next.next = start4;
  32.  
  33.         ListNode node5 = new ListNode(0);
  34.         ListNode start5 = node5;
  35.         node5.next = new ListNode(1);
  36.         node5.next.next = new ListNode(1);
  37.         node5.next.next.next = new ListNode(0);
  38.         node5.next.next.next.next = new ListNode(-2);
  39.         node5.next.next.next.next.next = start5;
  40.  
  41.         System.out.println(calculateMinimalTransactions(start, n) == 1);
  42.         System.out.println(calculateMinimalTransactions(start2, 4) == 2);
  43.         System.out.println(calculateMinimalTransactions(start3, 4) == 3);
  44.         System.out.println(calculateMinimalTransactions(start4, 5) == 1);
  45.         System.out.println(calculateMinimalTransactions(start5, 5) == 3);
  46.     }
  47.  
  48.     static int calculateMinimalTransactions(ListNode start, int n) {
  49.         int min = Integer.MAX_VALUE;
  50.         int temp;
  51.         for (int i = 0; i < n ; i++) {
  52.             if (start.val != 0) {
  53.                 temp = countTransactions(start, n);
  54.                 min = min > temp ? temp : min;
  55.             }
  56.             start = start.next;
  57.         }
  58.  
  59.         return min;
  60.     }
  61.  
  62.     // 5 0 -5
  63.     static int countTransactions(ListNode start, int n) {
  64.         // balance = 5
  65.         int balance = start.val;
  66.         int counter = 0;
  67.         start = start.next;
  68.         for (int i = 1; i < n; i++ ) {
  69.             if (start.val != 0 || balance != 0) {
  70.                 counter++;
  71.                 balance += start.val;
  72.             }
  73.             start = start.next;
  74.         }
  75.  
  76.         return counter;
  77.     }
  78.  
  79.  
  80.  
  81.     static class ListNode {
  82.         int val;
  83.         ListNode next;
  84.         ListNode(int x){
  85.             val = x;
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement