Showing posts with label Two rounds. Show all posts
Showing posts with label Two rounds. Show all posts

Monday, May 25, 2015

[LintCode] Candy

There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
  • Each child must have at least one candy.
  • Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?

Example
Given ratings = [1, 2], return 3.
Given ratings = [1, 1, 1], return 3.
Given ratings = [1, 2, 2], return 4. ([1,2,1]).
class Solution {
public:
    int candy(vector<int> &ratings) 
    {
        int num = ratings.size();
        if(0 == num) return 0;
        vector<int> candies(num, 1);
        
        for(int i=1;i<num;i++)
        {
            if(ratings[i]>ratings[i-1])
                candies[i] = candies[i-1]+1;
        }
        
        for(int i=num-2;i>=0;i--)
        {
            if(ratings[i] > ratings[i+1] && candies[i] <= candies[i+1])
            {
                candies[i] = candies[i+1] + 1;
            }
        }
        int sum = 0;
        for(int i=0;i<num;i++) sum += candies[i];
        
        return sum;
    }
};

Sunday, May 17, 2015

[LeetCode] Best Time to Buy and Sell Stock III

Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Analysis:

Just needs to calculate a vector, whose value at index i means the maximum profit from buying and selling once until the index. Also, it can hold the maximum profit from buying and selling from i+1 to end. Loop the array twice will get the vector.

You might be asked why can't you loop from the beginning firstly and from the end secondly in the following code.

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int len = prices.size();
        if(1 >= len) return 0;
        vector<int> profit(len, 0);
        int sell = prices[len-1];
        for(int i=len-2;i>=0;i--)
        {
            int diff = sell - prices[i];
            profit[i] = max(profit[i+1],  diff);
            if(diff<0) sell = prices[i];
        }
        
        int buy = prices[0];
        for(int i=1;i<len;i++)
        {
            int diff = prices[i] - buy;
            profit[i] = max(profit[i-1], profit[i] + diff);
            if(diff < 0) buy = prices[i];
        }
        
        return profit[len-1];
    }
};

Tuesday, April 28, 2015

[LeetCode] Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. 

Analysis: From the left for each index i, find the next index j whose value is larger than or equal to i, so that the interval between i and j may trap some water. And repeat the procedure from j till end. Do the same from right, but only find the next index whose value is larger than it to avoid duplication.
class Solution 
{
    static bool lt(int a, int b){return a>b;}
    static bool le(int a, int b){return a>=b;}
    
    int trap(vector<int>& height, 
        const function <bool (int, int)>& comp) 
    {   
        int res =0, i=0, sum=0;
        int l = i++;
        for(;i<height.size();i++)
        {
            if(comp(height[i],height[l]))
            {
                res += max(0,(i-l-1)* height[l]-sum);
                l = i;
                sum = 0;
            }
            else sum += height[i];
        }
        return res;
    }
public:
    int trap(vector<int>& height)
    {
        int leftValue = trap(height, le);
        reverse(height.begin(), height.end());
        int rightValue = trap(height, lt);
        return leftValue + rightValue;
    }
};