Skip to content
This repository was archived by the owner on Jun 19, 2020. It is now read-only.

Upload Solution Codility Lesson06.Sorting #29

Open
wants to merge 17 commits into
base: hojinjava
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions .github/ISSUE_TEMPLATE/Bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
name: Bug report
about: Create a report to help us improve

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]

**Smartphone (please complete the following information):**
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]

**Additional context**
Add any other context about the problem here.
7 changes: 7 additions & 0 deletions .github/ISSUE_TEMPLATE/Custom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
name: Custom issue template
about: Describe this issue template's purpose here.

---


17 changes: 17 additions & 0 deletions .github/ISSUE_TEMPLATE/Feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: Feature request
about: Suggest an idea for this project

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
9 changes: 9 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- [Problem link](실제 url을 적어요)

## 접근 방법
자유롭게 기술

## 채점 결과
| Task Score | Correctness | Performance |
| ------------ | ------------- | ------------- |
| 00% | 00% | 00% |
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*




3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# algorithmSolution
Codility, HackerRank 등 코딩테스트 플랫폼에서 알고리즘 풀이 수련

## Log
- 2018/06/09 Lesson03,04 풀이
37 changes: 37 additions & 0 deletions hojin/Algorithm/codility/lesson05/CountDiv.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
- [CountDiv](https:app.codility.com/programmers/lessons/5-prefix_sums/count_div/)

## 접근 방법
# 첫번 째 방법
1. end와 start사이에 k가 몇번 들어가는지 구하자
2. 그리고 start와 end가 k로 나누어 떨어진다면 +1씩 해주자.

# 두번 째 방법
1. 0부터 end까지 k의 몇배수 까지 들어갈 수 있는지 구하자
2. 0부터 start까지 k의 몇배수까지 들어갈 수 있는지 구하자
3. start가 k의 배수일때는 +1를 추가한다, 이유 : 문제 제시가 start를 포함한 범위부터 구하기를 제시하기 때문
4. (1)-(2)를 해주자

## 소스코드

~~~java
public int solution(int A, int B, int K) {

return this.test(A, B, K);
}
public int test(int start, int end, int k){
int temp = end/k - start/k;
if(start%k==0){
temp+=1;
}
return temp;
}
~~~

## 개선사항

## 시간복잡도를 주링기 위해 생각한 점, 반영한 점

## 채점 결과
| Task Score | Correctness | Performance |
| ------------ | ------------- | ------------- |
| 100% | 100% | 100% |
63 changes: 63 additions & 0 deletions hojin/Algorithm/codility/lesson05/GenomicRangeQuery.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
- [GenomicRangeQuery](https://app.codility.com/programmers/lessons/5-prefix_sums/genomic_range_query/)

## 접근 방법


# 첫번 째 방법
1. 비교를 쉽게 하기 위해서 String[]을 int[]로 변환 한다.
2. 변환시 for문을 최대한 적게 돌기 위하여 탐색에 필요한 부분만 변환한다.
3. p값과 q값에서 최소 값을 찾는다.
# 두번 째 방법

## 소스코드

~~~java
public int[] solution(String s, int[] p, int[] q){
int intS[] = new int[s.length()];
int result[] = new int[p.length];

for(int i=0;i<p.length;i++){
result[i] = this.getMinOfArray(s, p[i], q[i]);
// System.out.println(result[i]);
}
return result;
}

public int parserStringToInt(char str){
switch(str){
case 'A' : return 1;
case 'C' : return 2;
case 'G' : return 3;
case 'T' : return 4;
default : System.out.println("잘못된 문자열 값이 들어왔습니다.");
return -1;
}
}
public int getMinOfArray(String str, int start, int end){
int min = 5;
int parserInt = 0;
for(int i=start;i<=end;i++){
parserInt = this.parserStringToInt(str.charAt(i));
if(min > parserInt){
min = parserInt;
if(parserInt == 1){
return 1;
}
}
}
return min;
}
~~~

## 개선필요한 사항
1. String[] ->int[] parser를 미리 하는게 좋을 것 같다. ( 중복되서 parser를 하는 듯 하다. )
2. 시간복잡도가 N*M이므로 줄여야 된다.


## 시간복잡도를 주링기 위해 생각한 점, 반영한 점
1. 탐색해야되는 길이(M)만큼 for문을 돌면 안되고, 바로 값을 알 수 있는 연산이 필요하다. 즉, 2~4라고 가정했을 때 탐색없이 답을 찾아야 한다.

## 채점 결과
| Task Score | Correctness | Performance |
| ------------ | ------------- | ------------- |
| 62% | 100% | 0% |
63 changes: 63 additions & 0 deletions hojin/Algorithm/codility/lesson05/PassingCars.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
- [PassingCars](https://app.codility.com/programmers/lessons/5-prefix_sums/passing_cars/)

## 접근 방법
# 첫번 째 방법
1. 입력 배열값 중 값이 0인 인덱스를 값으로 저장되는 배열을 만든다 int zero[]
2. 입력 배열값 중 값이 1인 인덱스를 값으로 저장되는 배열을 만든다 int one[]
3. 이슈사항
1. (P,Q)를 쌍으로 지정할 때 P < Q라는 조건이 있다
2. 이를 해결하기 위해서 (1),(2)를 작업함
3. if(zero[i] > one[j]) 일 경우 탐색을 종료하고 i++를 진행 함 (반복문). ( 각 배열의 값은 입력 배열의 인덱스를 저장하고 있다. )


# 두번 째 방법
첫번째 방법은 2중 for문이 들어가기 때문에 개선이 필요함
`P < Q 해결하기`
1. 한 쌍의 기준을 (P,Q)로 정한다
1. P는 값이 0인 입력 배열의 인덱스
2. Q는 값이 1인 입력 배열의 인덱스
2. Q의 개수를 n개로 정한다.
1. 현재 P의 인덱스보다 작은 Q값을 다 빼야 된다. ( P < Q 조건 때문 ) --> n = n-1

## 소스코드

~~~java
public int solution(int[] A) {
// write your code in Java SE 8
return this.test(A);
}

public int test(int[] a){
int zeroCnt = 0;
int oneCnt = 0;
int total = 0;
for(int i=0; i<a.length;i++){
if(a[i]==0){
zeroCnt++;
}else{
oneCnt++;
}
}
for(int i=0;i<a.length;i++){
if(total >1000000000){
return -1;
}
if(a[i]==0){
total += oneCnt;
}else{
oneCnt--;
}
}
// System.out.println(total);
return total;
}
~~~

## 개선사항

## 시간복잡도를 주링기 위해 생각한 점, 반영한 점

## 채점 결과
| Task Score | Correctness | Performance |
| ------------ | ------------- | ------------- |
| 100% | 100% | 100% |
63 changes: 63 additions & 0 deletions hojin/Algorithm/codility/lesson06/Distinct.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
- [Distinct](https://app.codility.com/programmers/lessons/6-sorting/distinct/)

## 접근 방법
# 첫번 째 방법
1. 입력 배열값 중 값이 0인 인덱스를 값으로 저장되는 배열을 만든다 int zero[]
2. 입력 배열값 중 값이 1인 인덱스를 값으로 저장되는 배열을 만든다 int one[]
3. 이슈사항
1. (P,Q)를 쌍으로 지정할 때 P < Q라는 조건이 있다
2. 이를 해결하기 위해서 (1),(2)를 작업함
3. if(zero[i] > one[j]) 일 경우 탐색을 종료하고 i++를 진행 함 (반복문). ( 각 배열의 값은 입력 배열의 인덱스를 저장하고 있다. )


# 두번 째 방법
첫번째 방법은 2중 for문이 들어가기 때문에 개선이 필요함
`P < Q 해결하기`
1. 한 쌍의 기준을 (P,Q)로 정한다
1. P는 값이 0인 입력 배열의 인덱스
2. Q는 값이 1인 입력 배열의 인덱스
2. Q의 개수를 n개로 정한다.
1. 현재 P의 인덱스보다 작은 Q값을 다 빼야 된다. ( P < Q 조건 때문 ) --> n = n-1

## 소스코드

~~~java
public int solution(int[] A) {
// write your code in Java SE 8
return this.test(A);
}

public int test(int[] a){
int zeroCnt = 0;
int oneCnt = 0;
int total = 0;
for(int i=0; i<a.length;i++){
if(a[i]==0){
zeroCnt++;
}else{
oneCnt++;
}
}
for(int i=0;i<a.length;i++){
if(total >1000000000){
return -1;
}
if(a[i]==0){
total += oneCnt;
}else{
oneCnt--;
}
}
// System.out.println(total);
return total;
}
~~~

## 개선사항

## 시간복잡도를 주링기 위해 생각한 점, 반영한 점

## 채점 결과
| Task Score | Correctness | Performance |
| ------------ | ------------- | ------------- |
| 100% | 100% | 100% |
63 changes: 63 additions & 0 deletions hojin/Algorithm/codility/lesson06/GenomicRangeQuery.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
- [GenomicRangeQuery](https://app.codility.com/programmers/lessons/5-prefix_sums/genomic_range_query/)

## 접근 방법


# 첫번 째 방법
1. 비교를 쉽게 하기 위해서 String[]을 int[]로 변환 한다.
2. 변환시 for문을 최대한 적게 돌기 위하여 탐색에 필요한 부분만 변환한다.
3. p값과 q값에서 최소 값을 찾는다.
# 두번 째 방법

## 소스코드

~~~java
public int[] solution(String s, int[] p, int[] q){
int intS[] = new int[s.length()];
int result[] = new int[p.length];

for(int i=0;i<p.length;i++){
result[i] = this.getMinOfArray(s, p[i], q[i]);
// System.out.println(result[i]);
}
return result;
}

public int parserStringToInt(char str){
switch(str){
case 'A' : return 1;
case 'C' : return 2;
case 'G' : return 3;
case 'T' : return 4;
default : System.out.println("잘못된 문자열 값이 들어왔습니다.");
return -1;
}
}
public int getMinOfArray(String str, int start, int end){
int min = 5;
int parserInt = 0;
for(int i=start;i<=end;i++){
parserInt = this.parserStringToInt(str.charAt(i));
if(min > parserInt){
min = parserInt;
if(parserInt == 1){
return 1;
}
}
}
return min;
}
~~~

## 개선필요한 사항
1. String[] ->int[] parser를 미리 하는게 좋을 것 같다. ( 중복되서 parser를 하는 듯 하다. )
2. 시간복잡도가 N*M이므로 줄여야 된다.


## 시간복잡도를 주링기 위해 생각한 점, 반영한 점
1. 탐색해야되는 길이(M)만큼 for문을 돌면 안되고, 바로 값을 알 수 있는 연산이 필요하다. 즉, 2~4라고 가정했을 때 탐색없이 답을 찾아야 한다.

## 채점 결과
| Task Score | Correctness | Performance |
| ------------ | ------------- | ------------- |
| 62% | 100% | 0% |
Loading