inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

자바스크립트 알고리즘 문제풀이 입문(코딩테스트 대비)

졸업선물 문제 클래스 이용해서 풀었습니다 !

323

이준호
0

완전 탐색 문제는 어렵네요 강의 보기 전에 하나씩 풀어보는데 졸업선물 문제는 기억에 남아서 올려봅니다. 

그리고 강의도 재밌고 효율적으로 생각하는 방식을 잘 알려주시는 거 같습니다. 

<script>
            function solution(mproduct){
                class Product{
                    constructor(product = []){
                        this._product = product;
                    }
                    get price(){
                        return this._product[0];
                    }
                    get fare(){
                        return this._product[1];
                    }
                    disCountCost(){
                        return this.price / 2 + this.fare;
                    }
                    cost(){
                        return this.price + this.fare;
                    }
                }
                class Products{
                    constructor(products = []) {
                        this._products = products;        
                    }
                    get commodities(){
                        return this._products;
                    }
                    get numOfProducts(){
                        return this.commodities.length;
                    }
                    map(transform){
                        return new Products(transform(this.commodities));
                    }
                    totalCost(){
                        return this.commodities
                            .map(product => 
                                product.cost()).reduce((sumv=> sum + v0);
                    }
                }
                
                function possibleCost(products){
                    return products.commodities
                        .map((vi=> [vproducts.map(makeDrop(i))])
                        .map(([vproducts]) => v.disCountCost() + products.totalCost())
                        .reduce((minCostcurrent=> Math.min(minCostcurrent));
                }
                
                function makeChildren(products){
                    const total = products.numOfProducts;
                    return total > 1 ?
                    products.commodities
                    .map((_idx=> products.map(makeDrop(idx))) : 
                    [products];
                }

                function makeGeneration(depthproducts){
                    if(depth === 0return [products];
                    return makeChildren(products)
                        .map(sub => makeGeneration(depth - 1sub))
                        .flat();
                }

                const makeDrop = i => arr => arr.slice(0,i).concat(arr.slice(i+1));
                const commos = new Products(product.map(commo => new Product(commo)));
                let limit = commos.numOfProducts;
                for(let i = 0i < limiti++){
                    if(makeGeneration(icommos)
                        .filter(prod => possibleCost(prod) <= m)
                        .length > 0) {
                        return limit - i;
                    } 
                }
            }
            
            let arr=[[103], [66], [22], [43], [45]];
            console.log(solution(28arr));
        </script>

객체지향 졸업선물

답변 0

JAVA로 백엔드 프로젝트

0

63

1

자바 실무 단계

0

312

2

Desktop용 게임 개발에도 도움이 될까요?

0

202

1

공부 방향

0

319

1

코딩테스트 준비

0

468

1

공부하다 진도를 못 나가고 있어요 ㅜㅜ

0

409

1

프로젝트를 하기 위한 커리큘럼

0

373

1

코드 리뷰 부탁드립니다!

1

378

1

코딩테스트 + 프로젝트를 위한 진도 정도

0

363

1

커리큘럼 고민

0

371

1

복습 2회차인데,, 메소드,,,,

0

357

1

코틀린 개발자로 취업하게 되면서 고민이 생겼습니다.

0

405

1