{"id":10289,"date":"2025-04-06T14:02:47","date_gmt":"2025-04-06T21:02:47","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10289"},"modified":"2025-04-06T14:04:13","modified_gmt":"2025-04-06T21:04:13","slug":"leetcode-462-minimum-moves-to-equal-array-elements-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-462-minimum-moves-to-equal-array-elements-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 462. Minimum Moves to Equal Array Elements II"},"content":{"rendered":"\n<p>\u4e0d\u592a\u559c\u6b22\u8fd9\u6837\u7684\u9898\u76ee\uff0c\u4f60\u9700\u8981\u8bc1\u660e\/\u731c\u6d4b\u6700\u4f18\u89e3\u662f\u5c06\u6240\u6709\u6570\u5b57\u8f6c\u6362\u6210median\/\u4e2d\u4f4d\u6570\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528nth_element\u627e\u4e2d\u4f4d\u6570\uff0c\u7136\u540e\u518d\u5faa\u73af\u4e00\u904d\u6c42\u5408\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(n)<br>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(1)<\/p>\n\n\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int minMoves2(vector<int>& nums) {\n    const int n = nums.size();\n    nth_element(begin(nums), begin(nums) + n \/ 2, end(nums));\n    const int median = nums[n \/ 2];\n    return accumulate(begin(nums), end(nums), 0, [median](int s, int x) {\n      return s + abs(x - median);\n    });\n  }\n};\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0d\u592a\u559c\u6b22\u8fd9\u6837\u7684\u9898\u76ee\uff0c\u4f60\u9700\u8981\u8bc1\u660e\/\u731c\u6d4b\u6700\u4f18\u89e3\u662f\u5c06\u6240\u6709\u6570\u5b57\u8f6c\u6362\u6210median\/\u4e2d\u4f4d\u6570\u3002 \u4f7f\u7528nth_element\u627e\u4e2d\u4f4d\u6570\uff0c\u7136\u540e\u518d\u5faa\u73af\u4e00\u904d\u6c42\u5408\u5373\u53ef\u3002 \u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(n)\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(1) class Solution { public: int minMoves2(vector&#038; nums) { const int n = nums.size(); nth_element(begin(nums), begin(nums) + n \/ 2, end(nums)); const int&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,31,11,759],"class_list":["post-10289","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-math","tag-median","tag-nth_element","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10289"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10289\/revisions"}],"predecessor-version":[{"id":10291,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10289\/revisions\/10291"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}