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
- 코딩테스트
- 오브젝트 풀링
- 2468 c++
- 백준 17070번
- Algorithm
- 유니티
- c++
- Unity
- 17070번
- 백준 1103번
- 백준 c++ 2468번
- 백준 c++ 2870번
- 백준 1103번 게임
- 프로그래머스
- Lv2
- 2870번
- 백준
- Beakjoon
- 코테
- 수학숙제
- 백준 17070번 c++
- dfs
- Lv.3
- 백준 1103번 c++
- 백준 2870번
- 2870번 c++
- 2870번 수학숙제
- C#
- 2870번 수학숙제 c++
- 플레이어 이동
Archives
- Today
- Total
주녘공부일지
[프로그래머스 C#] Lv.2 귤 고르기 본문
https://school.programmers.co.kr/learn/courses/30/lessons/138476
1. 정답코드 및 핵심 아이디어, 유의사항
- 크기별로 귤의 개수를 카운트 -> 많은 개수부터 체크
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution
{
public int solution(int k, int[] tangerine)
{
Dictionary<int, int> dict = new Dictionary<int, int>();
int answer = 0;
// 귤 크기에 따라 딕셔너리에 담아 갯수파악
for (int i = 0; i < tangerine.Length; i++)
{
if (!dict.ContainsKey(tangerine[i]))
dict.Add(tangerine[i], 1);
else
dict[tangerine[i]]++;
}
// 딕셔너리의 값을 내림차순 정렬해서 List로 담음
var sortList = dict.OrderByDescending(x => x.Value).ToList();
int sum = 0;
// 가장 많은 수의 귤부터 먼저 넣어서 체크
for (int i = 0; i < sortList.Count; i++)
{
answer++;
sum += sortList[i].Value;
if (sum >= k)
break;
}
return answer;
}
}
'CodingTest > Programmers Lv.2' 카테고리의 다른 글
[프로그래머스 C#] Lv.2 연속된 부분 수열의 합 (0) | 2023.08.30 |
---|---|
[프로그래머스 C#] Lv.2 연속 부분 수열 합의 개수 (0) | 2023.08.25 |
[프로그래머스 C#] Lv.2 k진수에서 소수 개수 구하기 (0) | 2023.08.25 |
[프로그래머스 C#] Lv.2 호텔 대실 (0) | 2023.08.23 |
[프로그래머스 C#] Lv.2 소수 찾기 (2) | 2023.08.23 |