[Likou Brush Questions | Day 18]

Table of contents

Foreword:

1005. Array Sum Maximized After K Inversions - LeetCode

 134. Gas Station - LeetCode

 Summarize:


Foreword:

                Random questions today, no specific requirements for question types

1005. Array Sum Maximized After K Inversions - LeetCode

Given an array of integers nums and an integer k, modify the array as follows:

Select an index i and replace nums[i] with -nums[i].
Repeat this process exactly k times. The same subscript i can be selected multiple times.

After modifying an array in this way, return the largest sum possible for the array.

Problem-solving ideas: If we want to get the final array and the largest, we don't think too complicated, many people will think:

  • 1. The value of k is enough for all negative numbers to be positive , and there is a surplus. At this time, all the numbers are positive. If we want to get the maximum value, we need to continuously invert the minimum value until the K value is exhausted. This also yields all negative sums.
  • 2. The K value is not enough to invert all the negative numbers , so we only need to invert all the smaller negative numbers (the result will be bigger after the inversion of the smaller negative numbers), and this will also get the largest sum of array elements.
  • 3. If there are no negative numbers in the array at all , then it belongs to the sub-case of our case 1. To invert the set of all positive numbers to find the maximum value, you only need to always use the minimum value to invert it.

Many people think about this and stop thinking about it, resulting in the written code being too complicated and judging the situation complicated, but we can extract these three situations, in fact, it is constantly negating the smallest value in the entire array

When there are negative numbers, inverting the smallest value (negative number) is actually to get a larger positive number. We sort after each inversion to ensure that the first digit is always the smallest number.

When there are no negative numbers, we always reverse the first digit, and this process always operates on the smallest number.

In other words, the whole problem is a greedy solution: if you want to get the maximum sum of array elements, you must always invert the minimum value of the array. When the minimum value is negative, we negate the minimum value, which is equivalent to obtaining the largest negative value as much as possible.

Solution 1:

class Solution {
public:
    int largestSumAfterKNegations(vector<int>& nums, int k) {
          for(int i=0;i<k;i++)
          {
              sort(nums.begin(),nums.end());
              nums[0]=-nums[0];
          }
          int sum=0;
          for(int a :nums)
          {
              sum=sum+a;
          }
            return sum;
    }
};

 134. Gas Station - LeetCode

There are n gas stations on a loop, and the i-th gas station has gas[i] liters of gasoline.

You have a car with unlimited fuel tank capacity, and it takes cost[i] liters of gasoline to drive from the i-th gas station to the i+1-th gas station. You start off from one of the gas stations, starting with an empty tank.

Given two integer arrays gas and cost, return the number of the gas station at the time of departure if you can drive around the circle in order, otherwise return -1. If a solution exists, it is guaranteed to be unique.

In fact, we can also use the idea of ​​​​greedy algorithm to solve this question, that is to say: each station has its own fuel increase and fuel consumption. If the sum of the difference between the fuel consumption and fuel increase of each station is less than 0, then it is definitely impossible to complete the entire ring .

Solutions have to: 

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
         int n = gas.size();
         int start = 0;
         int total = 0;//起点开始到当前加油站的总汽油量减去总消耗量。
         int tank = 0;//剩余油量

    for (int i = 0; i < n; i++) {
        tank += gas[i] - cost[i];
        total += gas[i] - cost[i];

        if (tank < 0) {
            start = i + 1;
            tank = 0;
        }
    }

    return total >= 0 ? start : -1;
    }
};

 Summarize:

        I brushed up two greedy algorithm questions today, and I gained a lot.

If my content is helpful to you, please like, comment and bookmark . Creation is not easy, everyone's support is my motivation to persevere!

 

Guess you like

Origin blog.csdn.net/fckbb/article/details/131713456