Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BankTransactions {
- public static void main(String[] args) {
- int n = 3;
- ListNode node = new ListNode(5);
- ListNode start = node;
- node.next = new ListNode(0);
- node.next.next = new ListNode(-5);
- node.next.next.next = start;
- ListNode node2 = new ListNode(-1);
- ListNode start2 = node2;
- node2.next = new ListNode(0);
- node2.next.next = new ListNode(1);
- node2.next.next.next = new ListNode(0);
- node2.next.next.next.next = start2;
- ListNode node3 = new ListNode(1);
- ListNode start3 = node3;
- node3.next = new ListNode(2);
- node3.next.next = new ListNode(3);
- node3.next.next.next = new ListNode(-6);
- node3.next.next.next.next = start3;
- ListNode node4 = new ListNode(1);
- ListNode start4 = node4;
- node4.next = new ListNode(0);
- node4.next.next = new ListNode(0);
- node4.next.next.next = new ListNode(0);
- node4.next.next.next.next = new ListNode(-1);
- node4.next.next.next.next.next = start4;
- ListNode node5 = new ListNode(0);
- ListNode start5 = node5;
- node5.next = new ListNode(1);
- node5.next.next = new ListNode(1);
- node5.next.next.next = new ListNode(0);
- node5.next.next.next.next = new ListNode(-2);
- node5.next.next.next.next.next = start5;
- System.out.println(calculateMinimalTransactions(start, n) == 1);
- System.out.println(calculateMinimalTransactions(start2, 4) == 2);
- System.out.println(calculateMinimalTransactions(start3, 4) == 3);
- System.out.println(calculateMinimalTransactions(start4, 5) == 1);
- System.out.println(calculateMinimalTransactions(start5, 5) == 3);
- }
- static int calculateMinimalTransactions(ListNode start, int n) {
- int min = Integer.MAX_VALUE;
- int temp;
- for (int i = 0; i < n ; i++) {
- if (start.val != 0) {
- temp = countTransactions(start, n);
- min = min > temp ? temp : min;
- }
- start = start.next;
- }
- return min;
- }
- // 5 0 -5
- static int countTransactions(ListNode start, int n) {
- // balance = 5
- int balance = start.val;
- int counter = 0;
- start = start.next;
- for (int i = 1; i < n; i++ ) {
- if (start.val != 0 || balance != 0) {
- counter++;
- balance += start.val;
- }
- start = start.next;
- }
- return counter;
- }
- static class ListNode {
- int val;
- ListNode next;
- ListNode(int x){
- val = x;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement