coffee-yongsucheol / Java-Study-time

0 stars 0 forks source link

[하계 2주차] 코딩테스트 연습 #6

Open amazon7737 opened 1 month ago

amazon7737 commented 1 month ago

구명보트 1시간 풀다가 못풀어서 답지봄

괜찮다 싶은 서민우씨의 코드

image
amazon7737 commented 1 month ago

내가 짜다가 말은것

import java.util.*;

class Solution {
    public int solution(int[] people, int limit) {
        // 무게제한 맞춰서
        // 일단 사람들 순서를 작은 순으로 정렬
        // 사람이 두명정도 탔으면 바로 출발

        int boat = 0;
        int cnt =1;
        int boatHuman = 0;
        Arrays.sort(people);
        System.out.println("!!!"+ Arrays.toString(people));

        for(int i=0; i< people.length; i++){
            try{
                // 첫사람은 무조건 보트에 탄다
                // 둘째사람부터 보트 에대해서 생각하면 되는데

                // 안될경우 : 보트에 사람을 실을 수 없을때

                // 보트 + 이번에 탈사람 < limit 크면 -> 보트를 초기화 , 보트 탄 인원수 초기화

                // 

                if(boat + people[i] < limit){
                    boat += people[i];
                    boatHuman += 1;
                }else if((boat + people[i] > limit) && boatHuman > 2){
                    boat =0;
                    boatHuman =0;
                    cnt += 1;
                    boat += people[i];
                }else if((boatHuman > 2) && (boat + people[i] < limit)){
                    boat =0;
                    cnt += 1;
                    boatHuman =0;
                    boat += people[i];
                }

                //if(boatHuman<2 && (boat + people[i]) < limit){
                    //boat += people[i];
                    //boatHuman += 1;
                //}else if((boat + people[i+1]) > limit ){
                    //boatHuman = 0;
                    //boat = 0;
                    //cnt += 1;
                //}else if((boat + people[i]) >= limit || boatHuman >= 2){
                    //boatHuman=0;
                    //boat =0;
                    //cnt += 1;
            //    }
            }catch(ArrayIndexOutOfBoundsException e){
                continue;
            }

            //}
            //System.out.println("!!!"+ boat);
            //System.out.println("!!!"+ cnt);

        }
        return cnt;

    }
}
amazon7737 commented 1 month ago

K번째수 틀림

import java.util.*;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int start = 0;
        int end = 0;
        int index =0;
        int[] solution_array = new int[commands.length];

        for(int i=0; i< commands.length;i++){
            start = commands[i][0]; // 2
            end = commands[i][1]; // 5
            index = commands[i][2]; // 3

            //System.out.println(start);
            //System.out.println(end);
            //System.out.println(index);

            int[] array2 = new int[end-start+1];

            for(int j=0; j< array2.length;j++){
                System.out.println(commands[i][0]+j);
            }

            System.out.println(Arrays.toString(array2));

            //solution_array[i] = array2[index];

            System.out.println("!!");

       return solution_array;

    }
}

정적배열로 정렬 로직 다시 짜보기