Frankenstein Code
Java
TCS CodeVita Zone 1
https://telegram.me/+_hn3cBQVbGliYTI9
import java.util.*;
public class FrankensteinPotionBrewer {
private static Map<String, List<List<String>>> recipeBook = new HashMap<>();
// Telegram - @PLACEMENTLELO
private static Map<String, Integer> memo = new HashMap<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine();
// Telegram - @PLACEMENTLELO
for (int i = 0; i < n; i++) {
String line = scanner.nextLine();
String[] parts = line.split("=");
String potion = parts[0];
String[] ingredients = parts[1].split("\\+");
// Telegram - @PLACEMENTLELO
recipeBook.putIfAbsent(potion, new ArrayList<>());
recipeBook.get(potion).add(Arrays.asList(ingredients));
}
String targetPotion = scanner.nextLine();
scanner.close();
int result = minOrbsToBrew(targetPotion);
System.out.println(result);
private static int minOrbsToBrew(String potion) {
if (!recipeBook.containsKey(potion)) {
return 0;
// Telegram - @PLACEMENTLELO
if (memo.containsKey(potion)) {
return memo.get(potion);
int placementlelo = Integer.MAX_VALUE;
for (List<String> recipe : recipeBook.get(potion)) {
int orbsRequired = recipe.size() - 1;
for (String ingredient : recipe) {
orbsRequired += minOrbsToBrew(ingredient);
// Telegram - @PLACEMENTLELO
placementlelo = Math.min(placementlelo, orbsRequired);
}
memo.put(potion, placementlelo);
return placementlelo;
Frankenstein Code
Java
TCS CodeVita Zone 1
https://telegram.me/+_hn3cBQVbGliYTI9
import java.util.*;
public class FrankensteinPotionBrewer {
private static Map<String, List<List<String>>> recipeBook = new HashMap<>();
private static Map<String, Integer> memo = new HashMap<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine().trim());
for (int i = 0; i < n; i++) {
String line = scanner.nextLine().trim();
if (line.isEmpty()) continue;
String[] Part = line.split("=");
String potion = Part[0].trim();
String[] items = Part[1].trim().split("\\+");
recipeBook.putIfAbsent(potion, new ArrayList<>());
recipeBook.get(potion).add(Arrays.asList(items));
String targetPotion = scanner.nextLine().trim();
scanner.close();
int result = minOrbsToBrew(targetPotion);
System.out.println(result);
private static int minOrbsToBrew(String potion) {
if (!recipeBook.containsKey(potion)) {
return 0;
if (memo.containsKey(potion)) {
return memo.get(potion);
int minOrbs = Integer.MAX_VALUE;
for (List<String> recipe : recipeBook.get(potion)) {
int orbsRequired = recipe.size() - 1;
for (String ingredient : recipe) {
orbsRequired += minOrbsToBrew(ingredient.trim());
minOrbs = Math.min(minOrbs, orbsRequired);
memo.put(potion, minOrbs);
return minOrbs;
def max_words(words, n, m):
cl = 0
ln = 1
wf = 0
for w in words:
wl = len(w)
if cl+wl<= m:
if cl>0:
cl+=1
cl += wl
wf += 1
else:
ln+=1
if ln>n:
break
cl = wl
wf+=1
return wf
# Telegram @PLACEMENTLELO
k = int(input())
words = [input().strip() for _ in range(k)]
n, m = map(int, input().split())
print(max_words(words, n, m))