1 | int minMoves(vector<int>arr, int n, int k) { |
2 | |
3 | sort(arr.begin(), arr.end()); |
4 | |
5 | int i = 0, j = n - 1; |
6 | int moves = 0; |
7 | |
8 | while (i <= j) { |
9 | |
10 | if (arr[i] + arr[j] <= k) { |
11 | i++; |
12 | } |
13 | |
14 | j--; |
15 | |
16 | moves++; |
17 | } |
18 | |
19 | return moves; |
20 | } |
最後活躍
1 | int minMoves(vector<int>arr, int n, int k) { |
2 | |
3 | sort(arr.begin(), arr.end()); |
4 | |
5 | int i = 0, j = n - 1; |
6 | int moves = 0; |
7 | |
8 | while (i <= j) { |
9 | |
10 | if (arr[i] + arr[j] <= k) { |
11 | i++; |
12 | } |
13 | |
14 | j--; |
15 | |
16 | moves++; |
17 | } |
18 | |
19 | return moves; |
20 | } |