<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Array Sum up increment. 1526, 3229

      1526. Minimum Number of Increments on Subarrays to Form a Target Array

      You are given an integer array target. You have an integer array initial of the same size as target with all elements initially zeros.

      In one operation you can choose any subarray from initial and increment each value by one.

      Return the minimum number of operations to form a target array from initial.

      The test cases are generated so that the answer fits in a 32-bit integer.

       Example 1:

      Input: target = [1,2,3,2,1]
      Output: 3
      Explanation: We need at least 3 operations to form the target array from the initial array.
      [0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
      [1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
      [1,2,2,2,1] increment 1 at index 2.
      [1,2,3,2,1] target array is formed.
      

      Example 2:

      Input: target = [3,1,1,2]
      Output: 4
      Explanation: [0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2]
      

      Example 3:

      Input: target = [3,1,5,4,2]
      Output: 7
      Explanation: [0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1] -> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2]. 

      Constraints:

      • 1 <= target.length <= 105
      • 1 <= target[i] <= 105
      /**
      1,2,3,2,1
      ^
      result = 1
      
      1,2,3,2,1
        ^
      2比1需要額外的1個cost, 因此:
      result = 1 + 1
      
      1,2,3,2,1
          ^
      3比2需要額外的1個cost,因此:
      result = 1 + 1 + 1
      
      1,2,3,2,1
            ^
      2比3小,因此3的cost已經足以cover2,因此
      result = 1 + 1 + 1 + 0
      
      1,2,3,2,1
              ^
      1比2小,因此2的cost已經足以cover 1,因此
      result = 1 + 1 + 1 + 0 + 0
      
      答案: 3
      
       */
      
      class Solution {
          public int minNumberOperations(int[] target) {
              int result = target[0];
              for(int i = 1; i < target.length; i++) {
                  if(target[i] > target[i - 1]) result += target[i] - target[i - 1];
              }
              return result;
          }
      }

       

      You are given two positive integer arrays nums and target, of the same length. In a single operation, you can select any subarray
      of nums and increment or decrement each element within that subarray by 1.

      Return the minimum number of operations required to make nums equal to the array target.

      Example 1:

      Input: nums = [3,5,1,2], target = [4,6,2,4]

      Output: 2

      Explanation:

      We will perform the following operations to make nums equal to target:
      - Increment nums[0..3] by 1, nums = [4,6,2,3].
      - Increment nums[3..3] by 1, nums = [4,6,2,4].

      Example 2:

      Input: nums = [1,3,2], target = [2,1,4]

      Output: 5

      Explanation:

      We will perform the following operations to make nums equal to target:
      - Increment nums[0..0] by 1, nums = [2,3,2].
      - Decrement nums[1..1] by 1, nums = [2,2,2].
      - Decrement nums[1..1] by 1, nums = [2,1,2].
      - Increment nums[2..2] by 1, nums = [2,1,3].
      - Increment nums[2..2] by 1, nums = [2,1,4].

       Constraints:

      • 1 <= nums.length == target.length <= 105
      • 1 <= nums[i], target[i] <= 108
      class Solution {
          /**
          思路:
              1. 計算兩個數組差值,最終的答案就是將差值數組全部變成0的過程
              2. 將差值序列操作后變為全0,我們可以將正負數分開處理
                  如果x>y, 那么x在y cost基礎上,需要x-y的額外cost才能變成0, 
                  如果x<y, 那么x不需要額外cost
           */
          public long minimumOperations(int[] nums, int[] target) {
              // 計算差值
              int[] diff = new int[nums.length];
              for(int i = 0; i < nums.length; i++) {
                  diff[i] = target[i] - nums[i];
              }
       
              // 
              long result = Math.abs(diff[0]);
              for(int i = 1; i < diff.length; i++) {
                  // 如果都為正
                  if(diff[i - 1] >= 0 &&  diff[i] >= 0) {
                      if(diff[i - 1] < diff[i]) {
                          result += diff[i] - diff[i - 1];
                      }
                  }
                  // 如果都為負
                  else if(diff[i - 1] <= 0 &&  diff[i] <= 0) {
                      if(diff[i - 1] > diff[i]) {
                          result += diff[i - 1] - diff[i];
                      }
                  }
                  // 如果方向切換,證明從負->正,或者從正->負, 將當前起始差值加進去
                  else {
                      result += Math.abs(diff[i]);
                  }
              }
              return result;
          }
      }

       

      posted @ 2024-07-22 09:24  xiaoyongyong  閱讀(26)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 26uuu另类亚洲欧美日本| 国产av仑乱内谢| 欧美亚洲另类制服卡通动漫| 亚洲精品一区二区制服| 欧美成本人视频免费播放| 女人下边被添全过视频的网址| 92国产精品午夜福利免费| 激情亚洲内射一区二区三区| 日本一本正道综合久久dvd| 国产AV影片麻豆精品传媒| 少妇人妻精品无码专区视频| 粗壮挺进邻居人妻无码| 亚洲色欲久久久久综合网| 亚洲中文字幕av不卡无码| av小次郎网站| 国产漂亮白嫩美女在线观看| 免费人成视频网站在线观看18| 国产三级精品三级在线看| 久久高清超碰AV热热久久| 精品视频福利| 国产精品久久国产精麻豆| 国产精品一久久香蕉产线看 | 成人免费AA片在线观看| 国精品无码一区二区三区在线| 欧美性猛交xxxx免费看| 国产精品一区二区不卡视频| 欧美色欧美亚洲高清在线视频| 人人爽人人爽人人片av东京热 | 亚洲国家av一区二区| 99精品国产成人一区二区| 在线观看热码亚洲av每日更新| 国产高清精品在线一区二区| 国产精品免费AⅤ片在线观看| 99久热在线精品视频| 国产av一区二区午夜福利| 最新国产精品拍自在线观看| 平利县| 亚洲中文字幕一区二区| 麻豆成人精品国产免费| 国产女高清在线看免费观看| 美女人妻激情乱人伦|