내일배움캠프 28일차 TIL _ 6주 4일차
2024. 5. 23. 23:18ㆍTIL Java
- 오늘 있었던 일
- 개인과제
- 알고리즘
SPRIONG
- 스프링 정리
알고리즘 문제 풀기
● 최댓값과 최솟값
더보기
● 최댓값과 최솟값 //링크
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Solution {
public String solution(String s) {
List<Integer> list = Stream.of(s.split(" "))
.map(Integer::parseInt)
.collect(Collectors.toList());
String answer = Collections.min( list)+" "+Collections.max( list);
return answer;
}
}
- 첫 시도에 통과했다
-
- 스트림을 이용해 최대값과 최소값을 구했다.
-
- 밑은 다른 사람의 풀이
-
import java.util.Arrays; public class GetMinMaxString { public String getMinMaxString(String str) { String[] array = str.split(" "); int[] arrayInt = new int[array.length]; for (int i = 0; i < arrayInt.length; i++) { arrayInt[i] = Integer.parseInt(array[i]); } Arrays.sort(arrayInt); String result = arrayInt[0] + " " + arrayInt[arrayInt.length - 1]; return result; } public static void main(String[] args) { String str = "9 19"; GetMinMaxString minMax = new GetMinMaxString(); // 아래는 테스트로 출력해 보기 위한 코드입니다. System.out.println("최대값과 최소값은?" + minMax.getMinMaxString(str)); } }
- 배열을 이용해 간단하게 구했다.
● 신고 결과 받기
더보기
● 신고 결과 받기 //링크
import java.util.*;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
Map<String, Integer> userIndexMap = new HashMap<>();
for (int i = 0; i < id_list.length; i++) {
userIndexMap.put(id_list[i], i);
}
List<Set<Integer>> reportList = new ArrayList<>();
for (int i = 0; i < id_list.length; i++) {
reportList.add(new HashSet<>());
}
int[] reportCount = new int[id_list.length];
for (String r : report) {
String[] split = r.split(" ");
int reporterIndex = userIndexMap.get(split[0]);
int reportedIndex = userIndexMap.get(split[1]);
if (reportList.get(reporterIndex).add(reportedIndex)) {
reportCount[reportedIndex]++;
}
}
int[] result = new int[id_list.length];
for (int i = 0; i < reportCount.length; i++) {
if (reportCount[i] >= k) {
for (int j = 0; j < reportList.size(); j++) {
if (reportList.get(j).contains(i)) {
result[j]++;
}
}
}
}
return result;
}
}
- 첫 시도때 60%이상 성공했지만 나머지는 못했다.
- Map을 이용해 사용자 별로 신고 건수를 구해 마지막에 정지되는 값을 비교해 넣었다 = 60%
- 아에 전부 분해해서 하나씩 만들었다. ... 이게 왜 더 빠르지?
- 밑은 다른 사람의 풀이
-
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(); } }
- 스트림의 StreamAPI distinct 가 있었구나
당일 회고
- 강의대로 하고있는데 실행이 안된다.. 왜 이러지
'TIL Java' 카테고리의 다른 글
내일배움캠프 30일차 TIL _ 7주 1일차 (0) | 2024.05.27 |
---|---|
내일배움캠프 29일차 TIL _ 6주 5일차 (0) | 2024.05.24 |
내일배움캠프 27일차 TIL _ 6주 3일차 (0) | 2024.05.22 |
내일배움캠프 26일차 TIL _ 6주 2일차 (0) | 2024.05.21 |
내일배움캠프 25일차 TIL _ 6주 1일차 (0) | 2024.05.20 |