본문 바로가기
코딩테스트/programmers

프로그래머스 [#11] 코딩테스트 연습 JAVA - 11

by 8Antony 2023. 2. 16.

 

 

1. 영어 끝말잇기

 

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/12981?language=java 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

 

 

 

 

import java.util.*;
class Solution {
    public int[] solution(int n, String[] words) {
        //탈락자가 발생하지 않는 경우
        int[] answer = {0,0};
        int index = 0;
        
        //중복 체크를 하기 위해 
        LinkedList<String> link = new LinkedList<>();
        link.add(words[0]);
        for(int i = 1; i < words.length; ++i){
            // words[i - 1] 이전 단어 
            // words[i] 현재 단어 
            // words[i - 1].charAt(words[i - 1].length() - 1 이전 단어의 마지막 철자
            // words[i].charAt(0) 현재 단어의 처음 철자 
            
            if(link.contains(words[i]) || words[i - 1].charAt(words[i - 1].length() - 1) != words[i].charAt(0)){
                index = i;
                break;
            }
            link.add(words[i]);
        }
        if(index != 0){ //단어가 중복 or 차례가 이어지지 않음
            answer[0] = (index % n) + 1; //번호
            answer[1] = (index / n) + 1; //차례
        }

        return answer;
    }
}

 

 

 

 

 

2. 카펫 

 

 

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/42842

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

 

 

 

 

 

class Solution {
    public int[] solution(int brown, int yellow) {
        int[] answer = new int[2];
        int n = brown + yellow;
        for(int i = 1; i <= n; i++) {
            // n / i 나머지가 없고, i(가로)의 길이가 세로보다 기므로
            // && = and
            if(n % i == 0 && n / i <= i) {
                if(2*i + (n/i) * 2 - 4 == brown) {
                    answer[0] = i;
                    answer[1] = n/i;
                    break;
                }
            }
        }
        
        return answer;
    }
}

 

 

 

 

3. 구명보트

 

 

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/42885

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

 

 

 

 

 

 

 

 

import java.util.*;
class Solution {
    public int solution(int[] people, int limit) {
        int answer = 0;
        //사람을 정렬
        Arrays.sort(people);
        int min = 0;
        
        
        //초기값 max는 people 배열의 크기 -1 , max는 최댓값 위치의 역할
        //min ≤ max일 때까지 반복, 최솟값의 위치가 최댓값의 위치보다 커지면 반복 종료
        //people의 min 위치에 있는 값 + max 위치에 있는 값이 limit보다 작거나 같으면 min++, answer++ 위 if문에서 걸리지 않으면 현재 최댓값 혼자 구명보트를 탄 걸로 처리
        for (int max = people.length - 1; min <= max; --max) {
            if(people[min] + people[max] <= limit) {
                min += 1;
            }
            answer += 1;
        }
        return answer;
    }
}



 

 

4. 예상 대진표

 

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/12985

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

 

 

 

 

 

 

class Solution
{
    public int solution(int n, int a, int b)
    {
        int answer = 0;

       // 1,2번 참가자 --> 1번 | 3,4번 참가자 -> 2번 | 5,6번 참가자 -> 3번 
       // 순서 2번 참가자일 경우 2 / 2 + 2 % 2 = 1
       while(true) {
          a = a / 2 + a % 2;
          b = b / 2 + b % 2;
          answer++;
          if( a == b)
              break;
       }

        return answer;
    }
}

 

 

 


 

 

Appendix

 

1. charAt : 글자 

 

댓글