Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

algorithm - Calculate largest tanker volume in one dimensional array

I have the following puzzle: Create a method to calculate the largest tanker volume that will be used to hold floodwater. Method input: a one-dimensional array of integers and one integer value that represents the width of the tanker.

integer getLargestTanker(Integer width, Integer[] values)

The one-dimensional array contains integer values that represent the tanker heights. The distance between those integer values in the array represents the tanker length.

Example

Let's say we have the following array: {2, 9, 6, 3, 5, 7} We need to pick two numbers such that the product of the effective height and length (distance between those two numbers) is the maximum. If we pick 2 and 9, the effective tanker height is: 2 and the distance is 1. In this example, the maximum tanker volume value would be 7x4x(static given width). Because we should choose heights: 9 and 7. Explanation: the effective height is 7 (since it's a tanker that holds water). The length of the tanker is 4 (index-1 and index-7).

My Solution

Two nested loops. Complexity is O(n^2). I strongly believe that there should be a better solution, but I couldn't come up with one. Do you have any better idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When pairing two heights, the volume is determined by the minimum height.

Then, we focus here on the determination of the min height of this pair. For each index i, corresponding to a height A[i], we determine the minimum and the maximum indices corresponding to all A values greater than A[i].

For that, we sort the indices i = index[.] according to the values A[i]. For a given i = index[j], all canditate indices correspond to the index[k], for k > i.

A simple loop allow to find the minimum and maximum values of these index[k].

The complexity is dominated by the sorting: O(nlogn)

Here is a code in C++, rather simple and easy to understand whatever the language you are using.

#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>

int max_vol (const std::vector<int> &A) {
    int vmax = 0;
    int n = A.size();
    std::vector<int> index(n);
    std::iota (index.begin(), index.end(), 0);
    auto comp = [&] (int i, int j) {return A[i] < A[j];};
    std::sort (index.begin(), index.end(), comp);
    int index_max = index[n-1];
    int index_min = index[n-1];
    for (int i = n-2; i >= 0; --i) {
        int d0 = std::abs (index[i] - index_max);
        int d1 = std::abs (index[i] - index_min);
        int vol = std::max (d0, d1) * A[index[i]];
        if (vol > vmax) vmax = vol;
        index_max = std::max(index[i], index_max);
        index_min = std::min(index[i], index_min);      
    }
    return vmax;
}

int main() {
    std::vector<int> heights = {2, 9, 6, 3, 5, 7};
    auto max_volume = max_vol (heights);
    std::cout << "max volume = " << max_volume << std::endl;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...