Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. public static int solution(int[] A)
  2.     {
  3.         int minIndex = 1;
  4.         for(int i = 1; i < A.length -1; i++)           
  5.             if(A[i] < A[minIndex])
  6.                 minIndex = i;              
  7.        
  8.         int doNotTouchLeft = minIndex - 1;
  9.         int doNotTouchright = minIndex + 1;
  10.         int potentiallyBetterCut = 0;
  11.        
  12.         int secondMinIndex = 1;
  13.         if(minIndex == 1)
  14.             secondMinIndex = 3;
  15.         if(minIndex == 2)
  16.             secondMinIndex = 4;
  17.        
  18.         for(int i = 1; i < doNotTouchLeft; i++)            
  19.             if(A[i] < A[secondMinIndex])
  20.                 secondMinIndex = i;
  21.         for(int i = doNotTouchright + 1; i < A.length -1; i++)         
  22.             if(A[i] < A[secondMinIndex])
  23.                 secondMinIndex = i;
  24.        
  25.         System.out.println(potentiallyBetterCut);
  26.        
  27.         if(doNotTouchLeft != 1 && doNotTouchright != A.length -1)
  28.         {
  29.             potentiallyBetterCut = A[doNotTouchLeft] + A[doNotTouchright];
  30.             if(potentiallyBetterCut < A[minIndex] + A[secondMinIndex])     
  31.                 return potentiallyBetterCut;
  32.         }      
  33.        
  34.  
  35.         return A[minIndex] + A[secondMinIndex];        
  36.        
  37.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement