Maximum profit array c++ multiple sell - c++

Given an array of size n which represents prices of an object over n days . Each day you have to either buy a single object or you can sell any amount of objects you have to earn money.Assume that you can always buy an object. And you can sell only if you have some objects. So to maximize the total profit you have to choose particular day for selling and buying.
For example ..N=10 arr={5,0,9,2,9,0,2,7,4,7}, Now lets say initially total no of objects you have is n=0; and total profit p= 0
On day 1 you don't have any object so in any case you have to buy one ..
n=1 , p=-5 and now on day 2 you can buy second one with no amount to spend so n=2 and p=-5 on day 3 you can sell both objects and earn money so n=0 and p=13
similarly we can proceed further and maximum profit earned will be 35
Please help me with this question. I have already seen the problems where we have to choose the days to sell and buy ..but that seem to help me because here you have to either buy or sell each day ..Please give me some idea how to proceed

If you were to keep an average of the price, you could always buy when its below average, and sell when its above average.
You would have to keep a total of each day's price, so 5 for day one and two, 14 for day three and so on.
Then divide it by the number of days passed so far, so 14/3 = 4.66
if the day is greater than the average and you have objects, sell. Otherwise, buy.
Hope that helps!

Related

Measure to calculate Average Unit Cost providing unexpected values

I'd like to show the average unit cost in each year for a series of items I'm purchasing. I'll be purchasing 13 items over 10 years. In some years, I purchase multiple items, in others, I purchase only one.
My Measure is Sum(Cost)/DistinctCount(Items). In all cases, this results in a count of 13 items, rather than the number of items in a given year. For reference, I'm using this in a line chart, where years are along the x-axis, so I (wrongly) assumed that the Year context would apply. Any suggestions?
For example, in one year, I'm purchasing 5 items for $350 dollars, which should result in an average unit cost of $70, but is instead returning $26.92, which is 350/13.
If the year context doesn't apply, then it sounds like an issue with your data model. Have a look at whether there's a relationship from your Date table to your Fact table (the one with the item counts).

Calculate remaining amount available to invest in power BI

I am trying to create a table in Power BI that shows the remaining amount available to invest based on a 10% limit of total investments.
So for example, if we have 1,000,000 invested in 10 names, no name is allowed to be invested more than $100,000. What it looks like in excel is in the below image. it sumifs all the amounts that have days remaining >0 and then multiplies that amount by 10% to give us our 10% limit. It then sumifs each individual investment name for the total amount invested (A has 51,000,000 invested) and in the "remaining max allowed" subtracts that number from the 70,647,200 to show us how much more we can invest in that name
So far I have Created a measure that looks like this that gives me the amount invested with days remaining greater than 0, but I do not know how to then substract that number from 10% of the total amount of names invested with days remaining greater than 0
This was a lot of words, so I hope what I'm asking makes sense. Thanks

Creating new calculation to sum a Dax Calculated Measure in Power BI

I have a promised hours calculation that looks at two dates (Coalesce) and gets the number of weeks in the month and then multiply it by the max promised hours for an employee which has a row for every week of the month. The employee could have 5 rows for every entry a week and the promised hours shows up the same as for example 40 for that week, thus why I have to take the max instead of do a sum. But at the end I need to show the total number of promised hours per week for whatever timeframe is chosen in the date slicer per employee.
Promised Hours Calc = CALCULATE(WEEKNUM(Max([CoalesceActualStart_AbsenceStart]),1)-WEEKNUM(MIN([CoalesceActualStart_AbsenceStart]),1))*MAX(FSLData[PromisedHours])
I then need it to do a sum for every employee on the above calculation.
I am getting all sorts of errors trying to do a sum on the calculation as a whole.
What you write is confusing and incomprehensible. That's why you get the errors. As soon as you are able to formulate your problem in such a way that others can understand it, you will understand it yourself and the errors will go away.

DAX TOPN Filter Not Returning Enough Rows

I'm trying to apply a TOPN() visual filter to a Power BI sheet based on an Average Loan Amount measure. I want to see the top 5 employees with the highest average loan amount, ignoring employees who have disbursed 4 or fewer loans.
The problem I'm running into is that I don't get 5 rows returned, even though I've selected the top 5. I have to adjust the "TOPN" parameter (in the visuals) to include more than 5, just to get 5 rows.
This seems to be because when I have both the TOP5 average AND the loan count > 4 filters working, neither updates the other; that is, I can find the top 5 rows based on the average parameter, but once I include the "loan count > 4" condition, a few of the top 5 disappear, and they're not replaced by the runners-up to the original 5.
In the past, when I placed a top 5 filter for average and nothing came up, it was because all the top 5 entries all had a loan count of under 5. Once I relaxed the "TOPN" condition to be "TOP 52," I got 5 entries visible.
Does anyone know why this happens & how to fix it so I always get 5 rows returned?
EDITED TO ADD: For an example of the data, please click here. Please note that any employee with a loan count of 4 or less should be filtered out. I created the filter in PowerBI because the data sets are dynamic, and so are the filter results.
The fundamental problem is that you're applying 2 filters to the same visualization:
You only want to include employee's with a loan count of 5 or more
Of those, you want the 5 employees with the highest average loan amount
Power BI is applying both filters independently. So, it is taking the 5 employees with the highest average loan amount, and then removing 3 of them because their loan count is less than 5. I can imagine this is a common problem for people working with a Top N filter plus another filter.
One way to work around this (and I don't claim this is the only or even the best way), is to take into account the loan count before calculating the average.
For example, assuming you have the following two measures and the following data:
Loan Count = DISTINCTCOUNT(Employee[Loan Number])
Avg Loan Amt = AVERAGE(Employee[Loan Amount])
It's clear from the picture that Liz, Montgomery and Oscar are in the top 5 but have only 3 loans to their name.
Next, we can create a new measure that checks the Loan Count before calculating the average loan amount. If the loan count doesn't meet the threshold, you don't care about their average.
*Filtered Avg Loan Amt = IF([Loan Count] < 5, BLANK(), [Avg Loan Amt])
This creates the following result. Notice that Liz, Montgomery & Oscar now all have no average calculated because they don't have enough loans.
Now, you don't necessarily have to display the Filtered Avg Loan Amt measure on your table, but you can now use that measure in your Top N visual filter and that, by itself, will filter your table to the top 5 employees with a high enough loan count.
Notice that in my filters, I only have 1 filter (on Filtered Avg Loan Amt). I don't also need to filter to a loan count of 5 or greater. This results in the following top 5 employees:
I hope this solves the problem you're having!
Unrelated sidenote: if you're using this threshold of 5 in a few places, I would recommend sourcing the number from an external source (including possibly a disconnected table) rather than hard-coding 5 in the measure itself. That way, if someone decides that 5 isn't the right threshold, you only have to update it one place, rather than hunting through all your measures looking for the number 5. There's an article here on using a disconnected table so that end-users can pick the threshold themselves (though it could definitely be overkill for your situation): https://powerpivotpro.com/2013/08/moving-averages-controlled-by-slicer/

AWS Calculator - How reserved instance cost gets calculated?

I am using AWS calculator - http://calculator.s3.amazonaws.com/index.html.
When I chose reserve instances, in the estimate of monthly bill tab, I get "Total One-Time Payment" value.
Suppose, I have 3 servers cost to calculate. If I calculate the monthly cost of 3 servers and each server considered with 1 yr heavy reserve instance, then what the "Total One-Time Payment" value implies?
Whether that value is complete year upfront cost or 1 year upfront cost divided by 12?
Please help me understand.
Regards,
Chandan
It is the complete year upfront cost, meaning that you pay that amount only once at the beginning. After that you only pay for the compute time each month (the amount on the row labeled "Compute:").