1005. K times after taking an array of anti-maximizing and

1005. K times after taking an array of anti-maximizing and

 

description:

Given an array of integers A, we can modify the array by the following method: We selected an index i and A [i] is replaced -A [i], and this process is repeated a total of K times. (We can choose the same index i more than once.)

After modifying the array in this manner, and returns an array of the possible maximum.

Example 1:

Input: A = [4,2,3], K = 1
Output: 5
Explanation: select the index (1,), then A becomes [4, 2,3].
Example 2:

Input: A = [3, -1,0,2] , K = 3
Output: 6
Explanation: select the index (1, 2, 2), then A becomes [3,1,0,2].
Example 3:

Input: A = [2, -3, -1,5, -4], K = 2
Output: 13
Explanation: select the index (1, 4), and A to [2,3, -1,5,4 ].
 

prompt:

1 <= A.length <= 10000
1 <= K <= 10000
-100 <= A[i] <= 100

1 Solution 1: 96 MS     8.6 MB
 2  class Solution {
 . 3  public :
 . 4      int largestSumAfterKNegations (Vector < int > & A, int K) {
 . 5          / * ideas:
 6              array of values which may be positive, negative, k times negated ,
 7              to sort a large array of small, the front negative, positive, after,
 . 8              1: If the array inside the negative
 9              1.1 negative number> = K, it can be inverted negative k times, final summation
 10              1.2 If a negative number <k, then the number m of all the negative Negated, time remaining km,
 11          1.3 again, ordering, because the original negative becomes positive, and may be smaller, such as -10, -1, 4,5,
 12                 -1 becomes 1, then the minimum, so reorder
 13                 then operates the first non-negative, since the same index i can be selected multiple times.
14              1.4 may not be sorted, the definition of a variable representing the current minimum value of
 15              if (km)% 2 == 0; then a first positive non-negative, then summed
 16              if (km)% 2 == 1 then The first non-negative number is negative, then summed
 . 17              2: If a positive whole number, ranking, only the first non-negative number k of times, and then summed
 18 is          * /   
. 19          Sort (a.begin (), a .end ());
 20 is          int CNT = 0 ;
 21 is          
22 is          for ( int I = 0 ; I <a.size (); I ++ ) {
 23 is              IF (CNT == K) BREAK ;
 24              IF (A [I] <0&&cnt<K){
25                 A[i]=-A[i];
26                 cnt++;
27             }else{
28                 sort(A.begin(),A.end());
29                 if((K-cnt)%2!=0){//A[0]最小
30                     A[0]=-A[0];
31                    
32                 }
33          break;
34             }
35         }
36         int sum=0;
37         for(int i=0;i<A.size();i++){
38             sum+=A[i];
39         }
40         return sum;
41     }
42 };
1 Solution 2:     . 4 MS     8.7 MB
 2  class Solution {
 . 3  public :
 . 4      int largestSumAfterKNegations (Vector < int > & A, int K) {
 . 5          / * ideas:
 6              array of values which may be positive, negative, k times negated ,
 7              to sort a large array of small, the front negative, positive, after,
 . 8              1: If the array inside the negative
 9              1.1 negative number> = K, it can be inverted negative k times, final summation
 10              1.2 If a negative number <k, then the number m of all the negative Negated, time remaining km,
 11          1.3 again, ordering, because the original negative becomes positive, and may be smaller, such as -10, -1, 4,5,
 12                 -1 becomes 1, then the minimum, so reorder
 13                 then operates the first non-negative, since the same index i can be selected multiple times.
14              1.4 may not be sorted, the definition of a variable representing the current minimum value of
 15              if (km)% 2 == 0; then a first positive non-negative, then summed
 16              if (km)% 2 == 1 then The first non-negative number is negative, then summed
 . 17              2: If a positive whole number, ranking, only the first non-negative number k of times, and then summed
 18 is          * /   
. 19          Sort (a.begin (), a .end ());
 20 is          int CNT = 0 ;
 21 is          int Min = INT_MAX, ID = 0 ; // record a minimum, to be modified, and the subscript 
22 is          for ( int I = 0 ; I <a.size ( ); I ++ ) {
 23 is             IF (CNT == K) BREAK ;
 24              IF (A [I] < 0 && CNT < K) {
 25                  A [I] = - A [I];
 26 is                  CNT ++ ;
 27                  IF (Min> A [I]) { / / update the minimum and the subscript 
28                      Min = A [I];
 29                      ID = I;
 30                  }
 31 is              } the else {    // Sort (a.begin (), a.end ()); 
32                  IF ((K-CNT )% 2 ! = 0 ) { //Becomes negative and positive minimum value originally a first non-negative minimum value comparison 
33 is                      IF (Min <A [I]) A [ID] = - A [ID]; // to see who the minimum 
34 is                      the else A [I] = - A [I];
 35                  }
 36                  BREAK ;
 37 [              }
 38 is          }
 39          int SUM = 0 ;
 40          for ( int I = 0 ; I <a.size (); I ++ ) {
 41 is              SUM + = A [I ];
 42 is          }
 43 is          return SUM;
 44 is      }
 45 };

 

 

 

 

Guess you like

Origin www.cnblogs.com/NirobertEinteson/p/11992640.html