Skip to content
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

Fastcampus #6

Open
wants to merge 6 commits into
base: main
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 .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,41 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Launch P1495",
"request": "launch",
"mainClass": "FASTCAMPUS.ALGO.D45.P1495",
"projectName": "Again2018_18c6595"
},
{
"type": "java",
"name": "Launch P5557",
"request": "launch",
"mainClass": "FASTCAMPUS.ALGO.D45.P5557",
"projectName": "Again2018_18c6595"
},
{
"type": "java",
"name": "Launch P1562",
"request": "launch",
"mainClass": "FASTCAMPUS.ALGO.D44.P1562",
"projectName": "Again2018_18c6595"
},
{
"type": "java",
"name": "Launch Template",
"request": "launch",
"mainClass": "FASTCAMPUS.ALGO.D37.Template",
"projectName": "Again2018_18c6595"
},
{
"type": "java",
"name": "Launch Main",
"request": "launch",
"mainClass": "FASTCAMPUS.ALGO.D35.Main",
"projectName": "Again2018_7b8cc72b"
},
{
"type": "java",
"name": "Launch P14502",
Expand Down
17 changes: 9 additions & 8 deletions src/FASTCAMPUS/ALGO/D05/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,28 @@ class Heap {
this.array.pop();
let hasChild = this.moveDown(idx);
while (hasChild[0] !== -1 || hasChild[1] !== -1) {
console.log(hasChild);
let left = hasChild[0];
let right = hasChild[1];
if (left !== -1 && right !== -1) { //자식 노드가 둘다 존재하면
if (this.array[left] > this.array[right]) {
if (this.array[left] > this.array[idx]) {
if (this.array[left] < this.array[right]) {
if (this.array[left] < this.array[idx]) {
this.swap(left, idx);
idx = left;
}
}
else {
if (this.array[right] > this.array[idx]) {
if (this.array[right] < this.array[idx]) {
this.swap(right, idx);
idx = right;
}
}
}
else if (left != -1) { //왼쪽만 존재하면
if (this.array[left] > this.array[idx]) {
if (this.array[left] < this.array[idx]) {
this.swap(left, idx);
idx = left;
}else {
break;
}
}
//오른쪽만 존재할 순 없다.
Expand All @@ -69,7 +70,7 @@ class Heap {
return -1;
}
let parent_idx = Math.floor(idx / 2);
if (this.array[parent_idx] < this.array[idx]) {
if (this.array[parent_idx] > this.array[idx]) {
return parent_idx;
}
return -1;
Expand All @@ -91,5 +92,5 @@ heap.insert(8);
heap.insert(5);
heap.insert(4);
heap.insert(20);
heap.pop();
heap.printAll();
console.log(heap.pop());
//heap.printAll();
2 changes: 0 additions & 2 deletions src/FASTCAMPUS/ALGO/D15/P1182.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//시간복잡도 N^M (worst 7^7)
const fs = require('fs');
class Scanner {
read
stringToken
constructor(fileName){
this.read = fs.readFileSync(fileName).toString().split('\n');
this.stringToken = [];
Expand Down
63 changes: 26 additions & 37 deletions src/FASTCAMPUS/ALGO/D15/P15651.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const fs = require('fs');
const { collapseTextChangeRangesAcrossMultipleVersions } = require('typescript');
class Scanner {
read
stringToken
constructor(fileName){
this.read = fs.readFileSync(fileName).toString().split('\n');
this.stringToken = [];
Expand All @@ -12,9 +11,9 @@ class Scanner {
this.stringToken = this.read.shift().split(" ");
} else {
return null;
}
}
return this.stringToken.shift();
}
}
return this.stringToken.shift();
}

nextNumber(){
Expand All @@ -35,43 +34,33 @@ class Scanner {
}

}

}


// 1 부터 N 까지의 자연수 중 M 개를 고른 수열
// 같은 수를 여러번 골라도 됨 : 중복가능
// 순서가 다르면 다른 수열 : 순서 YES
// 시간 복잡도 N^M
// Max N : 7 | Max M : 7
// worst case 7^7
// 시간 제한 1초
// brute force
let N;
let M;
let selected = [0,];
let stringBuilder = "";
const input = function(){
const scanner = new Scanner('./stdin.txt');
const scanner = new Scanner('stdin.txt');
let N, M, selected;
const input = function (){
N = scanner.nextNumber();
M = scanner.nextNumber();
selected = [];
}

const rec_func = function( k ){
if(k === M + 1){
for(let i = 1; i < k; i ++){
stringBuilder += (selected[i] + " ");
}
stringBuilder += `\n`;
} else {
for(let i = 1; i <= N; i ++){
selected.push(i);
rec_func(k + 1);
selected.pop();
}
const rec_func = function(k){
if(selected.length == M){
console.log(selected);
return;
}
if(k > N) return;
selected.push(k);
rec_func(k + 1);
selected.pop();
rec_func(k + 1);
}
const pro = function (){
rec_func(1);
}

const solve = function(){
input();
pro();
}
input();

rec_func(1);
console.log(stringBuilder)
solve();
11 changes: 9 additions & 2 deletions src/FASTCAMPUS/ALGO/D15/note.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ N 개의 카드를 M개 만큼 나열하는법
|---|----|-----------|---------|------|
| YES | YES| O(N^M) | O(M) | 같은수를 여러번 골라도 된다. 순서가 다르면 다른 케이스
| NO | YES | O(N!/(N-M)!)| O(M)| 같은수를 반복해선 안된다. 순서가 다르면 다른 케이스
| YES | NO| O(N^M) > | O(M)| 같은수를 반복해도 된다 / 순서가 달라도 같은 케이스
| YES | NO| under O(N^M) | O(M)| 같은수를 반복해도 된다 / 순서가 달라도 같은 케이스
| NO | NO | O(N!/M!(N-M)!)| O(M)| 같은수를 반복해선 안된다 / 순서가 달라도 같은 케이스

N과M 문제로 보는 완전탐색 케이스
Expand Down Expand Up @@ -324,4 +324,11 @@ const rec_func = function(k){
input();
rec_func(1);
console.log(stringBuilder);
```
```


완전탐색
- 방문지점 체크 안하고 모두 방문하는 경우 N^M - 중복허용, 순서 다르면 다름
- 방문지점 체크해서 체크안한지점만 방문하는경우 - 중복x, 순서 다르면 다름 - 방문지점 체크를 통해 탐색수 낮아짐
- 인덱스가 방문지점보다 같거나 큰 경우만 방문하는경우
- 인덱스
3 changes: 1 addition & 2 deletions src/FASTCAMPUS/ALGO/D15/stdin.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
5 0
-7 -3 -2 5 8
4 2
95 changes: 95 additions & 0 deletions src/FASTCAMPUS/ALGO/D17/P2470.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const fs = require('fs');
class Scanner {
constructor(fileName){
this.read = fs.readFileSync(fileName).toString().split('\n');
this.stringToken = [];
}
next(){
if(this.stringToken.length === 0){
if(this.read.length > 0){
this.stringToken = this.read.shift().split(" ");
} else {
return null;
}
}
return this.stringToken.shift();
}

nextNumber(){
const next = this.next();
if(next){
return Number(next);
} else {
return null;
}

}

nextLine(){
if(this.read.length > 0){
return this.read.shift();
} else {
return null;
}

}
}
const scanner = new Scanner('stdin.txt');
let N, A;
const input = function (){
N = scanner.nextNumber();
A = [];
for(let i = 0; i < N; i ++){
A[i] = scanner.nextNumber();
}
A.sort((a,b) => a - b);
console.log(A);
}

const low_bound = function(A,L,R,T){
let result = L - 1;
while(L <= R){
let mid = parseInt((L + R) / 2);
if( A[mid] <= T){
result = mid;
L = mid + 1;
} else {
R = mid - 1;
}
}
return result;
}
const pro = function (){
let min = Number.MAX_VALUE;
let a = 0;
let b = 0;
for(let i = 0; i < N; i ++){
let result = low_bound(A, 0, N - 1, -A[i]);
//sconsole.log(`idx : ${i}, result : ${result}`);
if(result >= 0 && result != i){
if(min > Math.abs(A[i] + A[result])){
min = Math.abs(A[i] + A[result]);
a = A[i];
b = A[result]
}
}
if(result + 1 < N && result + 1 != i){
if(min > Math.abs(A[i] + A[result + 1])){
min = Math.abs(A[i] + A[result + 1]);
a = A[i];
b = A[result + 1]
}
}
}
if(a < b){
console.log(a + " " + b);
} else {
console.log(b + " " + a);
}
}

const solve = function(){
input();
pro();
}
solve();
Loading