전체 글(184)
-
977. Squares of a Sorted Array
Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order. Example 1: Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100]. Example 2: Input: nums = [-7,-3,2,3,11] Output: [4,9,9,49,121] Constraints: 1
2022.08.21 -
278. First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be..
2022.08.21 -
C++ min , max, index 구하기
HEADER : #include 일일히 for 문을 쓰지않아도, algorithm 라이브러리에 있는 min/max_elements를 사용하여 한 줄로도 간단하게 최대값을 구할 수 있다! 또한 해당 최소/최대 값의 index 값을 구할 수 있다. 이는 결과값에서 - v.begin()을 빼주면 된다. min/max_element의 결과로, 최대값을 가리키는 반복자를 반환하기 때문에 * 연산자를 사용한다. 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 #include #include #include using namespace std; vector nums; int main() { nums = {123,213,444,455,666,7..
2022.08.21 -
C++ - 2차배열 / 동적할당 패턴 연습 (+입력 정리, vector 인자로 넘길때)
입출력하고 배열에 우선 익숙해져야겠다..... 1. cin / cout 을 쓰는걸로하자. cin / cout 은 C++ 의 String 을 쓸수있다. 근데 printf/scanf는 저걸 못씀. 대신, cin / cout 쓰려면 조건이 몇가지 있다. ios::sync_with_stdio(0); // C++ stream과 C stream의 동기화를 끊어서 프로그램 수행시간 늘리도록 하는기능. cin.tie(0); // cin 명령을 수행하기 전에 cout 버퍼를 비우지 않도록 하는 코드. 이 2개를 같이 써야함. 입출력이 작을때는 상관없지만, 양이 많을때 ( 출력 100만번 / 입력 100만번 ) 지연되어 시간초과 발생할 수 있다. 그리고 , 이걸 쓸때 cin / cout 하고, printf / scanf ..
2022.08.19 -
(C++) vector - accumulate, clear/erase, unique(+sort)
# accumulate HEADER : #include C++ 배열의 합을 구하는 함수이다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include using namespace std; int main() { vector v{1,2,3,4,5,6,7,8,9,10}; int sum = accumulate(v.begin(), v.end() ,0); // accumulate( first iter, lase iter, initial val of sum ); vector v2 = { 1000000000, 2000000000 }; // long long int long long sum = accumulate(v.begin(), v.end(), 0LL); return 0; } Co..
2022.08.18 -
1491. Average Salary Excluding the Minimum and Maximum Salary (C++)
You are given an array of unique integers salary where salary[i] is the salary of the ith employee. Return the average salary of employees excluding the minimum and maximum salary. Answers within 10-5 of the actual answer will be accepted. Example 1: Input: salary = [4000,3000,1000,2000] Output: 2500.00000 Explanation: Minimum salary and maximum salary are 1000 and 4000 respectively. Average sal..
2022.08.17 -
26. Remove Duplicates from Sorted Array ( C++ )
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements aft..
2022.08.17 -
에라토스테네스의 체 ( Sieve of Eratosthenes ) - C++
오... 이건 또 무엇인가. 백준 17425번을 풀다가 접한 이론이다. 소수를 찾는 방법중 하나이다. 이 방법은 마치 체로 치듯이 수를 걸러낸다고 하여 '에라토스테네스의 체'라고 불린다. # 방법 # 임의의 자연수 N 에 대해 그 이하의 소수를 찾는 가장 간단하고 빠른 방법이다. 예를 들어 1 ~ 100 까지 숫자 중 소수를 찾는다고 하자. ( The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than N when N is smaller than 10 million or so. ) 1. 우선 1 ~ 100 까지의 수가 있다. ( Let us take an example when N = 100. So,..
2022.08.17 -
백준 17427 - 약수의 합(C++)
문제 두 자연수 A와 B가 있을 때, A = BC를 만족하는 자연수 C를 A의 약수라고 한다. 예를 들어, 2의 약수는 1, 2가 있고, 24의 약수는 1, 2, 3, 4, 6, 8, 12, 24가 있다. 자연수 A의 약수의 합은 A의 모든 약수를 더한 값이고, f(A)로 표현한다. x보다 작거나 같은 모든 자연수 y의 f(y)값을 더한 값은 g(x)로 표현한다. 자연수 N이 주어졌을 때, g(N)을 구해보자. 입력 첫째 줄에 자연수 N(1 ≤ N ≤ 1,000,000)이 주어진다. 출력 첫째 줄에 g(N)를 출력한다. 예제 입력 1 복사 1 예제 출력 1 복사 1 예제 입력 2 복사 2 예제 출력 2 복사 4 예제 입력 3 복사 10 예제 출력 3 복사 87 # 풀이 # 1 2 3 4 5 6 7 8 9..
2022.08.17