Given an array of integers nums, calculate the pivot index of this array.
The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right.
If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array.
Return the leftmost pivot index. If no such index exists, return -1.
Example 1:
Input: nums = [1,7,3,6,5,6]
Output: 3
Explanation:
The pivot index is 3.
Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11
Right sum = nums[4] + nums[5] = 5 + 6 = 11
Example 2:
Input: nums = [1,2,3]
Output: -1
Explanation:
There is no index that satisfies the conditions in the problem statement.
Example 3:
Input: nums = [2,1,-1]
Output: 0
Explanation:
The pivot index is 0.
Left sum = 0 (no elements to the left of index 0)
Right sum = nums[1] + nums[2] = 1 + -1 = 0
Constraints:
Note: This question is the same as 1991: https://leetcode.com/problems/find-the-middle-index-in-array/
func pivotIndex(nums []int) int {
length := len(nums)
right := make([]int, length+1)
sum := 0
for i := length - 1; i >= 1; i-- {
sum += nums[i]
right[i] = sum
}
sum = 0
for j := 0; j < length; j++ {
if sum == right[j+1] {
return j
}
sum += nums[j]
}
return -1
}
https://go.dev/play/p/wQz4MBAEGt5
func pivotIndex(nums []int) int {
if len(nums) < 2 {
return -1
}
left := 0
right := 0
for i := 1; i < len(nums); i++ {
right += nums[i]
}
var pivot int
for i := 0; i < len(nums); i++ {
if left == right {
return pivot
}
pivot = i + 1
left += nums[i]
if pivot < len(nums) {
right -= nums[pivot]
}
}
return -1
}
func pivotIndex(nums []int) int {
left := 0
right := 0
for i := 1; i < len(nums); i++ {
right += nums[i]
}
var pivot int
for i := 0; i < len(nums); i++ {
if left == right {
return pivot
}
pivot = i + 1
left += nums[i]
if pivot < len(nums) {
right -= nums[pivot]
}
}
return -1
}
func pivotIndex(nums []int) int {
sum, left := 0, 0
for i := range nums {
sum += nums[i]
}
for i := range nums {
if left == sum - left - nums[i] {
return i
}
left += nums[i]
}
return -1
}
func pivotIndex(nums []int) int {
var rightSum int
for _, v := range nums {
rightSum += v
}
var leftSum int
for i, v := range nums {
rightSum -= v
if leftSum == rightSum {
return i
}
leftSum += v
}
return -1
}
func pivotIndex(nums []int) int {
sum := make([]int, len(nums) + 1)
sum[0] = 0
for i := 1; i <= len(nums); i++ {
sum[i] = sum[i - 1] + nums[i - 1]
}
for j := 0; j < len(nums); j++ {
if sum[j] == sum[len(sum) - 1] - sum[j] - nums[j] {
return j
}
}
return -1
}
func pivotIndex(nums []int) int {
n := len(nums)
for index, _ := range nums {
if sum(nums[0:index]) == sum(nums[index+1:n]) {
return index
}
}
return -1
}
func sum(nums []int) int {
sum := 0
for _, eachNum := range nums {
sum = sum + eachNum
}
return sum
}
func pivotIndex(nums []int) int {
// naive solution, for each index, add all elements to left and right of it and see if equal: if so, return that index
for i, _ := range nums {
suml, sumr := getSums(nums, i)
fmt.Println(i, suml, sumr)
if suml == sumr {
return i
}
}
if sum(nums[1:]) == 0 {
return 0
} else if sum(nums[:len(nums)-1]) == 0 {
return len(nums)-1
}
return -1
}
func getSums(nums[]int, i int) (int, int) {
suml := 0
sumr := 0
for k := 0; k < i; k++ {
suml += nums[k]
}
for j := i+1; j < len(nums); j++ {
sumr += nums[j]
}
return suml, sumr
}
func sum(nums []int) int {
res := 0
for _, v := range nums {
res += v
}
return res
}
func pivotIndex(nums []int) int {
sum := 0
for _, v := range nums {
sum += v
}
left := 0
for i := 0; i < len(nums); i++ {
if left == sum-nums[i] {
return i
}
left += nums[i]
sum = sum - nums[i]
}
return -1
}
Array and String - Plus One in Go (0) | 2022.03.26 |
---|---|
Array and String - Largest Number At Least Twice of Others in Go (0) | 2022.03.24 |
Array and string - Introduction to Dynamic Array from Java to Go (0) | 2022.03.23 |
Array and String - Introduction to Array from java to Go (0) | 2022.03.22 |
Array101 Squares of a Sorted Array in Go (0) | 2022.03.21 |
댓글 영역