Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

22.01.07 - [PG] 튜플 #184

Closed
suhyunsim opened this issue Jan 7, 2022 · 0 comments
Closed

22.01.07 - [PG] 튜플 #184

suhyunsim opened this issue Jan 7, 2022 · 0 comments
Assignees
Labels
lv.2 프로그래머스 - level 2 문자열 문자열 성공 맞은 문제

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Jan 7, 2022

문제

핵심 아이디어

  • 풀어서 통과는 했는데 풀이가 너무 더러움
  • 다른 답안 보고 수정

어려운 점, 실수

풀이

package main.java.com.poogle.PG.Q64065;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class Solution {

    public static void main(String[] args) {
//        System.out.println(Arrays.toString(solution("{{2},{2,1},{2,1,3},{2,1,3,4}}"))); //2134
//        System.out.println(Arrays.toString(solution("{{1,2,3},{2,1},{1,2,4,3},{2}}"))); //2134
//        System.out.println(Arrays.toString(solution("{{20,111},{111}}"))); //111 20
//        System.out.println(Arrays.toString(solution("{{123}}")));
//        System.out.println(Arrays.toString(solution("{{4,2,3},{3},{2,3,4,1},{2,3}}"))); //3241
        System.out.println(Arrays.toString(solutionTwo("{{4,2,3},{3},{2,3,4,1},{2,3}}"))); //3241
    }

    private static int[] solution(String s) {
        List<Integer> ans = new ArrayList<>();
        String str = s.substring(1, s.length() - 1).replaceAll("[{}]", "");
        int[] strArr = Arrays.stream(str.split(",")).mapToInt(Integer::parseInt).toArray();
        HashMap<Integer, Integer> list = new LinkedHashMap<>();
        for (int j : strArr) {
            if (list.containsKey(j)) {
                list.replace(j, list.get(j) + 1);
            } else {
                list.put(j, 1);
            }
        }
        List<Map.Entry<Integer, Integer>> entries = list.entrySet().stream()
            .sorted(Map.Entry.comparingByValue())
            .collect(Collectors.toList());
        for (Map.Entry<Integer, Integer> entry : entries) {
            ans.add(entry.getKey());
        }
        Collections.reverse(ans);
        return ans.stream().mapToInt(i -> i).toArray();
    }

    private static int[] solutionTwo(String s) {
        Set<String> set = new HashSet<>();
        String[] arr = s.replaceAll("[{]", " ").replaceAll("[}]", " ").trim().split(" , ");
        System.out.println(Arrays.toString(arr));
        Arrays.sort(arr, (a, b)->{return a.length() - b.length();});
        int[] answer = new int[arr.length];
        int index = 0;
        for (String s1 : arr) {
            for (String s2 : s1.split(",")) {
                if (set.add(s2)) answer[index++] = Integer.parseInt(s2);
            }
        }
        return answer;
    }

}
@suhyunsim suhyunsim added 성공 맞은 문제 문자열 문자열 lv.2 프로그래머스 - level 2 labels Jan 7, 2022
@suhyunsim suhyunsim added this to the 1월 1주 차 milestone Jan 7, 2022
@suhyunsim suhyunsim self-assigned this Jan 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lv.2 프로그래머스 - level 2 문자열 문자열 성공 맞은 문제
Projects
None yet
Development

No branches or pull requests

1 participant