Notice
Recent Posts
Recent Comments
Link
복's
[ LeetCode - 1189 ] Maximum Number of Balloons 본문
728x90
https://leetcode.com/problems/maximum-number-of-balloons/description/
Maximum Number of Balloons - LeetCode
Can you solve this real interview question? Maximum Number of Balloons - 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 ma
leetcode.com
문제를 easy중에서 랜덤으로 뽑았다.
java의 경우 속도가 많이 늦어서 다른 코드들과 비교해 봤는데 아마 스트림으로 만들어서(이미 int 값들이 들어가 있는데) 작업한게 문제가 있었던 것 같다.
[ 📌 풀이 ]
- dictionary & map에 입력받은 문자를 한 글자씩 넣고, 카운팅 해준다.
- l, o는 'balloon'을 만들기 위해 2 글자씩 필요하다.
- 방법1) dictionary에서 한 글자씩 순회하면서 -1
- 방법2) 각 글자들의 수 중에서 최솟 값을 정답으로 제출한다. (l, o 2글자 고려)
[ 📌 코드 - Python ]
"""
# Author : Lee In Bok
# Date : 2023.11.26(Sun)
# Spend Time: 07m 25s
# Runtime : 35 ms (Beats 88.6%)
# Memory : 16.4 MB (Beats 6.62%)
# Algoritm : Dictionary
"""
from collections import Counter
class Solution:
def maxNumberOfBalloons(self, text: str) -> int:
ans = 0
target = ['b', 'a', 'l', 'l', 'o', 'o', 'n']
dic = Counter(text)
while True:
for ch in target:
if ch in dic and dic[ch] != 0:
dic[ch] -= 1
else:
return ans
ans += 1
s = Solution()
result = s.maxNumberOfBalloons("loonbalxballpoon")
print(result)
[ 📌 코드 - Java ]
/**
* Author : Lee In Bok
* Date : 2023.11.26(Sun)
* Spend Time: 07m 25s
* Runtime : 13 ms (Beats 9.34%)
* Memory : 42.1 MB (Beats 7.12%)
* Algoritm : Hashing
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Q1189_MaximumNumberOfBalloons {
public static void main(String[] args) {
Solution s = new Solution();
int result = s.maxNumberOfBalloons("loonbalxballpoon");
System.out.println(result);
}
}
class Solution {
public int maxNumberOfBalloons(String text) {
Map<Character, Integer> map = new HashMap<>();
List<Integer> list = new ArrayList<>();
for(int i = 0; i < text.length(); i++){
char key = text.charAt(i);
map.put(key, map.getOrDefault(key, 0) + 1);
}
list.add(map.getOrDefault('b', 0));
list.add(map.getOrDefault('a', 0));
list.add(map.getOrDefault('l', 0) / 2);
list.add(map.getOrDefault('o', 0) / 2);
list.add(map.getOrDefault('n', 0));
return list.stream().mapToInt(Integer::intValue).min().getAsInt();
}
}
[ 📌 다른 사람 코드 - Java ]
class Solution {
public int maxNumberOfBalloons(String text) {
int[] arr = new int[5];
for(char c : text.toCharArray()) {
switch (c) {
case 'b' : arr[0]++; break;
case 'a' : arr[1]++; break;
case 'n' : arr[2]++; break;
case 'l' : arr[3]++; break;
case 'o' : arr[4]++; break;
}
}
int min1 = Math.min(Math.min(arr[0], arr[1]), arr[2]);
int min2 = Math.min(arr[3], arr[4]);
return Math.min(min1, min2 / 2);
}
}
둘러 보니까 이런식의 코드가 제일 많았고 시간도 잘 나오는거 같다.
글자를 만드는데 들어가는 글자들 중에서 가장 크기가 작은게 만들 수 있는 글자의 수의 한계니까
[ 📌 결과 - Python ]
[ 📌 결과 - Java ]
728x90
'알고리즘 > LeetCode' 카테고리의 다른 글
[ LeetCode - 182 ] Duplicate Emails (0) | 2023.12.01 |
---|---|
[ LeetCode - 104 ] Maximum Depth of Binary Tree (1) | 2023.12.01 |
[ LeetCode - 180 ] Consecutive Numbers (0) | 2023.11.25 |
[ LeetCode - 178 ] Rank Scores (2) | 2023.11.20 |
[ LeetCode - 176 ] Second Highest Salary (1) | 2023.11.18 |