티스토리 뷰
[문제 링크]
https://www.hackerrank.com/challenges/countingsort1/problem?isFullScreen=true
Counting Sort 1 | HackerRank
Count the number of times each value appears.
www.hackerrank.com
public class CountingSort1 {
public static void main(String[] args) {
List<Integer> arr = Arrays.asList(1, 1, 3, 2, 1);
counting(arr);
}
public static List<Integer> counting(List<Integer> arr) {
int[] answer = new int[100];
for (int i = 0; i < 101; i++) {
for (int j = 0; j < arr.size(); j++) {
if (arr.get(j) == i) {
answer[i]++;
}
}
}
output(answer);
List<Integer> answerList = new ArrayList<>();
for (int i = 0; i < answer.length; i++) {
answerList.add(answer[i]);
}
return answerList;
}
public static void output(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
계수 정렬 알고리즘에서 누적합 배열만 만들면 되는 문제.
계수 정렬의 시간복잡도와 공간복잡도에 대한 것은 다음 문제에서 더 직관적으로 느낄 수 있을 것 같다.
처음엔 계수 정렬을 머릿 속에서만 이해하려고 하니 바로 이해가 되지 않았는데, 아래 링크를 보니 이 알고리즘을 바로 이해할 수 있었다!
https://www.cs.miami.edu/home/burt/learning/Csc517.091/workbook/countingsort.html
Counting Sort
This is an animation of the counting sort algorithm found in CLR's Algorithms book. Array C (counting array): PSEUDOCODE FOR COUNTING SORT (taken from CLR) Initialize counting array to all zeros. Count the number of times each value occurs in the input. Mo
www.cs.miami.edu
'알고리즘 문제' 카테고리의 다른 글
| [프로그래머스] 베스트앨범 (1) | 2023.04.14 |
|---|---|
| [hackerrank] Quicksort 1 - Partition - JAVA (0) | 2023.04.14 |
| [hackerrank] CorrectnessAndTheLoopInvariant (삽입정렬) (0) | 2023.04.13 |
| [프로그래머스]전화번호 목록 - JAVA (0) | 2023.04.13 |
| [프로그래머스] 위장 - JAVA (0) | 2023.04.13 |
댓글
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 자바
- 스프링faker
- 항해
- jmeter세션
- Python
- Redis
- 토큰
- 동적크롤링
- Java
- 대규모더미데이터
- jmeter쿠키
- pessimisticlock
- Spring
- jmeter시나리오
- Redisson
- 인덱스
- jmeter테스트
- jmeter토큰
- jmeter로그인
- 부하테스트시나리오
- Lock
- index
- CorrectnessAndTheLoopInvariant
- 프로그래머스
- hackerrank
- jmeter부하테스트
- bankersRounding
- CheckedException
- EC2
- jwt
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함