• [LeetCode] Find Pivot Index


    Given an array of integers nums, write a method that returns the "pivot" index of this array.

    We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.

    If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.

    Example 1:

    Input: 
    nums = [1, 7, 3, 6, 5, 6]
    Output: 3
    Explanation: 
    The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
    Also, 3 is the first index where this occurs.
    

    Example 2:

    Input: 
    nums = [1, 2, 3]
    Output: -1
    Explanation: 
    There is no index that satisfies the conditions in the problem statement.
    

    Note:

    • The length of nums will be in the range [0, 10000].
    • Each element nums[i] will be an integer in the range [-1000, 1000].

    A brute force solution is for each index, calculate the sum on its left and right side. This gives a O(n^2) runtime.

    A better solution is to use prefix sum to avoid repeatedly computing the sum of the same subarray. 

    Solution 1. O(n) runtime, O(n) space 

     1 class Solution {
     2     public int pivotIndex(int[] nums) {
     3         if(nums == null || nums.length == 0) {
     4             return -1;
     5         }
     6         int n = nums.length;
     7         int[] prefixSum = new int[n];
     8         prefixSum[0] = nums[0];
     9         for(int i = 1; i < n; i++) {
    10             prefixSum[i] = prefixSum[i - 1] + nums[i];
    11         }
    12         int leftSum, rightSum, pivotIndex;
    13         for(pivotIndex = 0; pivotIndex < n; pivotIndex++) {
    14             if(pivotIndex == 0) {
    15                 leftSum = 0;
    16             }
    17             else {
    18                 leftSum = prefixSum[pivotIndex - 1];
    19             }
    20             rightSum = prefixSum[n - 1] - prefixSum[pivotIndex];
    21             if(leftSum == rightSum) {
    22                 return pivotIndex;
    23             }
    24         }
    25         return -1;
    26     }
    27 }

    Even better, we can solve this problem using only O(1) space without actually storing the entire prefix result. A total sum of the entire input array is all we need.

     1 class Solution {
     2     public int pivotIndex(int[] nums) {
     3         int sum = 0, left = 0;
     4         for (int i = 0; i < nums.length; i++) sum += nums[i];
     5         
     6         for (int i = 0; i < nums.length; i++) {
     7             if (i != 0) left += nums[i - 1];
     8             if (sum - left - nums[i] == left) return i;
     9         }
    10         
    11         return -1;
    12     }
    13 }
  • 相关阅读:
    TopCoder SRM502 Div1 500 贪心 01背包
    TopCoder SRM502 Div1 1000 动态规划
    LOJ#6433. 「PKUSC2018」最大前缀和 状压dp
    Codeforces 830D Singer House 动态规划
    Codeforces 830C Bamboo Partition 其他
    UOJ#275. 【清华集训2016】组合数问题 数位dp
    Codeforces 806 D. Perishable Roads Dijkstra
    UOJ#53. 【UR #4】追击圣诞老人 树链剖分 k短路
    Java第二天——标识符命名规则、Java的知识、快捷键的使用、Scanner获取值的常用方法
    Scanner的例子
  • 原文地址:https://www.cnblogs.com/lz87/p/9919790.html
Copyright © 2020-2023  润新知