-
Notifications
You must be signed in to change notification settings - Fork 1
feat: 3주차 - BOJ_2240 자두나무 [김정호] #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Unoguna
wants to merge
1
commit into
main
Choose a base branch
from
feat/boj2240-Unoguna
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package week03.Unoguna; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
|
|
||
| public class BOJ_2240 { | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
| StringBuilder sb = new StringBuilder(); | ||
|
|
||
| String[] TW = (br.readLine()).split(" "); | ||
| int T = Integer.parseInt(TW[0]); | ||
| int W = Integer.parseInt(TW[1]); | ||
|
|
||
| //dp[w][t]에는 자두가 t번 떨어지는 동안 w번 위치를 바꿨을 때 얻을 수 있는 자두의 최대 개수. | ||
| int[][] dp = new int[W + 1][T + 1]; | ||
|
|
||
| //t=0인 경우 자두가 한번도 떨어지지 않았기 때문에 + index가 -1이 되기 때문에 고려X | ||
| for(int t = 1; t <= T; t++){ | ||
| int drop_idx = Integer.parseInt(br.readLine()); | ||
|
|
||
| //w=0인 경우는 index가 -1이 될 수 있기 때문에 따로 계산 | ||
| if(drop_idx == 1) dp[0][t] = dp[0][t - 1] + 1; //1번 나무에 자두가 떨어짐 | ||
| else dp[0][t] = dp[0][t-1]; //2번 나무에 자두가 떨어짐 | ||
|
|
||
| //자두가 t번 떨어졌을 때 자두는 최대 min(t, W)번 움직일 수 있다. | ||
| int max_w=Math.min(t, W); | ||
| for(int w = 1; w <= max_w; w++){ | ||
| //점화식 | ||
| if(drop_idx == 1){ //1번 나무에 자두가 떨어짐 | ||
| if(w%2 == 0){ //자두의 현재 위치에 자두가 떨어질 때 | ||
| dp[w][t] = Math.max(dp[w][t-1], dp[w-1][t-1]) + 1; | ||
| } | ||
| else{ //현재 위치에 자두가 떨어지지 않았을 때 | ||
| dp[w][t] = Math.max(dp[w][t-1], dp[w-1][t-1]); | ||
| } | ||
| } | ||
| else{ //2번 나무에 자두가 떨어짐 | ||
| if(w%2 == 1){ //자두의 현재 위치에 자두가 떨어질 때 | ||
| dp[w][t] = Math.max(dp[w][t-1], dp[w-1][t-1]) + 1; | ||
| } | ||
| else{ //현재 위치에 자두가 떨어지지 않았을 때 | ||
| dp[w][t] = Math.max(dp[w][t-1], dp[w-1][t-1]); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| //자두가 다 떨어졌을 때 얻을 수 있는 자두의 최대 개수 계산 | ||
| int answer = 0; | ||
| for(int w=0; w<W+1; w++){ | ||
| answer = Math.max(answer, dp[w][T]); | ||
| } | ||
|
|
||
| System.out.println(answer); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StringTokenizer 사용하면 공백 기준으로 토큰을 자동 분리할 수 있어서 split보다 메모리 사용량이 조금 더 효율적이고 속도도 약간 빠른 편이라고 알고 있습니다!!