Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Tangerine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.*;

class Tangerine {
public int solution(int k, int[] tangerine) {
int answer = 0;

Map <Integer, Integer> tangerineMap = new HashMap <>();

for (int t : tangerine)
{
tangerineMap.put (t, tangerineMap.getOrDefault(t, 0)+1);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자바에 있는 defaultValue() 함수가 지정된 키로 매핑된 값이 없거나 null이면 반환하는 함수라고 해서 넘 신기했어욤

}

List<Integer> valueList = new ArrayList<>(tangerineMap.keySet());
valueList.sort(((o1, o2) -> tangerineMap.get(o2) - tangerineMap.get(o1)));

for (int v : valueList) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반복문 사용할때 그냥 int v로 할 수도 있다는 것을 알았어용

k-= tangerineMap.get(v);
answer++;
if (k<=0)
break;
}

return answer;
}
}