zoukankan      html  css  js  c++  java
  • 1147. 递减元素使数组呈锯齿状

    class Solution {
    	public int movesToMakeZigzag(int[] nums) {
    	int temp1[] = new int[nums.length];
    			int temp2[] = new int[nums.length];
    			for (int i = 0; i < nums.length; i++) {
    				temp1[i] = nums[i];
    				temp2[i] = nums[i];
    			}
    			int ans1 = 0;
    			int ans2 = 0;
    			for (int i = 1; i < temp1.length; i+=2) { //两边小 中间大  
    				if (temp1[i - 1] >= temp1[i]) {
    					ans1 += temp1[i - 1] - temp1[i] + 1; 
    					temp1[i - 1] = temp1[i] - 1;
    				}
    
    				if (i + 1 < temp1.length && temp1[i + 1] >= temp1[i]) {
    					ans1 += temp1[i + 1] - temp1[i] + 1;
    					temp1[i + 1] = temp1[i] - 1;
    				}
    			}
    
    			for (int i = 1; i < temp2.length; i+=2) { //两边小,中间大
    				if (temp2[i - 1] <= temp2[i]) {
    					ans2 += temp2[i] - temp2[i - 1] + 1;
    					temp2[i] = temp2[i - 1] - 1;
    				}
    				if (i + 1 < temp2.length && temp2[i + 1] <= temp2[i]) {
    					ans2 += temp2[i] - temp2[i + 1] + 1;
    					temp2[i] = temp2[i + 1] - 1;
    				}
    			}
    			return ans1 <= ans2 ? ans1 : ans2;
    		}
    }
    
  • 相关阅读:
    关于位运算(转)
    计蒜客第三场
    数组与指针
    计蒜客第二场
    指针概念
    爬楼梯(动态规划)
    线性表基本操作的实现(合并)
    4123=喵帕斯之天才少女
    3889=神奇的函数
    1586=计算组合数
  • 原文地址:https://www.cnblogs.com/cznczai/p/11298277.html
Copyright © 2011-2022 走看看