문제 풀이 및 개발 공간

[백준] 25706번 자전거 묘기 (silver 4 본문

백준공부/java

[백준] 25706번 자전거 묘기 (silver 4

gomduri43 2024. 3. 15. 12:05

import java.io.*;
import java.util.*;

public class Main{
    public static void main(String[]args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
        int n=Integer.parseInt(br.readLine());
        StringTokenizer st=new StringTokenizer(br.readLine());
        int[] height=new int[n+1];
        for(int i=1; i<=n; i++){
            height[i]=Integer.parseInt(st.nextToken());
        }
        height[n]=1;
        for(int i=n-1; i>=1; i--){
            if(height[i]==0){
                height[i]=height[i+1]+1;
            }
            else{
                if(i+1+height[i]<=n){
                    height[i]=height[i+1+height[i]]+1;
                }
                else{
                    height[i]=1;
                }
            }
        }
       for(int i=1; i<=n; i++){
           bw.write(height[i]+" ");
       }
        bw.flush();
    }

}
import java.util.*;
import java.io.*;

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder("");
        int n=Integer.valueOf(br.readLine());
        int[] num=new int[n+1];
        StringTokenizer st=new StringTokenizer(br.readLine());
        for(int i=1; i<=n; i++){
            num[i]=Integer.valueOf(st.nextToken());
        }
        int[] dp=new int[n+1];
        dp[n]=1;
        for(int i=n-1; i>=1; i--){
            int t=i+num[i]+1;
            if(t>n){
                dp[i]=1;
            }
            else{
                dp[i]=dp[t]+1;
            }
        }
        for(int i=1; i<=n; i++){
            sb.append(dp[i]+" ");
        }
        System.out.println(sb.toString());
    }
}

//24.03.05코드 간결해진것 같다.