简体   繁体   中英

How to reduce the time complexity of bucket filling program?

I was solving a problem which states following:

There are n buckets in a row. A gardener waters the buckets. Each day he waters the buckets between positions i and j (inclusive). He does this for t days for different i and j .

Output the volume of the waters in the buckets assuming initially zero volume and each watering increases the volume by 1 .

Input : first line contains t and n seperated by spaces. The next t lines contain i and j seperated by spaces.

Output : a single line showing the volume in the n buckets seperated by spaces.

Example:

Input:

2 2
1 1
1 2

Output:

2 1

Constraints:

0 <= t <= 10 4 ; 1 <= n <= 10 5

I tried this problem. But I use O(n*t) algorithm. I increment each time the bucket from i to j at each step. But this shows time limit error. Is there any efficient algorithm to solve this problem. A small hint would suffice.

PS: I have used C++ and Java as tags bcoz the program can be programmed in both the languages.

而不是记住每个桶中的水量,记住每个桶和前一个桶之间的差异。

have two lists of the intervals, one sorted by upper, one by lower bound

then iterate over n starting with a volume v of 0.

On each iteration over n
  check if the next interval starts at n
    if so increase v by one and check the next interval.
  do the same for the upper bounds but decrease the volume
  print v
repeat with the next n

I think the key observation here is that you need to figure out a way to represent your (possibly) 10 5 buckets without actually allocating space for each and every one of them, and tracking them separately. You need to come up with a sparse representation to track your buckets, and the water inside.

The fact that your input comes in ranges gives you a good hint: you should probably make use of ranges in your sparse representation. You can do this by just tracking the buckets on the ends of each range.

I suggest you do this with a linked list. Each list node will contain 2 pieces of information:

  1. a bucket number
  2. the amount of water in that bucket

You assume that all buckets between the current bucket and the next bucket have the same volume of water.

Here's an example:

Input:
5 30
1 5
4 20
7 13
25 30
19 27

Here's what would happen on each step of the algorithm, with step 1 being the initial state, and each successive step being what you do after parsing a line.

  1. 1:0NULL (all buckets are 0)
  2. 1:16:0NULL (1-5 have 1, rest are 0)
  3. 1:14:26:121:0NULL (1-3 have 1, 4-5 have 2, 6-20 have 1, rest have 0)
  4. 1:14:26:17:214:121:0NULL
  5. 1:14:26:17:214:121:025:1NULL
  6. 1:14:26:17:214:119:221:125:228:1NULL

You should be able to infer from the above example that the complexity with this method is actually O(t 2 ) instead of O(n×t) , so this should be much faster. As I said in my comment above, the bottleneck this way should actually be the parsing and output rather than the actual computation.

Here's an algorithm with space and time complexity O(n)

I am using java since I am used to it

1) Create a hashset of n elements 2) Each time a watering is made increase the respective elements count 3) After file parsing is complete then iterate over hashset to calculate result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM