반응형
https://leetcode.com/problems/maximum-number-of-balloons/description/
문자열과 자료구조를 다룬 문제였습니다.
📕 풀이방법
📔 입력 및 초기화
key는 alphabat문자, value는 빈도수를 나타내는 변수 alphaMap을 선언 후 text를 순회하며 빈도수를 저장해줍니다. 정답 변수 ans를 선언하고 큰 값으로 초기화해줍니다.
📔 풀이과정
b, a, l, l, o, o, n 중 b, a, n은 1개씩 필요하며 빈도수와 ans를 비교해 최솟값을 저장합니다. l과 o는 문자열을 만들기 위해 필요한 개수가 2개이므로 나타난 빈도수의 / 2로 생성된 문자열 개수를 판별해 ans에 저장해줍니다.
📔 정답 출력 | 반환
ans를 반환합니다.
📕 Code
📔 C++
class Solution {
map <char, int> alphaMap;
public:
int maxNumberOfBalloons(string text) {
alphaMap.clear();
for(auto t: text) alphaMap[t]++;
int ans = 0x3f3f3f3f;
ans = min(alphaMap['b'], ans);
ans = min(alphaMap['a'], ans);
ans = min(alphaMap['l'] / 2, ans);
ans = min(alphaMap['o'] / 2, ans);
ans = min(alphaMap['n'], ans);
return ans;
}
};
*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.
'Algorithm > Implementation' 카테고리의 다른 글
(C++) - LeetCode (easy) 1207. Unique Number of Occurrences (1) | 2023.12.05 |
---|---|
(C++) - LeetCode (easy) 1200. Minimum Absolute Difference (0) | 2023.12.04 |
(C++) - LeetCode (easy) 1184. Distance Between Bus Stops (0) | 2023.11.27 |
(C++) - LeetCode (easy) 1175. Prime Arrangements (2) | 2023.11.20 |
(C++) - LeetCode (easy) 1154. Day of the Year (0) | 2023.11.14 |