[LeetCode] 2256. Minimum Average Difference

김민우·2022년 12월 4일
0

알고리즘

목록 보기
78/189

- Problem

2256. Minimum Average Difference
You are given a 0-indexed integer array nums of length n.

The average difference of the index i is the absolute difference between the average of the first i + 1 elements of nums and the average of the last n - i - 1 elements. Both averages should be rounded down to the nearest integer.

Return the index with the minimum average difference. If there are multiple such indices, return the smallest one.

Note:

  • The absolute difference of two numbers is the absolute value of their difference.
  • The average of n elements is the sum of the n elements divided (integer division) by n.
  • The average of 0 elements is considered to be 0.

Example 1:

Input: nums = [2,5,3,9,5,3]
Output: 3
Explanation:
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.

Example 2:

Input: nums = [0]
Output: 0
Explanation:
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.

Constraints:

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^5

- 내 풀이

class Solution:
    def minimumAverageDifference(self, nums: List[int]) -> int:
        N = len(nums)
        pre, post = [], [] 
        pre_tmp, post_tmp = 0, sum(nums)
        result, min_value = 0, float('inf')
        
        for i in range(N-1):
            pre_tmp += nums[i]
            post_tmp -= nums[i]
            pre.append(pre_tmp // (i+1))
            post.append(post_tmp // (N-i-1))
        
        pre.append((sum(nums) // N))
        post.append(0)
        
        for i in range(N):
            if abs(pre[i] - post[i]) < min_value:
                min_value = abs(pre[i]-post[i])
                result = i
        
        return result

왜 이렇게 풀었을까?

- 내 풀이 2

class Solution:
    def minimumAverageDifference(self, nums: List[int]) -> int:
        N = len(nums)
        pre_sum, post_sum = 0, sum(nums)
        result, min_ave = 0, float('inf')
        
        for i in range(N):
            pre_sum += nums[i]
            post_sum -= nums[i]

            pre_len = i + 1
            post_len = N-i-1 if i + 1 != N else 1
            
            abs_diff = abs((pre_sum // pre_len) - (post_sum // post_len))
            
            if abs_diff < min_ave:
                min_ave = abs_diff
                result = i

        return result

시간 복잡도 O(N), 공간 복잡도 O(1)

- 결과

profile
Pay it forward.

0개의 댓글