728x90
SMALL

using System;
using System.Collections;
using System.Collections.Generic;

public class Solution 
{
    public int[] solution(string[] id_list, string[] report, int k) 
    {
        int[] answer = new int[id_list.Length];

        Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();

        for (int i = 0; i < report.Length; i++)
        {
            string[] str = report[i].Split(' ');

            string give = str[0];
            string take = str[1];

            if (!dic.ContainsKey(take))
            {
                List<string> list = new List<string>();
                list.Add(give);
                dic.Add(take, list);
                continue;
            }

            if (!dic[take].Contains(give))
            {
                dic[take].Add(give);
            }
        }

        for (int i = 0; i < id_list.Length; i++)
        {
            foreach (KeyValuePair<string, List<string>> item in dic)
            {
                if (item.Value.Contains(id_list[i]))
                {
                    if (item.Value.Count >= k)
                    {
                        answer[i] = ++answer[i];
                    }
                }                
            }
        }

        return answer;
    }
}

 

 

//딕셔너리의 활용이 중요하다.

 

 

 

using System;
using System.Linq;

public class Solution {
    public int[] solution(string[] id_list, string[] report, int k) {
        int[] answer = new int[id_list.Length]; // 결과 배열.

        int[] receive = new int[id_list.Length]; // 신고받은 횟수.
        int[] send = new int[id_list.Length];    // 신고한 횟수.

        report = report.Distinct().ToArray();
        // 중복 삭제하기

        // 신고받은 횟수를 기록.
        for (int i = 0; i < report.Length; i++) 
        {
            string report_str = report[i].Split(' ')[1];
            int report_index = Array.IndexOf(id_list, report_str);
            // 신고받은 인덱스 횟수
            receive[report_index]++;
        }

        // 신고받은 횟수가 k보다 높을 시 answer 값을 상승.
        for (int i = 0; i < report.Length; i++) 
        {
            string report_str = report[i].Split(' ')[1];
            int report_index = Array.IndexOf(id_list, report_str);

            if (receive[report_index] >= k) 
            {
                string send_str = report[i].Split(' ')[0];
                int send_index = Array.IndexOf(id_list, send_str);
                answer[send_index]++;
            }
        }

        return answer;
    }
}

 

 

using System;
using System.Linq;
using System.Collections.Generic;

public class Solution {
    public int[] solution(string[] id_list, string[] report, int k) {
             var tReport = report.Distinct().
                Select(s => s.Split(' ')).
                GroupBy(g => g[1]).
                Where(w => w.Count() >= k).
                SelectMany(sm => sm.Select(s => s[0])).
                ToList();

            return id_list.ToDictionary(x => x, x => tReport.Count(c => x == c)).Values.ToArray();
    }
}

 

 

파이썬

//////

 

 

def solution(id_list, report, k):
    answer = [0] * len(id_list)    
    reports = {x : 0 for x in id_list}

    for r in set(report):
        reports[r.split()[1]] += 1

    for r in set(report):
        if reports[r.split()[1]] >= k:
            answer[id_list.index(r.split()[0])] += 1

    return answer

 

 

def solution(id_list, report, k):
    answer = [0] * len(id_list)
    dic_report = {id: [] for id in id_list} # 해당 유저를 신고한 ID
    for i in set(report):
        i = i.split()
        dic_report[i[1]].append(i[0])

    for key, value in dic_report.items():
        if len(value) >= k:
            for j in value:
                answer[id_list.index(j)] += 1

    return answer

 

 

자바

//////

 

 

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        List<String> list = Arrays.stream(report).distinct().collect(Collectors.toList());
        HashMap<String, Integer> count = new HashMap<>();
        
        for (String s : list) 
        {
            String target = s.split(" ")[1];
            count.put(target, count.getOrDefault(target, 0) + 1);
        }

        return Arrays.stream(id_list).map(_user -> {
            final String user = _user;
            List<String> reportList = list.stream().filter(s -> s.startsWith(user + " ")).collect(Collectors.toList());
            return reportList.stream().filter(s -> count.getOrDefault(s.split(" ")[1], 0) >= k).count();
        }).mapToInt(Long::intValue).toArray();
    }
}

 

 

///////

 

 

import java.util.*;
class Solution {
 public int[] solution(String[] id_list, String[] report, int k) {
        // key: 신고당한놈, value: 몇명한테 당했는지
        Map<String, Set<String>> map = new HashMap<>();

        for (String rep : report) 
        {
            String[] arr = rep.split(" ");
            Set<String> set = map.getOrDefault(arr[1], new HashSet<>());
            set.add(arr[0]);
            map.put(arr[1], set);
        }

        // key: 알림받을 놈, value: 몇번 알림받을지
        Map<String, Integer> countMap = new LinkedHashMap<>();

        for (String id : id_list) 
        {
            countMap.put(id, 0);
        }

        for (Map.Entry<String, Set<String>> entry : map.entrySet()) 
        {
            if (entry.getValue().size() >= k) 
            { // 정지당할놈
                for (String value : entry.getValue()) 
                {
                    countMap.put(value, countMap.getOrDefault(value, 0) + 1);
                }
            }
        }

        return countMap.values().stream().mapToInt(Integer::intValue).toArray();
    }
}
728x90

설정

트랙백

댓글