본문 바로가기

Algorithm/Implementation

(751)
(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, Rust) - LeetCode (easy) 1480. Running Sum of 1d Array https://leetcode.com/problems/running-sum-of-1d-array/description/간단 구현 문제였습니다.📕 풀이방법📔 입력 및 초기화📑 Pythonlist ans를 선언합니다.📑 Rust1. nums 의 len만큼의 capacity를 가진 정적 vector result를 mut로 선언합니다.2. 누적된 값을 저장하기 위한 mut 변수 sum을 선언 후 0으로 초기화합니다.📔 풀이과정📑 Python누적해 더한 값을 ans에 append합니다.📑 Rust누적해 더해진 sum을 result에 push합니다.📔 정답 출력 | 반환📑 Pythonans를 반환합니다.📑 Rustresult를 반환합니다.📕 Code📔 Pythonclass Solution: ..
(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..
(C++) - LeetCode (easy) 1422. Maximum Score After Splitting a String https://leetcode.com/problems/maximum-score-after-splitting-a-string/description/ 간단 전수조사 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 정답 변수 ms를 선언해 0으로 초기화해줍니다. 📔 풀이과정 1. s의 문자에 대해 loop를 수행하며 현재를 기준으로 왼쪽과 오른쪽 문자를 분리해 지역변수 a,b에 각각 저장합니다. 2. 문자열로부터 원하는 bit개수를 세 개수를 반환하는 countBit함수 문자 a,b에 대해서 수행합니다. 3. ms에 a의 0bit개수와 b의 1bit개수를 더한값과 비교해 최댓값을 저장합니다. 📔 정답 출력 | 반환 ms를 반환합니다. 📕 Code 📔 C++ class Solution { public: int c..