Skip to content

feat: 3주차 나머지 문제 #6

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

Merged
merged 1 commit into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/main/java/sgyj/inflearn/seunggu/week3/Solution29.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package sgyj.inflearn.seunggu.week3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Solution29 {
/**
* @title : 연속된 자연수의 합
* @description : N입력으로 양의 정수 N이 입력되면 2개 이상의 연속된 자연수의 합으로 정수 N을 표현하는 방법의 가짓수를 출력하는 프로그램을 작성하세요.
* 만약 N=15이면
* 7+8=15
* 4+5+6=15
* 1+2+3+4+5=15
* 와 같이 총 3가지의 경우가 존재한다.
* @input : 첫 번째 줄에 양의 정수 N(7<=N<1000)이 주어집니다.
* @output : 첫 줄에 총 경우수를 출력합니다.
*/
public static void main ( String[] args ) {
try ( BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) ) ) {
System.out.println(solution( reader ));
} catch ( IOException e ) {
e.printStackTrace();
}
}

static int solution ( BufferedReader reader ) throws IOException {
int number = Integer.parseInt( reader.readLine() );
int result = 0;
for(int i=1; i<number; i++) {
int sum = i;
for(int j=i+1; j <number; j++) {
sum += j;
if(sum > number) {
break;
}
if(sum == number) {
result++;
}
}
}
return result;
}
}
66 changes: 66 additions & 0 deletions src/main/java/sgyj/inflearn/seunggu/week3/Solution30.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package sgyj.inflearn.seunggu.week3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Solution30 {
/**
* @title : 최대 길이 연속부분수열
* @description : 0과 1로 구성된 길이가 N인 수열이 주어집니다. 여러분은 이 수열에서 최대 k번을 0을 1로 변경할 수 있습니다. 여러분이 최대 k번의 변경을 통해 이 수열에서 1로만 구성된 최대 길이의 연속부분수열을 찾는 프로그램을 작성하세요.
* 만약 길이가 길이가 14인 다음과 같은 수열이 주어지고 k=2라면
* 1 1 0 0 1 1 0 1 1 0 1 1 0 1
* 여러분이 만들 수 있는 1이 연속된 연속부분수열은
* 1 1 1 1 1 1 1 1
* 이며 그 길이는 8입니다.
* @input : 첫 번째 줄에 수열의 길이인 자연수 N(5<=N<100,000)이 주어집니다.
* 두 번째 줄에 N길이의 0과 1로 구성된 수열이 주어집니다.
* @output : 첫 줄에 최대 길이를 출력하세요.
*/
public static void main ( String[] args ) {
try ( BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) ) ) {
System.out.println(solution( reader ));
} catch ( IOException e ) {
e.printStackTrace();
}
}

static int solution ( BufferedReader reader ) throws IOException {
String[] n = reader.readLine().split( " " );
int length = Integer.parseInt( n[0] );
int k = Integer.parseInt( n[1] );
int result = 0;
int[] array = Arrays.stream( reader.readLine().split( " " ) ).mapToInt( Integer::parseInt ).toArray();
int[] zeroIndexArray = getZeroIndexArray( array );
int startIndex = 0;
int zeroCount = 0;
int sumLength = 0;
int zeroIndex = 0;
while(startIndex < length - k) {
for(int i=startIndex; i< length; i++) {
if( array[i] == 0 ) {
if(zeroCount == k) {
break;
}
zeroCount++;
}
sumLength++;
}
result = Math.max( result, sumLength );
sumLength=0;
zeroCount = 0;
startIndex = zeroIndexArray[zeroIndex++] + 1;
}

return result;
}

private static int[] getZeroIndexArray ( int[] array ) {
StringBuilder stringBuilder = new StringBuilder();
for ( int i = 0; i < array.length; i++ ) {
if( array[i] == 0) stringBuilder.append( i ).append( " " );
}
return Arrays.stream( stringBuilder.toString().split( " " ) ).mapToInt( Integer::parseInt ).toArray();
}
}
77 changes: 77 additions & 0 deletions src/test/java/sgyj/inflearn/seunggu/week3/Solution29Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package sgyj.inflearn.seunggu.week3;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.BufferedReader;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import sgyj.inflearn.common.TestFileUtil;

@DisplayName( "연속된 자연수의 합" )
class Solution29Test {
@Test
@DisplayName( "연속된 자연수의 합 테스트 케이스 1" )
void test_case_1 () throws Exception {
// given
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution29/test_case1.txt" );
// when
int solution = Solution29.solution( reader );

// then
assertEquals( 1, solution );

}

@Test
@DisplayName( "연속된 자연수의 합 테스트 케이스 2" )
void test_case_2 () throws Exception {
// given
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution29/test_case2.txt" );
// when
int solution = Solution29.solution( reader );

// then
assertEquals( 1, solution );

}

@Test
@DisplayName( "연속된 자연수의 합 테스트 케이스 3" )
void test_case_3 () throws Exception {
// given
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution29/test_case3.txt" );
// when
int solution = Solution29.solution( reader );

// then
assertEquals( 3, solution );

}

@Test
@DisplayName( "연속된 자연수의 합 테스트 케이스 4" )
void test_case_4 () throws Exception {
// given
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution29/test_case4.txt" );
// when
int solution = Solution29.solution( reader );

// then
assertEquals( 5, solution );

}

@Test
@DisplayName( "연속된 자연수의 합 테스트 케이스 5" )
void test_case_5 () throws Exception {
// given
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution29/test_case5.txt" );
// when
int solution = Solution29.solution( reader );

// then
assertEquals( 8, solution );

}

}
77 changes: 77 additions & 0 deletions src/test/java/sgyj/inflearn/seunggu/week3/Solution30Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package sgyj.inflearn.seunggu.week3;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.BufferedReader;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import sgyj.inflearn.common.TestFileUtil;

@DisplayName( "최대 길이 연속부분수열" )
class Solution30Test {
@Test
@DisplayName( "최대 길이 연속부분수열 테스트 케이스 1" )
void test_case_1 () throws Exception {
// given
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution30/test_case1.txt" );
// when
int solution = Solution30.solution( reader );

// then
assertEquals( 8, solution );

}

@Test
@DisplayName( "최대 길이 연속부분수열 테스트 케이스 2" )
void test_case_2 () throws Exception {
// given
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution30/test_case2.txt" );
// when
int solution = Solution30.solution( reader );

// then
assertEquals( 13, solution );

}

@Test
@DisplayName( "최대 길이 연속부분수열 테스트 케이스 3" )
void test_case_3 () throws Exception {
// given
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution30/test_case3.txt" );
// when
int solution = Solution30.solution( reader );

// then
assertEquals( 15, solution );

}

@Test
@DisplayName( "최대 길이 연속부분수열 테스트 케이스 4" )
void test_case_4 () throws Exception {
// given
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution30/test_case4.txt" );
// when
int solution = Solution30.solution( reader );

// then
assertEquals( 36, solution );

}

@Test
@DisplayName( "최대 길이 연속부분수열 테스트 케이스 5" )
void test_case_5 () throws Exception {
// given
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution30/test_case5.txt" );
// when
int solution = Solution30.solution( reader );

// then
assertEquals( 50, solution );

}

}
1 change: 1 addition & 0 deletions src/test/resources/static/solution29/test_case1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12
1 change: 1 addition & 0 deletions src/test/resources/static/solution29/test_case2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
19
1 change: 1 addition & 0 deletions src/test/resources/static/solution29/test_case3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
120
1 change: 1 addition & 0 deletions src/test/resources/static/solution29/test_case4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
720
1 change: 1 addition & 0 deletions src/test/resources/static/solution29/test_case5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
450
2 changes: 2 additions & 0 deletions src/test/resources/static/solution30/test_case1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
14 2
1 1 0 0 1 1 0 1 1 0 1 1 0 1
2 changes: 2 additions & 0 deletions src/test/resources/static/solution30/test_case2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
20 2
1 0 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 1 0 1
2 changes: 2 additions & 0 deletions src/test/resources/static/solution30/test_case3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
100 3
1 0 0 0 0 1 0 0 0 1 0 1 1 0 0 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 1 1 0 1 1 0 0 1 0 0 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 1
2 changes: 2 additions & 0 deletions src/test/resources/static/solution30/test_case4.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/test/resources/static/solution30/test_case5.txt

Large diffs are not rendered by default.