dybooksit / java-coding-interview Goto Github PK
View Code? Open in Web Editor NEW동양북스 『자바 코딩 인터뷰 완벽 가이드』 예제 저장소
License: MIT License
동양북스 『자바 코딩 인터뷰 완벽 가이드』 예제 저장소
License: MIT License
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);
}
}
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);
}
}
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);
}
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.