Advertisement
uopspop

Untitled

Feb 5th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package com;
  2.  
  3. /**
  4.  * 給定一個數列,
  5.  * 判斷最後能不能拿到一個非遞減的數列,若可以回傳true,若不行回傳false。
  6.  * 可以使用0或1次的swap來達到目的。
  7.  *
  8.  * 結果: 62% correctness
  9.  *
  10.  * @author sam
  11.  *
  12.  */
  13.  
  14. public class Tom_01 {
  15.     public static void main(String[] args){
  16.         int[] A = {1, 5, 3, 3, 7}; // true
  17.         int[] B = {1, 3, 5, 3, 4}; // false
  18.         int[] C = {1, 5, 3, 3, 3, 3, 3, 7}; // true
  19.         int[] D = {-1, 5, 3, 3, 3, 3, 2, 7}; // true
  20.         int[] E = {-1, 5, 3, 3, 3, 2, 3, 7}; // false
  21.         int[] F = {1, 1, 1, 1, 1}; // false
  22.         int[] G = {2, 2, 1, 1, 1}; // false
  23.         int[] H = {2, 1, 1, 1, 1}; // true
  24.         // ? 負數
  25.         Tom_01 question01 = new Tom_01();
  26. //      System.out.println("result: " + question01.solution(A));
  27. //      System.out.println("result: " + question01.solution(B));
  28. //      System.out.println("result: " + question01.solution(C));
  29. //      System.out.println("result: " + question01.solution(D));
  30. //      System.out.println("result: " + question01.solution(E));
  31. //      System.out.println("result: " + question01.solution(F));
  32. //      System.out.println("result: " + question01.solution(G));
  33. //      System.out.println("result: " + question01.solution(H));
  34.     }
  35.    
  36.     public boolean solution(int[] ary) {
  37.         // check if it's already sorted (not decreasing (= or >))
  38.         boolean isSorted = true;
  39.         int index_wrong_one = -1;
  40.         for (int i = 0; i < ary.length - 1; i++) { // attention: do not let i+1 exceed the length
  41.             if (ary[i] > ary[i + 1]) {
  42.                 isSorted = false;
  43.                 index_wrong_one = i;
  44.                 break;
  45.             }
  46.         }
  47.        
  48.         // if not , get the one the needs to be swapped
  49.         int index_smallest_to_swap = -1;
  50.         int smallest_to_swap = Integer.MAX_VALUE;
  51.         if (!isSorted) {
  52.             // find the smallest one after the wrong one in terms of index
  53.             for (int i = index_wrong_one + 1; i < ary.length; i++) {
  54.                 if (ary[i] <= smallest_to_swap) {
  55.                     smallest_to_swap = ary[i];
  56.                     index_smallest_to_swap = i;
  57.                 }
  58.             }
  59.         }else {
  60. //          System.out.println("end 01 true");
  61.             return true; // it's sorted already
  62.         }
  63.        
  64.         // actually swap them
  65.         int tmp_wrong_one = ary[index_wrong_one];
  66.         ary[index_wrong_one] = ary[index_smallest_to_swap];
  67.         ary[index_smallest_to_swap] = tmp_wrong_one;
  68.        
  69.         // check if it's already sorted again
  70.         isSorted = true;
  71.         for (int i = 0; i < ary.length - 1; i++) {
  72.             if (ary[i] > ary[i + 1]) {
  73.                 isSorted = false;
  74.             }
  75.         }      
  76.        
  77.         if (isSorted) {
  78.             return true; // it's sorted after one swap
  79.         }
  80.        
  81.         return false; // it's not sorted even after one swap
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement