Git Product home page Git Product logo

java-coding-interview's People

Contributors

dybooksit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

java-coding-interview's Issues

[코드 질문 / 코드 오류 의심] 08/BiggestColorSpots


상세 페이지 : P 246 ( 8.3.4 코딩 테스트 4 : 영역 구하기)

질문 내용 : 소스 코드 질문 (코드 오류 의심)



  • 질문 상세
    안녕하세요. 좋은 번역 해주셔서 감사하다는 말씀 먼저 드립니다.
    요구하는 문제에 대한 오류가 있는 것 같아서 글을 적게 되었습니다.
1 2 2 2 3
3 3 3 3 3
3 2 2 2 1
3 3 3 2 2
2 2 3 3 1

Color: 3 Bigest spot: 7

격자의 색을 위와 같이 고정하고 실행을 하게되면 Color: 3 Bigest spot: 7의 결과 값을 얻게 됩니다.
제가 이해하기론 Color: 3 Bigest spot: 12의 결과 값을 얻어야 바른 결과라고 생각이드는데 확인해주실 수 있으실까요?
답변주시면 너무 감사하겠습니다. ( _ _ )


  • 내가 생각하는 답안
    원본파일 BiggestColorSpots.computeColorSpot(int i, int j, int cols, int rows, int a[][], int color)에서 조건식을 아래와 같이 변경해야한다고 생각합니다.
 private void computeColorSpot(int i, int j, int cols, int rows, int a[][], int color) {
        a[i][j] = -a[i][j];
        currentColorSpot++;

        if (i >= 1 && a[i - 1][j] == color) { // 변경부분
            computeColorSpot(i - 1, j, cols, rows, a, color);
        }

        if ((i + 1) < rows && a[i + 1][j] == color) {
            computeColorSpot(i + 1, j, cols, rows, a, color);
        }

        if (j >= 1 && a[i][j - 1] == color) { // 변경부분
            computeColorSpot(i, j - 1, cols, rows, a, color);
        }

        if ((j + 1) < cols && a[i][j + 1] == color) {
            computeColorSpot(i, j + 1, cols, rows, a, color);
        }
    }


* 문제가 되는 소스코드

1. 격자 고정 Main소스 코드

package coding.challenge;

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        int cols = 5;
        int rows = 5;
        int color = 3; // 각 영역은 1, 2, 3번 색을 가질 수 있습니다.

        Random rnd = new Random();

        int[][] a = new int[rows][cols];

        a[0][0] = 1;
        a[0][1] = 2;
        a[0][2] = 2;
        a[0][3] = 2;
        a[0][4] = 3;

        a[1][0] = 3;
        a[1][1] = 3;
        a[1][2] = 3;
        a[1][3] = 3;
        a[1][4] = 3;

        a[2][0] = 3;
        a[2][1] = 2;
        a[2][2] = 2;
        a[2][3] = 2;
        a[2][4] = 1;

        a[3][0] = 3;
        a[3][1] = 3;
        a[3][2] = 3;
        a[3][3] = 2;
        a[3][4] = 2;

        a[4][0] = 2;
        a[4][1] = 2;
        a[4][2] = 3;
        a[4][3] = 3;
        a[4][4] = 1;

        // 화면에 영역 색 표시
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                System.out.print(a[i][j] + "  ");
            }
            System.out.println();
        }

        BiggestColorSpots spots = new BiggestColorSpots();
        spots.determineBiggestColorSpot(cols, rows, a);
    }
}

2. BiggestColorSpots.java (해당 파일은 수정하지 않았습니다.)

package coding.challenge;

public class BiggestColorSpots {
    private int currentColorSpot;

    void determineBiggestColorSpot(int cols, int rows, int a[][]) {
        if (a == null) {
            throw new IllegalArgumentException("The matrix a cannot be null");
        }

        if (cols <= 0 || rows <= 0) {
            throw new IllegalArgumentException("Grid cannot have 0 rows and/or cols!");
        }

        int biggestColorSpot = 0;
        int color = 0;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (a[i][j] > 0) {
                    currentColorSpot = 0;
                    computeColorSpot(i, j, cols, rows, a, a[i][j]);

                    if (currentColorSpot > biggestColorSpot) {
                        biggestColorSpot = currentColorSpot;
                        color = a[i][j] * (-1);
                    }
                }
            }
        }
        System.out.println("\nColor: " + color + " Bigest spot: " + biggestColorSpot);
    }

    private void computeColorSpot(int i, int j, int cols, int rows, int a[][], int color) {
        a[i][j] = -a[i][j];
        currentColorSpot++;

        if (i > 1 && a[i - 1][j] == color) {
            computeColorSpot(i - 1, j, cols, rows, a, color);
        }

        if ((i + 1) < rows && a[i + 1][j] == color) {
            computeColorSpot(i + 1, j, cols, rows, a, color);
        }

        if (j > 1 && a[i][j - 1] == color) {
            computeColorSpot(i, j - 1, cols, rows, a, color);
        }

        if ((j + 1) < cols && a[i][j + 1] == color) {
            computeColorSpot(i, j + 1, cols, rows, a, color);
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.