I propose to solve a scenario that is pretty familiar. I hope you can help me.

I have a set of home appliances and their power consumption (in watts).

  • TV: 250 W
  • Oven: 2400 W
  • Washing machine: 500 W

I need a way (an algorithm) to find the maximum power consumption in a given interval.

Given, for example that:

  • I watch TV from 2 to 4 and from 9 to 11
  • I put the oven from 2:30 to 3
  • I put the washing machine from 2 to 4

A set of sample results would be:

  • from 12 to 2: 250 W (since only the TV is on in the given interval)
  • from 2 to 5: 3150 W (there is a moment when the 3 appliances are ON)
  • from 2:30 to 3: 2650 W (oven + TV)

I hope the example is easy to get. Thanks in advance!

  • $\begingroup$ This should not be too hard. Go through the events where you turn an appliance on or off and adjust the power consumption accordingly. Then take the maximum of all such values. Am I overlooking something? $\endgroup$ – dkaeae May 23 '19 at 15:00
  • $\begingroup$ @dkaeae Thanks, I've corrected the expected values. I was on mobile and calculated them wrong. Sorry! $\endgroup$ – SuperJMN May 23 '19 at 15:05
  • $\begingroup$ @dkaeae I understand what you say, but I don't see how to design the algorithm. Maybe sorting the intervals when the appliance is ON and looping through it, looking for overlapping intervals? $\endgroup$ – SuperJMN May 23 '19 at 15:08
  • 1
    $\begingroup$ The changes in power consumption only occur at discrete time points, namely the ones defining the intervals. You just need to determine what the power consumption is after each one of these time points, and you can do so on the basis of what has been switched on or off. $\endgroup$ – dkaeae May 23 '19 at 15:10
  • $\begingroup$ OK, I have created an algorithm in C#, but it looks suboptimal. I don't know if it's a good idea to share it, since people in this Stack Exchange site seem to feel uncomfortable with code. $\endgroup$ – SuperJMN May 23 '19 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.