int hourglassSum(vector<vector<int>> arr) { int sumMax=arr[0][0]+arr[0][1]+arr[0][2] +arr[1][1] +arr[2][0]+arr[2][1]+arr[2][2];; int lNum=arr.size(); int vNum=arr[0].size();
vector<int> rotLeft(vector<int> a, int d) { int aSize=a.size(); int usefulRotationTime=d%aSize; vector<int> b(aSize,0); for(int i=0;i<aSize;i++){ if(i-usefulRotationTime<0){ b[i-usefulRotationTime+aSize]=a[i]; }else{ b[i-usefulRotationTime]=a[i];
int minSwaps(int arr[], int n) { // Create an array of pairs where first // element is array element and second element // is position of first element pair<int, int> arrPos[n]; for (int i = 0; i < n; i++) { arrPos[i].first = arr[i]; arrPos[i].second = i; } // Sort the array by array element values to // get right position of every element as second // element of pair. sort(arrPos, arrPos + n); // To keep track of visited elements. Initialize // all elements as not visited or false. vector<bool> vis(n, false); // Initialize result int ans = 0; // Traverse array elements for (int i = 0; i < n; i++) { // already swapped and corrected or // already present at correct pos if (vis[i] || arrPos[i].second == i) continue; // find out the number of node in // this cycle and add in ans int cycle_size = 0; int j = i; while (!vis[j]) { vis[j] = 1; // move to next node j = arrPos[j].second; cycle_size++; } // Update answer by adding current cycle. if (cycle_size > 0) { ans += (cycle_size - 1); } } // Return result return ans; } // Driver program to test the above function int main() { int arr[] = {1, 5, 4, 3, 2}; int n = (sizeof(arr) / sizeof(int)); cout << minSwaps(arr, n); return 0; }
Array Manipulation
硬算:超时
1 2 3 4 5 6 7 8 9 10 11
long arrayManipulation(int n, vector<vector<int>> queries) { vector<long> indexs(n,0); for(auto& q : queries){ for(int i=q[0]-1;i<=q[1]-1;++i){ indexs[i]=indexs[i]+q[2]; } } long it = *max_element(std::begin(indexs), std::end(indexs)); return it;