Algorithm/Programmers (5) 썸네일형 리스트형 [프로그래머스] 전화번호 목록 *제출 코드 #include #include #include using namespace std; bool solution(vector phone_book) { bool answer = true; sort(phone_book.begin(),phone_book.end()); for(int i=1; i=0; j--){ if(phone_book[i].find(phone_book[j]) == 0) return false; } } return answer; } *review 0. 전체적인 아이디어: string 배열을 오름차순으로 정렬하고, 특정 원소가 특정 원소보다 앞쪽에 있는 원소를 접두사로 가지는지 확인한다. 1. 왜 특정 원소보다 앞쪽에 위치한 원소만 확인할까? 접두사가 되려면 문자열의 길이가 어떤 문자열.. [프로그래머스] - 타겟넘버 *제출코드 all = [] #주어진 숫자를 모두 조합(더하기,빼기)해서 나올수 있는 값을 기록 sum = 0 #파라미터로 넘기지 않고 두 함수에서 쓰기위해 전역으로 선언 arr = [] # " def dfs(index): global all , arr, sum sum += arr[index] #이전까지 기록된 sum에 해당 인덱스의 값을 더한다 if index == len(arr)-1: #맨마지막 원소까지 더한 것이라면 all.append(sum) #값을 기록한다 else: dfs(index+1) #아니라면 다음 인덱스로 이동한다 sum -= arr[index] #sum에 더했던 인덱스 값을 원상복귀 시켜준다 sum -= arr[index] #이전까지 기록된 sum에서 해당 인덱스의 값을 뺀다 if in.. [프로그래머스] 가장 큰 수 *제출 코드 #include #include #include using namespace std; bool comp(int x, int y){ string xplusy = to_string(x) + to_string(y); string yplusx = to_string(y) + to_string(x); return xplusy > yplusx; } string solution(vector numbers) { string answer = ""; sort(numbers.begin(), numbers.end(), comp); bool allZero = true; for(int i=0; i"3" 이므로 "303"이라는 답이 나온다. 2.정렬 기준을 바꾸자 "3","30"이 있을때 "3"+"30" 이 큰지 "30.. [프로그래머스] 이분탐색 - 예산 *제출코드 #include #include #include using namespace std; long long sum(vector budgets,int high){ //high: 상한선 long long sum = 0; for(int i=0; i [프로그래머스] 정렬 -k번째 수 *제출 코드 #include #include using namespace std; vector solution(vector array, vector commands) { vector answer; int start,end,k; for(int i=0; ikey; s-- ) array2[s+1] = array2[s]; array2[s+1]= key; } 헤더를 include 하면 sort(array2.begin(), array2.end()) 한 줄로 구현 할 수있다. 사실 이 헤더와 sort함수를 알고 있었는데 정렬에 대한 문제라서 사용하지 않았는데, 사용해도 되나보다. =>쓸 수 있는 stl은 열심히 활용하자^^; - sort 함수 사용법 [Algorithm] C++에서 sort 함수를 이용해 정렬하기 P.. 이전 1 다음