Skip to content

Commit 3c085ed

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

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

fastPrep/amazon/2.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
1 <= threshold <= 109
6262
1 <= prices[i] <= 100
6363
64-
6564
*/
6665

6766
public int reduceGifts(int[] prices, int k, int threshold)

fastPrep/amazon/3.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
public int maximizeNegativePnLMonths(int[] PnL)
2+
{
3+
//Base Check
4+
if(PnL == null || PnL[0] <= 0)
5+
{
6+
return 0;
7+
}
8+
9+
//Init
10+
int value;
11+
12+
//Compute
13+
int count = 0, prevSum = PnL[0];
14+
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b.compareTo(a));
15+
for(int i = 1; i < PnL.length; i++)
16+
{
17+
//current sum
18+
int currentSum = prevSum + PnL[i];
19+
if(prevSum - PnL[i] > 0)
20+
{
21+
currentSum = prevSum - PnL[i];
22+
pq.offer(PnL[i]);
23+
count++;
24+
}
25+
else
26+
{
27+
if(!pq.isEmpty())
28+
{
29+
if((prevSum + pq.peek() * 2 - PnL[i] > 0) && (pq.peek() - PnL[i] > 0))
30+
{
31+
currentSum = prevSum + pq.peek() * 2 - PnL[i];
32+
pq.poll();
33+
pq.offer(PnL[i]);
34+
35+
}// if
36+
37+
}// if
38+
39+
}// else
40+
41+
//update currentSum
42+
prevSum = currentSum;
43+
44+
}// for
45+
46+
/** update velue */
47+
value = count;
48+
49+
//Return
50+
return value;
51+
52+
}
53+
54+
55+
/*
56+
*
57+
* Maximize Negative PnL Months
58+
🔥 FULLTIME
59+
📚
60+
RELATED PROBLEMS
61+
You are analyzing the market trends of Amazon stocks. An AWS financial service model returned an array of integers, PnL (Profit and Loss), for your portfolio representing that in the ith month, you will either gain or lose PnL[i]. All reported PnL values are positive, representing gains.
62+
63+
As part of the analysis, you will perform the following operation on the PnL array any number of times:
64+
65+
Choose any month (0 ≤ i < n) and multiply PnL[i] by -1
66+
Find the maximum number of months you can afford to face a loss, i.e., have a negative PnL, such that the cumulative PnL for each of the n months remains strictly positive i.e. remains greater than 0.
67+
68+
Note: The cumulative PnL for the ith month is defined as the sum of PnL from the starting month up to the ith month. For example, the cumulative PnL for the PnL = [3, -2, 5, -6, 1] is [3, 1, 6, 0, 1].
69+
70+
Function Description
71+
72+
Complete the function maximizeNegativePnLMonths in the editor.
73+
74+
maximizeNegativePnLMonths has the following parameter:
75+
76+
int[] PnL: an array of integers representing the Profit and Loss for each month
77+
Returns
78+
79+
int: the maximum number of months with a negative PnL such that the cumulative PnL remains positive
80+
*/

0 commit comments

Comments
 (0)