Calculate maximum number of rooms occupied at once [duplicate] - c++

This question already has answers here:
Given a set of intervals, how to find the maximum number of intersections among them,
(3 answers)
Closed 3 years ago.
On the input, we are given a number N stating how many presentations are to be given, which is followed by N rows of start and end times given in HHMM format
Example:
3
0800 0900
0830 1000
0900 1030
The code must calculate the maximum nuber of occupied rooms (one room can host only one presentation at a time, therefore expected output to the provided example is 2.
My first idea was to create a table of bools 1440 x N (number of minutes in a day by number of presentations) and fill each minute when a presentation is being held, and later go column by column and find the maximum value of presentations at a time. It works, but I'm sure it can be done faster and better. Can someone suggest how to do it in a better manner?

Pretty simple actually: we just simulate the process. First of all, it doesn't matter which presentations are going on right now. All we care about is the number of presentations happening. So we'll just have a counter which we update when a presentation starts or ends.
We could iterate over every minute for the simulation, but our counter only changes when a presentation starts or ends, so we can just make a big list of all the start and end events, sort the list by time, and iterate through the list adjusting our counter appropriately.

The way you propose needs 1440 x 3 = 4320 values. As you can tell, that's very inefficient. A better way is to store just the minutes that are actually required. To save even less values, divide the time into slots of 30 minutes.
Now use a dictionary (std::map) to keep a count of how many presentations are during the same time slot. For your example this gives:
std::map<std::string, int> slots =
{
{"0800", 1},
{"0830", 2},
{"0900", 2},
{"0930", 2},
{"1000", 1},
{"1030", 1},
}
I'll let you figure out how to implement this.

Related

Divide Large Number into Multiple Smaller Units

I am new to Stack overflow but I am having logic issues with Google sheets and need some advice on how to proceed.
My goal is I have 3 cup sizes, small medium and large. Example: Small holds 50ML, Medium 100ML,Large 200ML,etc
I want to take a large number and evenly distribute it among the cups to show me how many cups I will need with the least amount of cups used. Example : 170ML = 1 Large, 0 Medium, 0 Small. I only need 1 large to hold everything. Also, 240ML would suggest 1 Large, 0 medium, 1 small. Since small can hold the remaining 40 and medium would be too big of a container.
Problem is I don't understand how to break down my original large number into the smaller number as I have to check and compare if it will fit, I also have to be able to add more if there's a remainder and as far as I know the Google sheet functions only run and represent numbers once.
I've already tried breaking it down to my large container first then in my second row with medium cups I take the first result and subtract from my large number to see if anything is left. If there is, all I can do is add 1 or set the number, I can't seem to scale it up if it requires more than 1 cup which isn't what I want.
I've been going crazy trying to find an easy solution to this but it seems to get more complex as if I need IF statements of some kind.
If anyone has any ideas I'd be happy to hear them out.
you could use IF statement for example like this:
=ARRAYFORMULA(IF(A2:A<>"",
IF(A2:A>=B1, QUOTIENT(A2:A, B1),
IF(A2:A> C1+D1, QUOTIENT(B1, A2:A), 0)), ))

Can someone please tell me what is wrong with this logic?

I was solving a question on Hackerrank. The following is the question (in brief):
There exist n robbers trying to rob a bank. They can stay there for atmost G minutes. Only two robbers can enter a vault at a time.
a[]={a_1,a_2,...,a_n} is a user specified array such that a_i is the time the i_th robber wishes to stay in the vault.
A heist is successful if all the robbers get their wish.
Given n,G, a[]; the output must be "success" or "failure".
My logic was as follows:
sort(a) in descending order
define slot1 and slot2 for 1st and 2nd person in vault respectively
slot1=slot2=G
fill in slot1 and slot2 from the sorted a, such that whenever a robber is finished in the slot, the next one takes his place
If all robbers can be accommodated, then success, else failure.
I would try having a double pass. First, add up all the time the robbers would want, then halve and round up. That's your ideal time. (at this point, check if one of your robbers is at/over this amount; if so, then that's your limit.) Then, try to fit robbers into that time frame. If you can fit them evenly, you're good. Otherwise, increase the time and try again.
Edit: As JonTheMon noted above, your approach would fail when G = 6 and a = {2, 2, 2, 3, 3}.
Anyway, your idea is wrong and you would not be able to solve the problem in such a way. Here is a hint:
This is a classical DP problem.
old post with wrong test case:
Let G = 4 and a[] = {1, 2, 2, 3}.
As far as I understood your approach, you will first accommodate robbers a_1 and a_2. After a_1 finished, you will introduce a_3 on his place. However, this leaves a_4 without enough time in a vault - only 2 minutes where he wanted to be inside at least 3.

power function and arrays in c++

I'm trying to write a function that will take an array or vector and have its values taken to a "power of" and then display it's values. I'm not too familiar with arrays but simply put I'm trying to create something like
n = {2^1, 3^1, 5^1,2^2,3^2,5^2,....}
the "power of" is going to be looped.
I then plan to sort the array, and display 1500th term.
this problem corresponds to prime number sequence only divisible by 2 , 3 & 5;
I'm trying to find a more time efficient way than just if statements and mod operators.
If I remember correctly this is the Ugly Numbers problem I've faced some years ago in the UVa.
The idea to solve this problem is to use a priority queue with the numbers 2, 3 and 5 as initial values. At each step remove the topmost value t and insert the values 2*t, 3*t and 5*t in the priority queue, repeat this steps till the 1500th term is found.
See this forum for more info: http://online-judge.uva.es/board/viewtopic.php?t=93

How to record total values with rrdtool

I'm pretty sure this question has been asked several times, but either I did not find the correct answer or I didn't understand the solution.
To my current problem:
I have a sensor which measures the time a motor is running.
The sensor is reset after reading.
I'm not interested in the time the motor was running the last five minutes.
I'm more interested in how long the motor was running from the very beginning (or from the last reset).
When storing the values in an rrd, depending on the aggregate function, several values are recorded.
When working with GAUGE, the value read is 3000 (10th seconds) every five minutes.
When working with ABSOLUTE, the value is 10 every five minutes.
But what I would like to get is something like:
3000 after the first 5 minutes
6000 after the next 5 minutes (last value + 3000)
9000 after another 5 minutes (last value + 3000)
The accuracy of the older values (and slopes) is not so important, but the last value should reflect the time in seconds since the beginning as accurate as possible.
Is there a way to accomplish this?
I dont know if it is useful for ur need or not but maybe using TREND/TRENDNAN CDEF function is what u want, look at here:
TREND CDEF function
I now created a small SQLite database with one table and one column in that tabe.
The table has one row. I update that row every time my cron job runs and add the current value to the current value. So the current value of the one row and column is the cumualted value of my sensor. This is then fed into the rrd.
Any other (better) ideas?
The way that I'd tackle this (in Linux) is to write the value to a plain-text file and then use the value from that file for the RRDTool graph. I think that maybe using SQLite (or any other SQL server) just to keep track of this would be unnecessarily hard on a system just to keep track of something like this.

Most efficient way to process complex histogram data?

I'm currently implementing a histogram that will show a very large scale data using Qt and I have some doubts about which data structure(s) I should be using for my problem. I will be displaying the amount of queries received from users of an application and the way I should display is as follows -in a single application that will show different histograms upon clicking different "show me this data etc." buttons-
1) Display the histogram of total queries per every month -4 months of data here, I
kept four variables and incremented them as I caught queries belonging to those months
in the CSV file-
2) Display the histogram of total queries per every single day in a selected month -I was thinking of using 4 QVectors to represent the months for this one, incrementing every element of the vector (day), as I come by that specific day -e.g. the vector represents the month of August and whenever I come across a data with 2011-08-XY , I will increment the (XY + 1)th element of that vector by 1- my second alternative is to use 4 QLinkedList's for the sake of better complexity but I'm not sure if the ways I've come up with are efficient enough and I'm willing to listen to any other idea.
3) Here's where things get a bit complicated. Display the histogram of total queries per every hour in a selected day and month. The data represented is multiplied in a vast manner and I don't know which data structure -or combination of structures- I should use to implement this one. A list of lists perhaps?
Any ideas on my problems at 2) and 3) would be helpful, Thanks in advance.
Actually, it shouldn't be too unmanageable to always do queries per hour. Assuming that the number of queries per hour is never greater than the maximum int value, that's only 24 ints per day = 32 bits or 64 depending on your machine. Assuming 32 bits, then you could get up to 28 years worth of data per MB.
There's no need to transfer the month/year - your program can work that out. Just assign hour 0 to the earliest point in your data, which you keep as a constant, then work out the date based on hours passed since then.
This avoids having to have a list of lists or anything fancy - just use an array where each address contains the number of hours since hour 0, and the number of queries for that hour.
Why don't you simply use a classical database?
When you start asking these kind of question I think it is a good time to consider a more robust structure.There are multiple data structures implemented inside any DB, optimized either for different access type. You should considerate at least lookup, insertion, deletion, range queries. There is no structure which is better than the others in all costs, so there is always a trade-off.
Qt has some database classes you can use. I never used the Qt SQL library, but I think you should give it a shot. Fortunately, there is a Qt SQL programming guide at the end of the page linked.