View
[ํ๋ก๊ทธ๋๋จธ์ค] 92334๋ฒ: ์ ๊ณ ๊ฒฐ๊ณผ ๋ฐ๊ธฐ (JAVA)
Lily ๐ฉ๐ป 2022. 6. 6. 03:53๋ฌธ์ ๋งํฌ:
https://programmers.co.kr/learn/courses/30/lessons/92334
์์ค ์ฝ๋:
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++;
}
}
}
์ฒ์ ์ ์ถํ์ ๋๋ ์๊ฐ ์ด๊ณผ๊ฐ ๋ ์ ํผ๋๋ฐฑ ๋ฐ๊ณ ์ง์ ์์ ๋ค์ ํ์์.
๋ค์ ํ๋ฉด์ ๊ฐ์ฒด ์์ฑํ๋ ๊ฑธ ๋ชธ์ ์ตํ ๊ฒธ ๋ฌธ์ ํ์ด์๋ ์ ์ฉํด ๋ณด์๋ค...
์์ธํ ํ์ด๋ ์ดํ ์์ ํ ๊ฒ... ๐ช
'๊ฐ๋ฐ > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] 42586๋ฒ: ๊ธฐ๋ฅ ๊ฐ๋ฐ (JAVA) (0) | 2022.06.20 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] 17677๋ฒ: ๋ด์ค ํด๋ฌ์คํฐ๋ง (JAVA) (0) | 2022.06.20 |
[๋ฐฑ์ค/BOJ] 1406๋ฒ: ์๋ํฐ (JAVA) (0) | 2022.06.08 |
[๋ฐฑ์ค/BOJ] 15953๋ฒ: ์๊ธ ํํฐ (JAVA) (0) | 2022.06.05 |
reply