KEMBAR78
Frankenstein Code | PDF | Programming Paradigms | Computer Science
0% found this document useful (0 votes)
1K views6 pages

Frankenstein Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views6 pages

Frankenstein Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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))

You might also like