使用Java挑战问题

最近面临一个以上问题的面试代码挑战。有一个人有他的森林,那里以行的形式种植树木。每行应以视觉上美观的方式包含一棵树。例如。如上图所示:

image1

image2

以上树形图案在视觉上永远不会令人愉悦: image3

此处每一列代表行中的树及其高度。彼此之间不应有两棵树的高度相等,以使行在视觉上美观。农场主希望所有树木在视觉上都美观。为此,他最多可以连续砍伐一棵树。找出可以砍成一棵树以使一排树变得美观的方法。

即使某行在视觉上已经美观,然后返回0作为函数的输出。

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

例如:A [] = {3,4,5,3,7};那么可以通过3种方式使它具有视觉美感:移除3,A [] = {4,5,3,7}否则移除4,A [] = {3,5,3,7}否则移除5,A [ ] = {3,4,3,7},因此函数应返回3。

g B [] = {1,2,3,4,5};这种模式永远不会在视觉上具有美感,因此功能应返回-1。

例如c [] = {1,3,1,2};此模式在视觉上已经具有美学美感,因此应返回0。

我试图解决它,如下一个解决方案部分所示。有人可以建议一种更好的方法来解决问题,以减少代码复杂性并使用Java更快地工作吗?

iCMS 回答:使用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;
}
,

这是java中的简短解决方案。

class Solution {
    public int solution(int[] A) {
        if (A.length < 3) {
            return A[0] != A[1] ? 0 : 1;
        }
        int count = 0;
        for (int i = 0; i < A.length - 2 ; i += 2) {
            int a = A[i];
            int b = A[i+1];
            int c = A[i + 2];
            if (!(a - b > 0 && b - c < 0) && !(a - b < 0 && b - c > 0)) {
                count ++;
            }
        }
        return count;
    }
}
,

这是我的解决方案。

class Solution {
    public int solution(int[] A) {
        // check if the input array in the correct order
        int count = 0;
        for(int i = 0; i < A.length - 2; i++) {
            if(A[i] < A[i+1]) {
                if(!(A[i+1] > A[i+2])) {
                    count++;
                }
            }
            else if(A[i] > A[i+1]) {
                if(!(A[i+1] < A[i+2]))  {
                    count++;
                }
            }
        }
        
        if(count == 0)
            return count;
        // if false then copy to Arraylist and do the removal
        List<Integer> inputArr = new ArrayList<Integer>();
        for(int item : A) {
            inputArr.add(item);
        }
        return doTheRemoval(inputArr);
    }

    private int doTheRemoval(List<Integer> inputArr) {
        int count = -1;
        for(int i = 0; i < inputArr.size(); i++) {
            // remove tree sequencely
            inputArr.remove(i);
            // check the rest
            for(int j = 0; j < inputArr.size() - 2; j++) {
                int a = inputArr.get(j);
                int b = inputArr.get(j + 1);
                int c = inputArr.get(j + 2);
                if((a - b < 0 && b - c > 0) || (a - b > 0 && b - c < 0)) {
                    if(count == -1) {
                        count = 0;
                    }
                    count++;
                }
            }
        }
        return count;
    }
}
,

这是我在 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,4]
        Example test:   [1,1,2]
        */
        
        main ()
        {
        
          int A[] = { 3,7 };  // Expected 3 .^.^
          int B[] = { 1,4 }; // 
          int C[] = { 1,2 }; //
          int D[] = { 5,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;
        }
本文链接:https://www.f2er.com/2128589.html

大家都在问