View

๋ฌธ์ œ ๋งํฌ:

https://programmers.co.kr/learn/courses/30/lessons/92334

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

๋ฌธ์ œ ์„ค๋ช… ์‹ ์ž…์‚ฌ์› ๋ฌด์ง€๋Š” ๊ฒŒ์‹œํŒ ๋ถˆ๋Ÿ‰ ์ด์šฉ์ž๋ฅผ ์‹ ๊ณ ํ•˜๊ณ  ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ”์ผ๋กœ ๋ฐœ์†กํ•˜๋Š” ์‹œ์Šคํ…œ์„ ๊ฐœ๋ฐœํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋ฌด์ง€๊ฐ€ ๊ฐœ๋ฐœํ•˜๋ ค๋Š” ์‹œ์Šคํ…œ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ ์œ ์ €๋Š” ํ•œ ๋ฒˆ์— ํ•œ ๋ช…์˜

programmers.co.kr

 

์†Œ์Šค ์ฝ”๋“œ: 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

public class Solution {
	
	public int[] solution(String[] id_list, String[] report, int k) {
		// ์ค‘๋ณต ์ œ๊ฑฐํ•œ [์‹ ๊ณ ํ•œ ์œ ์ €]-[์‹ ๊ณ ๋‹นํ•œ ์œ ์ €] ๋ฐฐ์—ด
		report = Arrays.asList(report).stream()
				.distinct().toArray(String[]::new);
		
		List<User> user_list = new ArrayList<Solution.User>();
		
		for (int u = 0; u < id_list.length; u++) {
			User user = new User();
			user.setUser(id_list[u]);
			user_list.add(user);
		}
		
		for (int i = 0; i < report.length; i++) {
			StringTokenizer st = new StringTokenizer(report[i]);
			
			String userId   = st.nextToken();
			String reported = st.nextToken();
			
			for (User userE : user_list) {
				if (userE.getUser().equals(userId)) {
					userE.addReportedId(reported);
				}
				
				if (userE.getUser().equals(reported)) {
					userE.addReportedCnt();
				}
			}
		}
		
		// k๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋‹นํ•œ ์‚ฌ๋žŒ ์ฐพ๊ธฐ
		user_list.forEach(e -> {
			if (e.getReportedCnt() >= k) {
				for (User userE : user_list) {
					if (userE.getReportedId().contains(e.getUser())) {
						userE.sendMail();
					}
				}
				
			}
		});
		
		int[] answer = new int[user_list.size()];
		
		for (int z = 0; z < user_list.size(); z++) {
			answer[z] = user_list.get(z).getReceivedMail();
		}
		
		return answer;
	}
	
	class User {
		String user;
		List<String> reported_list = new ArrayList<String>();
		int reported_cnt  = 0;
		int received_mail = 0;
		
		public String getUser() {
			return user;
		}
		
		public void setUser(String user) {
			this.user = user;
		}
		
		public List<String> getReportedId() {
			return reported_list;
		}
		
		public void addReportedId(String reported_id) {
			this.reported_list.add(reported_id);
		}
		
		public int getReportedCnt() {
			return reported_cnt;
		}
		
		public void addReportedCnt() {
			this.reported_cnt++;
		}
		
		public int getReceivedMail() {
			return received_mail;
		}
		
		public void sendMail() {
			this.received_mail++;
		}
	}
}

์ฒ˜์Œ ์ œ์ถœํ–ˆ์„ ๋•Œ๋Š” ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋– ์„œ ํ”ผ๋“œ๋ฐฑ ๋ฐ›๊ณ  ์ง‘์— ์™€์„œ ๋‹ค์‹œ ํ’€์—ˆ์Œ.

๋‹ค์‹œ ํ’€๋ฉด์„œ ๊ฐ์ฒด ์ƒ์„ฑํ•˜๋Š” ๊ฑธ ๋ชธ์— ์ตํž ๊ฒธ ๋ฌธ์ œ ํ’€์ด์—๋„ ์ ์šฉํ•ด ๋ณด์•˜๋‹ค...

์ž์„ธํ•œ ํ’€์ด๋Š” ์ดํ›„ ์ˆ˜์ •ํ•  ๊ฒƒ... ๐Ÿ˜ช

Share Link
reply
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31