当前位置:网站首页>Leetcdoe 2037. Make each student have the minimum number of seat movements (yes, once)
Leetcdoe 2037. Make each student have the minimum number of seat movements (yes, once)
2022-06-12 10:27:00 【I'm not xiaohaiwa~~~~】

There is... In a room n Seats and n Famous student , The room is represented by a number axis . Give you a length of n Array of seats , among seats[i] It's No i A seat . At the same time, give you a length of n Array of students , among students[j] It's No j The position of a student .
You can do the following any number of times :
- Increase or decrease the number of i The position of a student , Each change is 1 ( That is to say, it will be i Students from position x Move to x + 1 perhaps x - 1)
Please go back to the place where all students have seats The minimum number of moves , And make sure no two students have the same seats .
Please note that , There may be more than one seat or more students at the beginning same Location .
Example 1:
Input :seats = [3,1,5], students = [2,7,4]
Output :4
explain : Students move as follows :
- The first student from position 2 Move to location 1 , Move 1 Time .
- The second student from the position 7 Move to location 5 , Move 2 Time .
- The third student from the position 4 Move to location 3 , Move 1 Time .
in total 1 + 2 + 1 = 4 Secondary mobility .
Example 2:
Input :seats = [4,1,5,9], students = [1,3,2,6]
Output :7
explain : Students move as follows :
- The first student doesn't move .
- The second student from the position 3 Move to location 4 , Move 1 Time .
- The third student from the position 2 Move to location 5 , Move 3 Time .
- The fourth student from the position 6 Move to location 9 , Move 3 Time .
in total 0 + 1 + 3 + 3 = 7 Secondary mobility .
Example 3:
Input :seats = [2,2,6,6], students = [1,3,2,6]
Output :4
explain : Students move as follows :
- The first student from position 1 Move to location 2 , Move 1 Time .
- The second student from the position 3 Move to location 6 , Move 3 Time .
- The third student doesn't move .
- The fourth student doesn't move .
in total 1 + 3 + 0 + 0 = 4 Secondary mobility .
Tips :
- n == seats.length == students.length
- 1 <= n <= 100
- 1 <= seats[i], students[j] <= 100
Main idea : Prioritize , Then find the absolute value
Code:
class Solution {
public:
int minMovesToSeat(vector<int>& seats, vector<int>& students) {
int res=0;
sort(seats.begin(),seats.end());
sort(students.begin(),students.end());
for(int i=0;i<seats.size();i++)
{
res+=abs(students[i]-seats[i]);
}
return res;
}
};
边栏推荐
- Getting started with cloud API basics -- basic knowledge of picgo writing plug-ins
- Remote link software xshell and xftp download address
- ASP. Net core permission system practice (zero)
- Redis (II) Memory mapped data structure
- 浅谈调和形状上下文特征HSC对3DSC的改进
- Summary method of lamp environment deployment
- Find the location of a function in PHP
- 3. Abstract Factory
- Class selectors and using pseudo class selectors with
- PHP wechat payment V3 interface
猜你喜欢

How to play the 2022 Taobao 618 Super Cat Games? What are the strategies for the Super Cat Games

使用cpolar远程办公(2)

Basic use of scratch

Remote desktop cannot copy and paste solution

MySQL 4 Database table storage structure & tablespace

Tp6 debugging (trace)
Docker compose integrates redis, MySQL and microservices, and services are containerized

Properties Chinese garbled code

pycharm 查看opencv当前的版本

学生管理系统
随机推荐
Pagoda chevereto1.6.2 the latest version of stepping on the pit tutorial in Chinese
ASP. Net core permission system practice (zero)
3. Abstract Factory
Golang start service background daemon
Simple use of autojs
[CEGUI] concept introduction
Jump to wechat in app and open wechat
不需要安装防毒软件的系统Chromebook
1268_ Implementation of FreeRTOS task context switching
How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?
High performance computing framework for image processing
Class selectors and using pseudo class selectors with
MySQL 4 Database table storage structure & tablespace
CTF freshman cup PHP deserialization question - EzPop
Docker compose integrates redis, MySQL and microservices, and services are containerized
性能指标的信仰危机
Propagation of transactions
浅谈调和形状上下文特征HSC对3DSC的改进
np. Meshgrid() function and coordinate position generation in 3D space and numpy Introduction to repeat() function
SAP Hana error message sys_ XSA authentication failed SQLSTATE - 28000