Light Blue Pointer
본문 바로가기
Coding Test

[프로그래머스] n^2 배열 자르기

by Greedy 2024. 2. 29.

문제 주소

https://school.programmers.co.kr/learn/courses/30/lessons/87390

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제 설명

문제 설명

정수 n, left, right가 주어집니다. 다음 과정을 거쳐서 1차원 배열을 만들고자 합니다.

  1. n행 n열 크기의 비어있는 2차원 배열을 만듭니다.
  2. i = 1, 2, 3, ..., n에 대해서, 다음 과정을 반복합니다.
    • 1행 1열부터 i행 i열까지의 영역 내의 모든 빈 칸을 숫자 i로 채웁니다.
  3. 1행, 2행, ..., n행을 잘라내어 모두 이어붙인 새로운 1차원 배열을 만듭니다.
  4. 새로운 1차원 배열을 arr이라 할 때, arr[left], arr[left+1], ..., arr[right]만 남기고 나머지는 지웁니다.

정수 n, left, right가 매개변수로 주어집니다. 주어진 과정대로 만들어진 1차원 배열을 return 하도록 solution 함수를 완성해주세요.


제한사항

  • 1 ≤ n ≤ 10
  • 7
  • 0 ≤ left ≤ right < n
  • 2
  • right - left < 10
  • 5

입출력 예

n left right result

3 2 5 [3,2,2,3]
4 7 14 [4,3,3,3,4,4,4,4]

입출력 예 설명

입출력 예 #1

  • 다음 애니메이션은 주어진 과정대로 1차원 배열을 만드는 과정을 나타낸 것입니다.

입출력 예 #2

  • 다음 애니메이션은 주어진 과정대로 1차원 배열을 만드는 과정을 나타낸 것입니다.

 

제출 코드

class Solution {
    public int[] solution(int n, long left, long right) {
        
        int length = (int)(right-left)+1;
        long[] answer = new long[length] ;
        
        for(int i = 0; i<answer.length; i++){
            
            long row = (left+(long)i)/(long)n;
            long col = (left+(long)i)%(long)n;
            if(row>=col){
                answer[i] = row;
            }else{
                answer[i] = col;
            }
            
        }
        
        int[] ans = new int[answer.length] ;
         for(int i = 0; i<answer.length; i++){
            ans[i] = (int)answer[i]+1;
        }   

        return ans;
    }
}

 

풀이과정

아이디어만 알면 간단한 문제이다!

  1. 인덱스의 값을 구해보기
  2. right-left 크기의 배열 만들기!

인덱스의 값을 어떻게 구할까~~

1 2 3

2 2 3

3 3 3

1,1→1

1,2→2

행과 열중에 더 큰거 고르는거네!

 

그러면 일차원 배열로 폈을때는

1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3

이렇게 됨

row = index/n

column = index%n

index = left + i

class Solution {
    public int[] solution(int n, long leftL, long rightL) {
        int left = (int)leftL;
        int right = (int)rightL;
        
        int[] answer = new int[right-left+1] ;
        for(int i = 0; i<answer.length; i++){
            answer[i] = ((left+i)/n)>=((left+i)%n)?(left+i)/n:(left+i)%n;
        }
        return answer;
    }
}
테스트 1
입력값 〉	3, 2, 5
기댓값 〉	[3, 2, 2, 3]
실행 결과 〉	실행한 결괏값 [2,1,1,2]이 기댓값 [3,2,2,3]과 다릅니다.
테스트 2
입력값 〉	4, 7, 14
기댓값 〉	[4, 3, 3, 3, 4, 4, 4, 4]
실행 결과 〉	실행한 결괏값 [3,2,2,2,3,3,3,3]이 기댓값 [4,3,3,3,4,4,4,4]과 다릅니다.
class Solution {
    public int[] solution(int n, long leftL, long rightL) {
        int left = (int)leftL;
        int right = (int)rightL;
        
        int[] answer = new int[right-left+1] ;
        for(int i = 0; i<answer.length; i++){
            answer[i] = ((left+i)/n)>=((left+i)%n)?(left+i)/n:(left+i)%n;
        }
         for(int i = 0; i<answer.length; i++){
            answer[i] += 1;
        }   

        return answer;
    }
}

테스트 1 〉 통과 (4.54ms, 90.5MB)

테스트 2 〉 통과 (3.80ms, 104MB)
테스트 3 〉 통과 (4.64ms, 111MB)
테스트 4 〉 통과 (0.02ms, 74.1MB)
테스트 5 〉 통과 (0.03ms, 72.9MB)
테스트 6 〉 통과 (2.89ms, 99.4MB)
테스트 7 〉 통과 (3.51ms, 103MB)
테스트 8 〉 통과 (3.61ms, 106MB)
테스트 9 〉 통과 (5.14ms, 88MB)
테스트 10 〉 통과 (3.76ms, 110MB)
테스트 11 〉 통과 (4.89ms, 88.4MB)
테스트 12 〉 실패 (4.46ms, 101MB)
테스트 13 〉 실패 (4.78ms, 110MB)
테스트 14 〉 실패 (3.05ms, 93.3MB)
테스트 15 〉 실패 (2.84ms, 106MB)
테스트 16 〉 실패 (2.98ms, 93.3MB)
테스트 17 〉 실패 (3.67ms, 96MB)
테스트 18 〉 실패 (5.38ms, 108MB)
테스트 19 〉 실패 (2.84ms, 101MB)
테스트 20 〉 실패 (2.66ms, 101MB)

long으로 다시 바꿔봄

class Solution {
    public int[] solution(int n, long leftL, long rightL) {
        
        long[] answer = new long[right-left+1L] ;
        
        for(int i = 0; i<answer.length; i++){
            answer[i] = ((left+(long)i)/(long)n)>=((left+(long)i)%(long)n)?(left+(long)i)/(long)n:(left+(long)i)%(long)n;
        }
        
        int[] ans = new int[answer.length] ;
         for(int i = 0; i<answer.length; i++){
            ans[i] = (int)answer[i]+1L;
        }   

        return ans;
    }
}
class Solution {
    public int[] solution(int n, long leftL, long rightL) {
        
        long[] answer = new long[right-left+(long)1] ;
        
        for(int i = 0; i<answer.length; i++){
            
            long row = (left+(long)i)/(long)n;
            long col = (left+(long)i)%(long)n;
            if(row>=col){
                answer[i] = row;
            }else{
                answer[i] = col;
            }
            
        }
        
        int[] ans = new int[answer.length] ;
         for(int i = 0; i<answer.length; i++){
            ans[i] = (int)answer[i]+1;
        }   

        return ans;
    }
}

/Solution.java:5: error: cannot find symbol

long[] answer = new long[right-left+(long)1] ;

^

symbol: variable right

location: class Solution

/Solution.java:5: error: cannot find symbol

long[] answer = new long[right-left+(long)1] ;

^

symbol: variable left

location: class Solution

/Solution.java:9: error: cannot find symbol

long row = (left+(long)i)/(long)n;

^

symbol: variable left

location: class Solution

/Solution.java:10: error: cannot find symbol

long col = (left+(long)i)%(long)n;

^

symbol: variable left

location: class Solution

class Solution {
    public int[] solution(int n, long left, long right) {
        
        long[] answer = new long[right-left+(long)1] ;
        
        for(int i = 0; i<answer.length; i++){
            
            long row = (left+(long)i)/(long)n;
            long col = (left+(long)i)%(long)n;
            if(row>=col){
                answer[i] = row;
            }else{
                answer[i] = col;
            }
            
        }
        
        int[] ans = new int[answer.length] ;
         for(int i = 0; i<answer.length; i++){
            ans[i] = (int)answer[i]+1;
        }   

        return ans;
    }
}

/Solution.java:5: error: incompatible types: possible lossy conversion from long to int

long[] answer = new long[right-left+(long)1] ;

class Solution {
    public int[] solution(int n, long left, long right) {
        
        long length = right-left;
        legnth++;
        long[] answer = new long[length] ;
        
        for(int i = 0; i<answer.length; i++){
            
            long row = (left+(long)i)/(long)n;
            long col = (left+(long)i)%(long)n;
            if(row>=col){
                answer[i] = row;
            }else{
                answer[i] = col;
            }
            
        }
        
        int[] ans = new int[answer.length] ;
         for(int i = 0; i<answer.length; i++){
            ans[i] = (int)answer[i]+1;
        }   

        return ans;
    }
}

/Solution.java:6: error: cannot find symbol

legnth++;

^

symbol: variable legnth

location: class Solution

/Solution.java:7: error: incompatible types: possible lossy conversion from long to int

long[] answer = new long[length] ;

class Solution {
    public int[] solution(int n, long left, long right) {
        
        int length = (int)(right-left)+1;
        long[] answer = new long[length] ;
        
        for(int i = 0; i<answer.length; i++){
            
            long row = (left+(long)i)/(long)n;
            long col = (left+(long)i)%(long)n;
            if(row>=col){
                answer[i] = row;
            }else{
                answer[i] = col;
            }
            
        }
        
        int[] ans = new int[answer.length] ;
         for(int i = 0; i<answer.length; i++){
            ans[i] = (int)answer[i]+1;
        }   

        return ans;
    }
}

 

통과됨~