본문 바로가기

Algorithm/자료구조

(110)
(C++) - LeetCode (easy) 1624. Largest Substring Between Two Equal Characters https://leetcode.com/problems/largest-substring-between-two-equal-characters/📕 풀이방법📔 입력 및 초기화1. alphabat별 index를 저장할 map 변수 alphaIndexMap을 선언해 줍니다.2. 정답 변수 ans를 선언 후 -1로 초기화합니다.📔 풀이과정s에 대해 for loop를 수행합니다.1. 한 번이라도 나온 적 있는 alphabat이라면 두 문자의 거리는 i - 해당index - 1이므로 ans와 거리 값 중 최댓값을 저장해줍니다.2. 한 번도 나온 적 없다면 최초의 alphabat의 index를 저장해줍니다.📔 정답 출력 | 반환ans를 반환합니다.📕 Code📔 C++class Solution {public: ..
(C++) - LeetCode (easy) 1614. Maximum Nesting Depth of the Parentheses https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/description/stack을 이용한 문제였습니다.📕 풀이방법📔 입력 및 초기화정답 변수 depth와 stack st를 선언 후 적절히 초기화해줍니다.📔 풀이과정( 부터 )까지는 하나의 depth 범위라고 할 수 있습니다. 따라서 (인 경우 stack에 넣고 )인 경우 stack에 남아있는 열린 괄호 (의 개수가 현재 범위의 depth가 됩니다.1. s의 문자에 대해 for loop를 수행합니다.  1-1. 닫힌 괄호 )라면 depth와 stack의 size를 비교해 더 큰 값을 저장해줍니다. 이후 계산이 완료되었으므로 stack에서 (를 pop해줍니다.  1-2. 열린..
(C++) - LeetCode (easy) 1544. Make The String Great https://leetcode.com/problems/make-the-string-great/description/ stack을 이용해 해결한 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 stack st, 정답 변수 ans를 선언해줍니다. 📔 풀이과정 1. s의 원소를 수행하면서 다음을 확인해줍니다. 1-1. st에 원소가 있으면 st의 top과 s[i]가 같은지를 확인해 한쪽이 대문자, 다른 한 쪽이 소문자라면 pop 해줍니다. 1-2. 아닌 경우 인접한 부분이 good하므로 st에 push합니다.2. while loop흫 수행하면서 ans에 st의 원소를 넣어줍니다. 이 때 순서에 주의합니다. stack이므로 거꾸로 저장되어 있어 top을 ans의 앞에 문자열을 붙여주면 됩니다. 📔 정답 출력 | ..
(C++) - LeetCode (easy) 1528. Shuffle String https://leetcode.com/problems/shuffle-string/ map 을 이용한 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 key 는 index, value로 문자를 저장할 map 변수 m과 정답 변수 ans를 선언해줍니다. 📔 풀이과정 indices에 대해 loop를 수행하며 현재 위치 indices[i]의 값은 s[i]임을 m에 key, value로 저장해줍니다. 📔 정답 출력 | 반환 ans를 반환해줍니다. 📕 Code 📔 C++ class Solution { public: string restoreString(string s, vector& indices) { map m; string ans; for(int i = 0; i
(C++) - LeetCode (easy) 1507. Reformat Date https://leetcode.com/problems/reformat-date/description/ map을 사용해본 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 1. 한 문자열을 공백을 구분자로 잘라 vector 으로 반환하는 split 함수를 구현합니다. stringstream을 사용하면 됩니다.2. split된 문자열을 dateInfo에 저장합니다. key는 변환할 문자, value에는 변환될 문자를 저장할 monthMap, dayMap를 선언 후 맞는 형태로 저장합니다. 11th, 12th 외 숫자 1의 자리가 1또는 2라면 st, nd를 붙여야 됨에 주의합니다. 📔 풀이과정 split 된 dateInfo의 day, month, year에 해당하는 부분을 지역변수를 선언해 저장합니다. 📔 정답..
(C++) - LeetCode (easy) 1408. String Matching in an Array https://leetcode.com/problems/string-matching-in-an-array/description/ set 과 문자열 검색 함수 find를 사용해본 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 set s와 vector ans를 선언해줍니다. 📔 풀이과정 1. 자기 자신이 아닌 문자열을 서로 비교해 부분 문자열이라면 해당 값을 s에 insert해줍니다. 2. s의 원소들을 ans에 push_back해줍니다. 📔 정답 출력 | 반환 ans를 반환합니다. 📕 Code 📔 C++ class Solution { public: vector stringMatching(vector& words) { set s; vector ans; for(int i = 0; i < words.size();..
(C++) - LeetCode (easy) 1389. Create Target Array in the Given Order https://leetcode.com/problems/create-target-array-in-the-given-order/description/ Create Target Array in the Given Order - LeetCode Can you solve this real interview question? Create Target Array in the Given Order - Given two arrays of integers nums and index. Your task is to create target array under the following rules: * Initially target array is empty. * From left to right read n leetcode.c..
(C++) - LeetCode (easy) 1331. Rank Transform of an Array https://leetcode.com/problems/rank-transform-of-an-array/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 여러 자료구조를 사용해 푼 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 1. 더 큰 원소가 queue front에 오도록 priority_queue pq를 선언 후 arr의 원소를 삽입합..