current position:Home>leetcode:462. Minimum number of moves to make array elements equal II [sort + find the middle]
leetcode:462. Minimum number of moves to make array elements equal II [sort + find the middle]
2022-04-29 20:25:13【Review of the white speed Dragon King】
analysis
After sorting, find the middle as target, Ask for all target The sum of the distances
Be careful , If it's even , Two target Calculate it all, find a minimum value and seek stability
ac code
class Solution:
def minMoves2(self, nums: List[int]) -> int:
# Lean in the middle
n = len(nums)
nums.sort()
# Special judgement
if n == 1:
return 0
if n % 2 == 1:
target = nums[n // 2]
ans = 0
for i in range(n):
ans += abs(nums[i] - target)
return ans
else:
target1, target2 = nums[n // 2 - 1], nums[n // 2]
ans1, ans2 = 0, 0
for i in range(n):
ans1 += abs(nums[i] - target1)
ans2 += abs(nums[i] - target2)
return min(ans1, ans2)
summary
Simple sorting and finding rules
copyright notice
author[Review of the white speed Dragon King],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291605388969.html
The sidebar is recommended
- Who is bunk, the new hero of endless dungeon? Introduction to new hero bunk skills
- Front end entry first bullet | development tools and configuration
- Nice HTML login page
- Vue error prone summary
- Summary of material resources website (icon | wallpaper | Adobe plug-in | jQuery plug-in | badge | online conversion | color splicing | continuous update)
- Step by step, configure nodesjs environment with hbuilder for Vue JS development
- Vue sets the global websocket and accepts message notification processing on the corresponding page
- Solve the problem that vuex data is reset during page refresh
- Java data structures and algorithms lesson 2 - generics
- Leetcode-82 - delete duplicate Element II in the sorting linked list
guess what you like
[front end] quickly create HTML
Vue router routing navigation
Vue router redirection and alias
Vue router named routes, (nested) named views
Dynamic components & slots & custom instructions for getting started with Vue Foundation
JavaScript algorithm -- heap sorting
K8s deployment-43-take you to learn ingress nginx (Part 2)
Detailed breakdown of actual combat of CMS background system project (VII)
Explore the store | new architecture blessing, performance and safety. This style is super personality_ Integra_ Honda_ platform
Sports kit blessing / replacement of four cylinder engine Chevrolet chuangku RS live shooting analysis_ Aspect_ Display screen_ Complete vehicle
Random recommended
- Talking about nodejs server
- Node. js< I & gt—— Encounter node and repl usage
- Vue basic API: calculation attribute + filter + listener
- 1-stm32 + mn316 (nb-iot) remote upgrade OTA (self built Internet of things platform) - STM32 uses HTTP to download program files and upgrade programs through mn316 (MCU program rotation check and update)
- Vue Axios response interception
- vue3 ref
- How does Vue transfer the data from the parent component to the child component intact?
- The back-end interface developed by springboot in idea and the Vue front-end developed by vscode. How to integrate Vue code into springboot?
- Fried cold rice series 4: scope and closure in JavaScript
- Typescript type compatibility learning
- Summary of bugs encountered in front-end development
- Chrome developer tool: performance analysis using web panel
- Collation of common semantic elements and global attributes in HTML
- Life cycle in Vue
- 5.1 fear of traffic jam? With a budget of less than 100000, these cars with adaptive cruise make it easy for you to travel
- Docker compose deploy nginx configure SSL
- The content of element type “mapper“ must match “(cache-ref|cache|resultMap*|parameterMap*|sql*|inse
- -CSS-
- Vue uses two-way binding to implement the user registration page
- Is Infiniti qx60 worth less than 400000 yuan? It depends on the discount
- "Element Fangjian: first heart version" public beta welfare release, go to the great God app to receive red envelopes and prizes
- What is the role of webpack cli in webpack packaging
- Vue3 configuration method using Axios
- How to configure Google reverse proxy on nginx server
- Volume comparison between Vue and react
- What are the three ways to define components in react
- How to install and configure the blogging program Typecho on the nginx server
- How to configure load balancing for TCP in nginx server
- How to configure nginx server under Windows system
- How to configure AB to do stress testing for nginx server
- Analysis of location configuration in nginx server
- How to integrate Linux and redmine into the redmine system
- How to build the production environment of nginx + PHP with PHP FPM
- How to optimize the performance of nginx supporting SSL
- How to configure nginx server to prevent flood attack
- [Axios learning] basic use of Axios
- [Axios learning] Axios request mode, concurrent request, global configuration, instance and interceptor
- Use the virtual queue implemented by list to view the first element of the queue in Python without taking it out
- This dependency was not found and to install it, you can run: NPM install score JS
- Front end serial communication