소프트웨어에 대한 모든 것

462. Minimum Moves to Equal Array Elements II 본문

알고리즘/LeetCode

462. Minimum Moves to Equal Array Elements II

앤테바 2022. 7. 1. 06:21
반응형

https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/

 

Minimum Moves to Equal Array Elements II - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

462. Minimum Moves to Equal Array Elements II

 

문제)

솔루션1)

1) 오름차순 정렬

2) 중앙값 검색

3) 중앙값을 기준으로 각 elements에 대해서 move 계산

class Solution:
    def minMoves2(self, nums: List[int]) -> int:
        nums.sort()
        median_num = nums[len(nums)//2]
        return sum(abs(median_num - n) for n in nums)

 

반응형

'알고리즘 > LeetCode' 카테고리의 다른 글

1026. Maximum Difference Between Node and Ancestor  (0) 2022.12.09
6. Zigzag Conversion  (0) 2022.12.07
5. Longest Palindromic Substring  (0) 2022.05.24
LeetCode 풀기 - 79. Word Search  (0) 2022.03.30
LeetCode 풀기 - 101. Symmetric Tree  (0) 2022.03.26
Comments