Merge transactions in bank sheets in the order of their occurrence such that their sum remains positive
Given an array arr[][] consisting of N lists representing N transactions, the task is to merge the given lists of transactions in the order of their occurrences, such that at any point of time, the sum of already performed transactions is non-negative. If found to negative, then print “-1”. Otherwise, print the merged list of transactions.
Examples:
Input: arr[][] = {{100 ? 400 ? -1000 ? -500}, {-300 ? 2000 ? -500}}
Output: 100 ? 400 ? -300 ? 2000 ? -500 ? -1000 ? -500
Explanation: The sum at every instant of the above list of transactions is given by {100, 500, 200, 2200, 1700, 700, 200}, which has no negative values.
Input: arr[][] = [[100 ? 400]]
Output: 100 400
Approach: The given problem can be visualized as a variation of merge K-sorted linked lists with criteria that the sum of the merged list of transactions at any instant should be non-negative.
Follow the steps below to solve the problem:
Below is the implementation of the above approach:-
C++
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Node {
int val;
Node* next;
Node( int val)
: val(val)
, next(NULL)
{
}
};
struct Compare {
bool operator()(Node* a, Node* b)
{
return a->val < b->val;
}
};
void mergeSheets(vector<Node*>& lists)
{
priority_queue<Node*, vector<Node*>, Compare> pq;
for ( int i = 0; i < lists.size(); i++)
{
if (lists[i])
{
pq.push(lists[i]);
}
}
Node* head = new Node(0);
Node* curr = head;
while (!pq.empty()) {
auto t = pq.top();
pq.pop();
curr->next = t;
curr = curr->next;
if (curr->next) {
pq.push(curr->next);
}
}
curr = head->next;
while (curr->next) {
cout << curr->val << " " ;
curr = curr->next;
}
cout << curr->val << endl;
}
int main()
{
int N = 2;
vector<Node*> lists(N);
lists[0] = new Node(100);
lists[0]->next = new Node(400);
lists[0]->next->next = new Node(-1000);
lists[0]->next->next->next = new Node(-500);
lists[1] = new Node(-300);
lists[1]->next = new Node(2000);
lists[1]->next->next = new Node(-500);
mergeSheets(lists);
return 0;
}
|
Java
import java.util.*;
class Node {
int val;
Node next;
Node( int val)
{
this .val = val;
this .next = null ;
}
}
class GFG {
public static void mergeSheets(
Node lists[])
{
PriorityQueue<Node> pq
= new PriorityQueue<>(
new Comparator<Node>() {
public int compare(Node a, Node b)
{
return b.val - a.val;
}
});
Node p, head = new Node( 0 );
p = head;
for ( int i = 0 ;
i < lists.length; i++) {
if (lists[i] != null ) {
pq.add(lists[i]);
}
}
while (!pq.isEmpty()) {
p.next = pq.poll();
p = p.next;
if (p.next != null )
pq.add(p.next);
}
p = head.next;
while (p.next != null ) {
System.out.print(p.val + " " );
p = p.next;
}
System.out.print(p.val);
}
public static void main(String[] args)
{
int N = 2 ;
Node arr[] = new Node[N];
arr[ 0 ] = new Node( 100 );
arr[ 0 ].next = new Node( 400 );
arr[ 0 ].next.next = new Node(- 1000 );
arr[ 0 ].next.next.next = new Node(- 500 );
arr[ 1 ] = new Node(- 300 );
arr[ 1 ].next = new Node( 2000 );
arr[ 1 ].next.next = new Node(- 500 );
mergeSheets(arr);
}
}
|
Python3
import heapq
class Node:
def __init__( self , val):
self .val = val
self . next = None
def mergeSheets(lists):
pq = []
for i in range ( len (lists)):
if lists[i] ! = None :
heapq.heappush(pq, ( - lists[i].val, lists[i]))
p = Node( 0 )
head = p
while len (pq) > 0 :
p. next = heapq.heappop(pq)[ 1 ]
p = p. next
if p. next ! = None :
heapq.heappush(pq, ( - p. next .val, p. next ))
p = head. next
while p. next ! = None :
print (p.val, end = " " )
p = p. next
print (p.val)
N = 2
lists = [ None ] * N
lists[ 0 ] = Node( 100 )
lists[ 0 ]. next = Node( 400 )
lists[ 0 ]. next . next = Node( - 1000 )
lists[ 0 ]. next . next . next = Node( - 500 )
lists[ 1 ] = Node( - 300 )
lists[ 1 ]. next = Node( 2000 )
lists[ 1 ]. next . next = Node( - 500 )
mergeSheets(lists)
|
C#
using System;
using System.Collections.Generic;
class Node
{
public int val;
public Node next;
public Node( int val)
{
this .val = val;
this .next = null ;
}
}
class Program
{
public static Node MergeSheets(List<Node> lists)
{
var pq = new List<Node>();
for ( int i = 0; i < lists.Count; i++)
{
if (lists[i] != null )
{
pq.Add(lists[i]);
}
}
pq.Sort((a, b) => b.val.CompareTo(a.val));
Node head = new Node(0);
Node p = head;
while (pq.Count > 0)
{
p.next = pq[0];
pq.RemoveAt(0);
p = p.next;
if (p.next != null )
{
pq.Add(p.next);
pq.Sort((a, b) => b.val.CompareTo(a.val));
}
}
return head.next;
}
public static void Main()
{
int N = 2;
var lists = new List<Node>();
lists.Add( null );
lists.Add( null );
lists[0] = new Node(100);
lists[0].next = new Node(400);
lists[0].next.next = new Node(-1000);
lists[0].next.next.next = new Node(-500);
lists[1] = new Node(-300);
lists[1].next = new Node(2000);
lists[1].next.next = new Node(-500);
Node p = MergeSheets(lists);
while (p.next != null )
{
Console.Write(p.val + " " );
p = p.next;
}
Console.Write(p.val);
}
}
|
Javascript
class Node {
constructor(val) {
this .val = val;
this .next = null ;
}
}
function mergeSheets(lists) {
const pq = new PriorityQueue((a, b) => b.val - a.val);
let p, head = new Node(0);
p = head;
for (let i = 0; i < lists.length; i++) {
if (lists[i] !== null ) {
pq.enqueue(lists[i]);
}
}
while (!pq.isEmpty()) {
p.next = pq.dequeue();
p = p.next;
if (p.next !== null ) {
pq.enqueue(p.next);
}
}
p = head.next;
let result = "" ;
while (p.next !== null ) {
result += p.val + " " ;
p = p.next;
}
result += p.val;
console.log(result);
}
class PriorityQueue {
constructor(comparator) {
this .comparator = comparator;
this .queue = [];
}
enqueue(item) {
this .queue.push(item);
this .queue.sort( this .comparator);
}
dequeue() {
return this .queue.shift();
}
isEmpty() {
return this .queue.length === 0;
}
}
function main() {
const N = 2;
const arr = new Array(N);
arr[0] = new Node(100);
arr[0].next = new Node(400);
arr[0].next.next = new Node(-1000);
arr[0].next.next.next = new Node(-500);
arr[1] = new Node(-300);
arr[1].next = new Node(2000);
arr[1].next.next = new Node(-500);
mergeSheets(arr);
}
main();
|
Output:
100 400 -300 2000 -500 -1000 -500
Time Complexity: O(N * log K)
Auxiliary Space: O(K)
Last Updated :
21 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...