링크 : https://programmers.co.kr/learn/courses/30/lessons/77485
import java.util.*;
import java.util.List;
class Solution {
public int[] solution(int rows, int columns, int[][] queries) {
int[] answer = {};
int[][] square = new int[rows][columns];
//세팅
int firstColumn = 1;
for(int i=0; i<rows; i++)
for(int j=0; j<columns; j++){
square[i][j] = firstColumn++;
}
//최소값 배열
List<Integer> minArray = new ArrayList<>();
for (int[] query : queries) {
//회전 행열의 변수 값
List<Integer> values = new ArrayList<>();
int lX = query[0];
int lY = query[1];
int rX = query[2];
int rY = query[3];
//주어진 2점 세팅
Point lP = new Point(lX, lY);
Point rP = new Point(rX, rY);
//사각형
Square sq = new Square(lP, rP);
//현재 시점
Point nowP = new Point(lP.getX(), lP.getY());
//회전
move(nowP, sq.rightTop, "right", square, values);
move(nowP, sq.rightBottom, "down", square, values);
move(nowP, sq.leftBottom, "left", square, values);
move(nowP, sq.leftTop, "up", square, values);
//최소값 구하기
minArray.add(values.stream().mapToInt(v->v).min().getAsInt());
}
// 정답 변환
answer = minArray.stream().mapToInt(i->i).toArray();
return answer;
}
private void move( Point nowP, Point targetP, String direction, int[][] square, List<Integer> values) {
// ; 조건; 움직임
for(;!nowP.equals(targetP);nowP.move(direction)){
//행렬값 추가
values.add(square[nowP.getX()-1][nowP.getY()-1]);
//이전 루프에 추가된 행렬값이 있다면 행렬값 교환
if(values.size() > 1) square[nowP.getX()-1][nowP.getY()-1] = values.get(values.size()-2);
}
//마지막 행렬값 교환
if(direction.equals("up")){
square[nowP.getX()-1][nowP.getY()-1] = values.get(values.size()-1);
}
}
class Square {
Point leftTop;
Point leftBottom;
Point rightBottom;
Point rightTop;
public Square(Point left, Point right) {
this.leftTop = left;
this.rightBottom = right;
this.rightTop = new Point( left.getX(), right.getY() );
this.leftBottom = new Point( right.getX(), left.getY());
}
@Override
public String toString() {
return "Square{" +
"leftTop=" + leftTop +
", leftBottom=" + leftBottom +
", rightBottom=" + rightBottom +
", rightTop=" + rightTop +
'}';
}
}
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point(Point other) {
this.x = other.x;
this.y = other.y;
}
public Point move(String direction){
Point point = new Point(this);
if(direction.equals("right")) moveRight();
if(direction.equals("down")) moveDown();
if(direction.equals("left")) moveLeft();
if(direction.equals("up")) moveUp();
return point;
}
public Point moveRight(){
this.y++;
return this;
}
public Point moveDown(){
this.x++;
return this;
}
public Point moveLeft(){
if(this.y > 0)
this.y--;
return this;
}
public Point moveUp(){
if(this.x > 0)
this.x--;
return this;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Point)) return false;
Point point = (Point) o;
return x == point.x && y == point.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
}
'교육 > 코테' 카테고리의 다른 글
[프로그래머스]JAVA 기능개발 (0) | 2022.07.01 |
---|---|
[프로그래머스]JAVA 124 나라의 숫자 (0) | 2022.07.01 |
[프로그래머스]JAVA 로또의 최고 순위와 최저 순위 (0) | 2022.06.29 |
[프로그래머스]JAVA 직사각형 별찍기 (0) | 2022.06.29 |
[프로그래머스]JAVA x만큼 간격이 있는 n개의 숫자 (0) | 2022.06.29 |