使用java challenge question查找并格式化森林中视觉上美观的树木模式

使用java challenge question查找并格式化森林中视觉上美观的树木模式,java,performance,correctness,Java,Performance,Correctness,最近面临着一个有上述问题的面试代码挑战。有一个家伙拥有他的森林,在那里树木以成排的形式种植。每行应包含一棵视觉上令人愉悦的树。如上图所示: 上述树木图案在视觉上永远不会令人愉悦: 在这里,每列代表行中的树及其高度。两棵树之间的高度不应相等,以使这排树在视觉上美观。农场的主人希望所有的树在视觉上都美观。为此,他一行最多只能砍一棵树。找出在一排树上砍一棵树的可能方法,使这排树美观 即使某一行已经在视觉上美观了,也会返回0作为函数的输出 否则,即使在砍掉任何一棵树后,这排树也可能永远无法形成视觉

最近面临着一个有上述问题的面试代码挑战。有一个家伙拥有他的森林,在那里树木以成排的形式种植。每行应包含一棵视觉上令人愉悦的树。如上图所示:

上述树木图案在视觉上永远不会令人愉悦:

在这里,每列代表行中的树及其高度。两棵树之间的高度不应相等,以使这排树在视觉上美观。农场的主人希望所有的树在视觉上都美观。为此,他一行最多只能砍一棵树。找出在一排树上砍一棵树的可能方法,使这排树美观

即使某一行已经在视觉上美观了,也会返回0作为函数的输出

否则,即使在砍掉任何一棵树后,这排树也可能永远无法形成视觉上的美学图案;然后函数应该返回-1

例如:A[]={3,4,5,3,7};然后可以通过3种方式使它在视觉上美观:remove 3,A[]={4,5,3,7}else remove 4,A[]={3,5,3,7}else remove 5,A[]={3,4,3,7},所以函数应该返回3

g。B[]={1,2,3,4,2,5};这种模式永远不会形成视觉美学,因此,函数应该返回-1

例如c[]={1,3,1,2};此模式在视觉上已经令人愉悦,因此它应该返回0


我试图解决它,如下一个解决方案部分所示。有人能提出更好的解决问题的方法,以降低代码复杂度并加快使用java的速度吗?

下面是我的代码。如果下面有任何问题,请纠正我

public int solution(int[] A) {

    int count = 0;
    List<Integer> arr = new ArrayList<Integer>();

    ArrayList<Integer> arrt = new ArrayList<Integer>();

    for (int i : A) {
        arr.add(i);
    }

    // first check if its already in correct sequence
    boolean check = false;
    for (int j = 0; j < A.length-2 ; j++) {
        if ((arr.get(j) - arr.get(j + 1) > 0) && (arr.get(j + 1) - arr.get(j + 2) < 0)) {
            check = true;
        } else if ((arr.get(j) - arr.get(j + 1) < 0) && (arr.get(j + 1) - arr.get(j + 2) > 0)) {
            check = true;
        } else {
            check = false;
            break;
        }
    }
    if (check) {
        return 0;
    }

    List<Integer> ab = new ArrayList<Integer>();

    for (int i = 0; i < A.length; i++) {
        ab.clear();
        ab.addAll(arr);
        ab.remove(i);
        int f = 0;
        boolean okay = false;
        while (f < A.length - 3) {
            if (!okay && f != 0) {
                break;
            }
            if ((ab.get(f) - ab.get(f + 1) > 0) && (ab.get(f + 1) - ab.get(f + 2) < 0)) {
                okay = true;
            } else if ((ab.get(f) - ab.get(f + 1) < 0) && (ab.get(f + 1) - ab.get(f + 2) > 0)) {
                okay = true;
            } else {
                okay = false;
            }
            f++;
        }
        if (okay) {
            count++;
        }
    }
    if (count == 0)
        count = -1;

    return count;
}
public int解决方案(int[]A){
整数计数=0;
List arr=new ArrayList();
ArrayList arrt=新的ArrayList();
对于(int i:A){
arr.add(i);
}
//首先检查其顺序是否正确
布尔检查=假;
对于(int j=0;j0)和&(arr.get(j+1)-arr.get(j+2)<0)){
检查=正确;
}否则如果((arr.get(j)-arr.get(j+1)<0)和&(arr.get(j+1)-arr.get(j+2)>0)){
检查=正确;
}否则{
检查=错误;
打破
}
}
如果(检查){
返回0;
}
List ab=新的ArrayList();
for(int i=0;i0)和&(ab.get(f+1)-ab.get(f+2)<0)){
OK=正确;
}else如果((ab.get(f)-ab.get(f+1)<0)和&(ab.get(f+1)-ab.get(f+2)>0)){
OK=正确;
}否则{
好=假;
}
f++;
}
如果(好的){
计数++;
}
}
如果(计数=0)
计数=-1;
返回计数;
}

下面是我的代码。如果下面有任何问题,请纠正我

public int solution(int[] A) {

    int count = 0;
    List<Integer> arr = new ArrayList<Integer>();

    ArrayList<Integer> arrt = new ArrayList<Integer>();

    for (int i : A) {
        arr.add(i);
    }

    // first check if its already in correct sequence
    boolean check = false;
    for (int j = 0; j < A.length-2 ; j++) {
        if ((arr.get(j) - arr.get(j + 1) > 0) && (arr.get(j + 1) - arr.get(j + 2) < 0)) {
            check = true;
        } else if ((arr.get(j) - arr.get(j + 1) < 0) && (arr.get(j + 1) - arr.get(j + 2) > 0)) {
            check = true;
        } else {
            check = false;
            break;
        }
    }
    if (check) {
        return 0;
    }

    List<Integer> ab = new ArrayList<Integer>();

    for (int i = 0; i < A.length; i++) {
        ab.clear();
        ab.addAll(arr);
        ab.remove(i);
        int f = 0;
        boolean okay = false;
        while (f < A.length - 3) {
            if (!okay && f != 0) {
                break;
            }
            if ((ab.get(f) - ab.get(f + 1) > 0) && (ab.get(f + 1) - ab.get(f + 2) < 0)) {
                okay = true;
            } else if ((ab.get(f) - ab.get(f + 1) < 0) && (ab.get(f + 1) - ab.get(f + 2) > 0)) {
                okay = true;
            } else {
                okay = false;
            }
            f++;
        }
        if (okay) {
            count++;
        }
    }
    if (count == 0)
        count = -1;

    return count;
}
public int解决方案(int[]A){
整数计数=0;
List arr=new ArrayList();
ArrayList arrt=新的ArrayList();
对于(int i:A){
arr.add(i);
}
//首先检查其顺序是否正确
布尔检查=假;
对于(int j=0;j0)和&(arr.get(j+1)-arr.get(j+2)<0)){
检查=正确;
}否则如果((arr.get(j)-arr.get(j+1)<0)和&(arr.get(j+1)-arr.get(j+2)>0)){
检查=正确;
}否则{
检查=错误;
打破
}
}
如果(检查){
返回0;
}
List ab=新的ArrayList();
for(int i=0;i0)和&(ab.get(f+1)-ab.get(f+2)<0)){
OK=正确;
}else如果((ab.get(f)-ab.get(f+1)<0)和&(ab.get(f+1)-ab.get(f+2)>0)){
OK=正确;
}否则{
好=假;
}
f++;
}
如果(好的){
计数++;
}
}
如果(计数=0)
计数=-1;
返回计数;
}

以下是我的c语言解决方案:

 #include <stdio.h>
        
    char aestheticallyPleasantCheck (int A[], int N) {
          int x;
          char pleasant = 0;
          for (x = 0; x < N - 2; x++){
                if ((A[x] < A[x + 1] && A[x + 1] > A[x + 2]) || (A[x] > A[x + 1] && A[x + 1] < A[x + 2])){
                    pleasant = 1;
                } else {
                    break;
                }
            }
            if (pleasant == 1 && x == N - 2) {
                return 0;
            } else {
                return 1;
            }
        }
        
        int solution (int A[], int N) {
          int count = 0;
          int ArrayCopy[N];
        
          if (aestheticallyPleasantCheck (A, N) == 0)
            {
              return 0;
            }
        
            for (int i = 0; i < N; i++){
                for (int m = 0; m < N; m++){
                    ArrayCopy[m] = A[m];
                }
            
                for (int m = i; m < N - 1; m++){
                    ArrayCopy[m] = ArrayCopy[m + 1];
                }
        
                if (aestheticallyPleasantCheck (ArrayCopy, N - 1) == 0){
                  count++;
                }
            }
        
            if (count == 0){
                count = -1;
            }
        
          return count;
        }
        
        
        /*
        Example test:   [3, 4, 5, 3, 7]
        Example test:   [1, 2, 3, 4]
        Example test:   [1, 3, 1, 2]
        */
        
        main ()
        {
        
          int A[] = { 3, 4, 5, 3, 7 };  // Expected 3 .^.^
          int B[] = { 1, 2, 3, 4 }; // 
          int C[] = { 1, 3, 1, 2 }; //
          int D[] = { 5, 3, 7, 8, 9 };
          printf ("Got %d : Expected 3\n", solution (A, sizeof (A) / sizeof (int)));
          printf("Got %d : Expected -1\n", solution( B, sizeof(B)/sizeof(int)));
          printf("Got %d : Expected 0\n", solution( C, sizeof(C)/sizeof(int)) );
          printf ("Got %d : Expected -1\n", solution (D, sizeof (D) / sizeof (int)));
          
          return 0;
        }
#包括
字符美学上的最小值检查(int A[],int N){
int x;
char=0;
对于(x=0;xA[x+2])| |(A[x]>A[x+1]&A[x+1]