LeetCode #1189 MaximumNumberOfBalloons. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 데이터베이스, sql, query, 쿼리
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Maximum Number of Balloons.
Memory Usage: 8.9 MB, less than 100.00% of C++ online submissions for Maximum Number of Balloons.
LeetCode #1189
Q.
Given a string text, you want to use the characters of text to form as many instances of the word "balloon" as possible.
You can use each character in text at most once. Return the maximum number of instances that can be formed.
주어진 문자열에서, "balloon" 이라는 문자의 객체를 가능한한 많이 만들고 싶다.
문자열에서 각 문자는 한번만 쓸 수 있다. 최대한 만들수 있는 개수를 반환해라.
Example 1:
Input: text = "nlaebolko"
Output: 1
Example 2:
Input: text = "loonbalxballpoon"
Output: 2
Example 3:
Input: text = "leetcode"
Output: 0
Constraints:
- 1 <= text.length <= 10^4
- text consists of lower case English letters only.
Process
// Process
//1. Input text string
//2. Make lowercaseTable
//3. Fill the table in with count
//4. Count balloon string
//5. Return count
// 처리과정
//1. 문자열을 입력받는다.
//2. 소문자 테이블 준비한다.
//3. 문자열에서 소문자테이블을 채워넣는다.
//4. 만들 수 있는 balloon 문자 수를 센다.
//5. 수를 반환한다.
Code.. lemme see example code!!!
코드.. 예제코드를 보자!!!
class Solution {
public:
int maxNumberOfBalloons(string text) {
int resultCount = 0;
//2.
vector<int> alphabetTable;
for (int i = 0; i < 26; ++i) {
alphabetTable.push_back(0);
}
//3.
for (int i = 0; i < text.length(); ++i) {
++alphabetTable[text[i] - 97];
}
//4.
bool isDone = false;
while (!isDone) {
if (alphabetTable[0]-- > 0 && alphabetTable[1]-- > 0
&& alphabetTable[11]-- > 1 && alphabetTable[14]-- > 1
&& alphabetTable[13]-- > 0)
{
++resultCount;
--alphabetTable[11];
--alphabetTable[14];
} else {
isDone = true;
}
}
return resultCount;
}
};
Something else you might like...?
2019/02/19 - [Life/Health care] - Lysine 라이신 usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법
2019/02/28 - [Life/Health care] - Vitamin K, 비타민 K usage/side effects/dosage 효능/부작용/성인,소아 용법