반응형

최댓값과 최솟값 : 연습문제 Lv2

 

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

 

프로그래머스

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

programmers.co.kr

 

class Solution {
    public String solution(String s) {
        String answer = "";
        
        String[] str = s.split(" ");
		
		int max = Integer.parseInt(str[0]);
		int min = Integer.parseInt(str[0]);
		
		for(int i=0;i<str.length;i++) {
			max = Math.max(max, Integer.parseInt(str[i]));
			min = Math.min(min, Integer.parseInt(str[i]));
		}
        
        answer = min + " " + max;
        
        return answer;
    }
}

 

 

반응형

'알고리즘 > 프로그래머스' 카테고리의 다른 글

게임 맵 최단거리  (0) 2024.04.06
JadenCase 문자열 만들기  (0) 2024.04.06
올바른 괄호  (0) 2024.03.26
카펫  (0) 2024.03.24
타겟 넘버  (0) 2024.03.23
반응형

올바른 괄호 : 스택/큐 Lv2

 

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

 

프로그래머스

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

programmers.co.kr

 

import java.util.LinkedList;
import java.util.Queue;

class Solution {
    boolean solution(String s) {
        boolean answer = true;

        char[] str = s.toCharArray();
		int leftSize = 0;
		int rightSize = 0;
		
		Queue<Character> block = new LinkedList<>();
		
		for(int i=0;i<str.length;i++) {
			if(str[0] == ')') {
				answer = false;
                return answer;
			}
			
			block.add(str[i]);
		}
		
		int idx = 0;
		int blockSize = block.size();
		while(idx < blockSize) {
			char cBlock = block.poll();
			if(cBlock == '(') leftSize++;
			else if(cBlock == ')') rightSize++;
			
			if(rightSize > leftSize) {
				answer = false;
			}
			
			idx++;
		}
		
		if(leftSize != rightSize) answer = false;

        return answer;
    }
}

 

반응형

'알고리즘 > 프로그래머스' 카테고리의 다른 글

JadenCase 문자열 만들기  (0) 2024.04.06
최댓값과 최솟값  (0) 2024.03.27
카펫  (0) 2024.03.24
타겟 넘버  (0) 2024.03.23
구명보트  (0) 2024.03.23
반응형

카펫 : 완전 탐색 Lv2

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 w = (brown + yellow) / 3; //가로 최대 길이 = 전체 격자 개수 / 3
		int h = 3; //세로 최소 길이 = 3
		
		while(w >= h) {
			if(w*h == brown + yellow && 2*(w + h) == (brown + 4)) {
				answer[0] = w;
				answer[1] = h;
				break;
			}
			h++;
			w = (brown + yellow) / h;
		}
        
        return answer;
    }
}
반응형

'알고리즘 > 프로그래머스' 카테고리의 다른 글

최댓값과 최솟값  (0) 2024.03.27
올바른 괄호  (0) 2024.03.26
타겟 넘버  (0) 2024.03.23
구명보트  (0) 2024.03.23
햄버거 만들기  (0) 2024.03.20
반응형

타겟 넘버 : 깊이/너비 우선 탐색(DFS/BFS) Lv2

 

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

 

프로그래머스

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

programmers.co.kr

 

class Solution {
    int answer = 0;
    
    public int solution(int[] numbers, int target) {
        getDFS(numbers, 0, target, 0);
        return answer;
    }
    
    public void getDFS(int[] numbers, int depth, int target, int sum) {
		if(depth == numbers.length) {
			if(target == sum) answer++; 
		} else {
			getDFS(numbers, depth + 1, target, sum + numbers[depth]);
			getDFS(numbers, depth + 1, target, sum - numbers[depth]);
		}
	}
}
반응형

'알고리즘 > 프로그래머스' 카테고리의 다른 글

올바른 괄호  (0) 2024.03.26
카펫  (0) 2024.03.24
구명보트  (0) 2024.03.23
햄버거 만들기  (0) 2024.03.20
크레인 인형뽑기 게임  (0) 2024.03.19
반응형

구명보트 : 탐욕법(Greedy) Lv.2

 

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

 

프로그래머스

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

programmers.co.kr

 

import java.util.Arrays;

class Solution {
    public int solution(int[] people, int limit) {
        int answer = 0;
        
        // TODO Auto-generated method stub
		//int[] people = {10,20,70,80,90};
		//int limit = 100;
		//int boat=0;
		
		int idx = 0;
		Arrays.sort(people);
		
		for(int i= people.length-1; i >= idx; i--) {
			if(people[idx] + people[i] <=limit) {
				idx++;
			}
			
			answer++;
		}
		
		//System.out.println(boat);
        
        return answer;
    }
}

 

반응형

'알고리즘 > 프로그래머스' 카테고리의 다른 글

카펫  (0) 2024.03.24
타겟 넘버  (0) 2024.03.23
햄버거 만들기  (0) 2024.03.20
크레인 인형뽑기 게임  (0) 2024.03.19
기사단원의 무기  (0) 2024.03.18

+ Recent posts