好的,所以我刚开始使用CodeFight,我得到了这个问题:
经过几个小时试图找到一个独立的方法解决它,我得到它完全功能,但显然它不在时间限制内,我不知道如何解决它更多解决它。
boolean almostIncreasingSequence(int[] sequence) {
List<Integer> A = new ArrayList<Integer>();
for (int i = 0; i < sequence.length; i++){
A.add(sequence[i]);
}
for (int i = 0; i < A.size(); i++){
ArrayList<Integer> AC = new ArrayList<Integer>(A);
AC.remove(i);
boolean ps = true;
for (int x = 0; x < AC.size()-1; x++){
if (AC.get(x) >= AC.get(x+1)){
ps = false;
break;
}
}
if (ps){
return true;
}
}
return false;
}
这就是我得到的
我知道有这样的问题,但我还没有找到解决方案。我希望有人能帮帮忙。我已经被困了几个小时了。
答案 0 :(得分:1)
您可能需要考虑另一种解决问题的方法。通过一次评估每个条目,可以在线性时间内解决该问题。一个条目可以是增加序列的一部分,也可以是几乎增加序列的一部分,或者两者都不是。
如果序列中包含少于两个几乎增加的条目,则序列将几乎增加,因此我们可以在最后检查它。
public enum EntryState {
INCREASING(0), ALMOST_INCREASING(1), DECREASING(2);
private final int state;
private EntryState(int state) {
this.state = state;
}
public int getValue() { return state; }
}
public boolean almostIncreasingSequence(int[] sequence) {
int l = sequence.length;
EntryState[] sequenceState = new EntryState[l];
for(int i = 0; i < l - 1; i++) {
if(sequence[i] <= sequence[i + 1])
sequenceState[i] = EntryState.INCREASING;
else if((i + 2 >= l) || (i + 2 < l && sequence[i] <= sequence[i + 2]))
sequenceState[i] = EntryState.ALMOST_INCREASING;
else
sequenceState[i] = EntryState.DECREASING;
}
sequenceState[l - 1] = EntryState.INCREASING;
int sum = 0;
for(EntryState e : sequenceState)
sum += e.getValue();
int sumWithoutStart = sum - sequenceState[0].getValue();
int sumWithoutEnd = sum - sequenceState[l - 1].getValue();
return sumWithoutStart == 0 || sumWithoutEnd == 0 || sum < 2;
}
答案 1 :(得分:1)
这个版本基本上只是为各种情况构建序列,然后检查我们创建的序列的大小。如果increasingSeq 的大小只比我们传入的数组的大小小1,那么我们知道只有一个错误。如果它的偏差超过 1,那么我们知道 seq 中有多个错误,必须返回 false。这花了我一段时间来思考它有点简单/奇怪的困难问题idk。这段代码也可以更简洁,但我还是继续吧。
boolean almostIncreasingSequence(int[] sequence)
{
ArrayList<Integer> increasingSeq = new ArrayList<Integer>();
if(sequence.length > 1)
{
int prev, cur;
for(int i = 1; i < sequence.length; i++)
{
prev = sequence[i - 1];
cur = sequence[i];
if(prev < cur)
{
if(increasingSeq.size() == 0 || increasingSeq.get(increasingSeq.size() - 1) < cur)
{
increasingSeq.add(cur);
}
}
else if(prev > cur)
{
if(increasingSeq.size() > 1 && increasingSeq.get(increasingSeq.size() - 2) < cur)
{
increasingSeq.remove((increasingSeq.size() - 1));
increasingSeq.add(cur);
}
}
}
}
if((increasingSeq.size() + 1) == (sequence.length - 1))
{
return true;
}
return false;
}
答案 2 :(得分:0)
我认为您的问题的代码应该看起来像这样
public static void main( String[] args )
{
System.out.println( "Output: " + almostIncreasingSequence( new int[]{1,3,2,1} ) );
System.out.println("\r\n");
System.out.println( "Output: " + almostIncreasingSequence( new int[]{1,3,2} ) ) ;
System.out.println("\r\n");
System.out.println( "Output: " + almostIncreasingSequence( new int[]{1,3,5,12,15,2} ) );
}
public static boolean almostIncreasingSequence( int[] array )
{
boolean hasIncreasingSequenceNature = false;
for( int x = 0; x < array.length; x++ )
{
boolean almostIncreasingWithRemovedElement = assumeRemovedElement(x , array);
System.out.println( "Removing " + array[x] + " would produce INCREASING SEQUENCE?: " + almostIncreasingWithRemovedElement);
if( almostIncreasingWithRemovedElement )
{
hasIncreasingSequenceNature = true;
}
}
return hasIncreasingSequenceNature;
}
public static boolean assumeRemovedElement( int assumeRemovedIndex , int[] data )
{
boolean isIncreasing = false;
for( int x = 0; x < data.length - 1; x++ )
{
if( x == ( assumeRemovedIndex - 1 ) )
{
if( (x < data.length - 2 ) )
{
if( data[x] < data[x + 2] )
{
isIncreasing = true;
}
else
{
isIncreasing = false;
break;
}
}
}
else if( x != assumeRemovedIndex )
{
if( data[x] < data[x + 1] )
{
isIncreasing = true;
}
else
{
isIncreasing = false;
break;
}
}
}
return isIncreasing;
}
我猜你困惑的部分是从数组中删除一个元素,但这并不要求你实际上尝试从数组中删除东西,而是找到迭代方法,这有助于您跳过假设删除的元素并让代码在迭代期间跟踪数组中增加的序列
解释代码, boolean almostIncreasingSequence(int [])实际上什么也没做,只是对列表中的每个元素进行了一次强力尝试,看看 assumeRemovedElement(如何) int byIndex,int [] yourSequence)在通过使用 byIndex 变量跳过数组元素来应用递增序列检查算法时返回。一旦我们发现 assumeRemovedElement(int,int [])返回true, almostIncreasingSequence(int [])就会将其 hasIncreasingSequenceNature 标记为是真还是假,这样才能分析最终输出,因为记住,你必须通过检查序列,假设删除数组中的每个元素,因此术语暴力
assumeRemovedElement(int assumeRemovedIndex,int [] data)通过确保我们不会遇到涉及与数组中假设删除的元素进行比较,当 if(x ==(assumeRemovedIndex - 1))和 else if(x!= assumeRemovedIndex)时采取这些安全措施)在循环中执行,我们很难用基本的if / else结构来维护我们不断增加的序列检查代码。希望澄清很多,但肯定会接受进一步的讨论