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
- 백준 1103번
- 프로그래머스
- 유니티
- 2870번 c++
- 수학숙제
- C#
- 2870번 수학숙제
- 백준
- c++
- 백준 2870번
- 플레이어 이동
- 백준 17070번 c++
- Algorithm
- 백준 c++ 2468번
- 백준 17070번
- 17070번
- 백준 c++ 2870번
- 코딩테스트
- 2870번
- dfs
- 백준 1103번 게임
- 코테
- 백준 1103번 c++
- 2468 c++
- Unity
- Lv.3
- Beakjoon
- 오브젝트 풀링
- 2870번 수학숙제 c++
- Lv2
Archives
- Today
- Total
주녘공부일지
[프로그래머스 C#] Lv.2 숫자 변환하기 본문
https://school.programmers.co.kr/learn/courses/30/lessons/154538
1. 정답코드 및 핵심 아이디어, 유의사항
- 최소 연산 횟수를 구하는 문제기 때문에 연산 전, 후로 구분하여 체크
- 연산 전 List<T>, 연산 후 HashSet<T>
https://godgjwnsgur7.tistory.com/46
using System;
using System.Collections.Generic;
public class Solution
{
public int solution(int x, int y, int n)
{
int answer = 0; // 연산 카운트
List<int> list = new List<int>(); // 연산 전
HashSet<int> hs = new HashSet<int>(); // 연산 후
// 초기 값
list.Add(x);
// 예외처리
if (x == y)
return 0;
// 연산 할 대상이 없을 때까지 수행
while (list.Count != 0)
{
answer++;
// 연산
foreach (int num in list)
{
if (num > y)
continue;
hs.Add(num + n);
hs.Add(num * 2);
hs.Add(num * 3);
}
// 값 찾음
if (hs.Contains(y))
return answer;
// 연산 전, 후 세팅
list.Clear();
list = new List<int>(hs);
hs.Clear();
}
return -1; // 불가능
}
}
'CodingTest > Programmers Lv.2' 카테고리의 다른 글
[프로그래머스 C#] Lv.2 시소 짝꿍 (0) | 2023.10.13 |
---|---|
[프로그래머스 C#] Lv.2 뒤에 있는 큰 수 찾기 (0) | 2023.09.18 |
[프로그래머스 C#] Lv.2 타겟 넘버 (0) | 2023.09.05 |
[프로그래머스 C#] Lv.2 기능개발 (0) | 2023.09.03 |
[프로그래머스 C#] Lv.2 연속된 부분 수열의 합 (0) | 2023.08.30 |