-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1674.txt
More file actions
39 lines (34 loc) · 1.04 KB
/
1674.txt
File metadata and controls
39 lines (34 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
Map<Integer,Integer>map=new HashMap<>();
public int minMoves(int[] A, int limit) {
int n=A.length;
int res=Integer.MAX_VALUE;
int bound[][]=new int[n/2][2];
int line[]=new int[limit*2+10];
for(int i=0;i<n/2;i++){
int mx=Math.max(A[i],A[n-i-1]);
int mn=Math.min(A[i],A[n-i-1]);
bound[i][0]=mx+limit;
bound[i][1]=mn+1;
line[bound[i][0]+1]--;
line[bound[i][1]]++;
if(!map.containsKey(mx+mn)){
map.put(mx+mn,1);
}
else{
map.put(mx+mn,map.get(mx+mn)+1);
}
}
int sum=0;
for(int com=1;com<line.length;com++){
sum+=line[com];//1 move
int need=sum+(n/2-sum)*2;
int repeat=0;
if(map.containsKey(com)){
repeat=map.get(com);
}
res=Math.min(res,need-repeat);
}
return res;
}
}