You will be fine

<Algorithm> 15. 10973번 이전순열

by BFine
반응형

1. 10973번 이전순열

  • 다음 순열과 차이는 탐색된 이전 숫자로 부터 작은 수를 찾는 것 ex) 5 7(탐색된 이전 숫자) 1 2 3(보다 작은수)    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Main {
 
    public static void main(String[] args) throws NumberFormatException, IOException {
        
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int N=Integer.parseInt(br.readLine());
        int[] input=new int[N];
 
        String in[]=br.readLine().split(" ");
        for(int i=0;i<N;i++) {
            input[i]=Integer.parseInt(in[i]);
        }
        
        if(prev(input)){
            for(int i=0;i<N;i++) {
                System.out.print(input[i]+" ");
            }
            
        } else {
            System.out.println("-1");
        }
        
    }
    
    public static boolean prev(int[] input) {
        
        int i=input.length-1;
        int j=input.length-1;
        int perv_i = 0;
        int point_i= 0;
        boolean checkValue=false;
        
        while(i>0) {
            if(input[i-1]>input[i]) {
                checkValue=true;
                perv_i=i-1;
                point_i=i;
                break;
            }
            i--;
        }
        
        if (!checkValue) {
            return checkValue;
        }
        
        
        while(j>perv_i) {
            
            if(input[perv_i]>input[j]) {
                int temp=input[perv_i];
                input[perv_i]=input[j];
                input[j]=temp;
                break;
            }
            
            j--;
        }
        
        int count=input.length-1;
        
        while(point_i<count) {
            int temp=input[point_i];
            input[point_i]=input[count];
            input[count]=temp;
            point_i++;
            count--;
        }
        
      return checkValue;    
    }    
    
}
 
cs







반응형

블로그의 정보

57개월 BackEnd

BFine

활동하기