본문 바로가기

Algorithm

(2138)
(C++) - LeetCode (easy) 1796. Second Largest Digit in a String https://leetcode.com/problems/maximum-ascending-subarray-sum/구현 문제였습니다.📕 풀이방법📔 입력 및 초기화이전 숫자를 저장할 변수 piv와 오르막 구간의 누적합의 최댓값 maxSum과 한 구간의 오르막 구간 누적합 sum을 선언한 후 모두 nums[0]번째 값으로 초기화합니다.📔 풀이과정1. 1번째 부터 nums.size() - 1 까지 for loop를 수행하며 다음을 진행합니다.  1-1. 이전 값 piv보다 현재 값이 크다면 오르막 구간이므로 sum에 현재 값을 누적해 줍니다.  1-2. 이전 값 piv가 현재 값 이상이라면 평지 혹은 내리막 구간이므로 현재까지의 sum값과 maxSum 중 최댓값을 maxSum에 저장합니다. 이후 sum은 다시..
(C++) - LeetCode (easy) 1796. Second Largest Digit in a String https://leetcode.com/problems/second-largest-digit-in-a-string/description/set을 사용해본 문제였습니다.📕 풀이방법📔 입력 및 초기화digit이 나온 문자들을 저장할 set numSet을 선언한 후 s에 대해 for loop를 수행하며 문자별 digit들을 저장해줍니다.📔 정답 출력 | 반환1. digit이 1개밖에 없다면 정답은 -1입니다2. set 자료구조는 오름차순으로 정렬되며 원소가 insert되므로 rbegin에서 iterator를 하나 증가시켜준 주소값을 반환해줍니다.📕 Code📔 C++class Solution {public: int secondHighest(string s) { set numSet; ..
(SQL) - LeetCode (easy) 1795. Rearrange Products Table https://leetcode.com/problems/rearrange-products-table/description/union all을 사용해본 문제였습니다.📕 풀이방법📔 정답 출력 | 반환기본적인 selection을 store별로 수행해 record들을 합쳐야하므로 union all을 해줘야합니다.📕 Code📔 ANSI SQLSELECT product_id, 'store1' AS store, store1 AS priceFROM ProductsWHERE store1 IS NOT NULLUNION ALLSELECT product_id, 'store2' AS store, store2 AS priceFROM ProductsWHERE store2 IS NOT NULLUNION ALLSELECT pro..
(C++) - LeetCode (easy) 1791. Find Center of Star Graph https://leetcode.com/problems/find-center-of-star-graph/description/자료구조 map을 활용한 문제였습니다.📕 풀이방법📔 입력 및 초기화1. key는 node번호, value는 node가 연결되어있는 다른 node들의 정보에 대한 map m을 선언해줍니다.2. edges의 정보에 대해 양방향 형식으로 map에 간선 정보를 저장합니다.3. nodeCnt를 선언해 m.size()가 곧 node의 총 개수이므로 이 값을 저장합니다.4. 정답 변수 ans를 선언해줍니다.📔 풀이과정m의 원소를 순회하며 el.second(node가 연결되어 있는 node들의 정보)가 nodeCnt - 1(자기 자신 node 제외)라면 ans에 node번호 el.first값을..
(C++) - LeetCode (easy) 1790. Check if One String Swap Can Make Strings Equal https://leetcode.com/problems/check-if-one-string-swap-can-make-strings-equal/description/전수조사 문제였습니다.📕 풀이방법📔 풀이과정1. s1 과 s2가 같은 문자열이면 true를 반환합니다.2. s1에대해 2차원 for loop를 돌며 i번째와 j번째를 swap해 s1과 s2가 같아졌는지 확인해줍니다. 같다면 true를 반환합니다.📔 정답 출력 | 반환false를 반환합니다.📕 Code📔 C++class Solution {public: bool areAlmostEqual(string s1, string s2) { if (s1 == s2) return true; for(int i = 0; i *..
(C++) - LeetCode (easy) 1784. Check if Binary String Has at Most One Segment of Ones https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/📕 풀이방법📔 입력 및 초기화연속된 1의 구간 개수 segment를 선언 후 0으로 초기화해줍니다.📔 풀이과정s의 원소를 순회하며 다음을 수행합니다.1. 현재 원소가 '1'이라면 연속된 1만큼 index를 증가시켜주고2. 해당 구간을 파악했으므로 segment를 1 더해줍니다.📔 정답출력segment 개수가 1이하인지 여부를 반환합니다.📕 Code📔 C++class Solution {public: bool checkOnesSegment(string s) { int segment = 0; for(int i = 0; ..
(C++) - LeetCode (easy) 1779. Find Nearest Point That Has the Same X or Y Coordinate https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/전수조사 문제였습니다.📕 풀이방법📔 입력 및 초기화정답 index ans와 최소 manhattan 거리에 대한 변수를 선언해 각각 -1, 최댓값(약 10억)으로 저장해줍니다.📔 풀이과정1. points의 원소를 순회하며 x나 y좌표가 같은 것들 중에 manhattan거리의 최솟값을 저장해줍니다.2. 다시 points를 순회하며 x나 y좌표가 같은 것들 중 최솟값을 가진 index를 ans에 저장하고 break합니다.📔 정답 출력 | 반환가장 앞 index를 가진 ans를 반환합니다.📕 Code📔 C++class Solution {public: ..
(C++) - LeetCode (easy) 1773. Count Items Matching a Rule https://leetcode.com/problems/count-items-matching-a-rule/📕 풀이방법📔 입력 및 초기화정답 변수 matches를 선언해 0으로 초기화해줍니다.📔 풀이과정items의 원소를 순회하며 다음을 수행합니다.1. ruleKey에 맞는 index를 찾아 idx에 저장해줍니다.2. 해당 item의 idx번째가 ruleValue와 같은지 검사합니다. 같다면 matches를 1씩 증가시켜줍니다.📔 정답 출력 | 반환matches를 반환합니다.📕 Code📔 C++class Solution {public: int getIndexToCompare(string ruleKey) { if (ruleKey == "type") return 0; i..