給出S個n個整數S = s1,s2,...,sn的序列。請計算如果 有可能將S分成兩部分:s1,s2,...,si和si + 1,si + 2,...,sn (n = i < n)第一部分嚴格減少,而第二部分嚴格增加一部分。首先以n爲輸入,然後再輸入n個整數,輸出是或否。排列順序/序列分析
這就是我想
import java.util.Scanner;
public class Sequence {
public static int c;
public static void main(String[] args) {
int n;
int count = 0;
Scanner s = new Scanner(System.in);
n = s.nextInt();
int a[] = new int [n];
for(int i = 0; i<n; i++) // loop for taking input
{
a[i] = s.nextInt();
}
for(int i = 0; i<n-2; i++) // loop for finding the minimum point
{
if(a[i]<a[i+2])
{ c = i; // associated minimum valued index to c
for(; i<n-2; i++) /* loop for checking whether after that the array
{ is decreasing or not*/
if(a[i+1]<a[i+2])
{
count = count+1;
}
else
{
}
}
}
if(count == n-2-c)
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
}
}
這個代碼不經過1測試用例上Hackerrank.com請提出了一些解決方案。
謝謝,因爲這個創造性的答案從來沒有想過使用二進制搜索這個 – dictator