coffee-yongsucheol / Java-Study-time

0 stars 0 forks source link

[하계 1주차] 코딩테스트 연습 #5

Open amazon7737 opened 3 months ago

amazon7737 commented 3 months ago
import java.util.*;
class Solution {
    public int solution(int n, int[] lost, int[] reserve) {
        int[] array = new int[n+1];
        int result =0;

        for(int i=0; i< lost.length;i++){
            array[lost[i]] -= 1;
            //System.out.println("lost:"+ lost[i]);
        }

        for(int i=0; i< reserve.length;i++){
            array[reserve[i]] += 1;
            //System.out.println("reserve:"+ array[i]);
        }

        for(int i=0; i< array.length; i++){
        try{
            if(array[i] == -1){
                if(array[i-1] >= 1){

                    array[i-1] -= 1;
                    array[i] += 1;
                    System.out.println("i-1:" + array[i]);

                }else if(array[i+1] >= 1){

                    array[i+1] -= 1;
                    array[i] += 1;
                    System.out.println("i+1:" + array[i]);

                }
            }
        }catch(ArrayIndexOutOfBoundsException e){
            continue;
        }
    }

        // 배열 문자로 출력
        //System.out.println(Arrays.toString(array));

        // 2차원 배열 문자로 출력
        //System.out.println(Arrays.deepToString())

        for(int i=1 ; i<array.length; i++){
            if(array[i] >=0){
                result += 1;
            }
        }

        return result;
        // 체육수업을 들을 수 있는 학생의 최댓값

    }
}
amazon7737 commented 3 months ago

구명보트 풀다 말음

import java.util.*;

class Solution {
    public int solution(int[] people, int limit) {
        int boat = 1;
        int[] boatPeople = new int[people.length];

        Arrays.sort(people);

        //System.out.println(Arrays.toString(people));

        //int totalBoatPeople = 0;

        for(int i=0; i< people.length;i++){

            int totalBoatPeople = 0;

            for(int j=0; j< boatPeople.length;j++){
                totalBoatPeople += boatPeople[j];
            }

            //System.out.println("totalBoatPeople:"+totalBoatPeople);

            if(totalBoatPeople+people[i] <= limit){
                boatPeople[i] = people[i];
                //System.out.println("boatPeople:"+ Arrays.toString(boatPeople));
            }else{
                boat += 1;
                boatPeople = new int[people.length];
                boatPeople[i] = people[i];
                //System.out.println("boatPeople:"+ Arrays.toString(boatPeople));
            }

        }

        return boat;
    }
}