1189. Maximum Number of Balloons

LeetCode easy original: C# #csharp #easy #leetcode #math #string
O texto da tarefa é traduzido do russo para o idioma selecionado. O código permanece sem alterações.

Дана string text. Вы хотите использовать символы строки text, чтобы сформировать как можно больше экземпляров слова "balloon".

Каждый символ строки text можно использовать не более одного раза. return максимальное количество экземпляров, которые можно сформировать.

Exemplo:

Input: text = "nlaebolko"

Output: 1

C# solução

correspondente/original
public class Solution {
    public int MaxNumberOfBalloons(string text) {
        int bCount = 0, aCount = 0, lCount = 0, oCount = 0, nCount = 0;
        foreach (char c in text) {
            if (c == 'b') {
                bCount++;
            } else if (c == 'a') {
                aCount++;
            } else if (c == 'l') {
                lCount++;
            } else if (c == 'o') {
                oCount++;
            } else if (c == 'n') {
                nCount++;
            }
        }
        lCount /= 2;
        oCount /= 2;
        return Math.Min(bCount, Math.Min(aCount, Math.Min(lCount, Math.Min(oCount, nCount))));
    }
}

C++ solução

rascunho automático, revisar antes de enviar
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
    public int MaxNumberOfBalloons(string text) {
        int bCount = 0, aCount = 0, lCount = 0, oCount = 0, nCount = 0;
        foreach (char c in text) {
            if (c == 'b') {
                bCount++;
            } else if (c == 'a') {
                aCount++;
            } else if (c == 'l') {
                lCount++;
            } else if (c == 'o') {
                oCount++;
            } else if (c == 'n') {
                nCount++;
            }
        }
        lCount /= 2;
        oCount /= 2;
        return min(bCount, min(aCount, min(lCount, min(oCount, nCount))));
    }
}

Java solução

correspondente/original
class Solution {
    public int maxNumberOfBalloons(String text) {
        int bCount = 0, aCount = 0, lCount = 0, oCount = 0, nCount = 0;

        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == 'b') {
                bCount++;
            } else if (text.charAt(i) == 'a') {
                aCount++;
            } else if (text.charAt(i) == 'l') {
                lCount++;
            } else if (text.charAt(i) == 'o') {
                oCount++;
            } else if (text.charAt(i) == 'n') {
                nCount++;
            }
        }

        lCount = lCount / 2;
        oCount = oCount / 2;

        return Math.min(bCount, Math.min(aCount, Math.min(lCount, Math.min(oCount, nCount))));
    }
}

Python solução

correspondente/original
class Solution:
    def maxNumberOfBalloons(self, text: str) -> int:
        bCount = 0
        aCount = 0
        lCount = 0
        oCount = 0
        nCount = 0

        for char in text:
            if char == 'b':
                bCount += 1
            elif char == 'a':
                aCount += 1
            elif char == 'l':
                lCount += 1
            elif char == 'o':
                oCount += 1
            elif char == 'n':
                nCount += 1

        lCount = lCount // 2
        oCount = oCount // 2

        return min(bCount, aCount, lCount, oCount, nCount)

Go solução

correspondente/original
func maxNumberOfBalloons(text string) int {
    bCount, aCount, lCount, oCount, nCount := 0, 0, 0, 0, 0

    for _, c := range text {
        switch c {
        case 'b':
            bCount++
        case 'a':
            aCount++
        case 'l':
            lCount++
        case 'o':
            oCount++
        case 'n':
            nCount++
        }
    }

    lCount /= 2
    oCount /= 2

    return min(bCount, aCount, lCount, oCount, nCount)
}

func min(a, b, c, d, e int) int {
    return min2(min2(a, b), min2(min2(c, d), e))
}

func min2(x, y int) int {
    if x < y {
        return x
    }
    return y
}

Algorithm

Подсчитайте количество появлений каждого символа 'b', 'a', 'l', 'o', 'n' в строке text.

Вычислите потенциал для каждого символа: для 'b' и 'a' потенциал равен количеству их появлений, для 'l' и 'o' потенциал равен количеству их появлений, деленному на 2, а для 'n' потенциал равен количеству его появлений.

find символ с наименьшим потенциалом среди 'b', 'a', 'l', 'o', 'n', который ограничивает количество возможных слов "balloon", и return это значение.

😎

Vacancies for this task

vagas ativas with overlapping task tags are mostradas.

Todas as vagas
Ainda não há vagas ativas.