John ' s business

Source: Internet
Author: User

There are n cities on a axis, numbers from 0 ~ n-1. John intends to did business in this N cities, he was interested in Armani ' s shipment. Each of the city has a price for these goods prices [i]. For City X, John can-buy-goods from the "city numbered" from X-k to X + K, and sell them to City X. We want to know how much of John can earn at most.

Considerations
Prices.length The range is [2, 100000], K <= 100000.

Examples
Given prices = [1, 3, 2, 1, 5], k = 2, return [0, 2, 1, 0, 4].

Explanation:
i = 0, John can go to the city 0 ~ 2. He can don't make money because the "prices in City 1" and "City 2 are both higher than" the price of city 0, which is, ans[0] = 0;
i = 1, John can go to the city 0~3. He can buy from City 0 or City 3 to earn the largest price difference. That is, ans[1] = 2.
i = 2, John can go to the city 0~4. Obviously, he can earn of the largest price difference by buying to City 3. That is, ans[2] = 1.
i = 3, John can go to the city 1~4. He can don't make money cause City 3 has the lowest price. That is, ans[3] = 0.
i = 4, John can go to the city 2~4. He can earn the "largest price difference" by buying to City 3. That is, ans[4] = 4.

Given prices = [1, 1, 1, 1, 1], k = 1, return [0, 0, 0, 0, 0]

Explanation: All cities are of the same price, and so
John can don't make, which is, all ans are 0.

For the first time, TLE

#ifndef c751_h #define C751_H #include <iostream> #include <vector> using namespace std; Class Solution {public:/** * @param a:the prices [i] * @param k: * @return: the ANS array */ve
        Ctor<int> Business (vector<int> &a, int k) {//Write your code here int len = A.size ();
        vector<int> Res (len, 0);
        int min = Int_max;
        for (auto c:a) {if (min > c) min = C;
            } if (k >= len) {for (auto &c:a) c-= min;
        return A;
            for (int i = 0; i < len; ++i) {int value = Int_min;
            if (i-k <= 0 && i + K >= len-1) {value = a[i]-min; else if (i-k <= 0 && i + K < len-1) {for (int j = 0; J <= i + K;
        ++J) {            Value = Maxval (value, A[i]-a[j]);  ' Else if ' (i-k>0 && i + k >= len-1) {for (int j = I-k; J < Len;
                ++J) {value = Maxval (value, A[i]-a[j]);
                    } else {for (int j = i-k; J <= i + K; ++j) {
                Value = Maxval (value, A[i]-a[j]);
        }} Res[i] = value;
    return res;
    int maxval (int a, int b) {return a>b? a:b;
}
}; #endif

The topic is essentially to find the minimum value min on the interval [i-k,i+k] for position I, and to take the new value a[i]-min. The
can take advantage of line-segment trees:

#ifndef c751_h #define C751_H #include <iostream> #include <vector> using namespace std;
    Class segmentnode{Public:int start, end, Min;
    Segmentnode *left, *right;
        Segmentnode (int start, int end, int min) {this->start = start;
        This->end = end;
        this->min;
    This->left = This->right = NULL;
}
}; Class Solution {public:/** * @param a:the prices [i] * @param k: * @return: the ANS array */ve
        Ctor<int> Business (vector<int> &a, int k) {//Write your code here int len = A.size ();
        vector<int> Res (len, 0);
        Segmentnode *root = Build (A, 0, len-1);
            for (int i = 0; i<len; ++i) {int start = I-k;
            int end = i + K;
            Fixed interval range if (Start < 0) start = 0;
            If (end >= len) end = len-1;
   Res[i] = a[i]-query (root, start, end);     return res;  //Query segment tree minimum value int query (segmentnode *root, int start, int end) on interval [start,end] {if (Start > End | |
        !root) return 0;
        Segmentnode *node = root;
        if (start = = Node->start&&end = node->end) return node->min;
        if (start >= node->right->start) return to query (Node->right, start, end);
        else if (end <= node->left->end) return query (Node->left, start, end); else {return minval query (Node->left, start, node->left->end), query (Node->right, NODE-&G
        T;right->start, end)); }//Build line segment tree, node contains minimum value within interval segmentnode *build (vector<int> &a, int start, int end) {if STA
        RT > end] Return NULL;
        Segmentnode *node = new Segmentnode (start, end, Int_max);
        if (start = = end) Node->min = A[start];
  else {          Node->left = Build (A, start, (start + end)/2);
            Node->right = Build (A, [start + end]/2 + 1, end);
        Node->min = Minval (Node->left->min, node->right->min);
    } return node;
    int minval (int a, int b) {return a < b a:b;
}
}; #endif
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.