프로그래머스 H-Index(citations) 정답 코드
뿌리튼튼 CS/Algorithm2019. 4. 15. 17:37
난이도 ★★☆☆☆
틀리기 쉬운 입출력 예제
입력 | 출력 |
{0} {0, 0} {2, 0} |
0 0 1 |
힌트
잘 생각해보시면 정렬 1번 + 서치 1번이면 끝납니다. |
이하는 코드입니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import java.util.Arrays;
public class Solution {
public int solution(int[] citations) {
int answer = 0;
Arrays.sort(citations);
for (int i = 0; i < citations.length; i++) {
int h = citations.length - i;
if (citations[i] >= h) {
answer = h;
break;
}
}
return answer;
}
}
|
cs |
'뿌리튼튼 CS > Algorithm' 카테고리의 다른 글
프로그래머스 프린터(priorities) 정답 코드 (0) | 2019.04.16 |
---|---|
프로그래머스 쇠막대기(arrangement) 정답 코드 (0) | 2019.04.15 |
프로그래머스 가장 큰 수(numbers) 정답 코드 (0) | 2019.04.15 |
프로그래머스 K번째수(commands) 정답 코드 (1) | 2019.04.15 |
프로그래머스 베스트앨범(genres) 정답 코드 (0) | 2019.04.15 |
프로그래머스 가장 큰 수(numbers) 정답 코드
뿌리튼튼 CS/Algorithm2019. 4. 15. 17:30
난이도 ★★★☆☆
틀리기 쉬운 입출력 예제 (출처 : 고건희님)
입력 | 출력 |
{40, 403} {40, 405} {40, 404} {12, 121} {2, 22, 223} {21, 212} {41, 415} {2, 22} {70, 0, 0, 0} {0, 0, 0, 0} {0, 0, 0, 1000} {12, 1213} |
40403 40540 40440 12121 223222 21221 41541 222 70000 0 1000000 121312 |
힌트
평범한 정렬문제이지만 설명 및 테스트케이스가 부족하여 잘못생각하기 쉬운 문제입니다. 매우 헤맸습니다.
1. 동일한 숫자가 입력될 수 있다는 점 2. "0000" → "0" 으로 출력해야하는 점 꼭 기억하세요. |
이하는 코드입니다.
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
32
33
34
35
36
37
38
39
40
41
42
43
|
import java.util.Arrays;
import java.util.Comparator;
public class Solution {
public String solution(int[] numbers) {
String[] nums = new String[numbers.length];
for (int i = 0; i < numbers.length; i++) {
nums[i] = String.valueOf(numbers[i]);
}
Arrays.sort(nums, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (o1.length() == o2.length()) { // simple case
return o2.compareTo(o1); // order by num desc
}
String sum = o1 + o2;
String reverseSum = o2 + o1;
return reverseSum.compareTo(sum);
}
});
String answer;
if (nums.length > 0 && "0".equals(nums[0])) { // means "0000"
answer = "0";
} else {
StringBuilder sb = new StringBuilder();
for (String num : nums) {
sb.append(num);
}
answer = sb.toString();
}
return answer;
}
}
|
cs |
'뿌리튼튼 CS > Algorithm' 카테고리의 다른 글
프로그래머스 쇠막대기(arrangement) 정답 코드 (0) | 2019.04.15 |
---|---|
프로그래머스 H-Index(citations) 정답 코드 (0) | 2019.04.15 |
프로그래머스 K번째수(commands) 정답 코드 (1) | 2019.04.15 |
프로그래머스 베스트앨범(genres) 정답 코드 (0) | 2019.04.15 |
프로그래머스 위장(clothes) 정답 코드 (0) | 2019.04.15 |
프로그래머스 K번째수(commands) 정답 코드
뿌리튼튼 CS/Algorithm2019. 4. 15. 17:11
난이도 ★☆☆☆☆
힌트
Java 내장 sort를 이용하면 쉽게 풀리는 기초문제 |
이하는 코드입니다.
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
|
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for (int i = 0; i < commands.length; i++) {
List<Integer> list = new ArrayList<>();
int start = commands[i][0] - 1;
int end = commands[i][1] - 1;
int pick = commands[i][2] - 1;
for (int j = start; j <= end; j++) {
list.add(array[j]);
}
Collections.sort(list);
answer[i] = list.get(pick);
}
return answer;
}
}
|
cs |
'뿌리튼튼 CS > Algorithm' 카테고리의 다른 글
프로그래머스 H-Index(citations) 정답 코드 (0) | 2019.04.15 |
---|---|
프로그래머스 가장 큰 수(numbers) 정답 코드 (0) | 2019.04.15 |
프로그래머스 베스트앨범(genres) 정답 코드 (0) | 2019.04.15 |
프로그래머스 위장(clothes) 정답 코드 (0) | 2019.04.15 |
프로그래머스 전화번호 목록(phone_book) 정답 코드 (0) | 2019.04.15 |
프로그래머스 베스트앨범(genres) 정답 코드
뿌리튼튼 CS/Algorithm2019. 4. 15. 16:54
난이도 ★★★☆☆
힌트
Java 자료구조 및 Comparable 을 잘 이용하면 쉽게 구현할 수 있습니다. 해시문제인지 정렬문제인지 헷갈리네요. |
이하는 코드입니다.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Solution {
public int[] solution(String[] genres, int[] plays) {
Map<String, Integer> indexMap = new HashMap<>();
List<Genre> genreList = new ArrayList<>();
for (int i = 0; i < genres.length; i++) {
Genre genre;
Integer index = indexMap.get(genres[i]);
if (index == null) {
genre = new Genre();
genreList.add(genre);
indexMap.put(genres[i], genreList.size() - 1);
}
else {
genre = genreList.get(index);
}
Song song = new Song();
song.id = i;
song.play = plays[i];
genre.songs.add(song);
genre.sum += plays[i];
}
Collections.sort(genreList);
int count = 0;
for (Genre genre : genreList) {
Collections.sort(genre.songs);
if (genre.songs.size() > 1) {
count += 2;
} else {
count += 1;
}
}
int[] answer = new int[count];
int answerIdx = 0;
for (Genre genre : genreList) {
for (int i = 0; i < genre.songs.size() && i < 2; i++) {
answer[answerIdx] = genre.songs.get(i).id;
answerIdx++;
}
}
return answer;
}
private class Genre implements Comparable<Genre> {
int sum;
List<Song> songs;
public Genre() {
sum = 0;
songs = new ArrayList<>();
}
@Override
public int compareTo(Genre that) {
return Integer.compare(that.sum, this.sum); // order by sum desc
}
}
private class Song implements Comparable<Song> {
int id;
int play;
@Override
public int compareTo(Song that) {
if (this.play == that.play) {
return Integer.compare(this.id, that.id); // order by id asc
}
return Integer.compare(that.play, this.play); // order by play desc
}
}
}
|
cs |
'뿌리튼튼 CS > Algorithm' 카테고리의 다른 글
프로그래머스 가장 큰 수(numbers) 정답 코드 (0) | 2019.04.15 |
---|---|
프로그래머스 K번째수(commands) 정답 코드 (1) | 2019.04.15 |
프로그래머스 위장(clothes) 정답 코드 (0) | 2019.04.15 |
프로그래머스 전화번호 목록(phone_book) 정답 코드 (0) | 2019.04.15 |
프로그래머스 완주하지 못한 선수(participant) 정답 코드 (0) | 2019.04.15 |