Skip to content

Commit b43f5c6

Browse files
committed
08-30
1 parent 3c085ed commit b43f5c6

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

fastPrep/amazon/3.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ public int maximizeNegativePnLMonths(int[] PnL)
2626
{
2727
if(!pq.isEmpty())
2828
{
29+
/**
30+
* why is there another condition "(pq.peek() - PnL[i] > 0)"
31+
* reason: we must make sure that the total sum after the conversion must be greater than the sum without change current element
32+
* to a negative element.
33+
* That is to say that (prevSum + pq.peek() * 2 - PnL[i] > 0) - (prevSum + PnL[i]) > 0
34+
* (prevSum + pq.peek() * 2 - PnL[i] > 0) - (prevSum + PnL[i]) > 0
35+
* ==> 2 * pq.peek() - 2 * PnL[i] > 0
36+
* ==> pq.peek() > PnL[i]
37+
*/
38+
2939
if((prevSum + pq.peek() * 2 - PnL[i] > 0) && (pq.peek() - PnL[i] > 0))
3040
{
3141
currentSum = prevSum + pq.peek() * 2 - PnL[i];

0 commit comments

Comments
 (0)