Skip to content

Instantly share code, notes, and snippets.

@sanikamal
Created September 8, 2022 14:55
Show Gist options
  • Save sanikamal/410275e8a539452f42709cf58cd3cfd5 to your computer and use it in GitHub Desktop.
Save sanikamal/410275e8a539452f42709cf58cd3cfd5 to your computer and use it in GitHub Desktop.
Figure out initial balance for bank accounts to avoid them going into negative balance after transfer transactions
<?php
function solution($R, $V) {
// write your code in PHP7.0
$minA = 0;
$minB = 0;
$balA = 0;
$balB = 0;
for ($i = 0; $i < strlen($R); $i++) {
if ($R[$i] == 'A') {
$balA += $V[$i];
$balB -= $V[$i];
if ($balB < $minB) {
$minB = $balB;
}
} else if ($R[$i] == 'B') {
$balB += $V[$i];
$balA -= $V[$i];
if ($balA < $minA) {
$minA = $balA;
}
}
}
return [-$minA, -$minB ];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment