https://school.programmers.co.kr/learn/courses/30/lessons/178870
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<int> sequence, int k) {
// 25.02.14
int s = 0, e = 0;
int sum = sequence[0]; // 부분 수열의 합
int subLen = sequence.size() + 1; // 부분 수열의 길이
pair<int, int> result; // 부분 수열의 시작 인덱스와 마지막 인덱스를 담은 객체
while (s <= e && e < sequence.size()) {
if (sum < k) sum += sequence[++e];
else if (sum == k) {
if (e - s + 1 < subLen) { // 길이가 더 짧은 수열이면
subLen = e - s + 1;
result = { s, e };
}
sum -= sequence[s++];
}
else sum -= sequence[s++];
}
return { result.first, result.second };
}
[level 2] Title: 연속된 부분 수열의 합, Time: 5.61 ms, Memory: 36.7 MB -Baekjo… · developeSHG/Algorithm-Baekjoon_Pro
…onHub
github.com
'Coding Test > Programmers' 카테고리의 다른 글
[Programmers] (깊이/너비 우선 탐색(DFS/BFS)) Lv 2. 게임 맵 최단거리 (0) | 2024.12.22 |
---|---|
[Programmers] (깊이/너비 우선 탐색(DFS/BFS)) Lv 2. 타겟 넘버 (0) | 2024.12.22 |
[Programmers] (그리디 or 완전탐색) Lv 2. 마법의 엘리베이터 (0) | 2024.12.18 |
[Programmers] (깊이/너비 우선 탐색(DFS/BFS)) Lv 2. 타겟 넘버 (0) | 2024.10.29 |
[Programmers] (동적계획법(Dynamic Programming)) Lv 3. N으로 표현 (0) | 2024.10.26 |
댓글