zoukankan      html  css  js  c++  java
  • [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 }
  • 相关阅读:
    【SpringFramework】Spring 事务控制
    Mini 学生管理器
    方法的重写override,重载overload。
    方法封装,属性调用以及设置。
    判断hdfs文件是否存在
    模拟(删除/远程拷贝)当前一周的日志文件
    2.上传hdfs系统:将logs目录下的日志文件每隔十分钟上传一次 要求:上传后的文件名修为:2017111513xx.log_copy
    使用定时器:在logs目录,每两分钟产生一个文件
    五个节点的hadoop集群--主要配置文件
    hadoop集群配置文件与功能对应解析
  • 原文地址:https://www.cnblogs.com/lz87/p/9919790.html
Copyright © 2011-2022 走看看