Skip to content

Commit 9fd09d6

Browse files
committed
Add maximum balloons problem
1 parent 95c0971 commit 9fd09d6

File tree

37 files changed

+144
-798
lines changed

37 files changed

+144
-798
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ List of Programs related to data structures and algorithms
205205
| 7 | Valid Sudoku | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/7.validSudoku/validSudoku.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/7.validSudoku/validSudoku.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/7.validSudoku/validSudoku.md) | Medium | Using Map and Set methods |
206206
| 8 | Letter combinations | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/8.letterCombinations/letterCombinations.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/8.letterCombinations/letterCombinations.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/8.letterCombinations/letterCombinations.md) | Medium | Backtracking with hash table mapping |
207207
| 9 | LRU Cache | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/9.lruCache/lruCache.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/9.lruCache/lruCache.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/9.lruCache/lruCache.md) | Medium | Combination of Hash Table and doubly linked list |
208+
| 10 | Maximum number of balloons | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/10.maximumBalloons/maximumBalloons.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/10.maximumBalloons/maximumBalloons.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/10.maximumBalloons/maximumBalloons.md) | Easy | Hash map on characters |
208209

209210
## Sorting
210211

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package java1.algorithms.hashmap.maximumBalloons;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class MaximumBalloons {
7+
private static int maxNumBalloons(String text){
8+
Map<Character, Integer> balloonMap = new HashMap<>();
9+
balloonMap.put('b', 1);
10+
balloonMap.put('a', 1);
11+
balloonMap.put('l', 2);
12+
balloonMap.put('o', 2);
13+
balloonMap.put('n', 1);
14+
15+
Map<Character, Integer> countTextMap = new HashMap<>();
16+
17+
for (Character ch : text.toCharArray()) {
18+
if(balloonMap.containsKey(ch)) {
19+
countTextMap.put(ch, countTextMap.getOrDefault(ch, 0)+1);
20+
}
21+
}
22+
23+
int minBalloons = Integer.MAX_VALUE;
24+
25+
for (Character ch : balloonMap.keySet()) {
26+
if(countTextMap.get(ch) == 0) {
27+
return 0;
28+
}
29+
30+
minBalloons = Math.min(minBalloons, countTextMap.get(ch)/balloonMap.get(ch));
31+
}
32+
33+
return minBalloons;
34+
}
35+
public static void main(String[] args) {
36+
String text1 = "lenobxlao";
37+
System.out.println(maxNumBalloons(text1));
38+
String text2 = "lollbcolatnaboon";
39+
System.out.println(maxNumBalloons(text2));
40+
}
41+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
**Problem statement:**
2+
Given a string `text`, find the maximum number of possible instances of the word "balloon" using the characters of given text. Each character in text is used at most once.
3+
4+
## Examples:
5+
Example 1:
6+
7+
Input: text = "lenobxlao"
8+
9+
Output: 1
10+
11+
12+
Example 2:
13+
14+
Input: text = "lollbcolatnaboon"
15+
16+
Output: 2
17+
18+
19+
**Algorithmic Steps**
20+
This problem is solved with the help of map(or object) data structure to store the character count. The algorithmic approach can be summarized as follows:
21+
22+
1. Initialize a balloon map(`balloonMap`) to store the number of occurances of each character in "balloon" word.
23+
24+
2. Define a character occurances of given text(`text`) in `countTextMap` variable.
25+
26+
3. Iterate over characters of given text and update `countTextMap` with it's character occurances. The character count is calculated only for the characters which present in "balloon" word.
27+
28+
4. Define a `minBalloons` variable, initialized to maximum value.
29+
30+
5. Iterate over characters of balloon word, calculate the possible number of balloon words based on given text.
31+
32+
6. Return `minBalloons` indicating the possible maximum number of balloon words.
33+
34+
35+
**Time and Space complexity:**
36+
This algorithm has a time complexity of `O(n)`, Where `n` is the length of the input string (`text`). This is because we traverse the entire string once to count characters and then iterate over the fixed size(5 characters) of the `balloonMap`.
37+
38+
Here, it takes constant time complexity of `O(1)`. This is because the size of `balloonMap` is constant and doesn't scale with input size. Eventhough `countTextMap` grows based on number of characters in `text`, its size is capped by the distinct characters of "balloon" word.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function maxNumBalloons(text){
2+
let balloonObj = {'b': 1, 'a': 1, 'l':2, 'o':2, 'n': 1};
3+
let countTextObj = {};
4+
5+
for (const ch of text) {
6+
if(balloonObj[ch]) {
7+
countTextObj[ch] = (countTextObj[ch] || 0) +1;
8+
}
9+
}
10+
11+
let minBalloons = Number.POSITIVE_INFINITY;
12+
13+
for (const ch in balloonObj) {
14+
if(!countTextObj[ch]) {
15+
return 0;
16+
}
17+
minBalloons = Math.min(minBalloons, Math.floor(countTextObj[ch]/balloonObj[ch]));
18+
}
19+
20+
return minBalloons;
21+
}
22+
23+
const text1 = "lenobxlao";
24+
console.log(maxNumBalloons(text1));
25+
const text2 = "lollbcolatnaboon";
26+
console.log(maxNumBalloons(text2));
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
**Problem statement:**
2+
Given a string `text`, find the maximum number of possible instances of the word "balloon" using the characters of given text. Each character in text is used at most once.
3+
4+
## Examples:
5+
Example 1:
6+
7+
Input: text = "lenobxlao"
8+
9+
Output: 1
10+
11+
12+
Example 2:
13+
14+
Input: text = "lollbcolatnaboon"
15+
16+
Output: 2
17+
18+
19+
**Algorithmic Steps**
20+
This problem is solved with the help of map(or object) data structure to store the character count. The algorithmic approach can be summarized as follows:
21+
22+
1. Initialize a balloon object(`balloonObj`) to store the number of occurances of each character in "balloon" word.
23+
24+
2. Define a character occurances of given text(`text`) in `countTextObj` variable.
25+
26+
3. Iterate over characters of given text and update `countTextObj` with it's character occurances. The character count is calculated only for the characters which present in "balloon" word.
27+
28+
4. Define a `minBalloons` variable, initialized to infinity.
29+
30+
5. Iterate over characters of balloon word, calculate the possible number of balloon words based on given text.
31+
32+
6. Return `minBalloons` indicating the possible maximum number of balloon words.
33+
34+
35+
**Time and Space complexity:**
36+
This algorithm has a time complexity of `O(n)`, Where `n` is the length of the input string (`text`). This is because we traverse the string once to count characters and then iterate over the fixed size(5 characters) of the `balloonObj`.
37+
38+
Here, it takes constant time complexity of `O(1)`. This is because the size of `balloonObj` is constant and doesn't scale with input size. Eventhough `countTextObj` grows based on number of characters in `text`, its size is capped by the distinct characters of "balloon" word.

src/javascript/algorithms/misc/lodash/array/square.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/javascript/algorithms/misc/lodash/chunk/chunk.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/javascript/algorithms/misc/lodash/clamping/clamping.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/javascript/algorithms/misc/lodash/compact/compact.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/javascript/algorithms/misc/lodash/debounce/debounce.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/javascript/algorithms/misc/lodash/difference/difference.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/javascript/algorithms/misc/lodash/dropRightWhile/dropRightWhile.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/javascript/algorithms/misc/lodash/dropWhile/dropWhile.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/javascript/algorithms/misc/lodash/fill/fill.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)