본문 바로가기

Algorithm Problem

(93)
Lesson 2. Arrays - OddOccurrencesInArray #2 (속도 개선) 1. 속도 개선 아이디어정답을 제외하고, 모든 요소는 짝수로 있다. 즉, 최소 2개 이상이다.vector A의 요소들을 정렬시키면, 중복되는 요소들이 나란히 붙어 있을 것이다.그리고 이들은 짝수개이므로, +2씩 점프하면서 확인할 수 있다. 2. 코드 (C++)#include int solution(vector &A){ //정렬 sort(A.begin(), A.end()); //검색 for(size_t i = 0; i   3. 결과
Lesson 2. Arrays - OddOccurrencesInArray #1 1. 문제A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.For example, in array A such that:A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9the elements at indexes 0 and 2 have value 9,the elements at ..
Lesson 2. Arrays - CyclicRotation 1. 문제An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).The goal is to rotate array A K times; that is, each elem..
Lesson 1. Iterations - BinaryGap 1. 문제A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has bina..
2884번: 알람 시계 문제상근이는 매일 아침 알람을 듣고 일어난다. 알람을 듣고 바로 일어나면 다행이겠지만, 항상 조금만 더 자려는 마음 때문에 매일 학교를 지각하고 있다.상근이는 모든 방법을 동원해보았지만, 조금만 더 자려는 마음은 그 어떤 것도 없앨 수가 없었다.이런 상근이를 불쌍하게 보던 창영이는 자신이 사용하는 방법을 추천해 주었다.바로 "45분 일찍 알람 설정하기"이다.이 방법은 단순하다. 원래 설정되어 있는 알람을 45분 앞서는 시간으로 바꾸는 것이다. 어차피 알람 소리를 들으면, 알람을 끄고 조금 더 잘 것이기 때문이다. 이 방법을 사용하면, 매일 아침 더 잤다는 기분을 느낄 수 있고, 학교도 지각하지 않게 된다.현재 상근이가 설정한 알람 시각이 주어졌을 때, 창영이의 방법을 사용한다면, 이를 언제로 고쳐야 하는..