Skip to content

9주차 알고리즘 문제 풀이 - 황희정 - #36

Open
hwangheejung wants to merge 4 commits into
mainfrom
heejung
Open

hwangheejung wants to merge 4 commits into
mainfrom
heejung

Conversation

@hwangheejung

@hwangheejung hwangheejung commented Aug 27, 2024 •

Copy link
Copy Markdown
Contributor

연속합

  • max값을 저장할 함수 하나를 만든다

  • vector와 sum을 더한것을 비교하면서 가장 큰 값을 구한다

피보나치 수열

  • c++로 푼다음 자바로 옮겨서 풀어봤다
  • 자바에는 Pair가 없어서 Pair를 하나 만들어서 진행하였다
  • List에 값들을 X값 끼리 Y 값 끼리 더해준다.

이친수

  • 초기 2번째 index까지는 세팅을 해준다
  • dp에 맞는 식을 따라 arr를 채워준다

정수삼각형

  • result에 값을 dp 식에 맞춰서 만들어간다
  • max값을 출력한다.

@hwangheejung hwangheejung self-assigned this Aug 27, 2024

@IToriginal IToriginal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Java 화이팅! 👏

public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제 풀이에 사용 되지 않은 코드로 삭제가 필요해 보여요.. 🥹

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringBuilder sb = new StringBuilder(); 이 부분 입니당

package _1932;

import java.io.*;
import java.sql.Array;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제 풀이에 사용 되지 않은 코드로 삭제가 필요해 보여요.. 🥹

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import java.sql.Array; 이 부분 입니당

@@ -0,0 +1,45 @@
package _1932;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

패키지 경로가 이상한데 한번 확인이 필요해 보여요!

package heejung.boj.DP._1932; 가 되어야 해요!

Comment on lines +12 to +20
int n = Integer.parseInt(br.readLine());
int [][] arr = new int[n][n];
StringTokenizer st;
for(int i=0;i<n;i++){
st = new StringTokenizer(br.readLine());
for(int j=0;j<=i;j++){
arr[i][j] = Integer.parseInt(st.nextToken());
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 StringTokenizer를 사용하지 않고 배열을 활용하고, 입력 받는 라인을 곧 바로 stream을 활용해 input 값을 초기화하곤 해요. 그러면 한번에 처리가 가능해서 생산성도 높아지고 가독성도 높아지더라구요!

아래의 코드는 제가 사용하는 방법인데 스타일에 따라 참고 해보셔도 좋을 것 같습니다! (코드가 틀렸다는 것은 아니에요!)

int n = Integer.parseInt(br.readLine()); // Input Line 1 

int[][] arr = new int[n][n];
for (int i = 0; i < n; i++) { // n번 반복
    arr[i] = Arrays.stream(br.readLine().split(" ")) // 1) Input을 통해 공백(" ")을 기준으로 나누기
                            .mapToInt(Integer::parseInt) // 2) 나눈 값을 Integer 타입으로 매핑(String -> Integer)
                            .toArray(); // 배열로 만들기
}

Comment on lines +24 to +39
for(int i=1;i<n;i++){
for(int j=0;j<=i;j++) {
if (j == 0) {
result[i][j] = result[i-1][j]+arr[i][j];
} else if(j==i){
result[i][j] = result[i-1][j-1]+arr[i][j];
} else{
result[i][j] = Math.max(result[i - 1][j] + arr[i][j],result[i-1][j-1]+arr[i][j] );


if(max<result[i][j]){
max = result[i][j];
}
}

}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드를 한번 점검해 볼 필요가 있을 것 같아요!(띄워쓰기, 코드 다음 라인 줄 맞춤..)
else 문 안에서 최대 값을 한번 result[i][j]에 초기화 하고 있기 때문에 매번 else 문 실행에서 if 조건을 확인할 필요가 없어 보여요.
그래서 else 문 밖(1라인의 for문이 돌았을 때)에서 라인의 최대값이 저장된 result[i][j]를 아래와 같이 max값과 비교해서 값을 변경해줘도 될 것 같다는 생각이 들었어요!

for (int i = 1; i < n; i++) {
    for (int j = 0; j <= i; j++) {
        if (j == 0) {
            result[i][j] = result[i - 1][j] + arr[i][j];
        } else if (j == i) {
            result[i][j] = result[i - 1][j - 1] + arr[i][j];
        } else {
            result[i][j] = Math.max(result[i - 1][j] + arr[i][j], result[i - 1][j - 1] + arr[i][j]);
        }
        max = Math.max(max, result[i][j]);
    }
}

추가로 위에서 아래로 문제를 해결하신 것으로 보이는데 저는 아래에서 위로 문제를 해결하여서 저와 다른 방향의 문제 풀이 방법이라 참고하시면 서로 도움될 것 같아요!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants