Leetcode-5147 Decrease Elements To Make Array Zigzag(递减元素使数组呈锯齿状)

时间:2022-05-15 23:44:10
 1 #define _for(i,a,b) for(int i = (a);i < b;i ++)
 2 typedef long long ll;
 3 using namespace std;
 4 
 5 class Solution
 6 {
 7     public:
 8         int movesToMakeZigzag(vector<int>& nums)
 9         {
10             int rnt1 = 0;
11             int rnt2 = 0;
12             
13             _for(i,1,nums.size())
14             {
15                 if(i&0x1)
16                 {
17                     if(i==nums.size()-1)
18                     {
19                         if(nums[i]>=nums[i-1])
20                         {
21                             rnt1 += nums[i]-(nums[i-1]-1);
22                         }
23                     }
24                     else
25                     {
26                         if(nums[i]>=nums[i-1] || nums[i]>=nums[i+1])
27                         {
28                             rnt1 += nums[i]-(min(nums[i-1],nums[i+1])-1);
29                         }
30                     }
31                 }
32                 
33             }
34             _for(i,0,nums.size())
35             {
36                 
37                 if((i&0x1)==0)
38                 {
39                     if(i==0)
40                     {
41                         if(nums[i]>=nums[i+1])
42                         {
43                             rnt2 += nums[i]-(nums[i+1]-1);
44                         }
45                     }
46                     else if(i==nums.size()-1)
47                     {
48                         if(nums[i]>=nums[i-1])
49                         {
50                             rnt2 += nums[i]-(nums[i-1]-1);
51                         }
52                     }
53                     else
54                     {
55                         if(nums[i]>=nums[i-1] || nums[i]>=nums[i+1])
56                         {
57                             rnt2 += nums[i]-(min(nums[i-1],nums[i+1])-1);
58                         }
59                     }
60                 }
61             //    cout << rnt2 << endl;
62             }
63          //   cout << rnt1 << " " << rnt2 << endl;
64             return min(rnt1,rnt2);
65         }
66 };