-
프로그래머스 연속 부분 수열 합의 개수 c++알고리즘/프로그래머스 2022. 10. 14. 10:00반응형
https://school.programmers.co.kr/learn/courses/30/lessons/131701
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <string> #include <vector> #include <set> using namespace std; int solution(vector<int> elements) { int answer = 0; set<int> s; int cnt = 0; while(cnt < elements.size()) { for(int i=0; i<elements.size(); i++) { int tmp = 0; for(int j=0; j<=cnt; j++) { tmp = tmp + elements[(i+j) % elements.size()]; } s.insert(tmp); } cnt++; } answer = s.size(); return answer; }
% 연산자로 인덱스를 초과할때만 고려해주면 되는 쉬운 문제입니다.
set 을 사용하지않고 vector 에 저장해서 중복체크를 하게되면 시간초과가 날수있으므로 주의합니다!
728x90반응형'알고리즘 > 프로그래머스' 카테고리의 다른 글
프로그래머스 롤케이크자르기 c++ (0) 2022.11.08 프로그래머스 택배상자 c++ (0) 2022.10.14 프로그래머스 할인행사 c++ (1) 2022.10.13 카카오 행렬 테두리 연습하기 c++ (0) 2022.10.02 2022 KAKAO BLIND RECRUITMENT 주차 요금 계산 c++ (0) 2022.08.25