Advertisement
1988coder

801. Minimum Swaps To Make Sequences Increasing

Dec 15th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.15 KB | None | 0 0
  1. /*
  2. Refer: https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/discuss/119835/Java-O(n)-DP-Solution
  3. Time Complexity: O(N)
  4. Space Complexity: O(1)
  5.  
  6. N = length of the A or B.
  7. */
  8. class Solution {
  9.     public int minSwap(int[] A, int[] B) throws IllegalArgumentException {
  10.         if (A == null || B == null || A.length != B.length) {
  11.             throw new IllegalArgumentException("Invalid input");
  12.         }
  13.         if (A.length <= 1) {
  14.             return 0;
  15.         }
  16.        
  17.         int swapRecord = 1;
  18.         int fixRecord = 0;
  19.        
  20.         for (int i = 1; i < A.length; i++) {
  21.             // if (Math.min(A[i-1], B[i-1]) >= Math.min(A[i], B[i])) {
  22.             //     /*
  23.             //     This case is true only if strictly increasing is not possible.
  24.             //     This is only required if it is not guaranteed that the given input
  25.             //     always makes it possible.
  26.             //     */
  27.             //     return -1;
  28.             // }
  29.             if (A[i-1] >= B[i] || B[i-1] >= A[i]) {
  30.                 /*
  31.                 A[1] >= B[2], which means the manipulation of A[2] & B[2] should
  32.                 be same as A[1] & B[1], if A[1] & B[1] swap, A[2] & B[2] should swap,
  33.                 vice versa.
  34.                 */
  35.                 swapRecord++;
  36.             } else if (A[i-1] >= A[i] || B[i-1] >= B[i]) {
  37.                 /*
  38.                 A[1] >= A[2], which mean the manipulation of A[2] & B[2]
  39.                 and A[1] & B[1] should be opposite.
  40.                 */
  41.                 int temp = swapRecord;
  42.                 swapRecord = fixRecord + 1;
  43.                 fixRecord = temp;
  44.             } else {
  45.                 /*
  46.                 Either swap or fix.. Both are OK. Thus find the previous minimum
  47.                 and set the new fix and swap counts.
  48.                 */
  49.                 int min = Math.min(fixRecord, swapRecord);
  50.                 fixRecord = min;
  51.                 swapRecord = min+1;
  52.             }
  53.         }
  54.        
  55.         return Math.min(swapRecord, fixRecord);
  56.     }
  57. }
  58.  
  59.  
  60. /*
  61. Time Complexity: O(N)
  62. Space Complexity: O(N)
  63.  
  64. N = length of the A or B.
  65. */
  66. class Solution {
  67.     public int minSwap(int[] A, int[] B) throws IllegalArgumentException {
  68.         if (A == null || B == null || A.length != B.length) {
  69.             throw new IllegalArgumentException("Invalid input");
  70.         }
  71.         if (A.length <= 1) {
  72.             return 0;
  73.         }
  74.        
  75.         int[] swapRecord = new int[A.length];
  76.         int[] fixRecord = new int[A.length];
  77.         swapRecord[0] = 1;
  78.        
  79.         for (int i = 1; i < A.length; i++) {
  80.             // if (Math.min(A[i-1], B[i-1]) >= Math.min(A[i], B[i])) {
  81.             //     /*
  82.             //     This case is true only if strictly increasing is not possible.
  83.             //     This is only required if it is not guaranteed that the given input
  84.             //     always makes it possible.
  85.             //     */
  86.             //     return -1;
  87.             // }
  88.             if (A[i-1] >= B[i] || B[i-1] >= A[i]) {
  89.                 /*
  90.                 A[1] >= B[2], which means the manipulation of A[2] & B[2] should
  91.                 be same as A[1] & B[1], if A[1] & B[1] swap, A[2] & B[2] should swap,
  92.                 vice versa.
  93.                 */
  94.                 fixRecord[i] = fixRecord[i-1];
  95.                 swapRecord[i] = swapRecord[i-1] + 1;
  96.             } else if (A[i-1] >= A[i] || B[i-1] >= B[i]) {
  97.                 /*
  98.                 A[1] >= A[2], which mean the manipulation of A[2] & B[2]
  99.                 and A[1] & B[1] should be opposite.
  100.                 */
  101.                 fixRecord[i] = swapRecord[i-1];
  102.                 swapRecord[i] = fixRecord[i-1] + 1;
  103.             } else {
  104.                 /*
  105.                 Either swap or fix.. Both are OK. Thus find the previous minimum
  106.                 and set the new fix and swap counts.
  107.                 */
  108.                 int min = Math.min(fixRecord[i-1], swapRecord[i-1]);
  109.                 fixRecord[i] = min;
  110.                 swapRecord[i] = min+1;
  111.             }
  112.         }
  113.        
  114.         return Math.min(swapRecord[A.length - 1], fixRecord[A.length - 1]);
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement