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
- Lv2
- Unity
- 유니티
- 2870번
- Beakjoon
- 백준 17070번
- 프로그래머스
- 백준 c++ 2870번
- 오브젝트 풀링
- 2870번 c++
- 2870번 수학숙제
- 17070번
- 백준 1103번
- Algorithm
- dfs
- 백준 1103번 게임
- 백준
- 2468 c++
- Lv.3
- 백준 17070번 c++
- 백준 2870번
- 백준 1103번 c++
- c++
- 2870번 수학숙제 c++
- C#
- 플레이어 이동
- 코딩테스트
- 코테
- 수학숙제
- 백준 c++ 2468번
Archives
- Today
- Total
주녘공부일지
[프로그래머스 C#] Lv.2 연속된 부분 수열의 합 본문
https://school.programmers.co.kr/learn/courses/30/lessons/178870
1. 정답코드 및 핵심 아이디어, 유의사항
가장 짧은 길이의 조건을 만족하는 부분 수열의 합을 구하는 문제.
- Queue에는 가장 큰 수부터 입력되고, queue 안의 모든 요소의 합이 k보다 커지면 가장 큰 수를 지워나감
+ Queue<T>
https://godgjwnsgur7.tistory.com/46
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution
{
public int[] solution(int[] sequence, int k)
{
Queue<int> queue = new Queue<int>();
// 제일 큰 수부터 확인
for (int i = sequence.Length - 1; i >= 0; i--)
{
if (queue.Sum() > k)
queue.Dequeue();
queue.Enqueue(sequence[i]);
if (queue.Sum() == k)
{
// 만약, queue의 제일 큰 수와 다음 확인 예정인 수가 같을 때
if (i > 0 && sequence[i - 1] == queue.Peek())
while (i > 0 && sequence[i - 1] == queue.Peek())
i--;
return new int[2] { i, i + queue.Count - 1 };
}
}
return new int[2] { -1, -1 };
}
}
'CodingTest > Programmers Lv.2' 카테고리의 다른 글
[프로그래머스 C#] Lv.2 타겟 넘버 (0) | 2023.09.05 |
---|---|
[프로그래머스 C#] Lv.2 기능개발 (0) | 2023.09.03 |
[프로그래머스 C#] Lv.2 연속 부분 수열 합의 개수 (0) | 2023.08.25 |
[프로그래머스 C#] Lv.2 귤 고르기 (0) | 2023.08.25 |
[프로그래머스 C#] Lv.2 k진수에서 소수 개수 구하기 (0) | 2023.08.25 |