Created
September 8, 2022 14:55
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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