본문 바로가기

전체 글

(1939)
(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) 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,..