A bank has an ATM machine. For a particular week, the usage of cash in millions as below.
5- Monday
4- Tuesday
1- Wednesday
15- Thursday
6- Friday
2- Saturday
4- Sunday
The bank hires a depositing company to deposit money in 5, 3, or 1 rounds per week.
The depositing company provides following packages to the bank when charging for depositing money,
Cost for depositing in 4 rounds per month- 21135
Cost for depositing in 12 rounds per month- 32000
Cost for depositing in 20 rounds per month- 41975
Ordering remains as Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday. This order shouldn’t be violated when categorizing values.
Example
5 rounds
[(5+4),1, 15, 6, (2+4)]
[(5+4), 1, (15+6)=20+1, 2, 4]
can have many other combinations which don't break order.
3 rounds
[(5+4+1), 15, (6+2+4)]
[(5+4), (1+15), (6+2+4)]
can have many other combinations which don't break order.
1 round
[(5+4+1+15+6+2+4)]
Also the bank has to bear a holding cost of 0.019% of the remaining amount at the end of the day.
Example
Consider 1st week usage of cash as follows.( in millions)
Mon- 13
Tue- 5
Wed- 4
Thu- 4
Fri- 2
Sat- 11
Sun- 1
5 - rounds
1st week Cash depositing order - 13, (5+4), 4, (2+11), 1
Assuming depositing is done in 5 rounds for all 4 weeks of the month,
(5*4 = 20)
Total depositing cost = 41975
1- 13 deposited,
13 withdrawn,
0 remaining,
0 holding cost
2- (5+4) deposited,
5 withdrawn,
4 remaining,
4*0.00019 holding cost
3- 0 deposited,
4 withdrawn,
0 remaining,
0 holding cost
4- 4 deposited,
4 withdrawn,
0 remaining,
0 holding cost
5- (2+11) deposited,
2 withdrawn,
11 remaining,
11*0.00019 holding cost
6- 0 deposited,
11 withdrawn,
0 remaining,
0 holding cost
7- 1 deposited,
1 withdrawn,
0 remaining,
0 holding cost
Total holding cost for 1st week = 4*0.00019 + 11*0.00019 = 0.00285 millions= 2850
Likewise I need to find the total holding cost for the month considering each particular week.
3- rounds
Cash depositing order for 1st week - 13, (5+4+4), (2+11+1)=(1+1+12)
Edit- Assuming 12 rounds per month package is choosen, therefore 3 rounds per week( 3*4 =12)
Total depositing cost = 32000
1 - 13 deposited,
13 withdrawn,
0 remaining, 0 holding cost
2- (5+4+4) deposited, 5 withdrawn, (4+4) remaining, (4+4)*0.00019 holding cost
3- 0 deposited, 4 withdrawn, 4 remaining, 4*0.00019 holding cost
4- 0 deposited, 4 withdrawn, 0 remaining, 0 holding cost
5- (2+11+1) deposited, 2 withdrawn, (11+1) remaining, (11+1)*0.00019 holding cost
6- 0 deposited, 11 withdrawn, 1 remaining, 1*0.00019 holding cost
7- 0 deposited, 1 withdrawn, 0 remaining, 0 holding cost
Total holding cost for 1st week = (4+4)*0.00019 + 4*0.00019 + (11+1)*0.00019 + 1*0.00019 = 0.00475 millions = 4750
Likewise I need to find the total holding cost for the month considering each week.
Edit - suppose the 41975 package is picked. Then it means cash deposited in 20 rounds per month. That means 5 rounds per week. If the 32000 package is picked, then 12 rounds per month. That means 3 rounds per week. If the 21135 package is picked, then it means for 4 rounds per month, that means 1 round per week. There are no mixed combinations of 5,3,1 for the four weeks of a particular month. Only all four weeks are done in 1, 3 or 5 rounds. We have to select the best package considering holding cost and package cost.
A good combination of 5 rounds which doesn't violate order, can be better than all the 3 rounds solutions and the 1 round solution. Same applies for 3 rounds solution aswell. Or else 1 round solution can be better than all 5 rounds and 3 rounds solutions.
When depositing rounds increase, holding cost reduces but depositing cost increases. When rounds decrease, depositing cost reduces but holding cost increases. So I need to find the order of depositing money for each week of the month and the monthly depositing package which can make a good tradeoff between total holding cost and total depositing cost, consuming the least time.
Any insight to the approach will be really helpful.
In your case you have a fixed monthly cost (FMC) and a variable monthly cost (VMC) for each choice of package. FMC is in {x, x+10000, x+20000}, while VMC is the sum of the variable weekly costs VWC for the 4 weeks. VWC is determined by the partition of the interval of the set of days D = (M,T,W,T,F,S,S) into k disjoint sub-intervals, with k in {1,3,5}.
Therefore you have to choose min{FMC1+VMC1*, FMC3+VMC3*, FMC5+VMC5*}, where VMCk* denotes the minimum variable monthly cost for partitioning D into k intervals (Note that for the case of k=1 the answer is trivial since there exists a single partition for each week). Since the variable weekly cost is VWC= 0.7*(r1+r2+r3+r4+r5+r6+r7), ri being the remaining amount of day i, it all boils down to minimizing the remaining quantity of each week. In calculating the VMCk* you can use DP-algorithm described in this paper, with the objective of minimizing the remaining amount of each week.
So in high level:
Obtain the minimum variable weekly cost -> minimum remaining amount of each week using DP, for each package of deposits {1,3,5}. And then the variable monthly cost as the sum of the 4 weeks.
Choose the minimum total cost from considering the fixed cost of each package and the variable one obtained at 1.
We have one distribution center ( ware house ) and we are getting orders in real time whose time/distance from ware house and other order locations is known.
time matrix=
W O1 O2 O3
W 0 5 20 2
O1 5 0 21 7
O2 20 21 0 11
O3 2 7 11 0
order time of O1= 10:00 AM
order time of O2= 10:20 AM
order time of O3= 10:25 AM
I want to club as many as order possible such that delivery time of any order does not exceed by 2 hours of its order time. Thus the question is to reduce the number of trips(Trip is when delivery agent goes for delivery).
I am trying to come up with algorithm for this. there are two competing factors when
We can combine all the orders in the sequence as they are coming till it satisfies the constraint of delivery of the order within 2 hours of its ordering time.
We can modify above approach to find the bottleneck order(due to which we can not club more order now in approach 1). and pull it out from trip1 and make it a part of trip 2(new order) and wait for other orders to club it with trip1 or trip2 depending.
All the orders are coming in realtime. What will be the best approach to conquer this situation. Let me know if you need more clarity on this.
Very safe and easy algorithm which is guaranteed to not exceed the maximal waiting time for an order:
Let TSP() be a function which returns the estimate of time spent to visit given places. The estimate is pessimistic, i.e. the actual ride time can be shorter or equals to estimate, but not longer. For the good start you can implement TSP() very easily in a greedy way: from each place go to the nearest place. You can subtract the length of the longer edge coming out from W to have better estimate (so a car will always take the shorter edge coming out of W). If TSP() would happen to be optimal, then the whole algorithm presented here would be also optimal. The overall algorithm is as good as TSP() implementation is, it highly depends on good estimation.
Let earliestOrderTime be a time of the earliest not handled yet order.
Repeat every minute:
If there is a new order: If s is empty, set earliestOrderTime to current time. Add it to a set s. Calculate t = TSP(s + W).
If (current time + t >= earliestOrderTime + 2 hours): send a car for a TSP(s + W) trip. Make s an empty set.
Example
For your exemplary data it will work like this:
10:00. earliestOrderTime = 10:00. s = {O1}. t = TSP({01, W}) = 10 - 5 = 5.
10:00 + 0:05 < 10:00 + 2:00, so we don't send a car yet, we wait.
...
10:20. s = {O1, O2}. t = 46 - 20 = 26.
10:20 + 0:26 < 10:00 + 2:00, so we wait.
...
10:25. s = {O1, O2, O3}. t = 2 + 7 + 21 + 20 - 20 = 30.
10:25 + 0:30 < 10:00 + 2:00, so we wait.
...
11.30.
11:30 + 0:30 >= 10:00 + 2:00, so we send a car to go to O3, O1, O2 and back to W. He visits orders at 11:32, 11:39, 12:00 and come backs at 12:20. Guys where waiting 67, 99 and 100 minutes.
I have this table :
Process Time of arrival Required time Priority
-------- --------------- ------------- --------
P1 6 4 5
P2 2 9 2
P3 4 6 1
P4 10 4 30
SJF, SRTF, and Priority (Preemptive and non preemptive)
In the examples I see Burst time used , what is equivalent of that word should I use required time ?
OK, this is a question I got for my advance algorithm class. I already turned in my solution once but got rejected by my instructor due to efficiency issue, in other words, I already made the efforts on my part but could not get it even after his hint, so please be gentle. I will give his hint below
Given an array of intervals with both start point and end point, find the number of other intervals fall within it for each interval. number of intervals is less than 10^9 and their ids are distinct. start and end are less than 10^18, the input files don't contain duplicate number for start and end. All the numbers above are integers
the hint is: considering a data structure with buckets. The algorithm should be faster than O(n^2)
sample input and output
input:
5 %% number of intervals
2 100 200 %% id, start,end. all lines below follows this
3 110 190
4 105 145
1 90 150
5 102 198
output:
3 0
4 0
1 1
5 2
2 3
The numbers are pretty big so O(N log N) might be a little to much but here's an idea.
First things first normalize the values, that means turning them smaller while keepinging the same ordering. In your example the normalize would be
90 100 102 105 110 145 150 190 198 200
1 2 3 4 5 6 7 8 9 10
So you're new intervals are:
5
2 2 10
3 5 8
4 4 6
1 1 7
5 3 9
Now the edges of the intervals are in the range of [1, 2N].
Now sort the intervals by their end:
5
4 4 6
1 1 7
3 5 8
5 3 9
2 2 10
When you reach an interval you can say that all the intervals that start before it and have not been encountered yet should have their answer increased by one. This can be done with a SegmentTree.
What you do when you get an interval [x, y] you increase all values in the range [1, x - 1] by 1 and then compute its answer as the value at x in the segment tree. That's just addition on an interval and query on a point, a common segment tree problem.
I don't really think you can solve this problem with less than O(N log N) time and O(N) memory, so this solution should be the asymptotically best solution in both time and space.
Here is a probability problem: you observe .5 cars on average passing in front of you every 5 minutes on a road. What is the probability of seeing at least 1 car in 10 minutes?
I'm trying to solve this in 2 ways. The first way is to say: P(no car in 5 minutes) = 1 - .5 = .5. P(no car in first 5 minutes and no car in second 5 minutes) = P(no car in first 5 minutes) * P(no car in second 5 minutes) by independence. Therefore P(at least 1 car in 10 minutes) = 1 - .5*.5 = .75.
However, if I try the same, with a Poisson distribution with rate lambda = .5 per unit of time, for 2 units of time, I get: P(at least 1 car in 2 units of time) = 1 - exp(-2*lambda) = .63.
Am I doing something wrong? If not, what explains the discrepancy?
Thanks!
Your first calculation is incorrect. An average .5 cars / 5 minutes does not imply P(no car in 5 minutes) = 0.5. Consider for instance a process where every five minute, you see either no car with probability 90%, or 5 cars with probability 10%. On average you will see 0.5 cars every five minute, but the probability you see 0 cars in the next 5 minutes is clearly not 50%.
I haven't checked the computations for your second example; the calculation logic is looks correct, but the conclusion is incorrect: you are making an assumption about the distribution (Poisson) which is plausible but not implied by the problem statement.
If you take again my example, which is consistent with your problem description, the probability to see 0 cars in 10 minutes is 0.9 x 0.9 = 0.81, which gives you 19% of seeing one car or more. We could arbitrarily change my example to give you a wide variety of probabilities.
From your problem statement, the only thing you can say is that "in the long run, you'll see 0.5 cars every 5 minutes". Beyond that you can't make a statement on what should be expected within 10 minutes, unless you make some assumptions about the distribution of the cars arrivals.