본문 바로가기

Algorithm/Brute Force

(118)
(C++) - LeetCode (easy) 1566. Detect Pattern of Length M Repeated K or More Times https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times/description/전수조사 문제였습니다.📕 풀이방법📔 입력 및 초기화연속된 pattern 개수 patternCount 선언 후 0으로 초기화해줍니다.📔 풀이과정시작점 start부터 arr.size() - 1까지 loop를 수행하며 다음을 진행합니다. 1. start ~ m-1까지 구간을 한 pattern으로 정하고 m만큼 건너뛰면서 for loop를 수행해 매 start+m+i ~ start+m*2-1 + i 구간별 pattern이 맞는지 확인하는 isPattern함수를 수행합니다.2. pattern이 맞으면 patternCount를 1..
(C++) - LeetCode (easy) 1534. Count Good Triplets https://leetcode.com/problems/count-good-triplets/description/ 간단 전수조사 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 정답변수 triples, arr의 길이 arrLength를 선언한 후 각각 0, arr.size()값을 저장해줍니다. 📔 풀이과정 arr에 대해 3중 for loop를 수행하며 조건에 맞으면 triples를 1씩 더해줍니다. 📔 정답 출력 | 반환 triples를 반환합니다. 📕 Code 📔 C++ class Solution { public: int countGoodTriplets(vector& arr, int a, int b, int c) { int triples = 0, arrLength = arr.size(); for(int i..
(C++) - LeetCode (easy) 1464. Maximum Product of Two Elements in an Array https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/description/ 간단 전수조사 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 정답변수 maxNum을 선언해 0으로 초기화합니다. 📔 풀이과정 nums의 원소를 2차원으로 순회하며 (nums[i]-1)*(nums[j]-1)의 최댓값을 찾아 maxNum에 저장합니다. 📔 정답 출력 | 반환 maxNum을 반환합니다. 📕 Code 📔 C++ class Solution { public: int maxProduct(vector& nums) { int maxNum = 0; for(int i = 0; i < nums.size(); i++) { for(int j = 0; j < n..
(C++) - LeetCode (easy) 1436. Destination City https://leetcode.com/problems/destination-city/description/ 전수조사로 푼 문제였습니다 📕 풀이방법 📔 입력 및 초기화 마지막 목적지를 저장할 curCity를 선언 후 첫 번쨰 길의 목적지값을 저장해줍니다. 📔 풀이과정 1. 다음 길이 없을때까지 for loop를 수행합니다. 2. paths의 원소를 순회하며 curCity를 시작점으로 가진 길을 찾아줍니다. 2-1. 찾았다면 curCity를 그 길의 도착점으로 갱신하고 다음길을 찾았음을 알려줄 지역변수 findNext를 1로 만든 후 break합니다. 3. findNext가 0이면 break합니다. 📔 정답 출력 | 반환 curCity를 반환합니다. 📕 Code 📔 C++ class Solution { pu..
(C++) - LeetCode (easy) 1399. Count Largest Group https://leetcode.com/problems/count-largest-group/ 전수조사 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 key는 각 자리수 합, value는 그 개수로 map 변수 groupMap, 정답 변수 ans와 largestGroupNum를 선언 후 적잘히 초기화해줍니다. 📔 풀이과정 1. 1 ~ n까지 loop를 수행하며 다음을 수행합니다. 1-1. 현재 원소 i의 각 자리수 합 값을 구합니다. 이 값을 sumDigits에 저장합니다. 1-2. sumDigits를 key로 해 value값을 증가시켜줍니다. 1-3. largestGroupNum에 갱신된 값과 비교해 최댓값을 넣어줍니다. 2. groupMap의 원소를 순회하며 largestGroupNum과 같은 값의 개수..
(C++) - LeetCode (easy) 1385. Find the Distance Value Between Two Arrays https://leetcode.com/problems/find-the-distance-value-between-two-arrays/description/ Find the Distance Value Between Two Arrays - LeetCode Can you solve this real interview question? Find the Distance Value Between Two Arrays - Given two integer arrays arr1 and arr2, and the integer d, return the distance value between the two arrays. The distance value is defined as the number of elements ar l..
(C++) - LeetCode (easy) 1380. Lucky Numbers in a Matrix https://leetcode.com/problems/lucky-numbers-in-a-matrix/description/ 전수조사 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 정답 vector luckies, 행 r, 열 c를 선언 후 적절히 초기화해줍니다. 📔 풀이과정 matrix의 모든 원소를 조사하며 매 원소마다 다음을 수행합니다. 1. 현재 행에서 가장 작은 값을 minRowNum에 저장해줍니다. 2. 현재 열에서 가장 큰 값을 maxRowNum에 저장해줍니다. 3. maxRowNum, minRowNum이 같다면 lucky number이므로 luckies 배열에 현재 원소를 push_back해줍니다. 📔 정답 출력 | 반환 luckies를 반환합니다. 📕 Code 📔 C++ class Solu..
(C++) - LeetCode (easy) 997. Find the Town Judge https://leetcode.com/problems/find-the-town-judge/description/ LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 전수조사 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 i번째 사람을 믿는 사람 수를 세기 위한 vector trusted와 i번째가 믿는 사람이 존재하는지 여부를 저장할 vector isTrust를 ..