Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int solution(int[] A)
- {
- int minIndex = 1;
- for(int i = 1; i < A.length -1; i++)
- if(A[i] < A[minIndex])
- minIndex = i;
- int doNotTouchLeft = minIndex - 1;
- int doNotTouchright = minIndex + 1;
- int potentiallyBetterCut = 0;
- int secondMinIndex = 1;
- if(minIndex == 1)
- secondMinIndex = 3;
- if(minIndex == 2)
- secondMinIndex = 4;
- for(int i = 1; i < doNotTouchLeft; i++)
- if(A[i] < A[secondMinIndex])
- secondMinIndex = i;
- for(int i = doNotTouchright + 1; i < A.length -1; i++)
- if(A[i] < A[secondMinIndex])
- secondMinIndex = i;
- System.out.println(potentiallyBetterCut);
- if(doNotTouchLeft != 1 && doNotTouchright != A.length -1)
- {
- potentiallyBetterCut = A[doNotTouchLeft] + A[doNotTouchright];
- if(potentiallyBetterCut < A[minIndex] + A[secondMinIndex])
- return potentiallyBetterCut;
- }
- return A[minIndex] + A[secondMinIndex];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement