본문 바로가기

Algorithm/Implementation

(512)
(C++) - LeetCode (easy) 1491. Average Salary Excluding the Minimum and Maximum Salary https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/description/ 간단 정렬 및 소수점 type을 확인해본 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 1. 평균을 구할 sumOfExcluded를 선언 후 0으로 초기화합니다.2. salary를 오름차순으로 정렬합니다. 📔 풀이과정 salary의 첫 번째와 마지막 원소는 각각 최소, 최댓값입니다. 따라서 평균을 구하기 위해 먼저 이들을 제외한 원소의 누적합을 salary의 원소를 순회하며 sumOfExcluded에 저장합니다. 📔 정답 출력 | 반환 평균인 sumOfExcluded / (salary원소 개수 - 2)를 반환합니다. 📕 Code ..
(C++) - LeetCode (easy) 1486. XOR Operation in an Array https://leetcode.com/problems/xor-operation-in-an-array/description/ 간단 구현 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 xor연산을 적용할 배열 vector xors를 선언 후 start원소를 넣습니다. 📔 풀이과정 1. n만큼 start + 2 * i값을 xors에 넣어줍니다.2. 정답 변수 x를 선언 후 xors의 첫 번째 원소를 저장합니다. 3. xors원소만큼 loop를 돌면서 xor연산의 적용을 s에 누적해 적용해줍니다. 📔 정답 출력 | 반환 s를 반환합니다. 📕 Code 📔 C++ class Solution { public: int xorOperation(int n, int start) { vector xors(1, start); ..
(Python) - LeetCode (easy) 1480. Running Sum of 1d Array https://leetcode.com/problems/running-sum-of-1d-array/description/ 간단 구현 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 list ans를 선언합니다. 📔 풀이과정 누적해 더한 값을 ans에 append합니다. 📔 정답 출력 | 반환 ans를 반환합니다. 📕 Code 📔 Python class Solution: def runningSum(self, nums: List[int]) -> List[int]: ans = [nums[0]] for i in range(1,len(nums)): ans.append(ans[i-1] + nums[i]) return ans *더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.
(C++) - LeetCode (easy) 1475. Final Prices With a Special Discount in a Shop https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/description/ 간단 구현 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 1. 구조체 Info를 선언 후 member변수에 index와 price를 선언합니다.2. 정답 vector ans를 선언해줍니다. 📔 풀이과정 prices에 대해 2차원 for loop를 수행합니다. 1. 지역변수 info를 선언해 현재 price의 index와 price정보를 저장합니다. 2. 다시 for loop를 현재 index다음부터 수행하면서 현재 price보다 작다면 index와 price를 갱신합니다. 3. 갱신된 정보가 없다면 현재 price값을, 아니라면 할인된 가격을 a..
(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..