Advertisement
uopspop

Untitled

Sep 17th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. package prac;
  2.  
  3. import java.util.Arrays;
  4. import java.util.stream.IntStream;
  5.  
  6. public class question04_triangular {
  7.     public static void main(String[] args){
  8.        
  9. //      int[] testA = new int[]{10, 2, 5, 18, 20};
  10. //      int[] testA = new int[]{10, 50, 5, 1};
  11. //      int[] testA = new int[]{1, 3, 9, 10, 15, 50 ,60, 62, 80};
  12. //      int[] testA = new int[]{1, 3, 50 ,60, 62, 80};
  13. //      int[] testA = new int[]{1, 3, 9, 10, 15, 15, 50, 50,60 , 62, 80};
  14. //      int[] testA = new int[]{1, 2, 5, 8, 10, 20};
  15.         int[] testA = new int[]{5,3,3};
  16. //      int[] testA = new int[]{5,3,1};
  17. //      int[] testA = new int[]{5,3,1,5,7};
  18. //      int[] testA = new int[]{1, 1, 2, 3, 5};
  19. //      int[] testA = new int[0];
  20. //      int[] testA = new int[]{-100, 5, 4, 3};
  21. //      int[] testA = new int[]{10, -2147483648, -2147483648};
  22.        
  23.         question04_triangular question = new question04_triangular();
  24.         System.out.println("result: " + question.solution(testA));
  25.     }
  26.    
  27.     // O(n(log n) + n*((log n)**2))
  28.     public int solution(int[] A) {
  29.         int result = 0;
  30.        
  31.         // 先sorting O(n(log n))
  32.         Arrays.sort(A);
  33.        
  34.         // 搜尋: O(n((log n)**2))
  35.         int length = A.length;
  36.         // 第一層: O(n)
  37.         for (int i = length-1; i > 1; i--){
  38.             int max_num_index = i;
  39. //          int initial_mid = (curr_max + 1) / 2;
  40.            
  41.             int second_num_index = (max_num_index + 1) / 2; // 若有兩個數在中間,則取左邊那個
  42.             int curr_max_val = A[max_num_index];
  43.            
  44.             // 最大值若為零或負值,則不用再比
  45.             if (curr_max_val <= 0){
  46.                 break;
  47.             }
  48.            
  49.             // 第二層: O(log(n))
  50.             while(result == 0 && second_num_index < max_num_index){
  51.                 int second_num_val = A[second_num_index];
  52.                
  53.                 if (second_num_val > 0){
  54.                     // 若有比此gap大的數,則代表找到一個三角形了
  55.                     result = checkIfAryHasElmtBiggerThanTheGap(A, max_num_index,second_num_index);
  56.                     if (result != 0){
  57.                         break;
  58.                     }
  59.                 }
  60.                
  61.                 second_num_index = (second_num_index + max_num_index + 1)/2;
  62.                
  63.             }// end of while
  64.             if (result != 0){
  65.                 break;
  66.             }
  67.         }// end of for
  68.        
  69.         return result;
  70.     }
  71.    
  72.     private int checkIfAryHasElmtBiggerThanTheGap(int[] A, int max_num_index, int second_num_index){
  73.        
  74.         int gap = A[max_num_index] - A[second_num_index];
  75.         int low = 0;
  76.         int high = max_num_index - 1; // 不要再找自己 (因為其為目前挑選的第一個數字)
  77.        
  78.         // 第三層: O(log(n))
  79.         while (low <= high) {
  80.             int mid = (low + high) / 2;
  81.             if (A[mid] > gap && mid != second_num_index){ // 不要再找中間數 (因為其為目前挑選的第二個數字)
  82.                 // 若走進此區塊,則表示我們找到了第三個數字
  83.                 // 在此定義: (最大數 - 第二個數字) = k;
  84.                 // 證明若(第三個數字) > k, 則三角形成立:
  85.                 //
  86.                 // 有鑑於 (第二個數字 + k = 最大數 ),
  87.                 // 由 (第三個數字) > k,
  88.                 // 可推得 (第二個數字 + (第三個數字)) >  (第二個數字 + k) == 最大數)
  89.                 // 故可得 (第二個數字 + (第三個數字)) > 最大數
  90.                 // 即代表 最小的兩個數相加 > 最大數
  91.                 //
  92.                 // 即可證此三數可以組成一個三角形
  93.                 return 1;
  94.             }else{
  95.                 low = mid + 1;
  96.             }
  97.         }// end of while
  98.        
  99.         return 0;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement