Skip to content

Commit 17d7c65

Browse files
committed
2340 solved.
1 parent 0b4fb49 commit 17d7c65

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(cpp_2340)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_2340 main.cpp)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/// Source : https://leetcode.com/problems/minimum-adjacent-swaps-to-make-a-valid-array/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-07-14
4+
5+
#include <iostream>
6+
#include <algorithm>
7+
#include <numeric>
8+
#include <climits>
9+
#include <vector>
10+
11+
using namespace std;
12+
13+
14+
/// Ad-Hoc
15+
/// Time Complexity: O(n)
16+
/// Space Complexity: O(1)
17+
class Solution {
18+
public:
19+
int minimumSwaps(vector<int>& nums) {
20+
21+
int n = nums.size();
22+
23+
int min_v = nums[0], min_v_index = 0;
24+
for(int i = 1; i < n; i ++)
25+
if(nums[i] < min_v) min_v = nums[i], min_v_index = i;
26+
27+
int max_v = nums[0], max_v_index = 0;
28+
for(int i = 1; i < n; i ++)
29+
if(nums[i] >= max_v) max_v = nums[i], max_v_index = i;
30+
31+
if(min_v_index == 0 && max_v_index == n - 1) return 0;
32+
if(min_v_index < max_v_index) return min_v_index + n - 1 - max_v_index;
33+
return min_v_index + n - 2 - max_v_index;
34+
}
35+
};
36+
37+
38+
int main() {
39+
40+
return 0;
41+
}

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,6 +2187,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
21872187
| 2336 | [Smallest Number in Infinite Set](https://leetcode.com/problems/smallest-number-in-infinite-set/) | [] | [C++](2001-2500/2336-Smallest-Number-in-Infinite-Set/cpp-2336/) | | |
21882188
| 2337 | [Move Pieces to Obtain a String](https://leetcode.com/problems/move-pieces-to-obtain-a-string/) | [] | [C++](2001-2500/2337-Move-Pieces-to-Obtain-a-String/cpp-2337/) | | |
21892189
| 2338 | [Count the Number of Ideal Arrays](https://leetcode.com/problems/count-the-number-of-ideal-arrays/) | [] | [C++](2001-2500/2338-Count-the-Number-of-Ideal-Arrays/cpp-2338/) | | |
2190+
| 2339 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - |
2191+
| 2340 | [Minimum Adjacent Swaps to Make a Valid Array](https://leetcode.com/problems/minimum-adjacent-swaps-to-make-a-valid-array/) | [] | [C++](2001-2500/2340-Minimum-Adjacent-Swaps-to-Make-a-Valid-Array/cpp-2340/) | | |
21902192
| | | | | | |
21912193

21922194
## 力扣中文站比赛

0 commit comments

Comments
 (0)