Advertisement
nikunjsoni

801

Jun 14th, 2021
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int minSwap(vector<int>& A, vector<int>& B) {
  4.         int N = A.size();
  5.         int not_swap[1000] = {0};
  6.         int swap[1000] = {1};
  7.         for (int i = 1; i < N; ++i) {
  8.             not_swap[i] = swap[i] = INT_MAX;
  9.             if(A[i - 1] < A[i] && B[i - 1] < B[i]) {
  10.                 swap[i] = swap[i - 1] + 1;
  11.                 not_swap[i] = not_swap[i - 1];
  12.             }
  13.             if(A[i - 1] < B[i] && B[i - 1] < A[i]) {
  14.                 swap[i] = min(swap[i], not_swap[i - 1] + 1);
  15.                 not_swap[i] = min(not_swap[i], swap[i - 1]);
  16.             }
  17.         }
  18.         return min(swap[N - 1], not_swap[N - 1]);
  19.     }
  20. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement