I want to find the best common time to change the other times the least (that the sum of the differences is the lowest possible).
On input, I have the array of times.
On output, there should be the new, common time.
Note that the times can be not ordered and be absolutely different (e.g. 02:00, 02:30 and 03:30)
Example 1
Input: ["01:00", "02:00", "03:00", "04:00", "05:00"]
Output should be 03:00, because it's the best to change 01:00 to 03:00 (2 hours of change), 02:00 to 03:00 (1 hour of change), keep 03:00 the same, 04:00 to 03:00 (1 hour), and 05:00 to 03:00 (2 hours).
Example 2
Input: ["12:00", "13:00", "14:00"]
Output should be 13:00 - change 12:00 to 13:00 (1 hour) and 14:00 to 13:00 (1 hour)
Example 3 (tricky)
Input: ["23:00", "01:00", "02:00"]
Output should be 01:00 - change 23:00 to 01:00 (2 hours) and 02:00 to 01:00 (1 hour) (the tricky thing is 23:00 - the best time is not 13:00)
I tried functions that make the average of times, e.g. https://stackoverflow.com/a/52839039/19022995, but unfortunately it doesn't work
I would be really grateful for tips how to do this
Thank you in advance
Lemma: an optimal answer coincides with one of the given times.
To prove it, consider a solution that doesn't coincide with any of the given times. Try to move it a bit forward and then a bit backward. In at least one of the cases, the total difference does not increase. So, continue moving in that direction until you hit one of the given times.
What is left now is to write a function that computes the difference, taking wraparound from 23:59 to 00:00 into account.
After that, try every given time as a candidate answer, calculate the total difference for each, and pick the best one as the final answer.
In pseudocode:
time (h, m) = h * 60 + m
diff (t1, t2) = min (abs (t1 - t2), 60 * 24 - abs (t1 - t2))
t[0..n) = given times
total (x) = sum {diff (x, t[i])} for i in [0..n)
answer = arg min {total (t[i])} for i in [0..n)
One approach is to look for the smallest "window" containing all the times. To do this, sort the times (naively) and, consider each time as being the "first". For instance, in your third example, considering 1:00 as the first time would lead to a 22 hour window (because that makes the "last" time 23:00); considering 2:00 as the first time would lead to a 23 hour window; but considering 23:00 as the first time would lead to a 3 hour window. From there, simply calculate relative to that first time, calculating differences mod 24.
Related
Lets say we have the following fares available :
1 trip
2 trips
10 trips
unlimited weekend (saturday to sunday, not 2 days)
1 day
3 days
unlimited week (monday to sunday, not 7 days)
unlimited month (1st to last day of month)
... with a price for every one of them.
The problem is : **How to determine what set of subscriptions to chose given a date of arrival and a date of departure ? **
Lets say we want the solution for n between 1 and 8, n being the number of time we take the metro daily (so we assume we take the metro the same number of time every day)
For example it would say something like :
n = 1
Arriving on Friday 19th and leaving Thursday 23th, the best is taking the 1 trip, then the weekend, then the 2 trips (didnt calculate but you see the point)
n = 2
...
I have found examples with only 1 day, 2 days, 7 days fares with dynamic programming, but it looks a lot harder when you considerate the days of the week.
Thanks :)
I like to view this kind of dynamic program as finding a shortest path in a directed acyclic graph.
Each node of the graph encodes
what the current day is (either during the travel period or the day after), and
how many trips remain on trip-limited passes (at most n + 9).
Each arc represents either
purchasing a specific pass at a specific time (the length of the arc is the cost of this pass), or
using trip-limited passes to cover the day's trips (the length of the arc is zero).
The time-limited passes advance the day to the first day they no longer work. The trip-limited passes increase the number of remaining trips. The zero-cost arcs advance the day by one while decreasing the number of remaining trips by n.
Given the shortest path, it is easy to decode it to a plan for purchasing passes.
(P.S. I don't know what the rules are on, e.g., purchasing a week pass on a Tuesday for the rest of the week. Even if this is not allowed, you're going to want to put arcs for the time-limited passes that could have been purchased on a previous day during the travel period.)
I have Job Sequencing Problem with three parameters where every task has time to be completed(in weeks) and a deadline that mush be finished before it.
In other words, any week, Job can be worked on at most. All jobs have a hard deadline, which means they must be completed before the deadline. The task is to arrange the jobs so that a high profit shall be accumulated.
Example
Input:
JobID Time Profit Deadline
1 8 100 13
2 1 100 1
3 1 100 3
4 1 100 2
5 4 100 6
Output
Total profit: 400
Jobs in order: 2 4 3 1
I have been trying to apply greedy algorithm but it only works with two parameters(profit & deadline) but here I have to take time into consideration
The solution can be obtained through dynamic programming. Before diving into the recursion, something to develop the intuition:
(I'm assuming any task will start no earlier than the earliest date in the deadline. That is, for your example, it would be day 1.)
Let D_i denote the time in days it takes to complete Task (or job) T_i. Denote the profits for task T_i as P_is.
On a calendar day C (the hypothetical calendar starting on earliest day of the deadline), you can be doing only one task per the problem definition. It could be one of T_1, T_2, T_3, T_4 or T_5.
If you were doing task T_1 on day C, it means that you started the task on C - D_1 day of the calendar. Similarly, if you were doing task T_2 on day C, then you started on the task on day C - D_2, and so on.
You could be doing any one of the tasks T_1, T_2, T_3, T_4 or T_5 on day C. Therefore, the profit you get by doing the last task in the sequence on day C is one of P_1, P_2, P_3, P_4 or P_5. If you add the profit of Task T_i that you were doing on the last day (i.e. P_i) to the maximum profit until day C - D_i, not including that task, for each 1<= i <= 5, you'll get the set of maximum profits you could possibly make on day C. The highest among these profits is what you're looking for. In the end, the answer would be the solution for day 13. (C = 13)
(I've tweaked your example a slight bit for more generality)
I'll leave it up to you to figure out how to codify this into a programming construct. On a broad level, you'll need two variables for your dynamic programming recursion:
Set of tasks (i.e., modeling T_i)
Calendar Days (i.e., modeling C)
Prioritize the jobs by profit per unit of time, breaking ties for the one that finishes first.
In other words think of it as dollars per hour and work on the job that pays you fastest.
JobID Time Profit Deadline Priority
1 8 100 13 12.5
2 1 100 1 100
3 1 100 3 100
4 1 100 2 100
5 4 100 6 25
And now we look at jobs 2, 4, 3, 5, then 1 in that order. We can do the first three and the last.
If I am given the total number of occurrences of an event over the last hour, and I can get this data at arbitrary times ( but at least once an hour ), how can I work out the total number of occurrences over a 24 hour period?
Obviously, you can't. For example -- if the first two observations overlap then it would be impossible to determine the number of kills during the overlap. If there is a time gap between the first two observations then there is no way to determine what happened during the gap. You could try to set up a system of equations -- but the resulting system will be underdetermined (but it could give you both a min and a max, which might be relevant).
Why not adopt a statistical approach? Let X = kills over a 1 hour period. This is a random variable. Estimate its expected value by sampling it at randomly chosen times and multiply your estimate by 24.
Can someone tell me how i can make arrival time counting (17:00) minus 22 minutes?
Arrival Time Time On Bay Time Off Bay
17:00
So every time when I put something in, say "time" I will get "time minus 25 minutes" in arrival.
In Excel, you can do simple arithmetic on time values. The difference between two times is a (fractional) number of days. For example, 0.25 represents a time difference of 6 hours.
As such, you can add/subtract a (fractional) number of days to a time. Example:
cell A1 is a fixed time value 17:00
cell A2 is a formula =A1-0.25; this will evaluate to 11:00
To work with minutes, you will have to convert the amount of minutes to an amount of days; just divide the amount of minutes by 1440.
So a time difference of 25 minutes is represented by 25/1440. Example:
cell A1 is a fixed time value 17:00
cell A2 is a formula =A1-25/1440; this will evaluate to 16:35
I am trying to convert a timestamp in the format "yyyy-mm-dd" to an integer fiscal week. Currently, my algorithm is 4(k-1) + floor(d/7)+1 where k is the integer month and d is the integer day of the month. Saturday starts the new fiscal week.
This has some flaws and is incorrect. For instance consider Saturday, January 28th 2012:
My algorithm computes 5(which is correct).
Next consider, Friday February 3rd:
My algorithm computes 5(which is correct).
Now consider Saturday February 4th.
My algorithm computes 5(this is incorrect).
It appears my algorithm will always fail in between months and thus accumulate an increasing error.
How can I compute the correct fiscal week?
Compute the number of days elapsed before the beginning of the current month, add it to dd and then divide by 7. Finally, add 1 to the resulting number.
So for Feb 4th, the answer would be (31 + 4)/7 + 1 = 6.