본문 바로가기

분류 전체보기

(1905)
(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) 1460. Make Two Arrays Equal by Reversing Subarrays https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/description/ 간단 정렬 문제였습니다. 📕 풀이방법 📔 풀이과정 부분 배열을 뒤집어서 target배열로 만들려면 size와 원소가 같고 index만 다르면 됩니다. 1. target과 arr을 오름차순으로 정렬해줍니다. 2. 원소를 순회하며 모두 같은지를 비교합니다. 📔 정답 출력 | 반환 모두 같다면 true를 아니면 false를 반환합니다. 📕 Code 📔 C++ class Solution { public: bool canBeEqual(vector& target, vector& arr) { sort(target.begin(), target.end()); sor..
(C++) - LeetCode (easy) 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/description/ find함수 사용과 split을 구현한 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 📔 풀이과정 1. stringstream을 이용, 구분자를 공백 한 글자로 split함수를 구현해 수행한 결과를 vector s에 저장해줍니다. 2. s에 대해 원소를 순회하며 접두어로 searchWord가 나왔는지 find함수의 결과로 확인합니다. 2-1. 찾지 못했다면 string::npos, 찾았다면 string::size_type(unsigned long long)을 반환하므로 0인지 확인하면 됩니다. 2-2. 찾았다면 i+1을 ..
(C++) - LeetCode (easy) 1450. Number of Students Doing Homework at a Given Time https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/description/ 간단 구현 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 정답 변수 studentCnt, 전체 학생 수 totalStudents를 선언 후 적절한 값으로 초기화해줍니다. 📔 풀이과정 totalStudent만큼 for loop를 수행하며 다음을 실행합니다. queryTime이 시작시간과 끝시간 사이에 있다면 studentCnt를 1씩 증가시켜줍니다. 📔 정답 출력 | 반환 studentCnt를 반환합니다. 📕 Code 📔 C++ class Solution { public: int busyStudent(vector& startTime, vect..
(C++) - LeetCode (easy) 1446. Consecutive Characters https://leetcode.com/problems/consecutive-characters/ 간단 구현 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 maxPower, curPower를 선언 후 1로 초기화해줍니다. 📔 풀이과정 1. 인접 문자가 같다면 현재 power를 1증가시켜준 후 maxPower와 최댓값을 비교해 maxPower를 갱신해줍니다. 2. 다르다면 현재 power를 1로 초기화해줍니다. 📔 정답 출력 | 반환 maxPower를 반환합니다. 📕 Code 📔 C++ class Solution { public: int maxPower(string s) { int maxPower = 1; int curPower = 1; for(int i = 1; i < s.size(); i++) { if(..
(C++) - LeetCode (easy) 1437. Check If All 1's Are at Least Length K Places Away https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/description/ 간단 구현문제였습니다. 📕 풀이방법 📔 입력 및 초기화 1의 index를 저장할 vector idx를 선언 후 nums를 순회하며 값을 저장해줍니다. 📔 풀이과정 1. 1이 없는경우 idx의 size가 0이고 거리는 항상 k이하이므로 true를 반환합니다. 2. idx의 원소를 순회하며 인접 원소의 거리가 k미만이라면 false를 반환합니다. 3. loop이후에는 모든 원소의 거리가 k이상이므로 true를 반환합니다. 📕 Code 📔 C++ class Solution { public: bool kLengthApart(vector& nums,..
(C++) - LeetCode (easy) 1431. Kids With the Greatest Number of Candies https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/ 구현 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 가진 사탕의 최댓값 maxRepresentCandyNum와 정답을 저장할 변수 canTakeMaxCandies를 선언해 적절히 초기화해 줍니다. 📔 풀이과정 candies에 대해 loop를 수행합니다. 1. 현재 사탕 개수에 extraCandies를 받았을 때 maxRepresentCandyNum이상이라면 true를, 아니라면 false를 canTakeMaxCandies에 push_back해줍니다. 📔 정답 출력 | 반환 canTakeMaxCandies를 반환합니다. 📕 Code 📔 C++ class Solution { pub..
(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..