목록알고리즘/leetcode (2)
흔한 덕후의 잡동사니
문제 : https://leetcode.com/problems/flood-fill/description/Solution인자로 받은 위치의 번호로부터 시작하여 4방향으로 인접한 위치에 같은 번호가 있을 경우 번호들을 모두 다른 값으로 바꾸는 문제이다. Flood Fill이라는 이름답게 이동할 수 있는 모든 위치를 찾는 문제가 보면 된다.#include #include #include class Solution{public: // 인자로 2차원 배열, 시작 위치(sr, sc), 변경하고자하는 색상 을 받는다. // 시작 위치에 있는 색상과 인접한 같은 색상을 인자로 받은 색상으로 변경하고, 변경된 2차원 배열을 반환하는 문제이다. std::vector> floodFill(std::vecto..

문제 : https://leetcode.com/problems/largest-rectangle-in-histogram/description/Solution히스토그램의 각 막대의 높이가 배열로 주어질 때 히스토그램 내 가장 넓이가 큰 직사각형의 넓이를 반환하는 문제이다. #include #include #include int getMaxArea(std::vector& heights) { heights.push_back(0); // 마지막에 0을 추가하여 모든 높이를 처리할 수 있도록 함 std::stack st; int maxArea = 0; // 전체를 N번 순회, O(n) for (int i = 0; i = heights[i]) { int h = heigh..