2024_09_30 Kata

SJ.CHO·2024년 9월 30일

알고리즘 Kata

75.

답안 :


class Solution {
	public String solution(String s) {
		String[] words = new String[s.length()];
		words = s.split(" ");
		int max = Integer.MIN_VALUE;
		int min = Integer.MAX_VALUE;
		for (String num : words) {
			max = Integer.parseInt(num) > max ? Integer.parseInt(num) : max;
			min = Integer.parseInt(num) < min ? Integer.parseInt(num) : min;
		}
		String answer = min + " " + max;
		return answer;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Solution s = new Solution();
		String s1 = "1 2 3 4";
		System.out.println(s.solution(s1));
	}
}
  • 알고리즘 설명 :
    • 주어진 문자열을 String.split() 메소드를 통해 구분화
    • max값 과 min값을 비교후 저장, 출력

76.

답안 :

class Solution {
	public String solution(String s) {
		StringBuffer sb = new StringBuffer();
		char[] words = new char[s.length()];
		for (int i = 0; i < s.length(); i++) {
			words[i] = s.charAt(i);
		}
		if (Character.isDigit(words[0])) {
			sb.append(words[0]);
		} else {
			sb.append(Character.toUpperCase(words[0]));
		}
		for (int i = 1; i < words.length; i++) {
			if (words[i - 1] == ' ') {
				sb.append(Character.toUpperCase(words[i]));
			} else {
				sb.append(Character.toLowerCase(words[i]));
			}
		}
		String answer = sb.toString();
		return answer;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Solution s = new Solution();
		String s1 = "3people unFollowed me";
		System.out.println(s.solution(s1));
	}
}
  • 알고리즘 설명 :
    • 주어진 문자열을 한문자씩 구분화
    • 가장 첫 문자가 숫자인지 문자인지 검사, 문자라면 대문자화.
    • 이후 공백의 뒤문자가 소문자라면 변환 아니라면 그대로 저장 및 출력
    • 처음에는 숫자와 공백도 비교해줬지만 대소문자의 변환이라 변경점이 없어 생략.
    • 200자 이하이기에 로직 신속성이 크지않지만 긴문자열이면 쓰기 힘들듯.

SQL Kata

86.

답안 :

SELECT 
    machine_id,
    ROUND(SUM(CASE WHEN activity_type='start' THEN timestamp*-1 ELSE timestamp END)*1.0
    / (SELECT COUNT(DISTINCT process_id)),3) AS processing_time
FROM 
    Activity
GROUP BY machine_id
  • 알고리즘 설명 :
    • start 와 end의 합을 구해 해당 기계의 count 수만큼 나눠서 평균을 도출, 출력
profile
70살까지 개발하고싶은 개발자

0개의 댓글