Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flip cards to get maximum sum

Tags:

c++

algorithm

Given N cards where if ith card has number x on its front side then it will have -x on back side and a single operation that can be done only once that is to flip any number of cards in consecutive order only once.

Now we need to flip cards in such a way that sum of number of upper face of cards is maximum.

Example : If N=5 and cards[] be {-2,3,-1,-4,-2} then here answer is 8 as we can flip last 3 cards to get configuration {-2,3,1,4,2} which sum to 8.

My Approach :

Go for each possible way for each ith position as start position and find the maximum.But is their any better solution to this problem?

My Code : Am not able to find problem till yet

#include<bits/stdc++.h>
using namespace std;
int solve(std::vector<int> const & numbers)
{
    int min_so_far  = numbers[0], min_ending_here = numbers[0];
    size_t begin = 0;
    size_t begin_temp = 0;
    size_t end = 0;
    for(size_t i = 1; i < numbers.size(); i++)
    {
            if(min_ending_here > 0)
            {
                    min_ending_here = numbers[i];
                    begin_temp = i;
            }
            else
            {
                    min_ending_here += numbers[i];
            }

            if(min_ending_here <= min_so_far )
            {
                    min_so_far  = min_ending_here;
                    begin = begin_temp;
                    end = i;
            }
    }
    int sum=0;
    for(int i=0;i<begin;i++){
        sum+=numbers[i];
    }
    for(int i=begin;i<=end;i++){
        sum-=numbers[i];
    }
    for(int i=end+1;i<numbers.size();i++){
        sum+=numbers[i];
    }
    return sum;

}
int main(){
int n;
cin>>n;
vector<int> arr;
for(int i=0;i<n;i++){
    int x;
    cin>>x;
    arr.push_back(x);
}

  cout<<solve(arr)<<"\n";
}
like image 364
user3878046 Avatar asked Jul 25 '14 20:07

user3878046


People also ask

How many cards can you flip?

You can flip any number of cards (possibly zero). After choosing the front and the back of each card, you will pick each card, and if the integer printed on the back of this card is not printed on the front of any other card, then this integer is good.

What happens when you flip a card with no good numbers?

Return the smallest good and integer after flipping the cards. If there are no good integers, return 0. Note that a flip swaps the front and back numbers, so the value on the front is now on the back and vice versa.

How to obtain maximum points from cards?

Maximum Points You Can Obtain from Cards - LeetCode There are several cards arranged in a row, and each card has an associated number of points. The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row.

What is the input and output value of cardpoints?

Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards.


1 Answers

The only thing you need to find is the minimum sum that you can form with consecutive numbers, and then flip those. In your example, the last three numbers add up to -7, and there is no other set of consecutive number which have a lower sum, so flipping them does the trick. If the minimum sum is non negative, then you don't need to flip them.

Now, what I described above is a well known algorithm, and it is called Kadane's algorithm, which can be solve in O(n), notice that the Wikipedia link shows how to do it for the maximum, but you can easily modify it to find the minimum.

like image 118
alejopelaez Avatar answered Sep 20 '22 12:09

alejopelaez