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

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

by 8Antony 2023. 2. 1.

 

 

1. 점프와 순간 이동

 

 

 

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

 

프로그래머스

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

programmers.co.kr

 

 

 

 

 

 

 

 

 

import java.util.*;

public class Solution {
    public int solution(int n) {
        int cnt = 0;

        while (n !=0) { //n이 0이 아닐때 까지 
            if (n % 2 == 0) {
                n /= 2; //나누어 떨어지면 
            }
            else {
                n--; // 안나누어 떨어지면 1빼고
                cnt++; // cnt 하나 추가 
            }
        }
        return cnt;
    }
}

 

 

 

 

 

2. 행렬의 곱셈 

 

 

 

 

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

 

프로그래머스

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

programmers.co.kr

 

 

 

 

 

 

 

 

 

 

class Solution {
    public int[][] solution(int[][] arr1, int[][] arr2) {
        int[][] answer = new int[arr1.length][arr2[0].length];
        
        for (int i=0; i<arr1.length; i++) {
            for (int j=0; j<arr2[0].length; j++) {
                 for (int k=0; k<arr1[0].length; k++) {
                     answer[i][j] += arr1[i][k] * arr2[k][j];
                 }
            }
        }
        return answer;
    }
}

 

 

 

 

3. 귤 고르기 

 

 

 

 

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

 

프로그래머스

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

programmers.co.kr

 

 

 

 

 

 

 

 

 

 

 

import java.util.*;

class Solution{
    public int solution(int k, int[] tangerine){
        int answer = 0;
        int cnt = 0;
        int[] flag = new int[10000001]; //새로운 배열에 담고 
        
        for(int i = 0; i < tangerine.length; ++i){
            flag[tangerine[i]] += 1; 
        }
        Arrays.sort(flag); // 오름차순으로 정렬 
        
        for(int i = flag.length - 1; i >= 0; --i) {
            answer += 1; //1의 갯수인지, 2의 갯수인지 
            cnt += flag[i]; 
            if(cnt >= k){
                break;
            }
        }
        return answer;
    }
}

 

 



4. n^2 배열 자르기   

 

 

 

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

 

프로그래머스

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

programmers.co.kr

 

 

 

 

 

 

 

 

 

 

import java.util.*;

class Solution{
    public ArrayList<Long> solution(int n, long left, long right){
        ArrayList<Long> list = new ArrayList<>(); //배열 담을것들 만들고 
        for(long i = left; i <= right; ++i){ 
            list.add(Math.max(i / n, i % n) + 1); //목과 나머지 중에 최댓값 +1 
        }
        return list;
    }
}

 

   

 

 

 

5. H-Index

 

 

 

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

 

프로그래머스

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

programmers.co.kr

 

 

 

 

 

 

 

 

import java.util.Arrays;
class Solution {
    public int solution(int[] citations) {
        int answer = 0;
        Arrays.sort(citations);
        int max_h = citations[citations.length - 1];
        for(int i = 0; i < max_h; ++i){
            int cnt = 0;
            for(int j = 0; j < citations.length; ++j){
                if(citations[j] >= i){
                    cnt += 1;
                }
            }
            if(cnt >= i){
                answer = i;
            }
        }
        return answer;
    }
}

   

 

 

 

 

 

댓글