Problem Solving/Programmers

[LV2]2018 KAKAO BLIND RECRUITMENT[1차] 뉴스 클러스터링

TimeSave 2022. 8. 17. 04:57

1차 뉴스클러스터링.txt
0.00MB

 

복붙방지 일부해제가 안되서, 코드는 파일로 첨부

 

문제해설 : 카카오 신입 공채 1차 코딩 테스트 문제 해설 – tech.kakao.com

 

내코드(1 Try)

import java.util.ArrayList;
import java.util.Collections;

class Solution {
	public int solution(String str1, String str2) {

		str1 = str1.toUpperCase();
		str2 = str2.toUpperCase();

		// 잡문자 날려야함
		String regex = "^[A-Z]*$";
        
		ArrayList<String> firstList = new ArrayList<>();
		ArrayList<String> secondList = new ArrayList<>();
		ArrayList<String> union = new ArrayList<>();
		ArrayList<String> inter = new ArrayList<>();

		for (int i = 0; i < str1.length() - 1; ++i) {
			String element = str1.substring(i, i + 2);
			if (element.contains(" ")) {
				continue;
			}
			if (!element.matches(regex)) {
				continue;
			}
			firstList.add(element);
			 union.add(element);

		}
		for (int i = 0; i < str2.length() - 1; ++i) {
			String element = str2.substring(i, i + 2);
			if (element.contains(" ")) {
				continue;
			}
			if (!element.matches(regex)) {
				continue;
			}
			secondList.add(element);
			 union.add(element);

		}

		if (firstList.size() > secondList.size()) {
			for (String element : secondList) {
				if (secondList.contains(element)) {
					inter.add(element);
				}
			}
		} else {
			for (String element : firstList) {
				if (secondList.contains(element)) {
					inter.add(element);
				}
			}
		}

		for (String element : inter) {
			union.remove(element);
		}

		int answer = 0;

		if (union.size() == 0) {
			answer = 1 * 65536;
		} else {

			double J = (double) inter.size() / (double) union.size();
			answer = (int) (65536 * J);
		}

		return answer;
	}
}

 

 

 

 

Fail - 4,7,9,10,11,13

 

이후, 정답참고

내코드(2 Try - 1에 sort 추가)

import java.util.ArrayList;
import java.util.Collections;

class Solution {
	public int solution(String str1, String str2) {

		str1 = str1.toUpperCase();
		str2 = str2.toUpperCase();

		// 잡문자 날려야함
		String regex = "^[A-Z]*$";

		ArrayList<String> firstList = new ArrayList<>();
		ArrayList<String> secondList = new ArrayList<>();
		ArrayList<String> union = new ArrayList<>();
		ArrayList<String> inter = new ArrayList<>();

		for (int i = 0; i < str1.length() - 1; ++i) {
			String element = str1.substring(i, i + 2);
			if (element.contains(" ")) {
				continue;
			}
			if (!element.matches(regex)) {
				continue;
			}
			firstList.add(element);
			 union.add(element);

		}
		for (int i = 0; i < str2.length() - 1; ++i) {
			String element = str2.substring(i, i + 2);
			if (element.contains(" ")) {
				continue;
			}
			if (!element.matches(regex)) {
				continue;
			}
			secondList.add(element);
			 union.add(element);

		}

		//추가항목
		Collections.sort(firstList);
		Collections.sort(secondList);
		//추가항목

		if (firstList.size() > secondList.size()) {
			for (String element : secondList) {
				if (secondList.contains(element)) {
					inter.add(element);
				}
			}
		} else {
			for (String element : firstList) {
				if (secondList.contains(element)) {
					inter.add(element);
				}
			}
		}

		for (String element : inter) {
			union.remove(element);
		}

		int answer = 0;

		if (union.size() == 0) {
			answer = 1 * 65536;
		} else {

			double J = (double) inter.size() / (double) union.size();
			answer = (int) (65536 * J);
		}

		return answer;
	}
}

Fail - 4,7,9,10,11,13

역시 Sorting 은 전혀 상관없다.

 

 

 

내코드(To-Be, 정답 참고함)

 

통과 코드를 보니, 다른 차이는 없었지만, Union, Intersection 구하는 방식에서 차이가 났다.

원소의 중복을 허용하는 다중집합multiset 을 잘 이해 못해서 발생한 문제로 보인다.

 

 

원인파악은 추후에 추가적으로 할 것.

As-Is Sequence

: 합집합 = A+B를 먼저 한 후 교집합을 뺀다.

 

1. String 1을 체크하며, list1에 담는다. "동시에" Union List에 추가한다.

2. String 2를 체크하며, list2에 담는다. "동시에" Union List에 추가한다.

3. list1, 2의 사이즈 비교 후, list1, list2에 둘다 있는 항목은 InterSection List에 추가한다.

4. Intersection List에서 반복을 돌며, Union List에 있다면 Union List에서 제거한다.

 

 

To-Be Sequence(A = only A + intersection, A+B = A+ onlyB)

: A를 담고, B만 있는 것을 선별해서 담는다.

 

1. String 1을 체크하며, list1에 담는다.

2. String 2를 체크하며, list2에 담는다.

3. list1, 2를 정렬한다.

4. List1을 기준으로 반복을 돈다.

  4-1. Union에 넣는다.

  4-2. List2에 있는 것은 Intersection List에 넣는다.

5. List2를 기준으로 반복을 돌며, Union에 넣는다.

 

 

import java.util.ArrayList;
import java.util.Collections;

class Solution {
	public int solution(String str1, String str2) {

		str1 = str1.toUpperCase();
		str2 = str2.toUpperCase();

		// 잡문자 날려야함
		String regex = "^[A-Z]*$";
        
		ArrayList<String> firstList = new ArrayList<>();
		ArrayList<String> secondList = new ArrayList<>();
		ArrayList<String> union = new ArrayList<>();
		ArrayList<String> inter = new ArrayList<>();

		for (int i = 0; i < str1.length() - 1; ++i) {
			String element = str1.substring(i, i + 2);
			if (element.contains(" ")) {
				continue;
			}
			if (!element.matches(regex)) {
				continue;
			}
			firstList.add(element);
			// union.add(element);

		}
		for (int i = 0; i < str2.length() - 1; ++i) {
			String element = str2.substring(i, i + 2);
			if (element.contains(" ")) {
				continue;
			}
			if (!element.matches(regex)) {
				continue;
			}
			secondList.add(element);
			// union.add(element);

		}

		Collections.sort(firstList);
		Collections.sort(secondList);

		for (String element : firstList) {
			if (secondList.remove(element)) {
				inter.add(element);
			}
			union.add(element);
		}
		for (String element : secondList) {
			union.add(element);
		}

		int answer = 0;

		if (union.size() == 0) {
			answer = 1 * 65536;
		} else {

			double J = (double) inter.size() / (double) union.size();
			answer = (int) (65536 * J);
		}

		return answer;
	}
}

 

비교코드 : 

[프로그래머스 - Java] [1차] 뉴스 클러스터링 (2018 KAKAO BLIND RECRUITMENT) (tistory.com)