zoukankan      html  css  js  c++  java
  • 1664. Ways to Make a Fair Array

    package LeetCode_1664
    
    /**
     * 1664. Ways to Make a Fair Array
     * https://leetcode.com/problems/ways-to-make-a-fair-array/
     * You are given an integer array nums. You can choose exactly one index (0-indexed) and remove the element.
     * Notice that the index of the elements may change after the removal.
    For example, if nums = [6,1,7,4,1]:
    Choosing to remove index 1 results in nums = [6,7,4,1].
    Choosing to remove index 2 results in nums = [6,1,4,1].
    Choosing to remove index 4 results in nums = [6,1,7,4].
    An array is fair if the sum of the odd-indexed values equals the sum of the even-indexed values.
    Return the number of indices that you could choose such that after the removal, nums is fair.
    
    Example 1:
    Input: nums = [2,1,6,4]
    Output: 1
    Explanation:
    Remove index 0: [1,6,4] -> Even sum: 1 + 4 = 5. Odd sum: 6. Not fair.
    Remove index 1: [2,6,4] -> Even sum: 2 + 4 = 6. Odd sum: 6. Fair.
    Remove index 2: [2,1,4] -> Even sum: 2 + 4 = 6. Odd sum: 1. Not fair.
    Remove index 3: [2,1,6] -> Even sum: 2 + 6 = 8. Odd sum: 1. Not fair.
    There is 1 index that you can remove to make nums fair.
    
    Example 2:
    Input: nums = [1,1,1]
    Output: 3
    Explanation: You can remove any index and the remaining array is fair.
    
    Example 3:
    Input: nums = [1,2,3]
    Output: 0
    Explanation: You cannot make a fair array after removing any index.
    
    Constraints:
    1. 1 <= nums.length <= 105
    2. 1 <= nums[i] <= 104
     * */
    class Solution {
        /*
        * solution: increasing and decreasing even, odd; then compare two side if equals,
        * Time:O(n), Space:O(1)
        * */
        fun waysToMakeFair(nums: IntArray): Int {
            var even = 0
            var odd = 0
            for (i in nums.indices) {
                if (i % 2 == 0) {
                    even += nums[i]
                } else {
                    odd += nums[i]
                }
            }
            var even2 = 0
            var odd2 = 0
            var result = 0
            for (i in nums.indices) {
                //decreasing
                if (i % 2 == 0) {
                    even -= nums[i]
                } else {
                    odd -= nums[i]
                }
                //compare two side
                if (even + odd2 == odd + even2) {
                    result++
                }
                //increasing
                if (i % 2 == 0) {
                    even2 += nums[i]
                } else {
                    odd2 += nums[i]
                }
            }
            return result
        }
    }
  • 相关阅读:
    在Arcscene绘制管线三维横断面(AE绘制三维点阵文字)
    MapControl控件AddLayer出现错误-引发类型为“System.Windows.Forms.AxHost+InvalidActiveXStateException”的异常
    c# 集合
    springmvc:第一个springmvc程序
    springmvc:简介
    VocabularyAccumulation
    Spring:整合Mybatis
    Spring:动态代理及Aop
    Spring:自动装配及注解
    Spring:Ioc(依赖注入)
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/14052854.html
Copyright © 2011-2022 走看看