본문 바로가기

c++65

C++ ] leetCode 1418 - Display Table of Food Orders in a Restaurant 리트코드 1418 문제 Given the array orders, which represents the orders that customers have done in a restaurant. More specifically orders[i]=[customerNamei,tableNumberi,foodItemi] where customerNamei is the name of the customer, tableNumberi is the table customer sit at, and foodItemi is the item customer orders. Return the restaurant's “display table”. The “display table” is a table whose row entries.. 2024. 4. 20.
C++ ] std::iota 사용법 처음엔 itoa의 오타인 줄 알았다. std::iota는 헤더에 정의된 C++ 표준 라이브러리 함수로 배열이나 컨테이너에 연속적 숫자를 할당한다. 시작 반복자, 종료 반복자, 그리고 초기 값의 세 개의 매개변수를 받는다. #include #include std::vector v(10); std::iota(v.begin(), v.end(), 0); // v는 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}로 초기화됨 for 반복문을 사용해서 동일한 작업을 수행할 수 있다. 둘 다 O(n) 시간 복잡도를 가지며 유의미한 성능차이는 없을 것 같다. std::vector v(10); for(int i = 0; i < v.size(); ++i) { v[i] = i; } 2024. 4. 19.
C++ ] leetCode 3005 - Count Elements With Maximum Frequency 리트코드 3005 문제 You are given an array nums consisting of positive integers. Return the total frequencies of elements in nums such that those elements all have the maximum frequency. The frequency of an element is the number of occurrences of that element in the array. Example 1: Input: nums = [1,2,2,3,1,4] Output: 4 Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequ.. 2024. 4. 16.
C++ ] leetCode 2596 - Check Knight Tour Configuration 리트코드 2596번 문제 There is a knight on an n x n chessboard. In a valid configuration, the knight starts at the top-left cell of the board and visits every cell on the board exactly once. You are given an n x n integer matrix grid consisting of distinct integers from the range [0, n * n - 1] where grid[row][col] indicates that the cell (row, col) is the grid[row][col]th cell that the knight visited. .. 2024. 4. 2.
C++ ] std::stack 사용법 C++의 std::stack은 STL(Standard Template Library)의 일부로 후입선출(LIFO: Last In, First Out) 방식을 따르는 자료구조이며 데이터의 삽입(push)과 삭제(pop)가 한 쪽 끝에서만 이루어지는 특징을 가지고 있다. 1. stack 헤더 파일 포함 #include 2. stack 객체 생성 std::stack myStack; 3. 요소 삽입 (push) myStack.push(10); 4. 요소 제거 (pop) myStack.pop(); 5. 최상위 요소 접근 (top) std::cout 2024. 3. 20.
C++ ] std::pair 사용법 C++에서 pair는 두 개의 값을 하나의 단위로 묶을 때 유용한 STL(Standard Template Library)의 일부이다. pair를 사용하면 문자열과 정수, 정수와 정수 등 다양한 데이터 타입의 두 값을 쌍으로 묶어 쉽게 관리할 수 있다. 1. std::pair 사용을 위한 헤더 포함 #include std::pair는 utility 헤더를 포함시키면 사용할 수 있지만 이미 vector나 map, algorithm 같이 자주쓰는 헤더에 utility 헤더가 포함되어 있기 때문에 별도로 utility 헤더를 포함시키지 않아도 쓸 수 있는 경우가 많다. 2. pair 생성하고 요소에 접근하기 #include #include #include using namespace std; int main().. 2024. 3. 1.
C++ ] std::sort 사용법 with 람다식 C++에서 제공하는 표준 라이브러리 함수 std::sort는 벡터, 리스트, 배열 등 다양한 컨테이너를 정렬하는 데 사용된다. 1. algorithm 헤더 포함 #include 2. std::sort 함수 원형 template void sort(RandomIt first, RandomIt last); template void sort(RandomIt first, RandomIt last, Compare comp); first: 정렬을 시작할 범위의 첫 번째 요소를 가리키는 반복자 last: 정렬을 종료할 범위의 마지막 다음 요소를 가리키는 반복자 comp: (optional) 정렬 기준을 제공하는 함수나 함수 객체로 람다식으로 표현될 수 있다 만약 비교함수 comp가 사용자에 의해 제공되지 않은 경우 기.. 2024. 2. 20.
C++ 에서 구조체와 클래스의 차이 아래에서 보듯이 C++에서 구조체와 클래스는 사용방법과 기능이 매우 유사하다. C++의 구조체는 멤버함수를 추가할 수도 있으며, 명시적으로 생성자/소멸자를 추가할 수 있고 명시적으로 정의하지 않으면 수명주기에 따라 호출되는 기본 생성자/소멸자가 컴파일러에 의해 제공된다는 점이 클래스와 동일하다. 반면 구조체와 클래스의 주요 차이는 접근제어 지시자의 기본값이 다르다는 것이다. 접근제어 지시자를 따로 명시하지 않고 정의한 경우 구조체는 기본적으로 public 이고, 클래스는 기본적으로 private이다. #include using namespace std; struct Point { int x; int y; Point(int _x, int _y) : x(_x), y(_y) { cout 2024. 2. 14.
C++ ] 벡터의 최대값과 최소값 찾기, max_element, min_element C++에서는 헤더에 포함된 std::max_element와 std::min_element 함수를 사용하여 벡터의 최대값과 최소값을 찾을 수 있다. 리턴값은 가장 큰 또는 가장 작은 원소의 iterator이고 애스터리스크를 붙여 역참조해서 값을 가져올 수 있다. max_element 함수 사용 예시 #include #include #include int main() { std::vector numbers = {10, 5, 8, 3, 12, 7}; auto max_iterator = std::max_element(numbers.begin(), numbers.end()); int max_value = *max_iterator; std::cout 2024. 2. 10.
C++ ] leetCode 807 - Max Increase to Keep City Skyline 리트코드 807 문제 There is a city composed of n x n blocks, where each block contains a single building shaped like a vertical square prism. You are given a 0-indexed n x n integer matrix grid where grid[r][c] represents the height of the building located in the block at row r and column c. A city's skyline is the outer contour formed by all the building when viewing the side of the city from a distan.. 2024. 2. 8.