Hướng dẫn growth in 2 dimensions hackerrank c++ - tăng trưởng theo 2 chiều hackerrank c ++

Tôi đã thực hiện một đánh giá cho phỏng vấn xin việc. Một trong 3 vấn đề mà tôi phải giải quyết trong một giờ là tìm ra giá trị tối đa trong một lưới nơi bạn đi qua nó và thêm 1 vào các yếu tố dựa trên các tọa độ được đưa ra. Tôi đã dành một chút thời gian cho vấn đề thứ hai và chỉ kết thúc với khoảng 20 phút cho lần này. Tôi đã không hoàn thành nó kịp thời nên nó làm phiền tôi.

Tôi chỉ muốn đảm bảo rằng giải pháp cho vấn đề vì tôi nhớ nó được tối ưu hóa.

Đầu vào là một mảng chuỗi gồm hai giá trị INT và kích thước của lưới.

Để minh họa, nếu các tọa độ được đưa ra là (3,2) (2,2) (1,3) thì

[1][1][0]     
[1][1][0]     
[1][1][0]     

[1][1][0]     
[2][2][0]     
[2][2][0]     

[1][1][0]
[2][2][0]
[3][3][1]

và như thế...

Tôi tin rằng kết quả cần thiết là giá trị tối đa không có trong (1,1) và số lần nó tồn tại trong lưới.

Đây là giải pháp tôi đưa ra. Có cách nào để tối ưu hóa nó không?

public static List twoDimensions(String[] coordinates, int n) {

    List maxAndCount = new ArrayList();
    int[][] grid = new int[n][n];
    int arrLength = coordinates.length;
    int max = Integer.MIN_VALUE;
    int count = 1;

    for (int i = 0; i < arrLength; i++) {

        String[] coors = coordinates[i].split(" ");
        int row = Integer.parseInt(coors[0]);
        int column = Integer.parseInt(coors[1]);

        for (int j = 0; j < row; j++) {
            for (int k = 0; k < column; k++) {

                grid[j][k] += 1;
                System.out.println("grid (" + j + "," + k + "): " + grid[j][k]);

                if (!(j == 0 & k == 0) && grid[j][k] > max) {

                    max = grid[j][k];
                    count = 1;

                } else if (grid[j][k] == max) {

                    count++;

                }
            }
        }
    }

    maxAndCount.add(max);
    maxAndCount.add(count);

    return maxAndCount;
}

public static void main(String[] args) {

    String[] coors = { "1 3", "2 4", "4 1", "3 2" };
    System.out.println("The Max and count Are:" + twoDimensions(coors, 4).toString());
}

Sắp xếp 1785 Thảo luận, bởi:

Vui lòng đăng nhập để đăng bình luận

  • Hướng dẫn growth in 2 dimensions hackerrank c++ - tăng trưởng theo 2 chiều hackerrank c ++

    4 ngày trước+ 0 bình luận+ 0 comments

    Nhập toán

    Nhập hệ điều hành

    Nhập ngẫu nhiên

    Nhập RE

    nhập khẩu sys

    def get_hourglass_sum (AR, R, C):

    sum = 0
    
    for ri in range(r,r+3):
    
        for ci in range(c,c+3):
    
            sum+=ar[ri][ci]
    
    sum = sum - ar[r+1][c] - ar[r+1][c+2]
    
    return sum
    

    Nếu name == 'chính':name == 'main':

    arr = []
    max_sum = -1000
    
    for _ in range(6):
        arr.append(list(map(int, input().rstrip().split())))
    
    for r in range(len(arr)-2):
        for c in range(len(arr[r])-2):
            new_sum = get_hourglass_sum(arr,r,c)
            if new_sum > max_sum:
                max_sum = new_sum
    
    print(max_sum)
    

  • Hướng dẫn growth in 2 dimensions hackerrank c++ - tăng trưởng theo 2 chiều hackerrank c ++

    6 ngày trước+ 0 bình luận+ 0 comments

    if __name__ == '__main__':
        arr = []
        for _ in range(6):
            arr.append(list(map(int, input().rstrip().split())))
        maxim = - 100   
        for i in range(1,5):
            for j in range(1,5):
                suma =  arr[i-1][j-1] + arr[i-1][j] + arr[i-1][j+1] + arr[i][j]+arr[i+1][j-1]+arr[i+1][j]+arr[i+1][j+1]
                if suma>maxim:
                    maxim = suma              
        print(maxim)
    

  • Hướng dẫn growth in 2 dimensions hackerrank c++ - tăng trưởng theo 2 chiều hackerrank c ++

    7 ngày trước+ 0 bình luận+ 0 comments

    if __name__ == '__main__':
        arr = []
        for _ in range(6):
            arr.append(list(map(int, input().rstrip().split())))
        def sub_arr_sum(arr):
            return sum(arr[0]) + arr[1][1] + sum(arr[2])
        print(max([sub_arr_sum([row[j: j+3] for row in arr[i: i+3]]) for j in range(4) for i in range(4)]))
    

  • Hướng dẫn growth in 2 dimensions hackerrank c++ - tăng trưởng theo 2 chiều hackerrank c ++

    1 tuần trước+ 0 bình luận+ 0 comments

    private static int maximumHourGlass(List<List<Integer>> arr) {
            List<Integer> maxes = new ArrayList<>();
            int looper = 1;
            int rowp = 0;
            int colp = 0;
            int sum = 0;
            for(int i = 0; i < 6;){//loop outer i
                for(int j = 0; j < 6;) {//loop inner j                
                    if(looper <= 9) {//if <= 9 iterations
                        //System.out.println(i+","+j);                    
                        if(looper != 4 && looper != 6){//if   not 4 or 6
                            sum += arr.get(i).get(j);//add to sum
                        }
                        if(looper % 3 == 0) {//if module 3 == 0
                            i++;//move row down
                            j = colp - 1;
                        }
                        j++;                    
                        looper++;
                    }
                    else{    
                        //decide how to move
                        //System.out.println("rowp:"+ rowp +" | colp:"+colp);
                        if(colp <= 2){
                            i = rowp;
                            colp++;
                            j = colp;
                        }
                        else if(rowp <= 2) {//else if row pointer < 4 add 1 to row pointer
                            colp = j = 0;
                            rowp++;
                            i = rowp;
                        }
                        else{//else
                            i = 6;
                            j = 6;//set i = 5  and j = 5;
                        }
                        looper = 1;//reset looper
                        maxes.add(sum);////add max to list of maxes
                        sum = 0;//reset sum
                        //System.out.println("rowp:"+ rowp +" | colp:"+colp);
                    }
                }// end loop
            }//end loop
            return Collections.max(maxes);
        }
    

  • Hướng dẫn growth in 2 dimensions hackerrank c++ - tăng trưởng theo 2 chiều hackerrank c ++

    2 tuần trước+ 0 bình luận+ 0 comments

    c:

        int max_sum = (7 * (-9));                   // lowest possible sum, all -9
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                int sum = 0;
                sum  = arr[i+0][j+0] + arr[i+0][j+1] + arr[i+0][j+2];   // a b c
                sum +=                         arr[i+1][j+1];                            //   d
                sum += arr[i+2][j+0] + arr[i+2][j+1] + arr[i+2][j+2];  // e f g
                
                if (sum > max_sum) max_sum = sum;
            }
        }
    
        printf("%d\n", max_sum);