Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 백준 17070번 c++
- 2468 c++
- 프로그래머스
- 백준 c++ 2870번
- 백준 2870번
- 코딩테스트
- C#
- c++
- Unity
- 백준
- 2870번 수학숙제 c++
- Lv.3
- dfs
- 백준 c++ 2468번
- Lv2
- 유니티
- 백준 1103번
- 백준 1103번 게임
- Algorithm
- 백준 17070번
- 오브젝트 풀링
- 17070번
- 플레이어 이동
- 2870번 수학숙제
- 코테
- Beakjoon
- 2870번
- 수학숙제
- 2870번 c++
- 백준 1103번 c++
Archives
- Today
- Total
주녘공부일지
[프로그래머스 C#] Lv.2 귤 고르기 본문
https://school.programmers.co.kr/learn/courses/30/lessons/138476
1. 정답코드 및 핵심 아이디어, 유의사항
한 상자를 k개의 귤로 채울 때, 크기가 서로 다른 종류의 수의 최소 값을 구하는 문제
- 구하려는 상자는 여러 개가 아닌 단 하나라는 게 핵심
-> 귤의 크기에 따라 분류하고, 크기에 따라 가장 개수가 많은 귤부터 상자에 담으면 됨
- 딕셔너리에 귤의 크기를 key로 하여 개수를 저장
- 리스트에 내림차순 정렬해 개수가 가장 많은 귤부터 상자에 담음
주석 참조
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution
{
public int solution(int k, int[] tangerine)
{
int answer = 0;
var dict = new Dictionary<int, int>();
// 딕셔너리 안에 size별로 담음
foreach (int size in tangerine)
{
if (dict.ContainsKey(size))
dict[size]++;
else
dict.Add(size, 1);
}
// list에 내림차순 정렬해서 담음 (List안의 자료형은 KeyValuePair<int, int>로 들어감)
var list = dict.OrderByDescending(x => x.Value).ToList();
// 가장 큰 귤부터 상자에 담음
for (int i = 0; i < list.Count; i++)
{
answer++;
k -= list[i].Value;
// 상자가 가득 참
if (k <= 0)
break;
}
return answer;
}
}
'CodingTest > Programmers Lv.2' 카테고리의 다른 글
[프로그래머스 C#] Lv.2 예상 대진표 (0) | 2023.12.10 |
---|---|
[프로그래머스 C#] Lv.2 삼각 달팽이 (1) | 2023.12.08 |
[프로그래머스 C#] Lv.2 게임 맵 최단거리 (1) | 2023.12.08 |
[프로그래머스 C#] Lv.2 N-Queen (1) | 2023.12.07 |
[프로그래머스 C#] Lv.2 최솟값 만들기 (1) | 2023.12.07 |