登录
OAmaster

You have an integer servers, which denote the number of serve

Constraints

N/A

Example 1

Input:

servers = 5

Output:

"funny"

Explanation: eric-explaination

解法

题目为占位测试题(Eric Test),直接返回字符串 "funny"

def eric_test(servers: int) -> str:
 return "funny"
class Solution {
    String ericTest(int servers) { return "funny"; }
}
class Solution {
public:
    string ericTest(int servers) { return "funny"; }
};

A good subsequence is defined as any subsequence obtained from a string by removing 0 - any numbers of characters. The length of the subsequence should be even. If the length of the subsequence is m then the first m/2 characters should be equal and the next m/2 characters should be equal. For example, '2222', '333444' are good subsequences, while '2221', '2344234' are not good subsequences. Find the length of the longest good subsequence. Function Description Complete the function findLengthOfLongestGoodSubsequence in the editor. findLengthOfLongestGoodSubsequence has the following parameter:

  • String s: the string to analyze Returns int: the length of the longest good subsequence

Constraints

An unkown mystery for now

Example 1

Input:

s = "2222"

Output:

4

Explanation: The entire string is a good subsequence since the first two characters '22' are equal and the next two characters '22' are also equal. Hence, the length of the longest good subsequence is 4. (Provided by Groot with from the west coast. PLS DONT HESITATE to correct me if you find it wrong. Many thanks in advance!)

Example 2

Input:

s = "333444"

Output:

6

Explanation: The entire string is a good subsequence since the first three characters '333' are equal and the next three characters '444' are also equal. Hence, the length of the longest good subsequence is 6. (Provided by Groot with from the west coast. PLS DONT HESITATE to correct me if you find it wrong. Many thanks in advance!)

Example 3

Input:

s = "2221"

Output:

0

Explanation: There is no subsequence that meets the criteria for a good subsequence. Hence, the length of the longest good subsequence is 0. (Provided by Groot with from the west coast. PLS DONT HESITATE to correct me if you find it wrong. Many thanks in advance!)

Example 4

Input:

s = "2344234"

Output:

4

Explanation: The subsequence '3443' is a good subsequence since the first two characters '34' are equal to the next two characters '34'. Hence, the length of the longest good subsequence is 4. (Provided by Groot with from the west coast. PLS DONT HESITATE to correct me if you find it wrong. Many thanks in advance!)

解法

设字符 c 出现 cnt[c] 次。如果只用一种字符 c,最长子序列 = cnt[c] 取偶后值。如果用两种 a, b 拼 "aa..bb..",需要 ab 前出现 ≥ 各 m/2 次 ⇒ 找两个字符 a, b,使 a 的前缀某些位置后 b 后缀数量都 ≥ 某值。预计算前缀计数 pre[i][c]、后缀计数 suf[i][c]。对每对 (a, b)(含 a == b)扫描分割点 i,记 len = 2 * min(pre[i][a], suf[i][b]),更新答案。复杂度 O(n · 10²),空间 O(n · 10),对数字串。

def find_length_of_longest_good_subsequence(s: str) -> int:
    n = len(s)
    pre = [[0] * 128 for _ in range(n + 1)]
    suf = [[0] * 128 for _ in range(n + 1)]
    for i in range(n):
        for c in range(128):
            pre[i + 1][c] = pre[i][c]
        pre[i + 1][ord(s[i])] += 1
    for i in range(n - 1, -1, -1):
        for c in range(128):
            suf[i][c] = suf[i + 1][c]
        suf[i][ord(s[i])] += 1
    best = 0
    for i in range(n + 1):
        for a in range(128):
            for b in range(128):
                m = min(pre[i][a], suf[i][b])
                if 2 * m > best:
                    best = 2 * m
    return best
class Solution {
    int findLengthOfLongestGoodSubsequence(String s) {
        int n = s.length();
        int[][] pre = new int[n + 1][128];
        int[][] suf = new int[n + 1][128];
        for (int i = 0; i < n; i++) {
            for (int c = 0; c < 128; c++) pre[i + 1][c] = pre[i][c];
            pre[i + 1][s.charAt(i)]++;
        }
        for (int i = n - 1; i >= 0; i--) {
            for (int c = 0; c < 128; c++) suf[i][c] = suf[i + 1][c];
            suf[i][s.charAt(i)]++;
        }
        int best = 0;
        for (int i = 0; i <= n; i++)
            for (int a = 0; a < 128; a++)
                for (int b = 0; b < 128; b++) {
                    int m = Math.min(pre[i][a], suf[i][b]);
                    if (2 * m > best) best = 2 * m;
                }
        return best;
    }
}
class Solution {
public:
    int findLengthOfLongestGoodSubsequence(string s) {
        int n = s.size();
        vector<vector<int>> pre(n + 1, vector<int>(128, 0));
        vector<vector<int>> suf(n + 1, vector<int>(128, 0));
        for (int i = 0; i < n; i++) {
            pre[i + 1] = pre[i];
            pre[i + 1][(int) s[i]]++;
        }
        for (int i = n - 1; i >= 0; i--) {
            suf[i] = suf[i + 1];
            suf[i][(int) s[i]]++;
        }
        int best = 0;
        for (int i = 0; i <= n; i++)
            for (int a = 0; a < 128; a++)
                for (int b = 0; b < 128; b++) {
                    int m = min(pre[i][a], suf[i][b]);
                    if (2 * m > best) best = 2 * m;
                }
        return best;
    }
};

We have three candies of different taste. A candies of type 1 B candies of type 2 C candies of type 3. We can perform the following operation any number of times: Choose any two candies of different taste and turn them into candies of remaining taste. Suppose, we take candy of type 1 and type 2 then they will be converted to type 3. Our goal is to ensure that all candies have the same taste. If this condition is not achievable, the print -1, else print, 'n', the minimum number of moves required to do so. Input Format The first line contains three space-separated integers denoting the Number of candies of type 1(A), Number of Candies of Type 2(B), and Number of candies of type 3(C) Output Format Print n which is the minimum number of moves required if the goal is achievable. If goal is not achievable print -1. Constraints 1≤A,B,C≤10⁸

Constraints

1 ≤ A, B, C ≤ 10⁸

Example 1

Input:

A = 1
B = 2
C = 3

Output:

-1

Explanation: It is not possible to make all candies of the same type with the given operation.

解法

每次操作消耗各 1 颗不同类型的糖,生成 2 颗第三种 ⇒ A ↔ A - 1, B ↔ B - 1, C ↔ C + 2(或其它对称组合)。不变量:A + B + C 守恒、(A - B) mod 3 守恒。要让 (A, B, C) -> (T, 0, 0)(任选一种为目标),需 (0, 0)(B, C) 满足模 3 关系。逐一检查目标 = A 全归 1(B = 0, C = 0):要求 B ≡ C (mod 3) 同时 A + B + C ≡ A (mod 3)(B + C) % 3 == 0。最少步数 = B + C(每一步至少消掉 1 颗 B 和 1 颗 C 或类似)。三种目标各检查可行性再取 min。

def minimum_moves_to_equal_candy_bars(A: int, B: int, C: int) -> int:
    best = float("inf")
    for tgt in [(B, C), (A, C), (A, B)]:
        x, y = tgt
        if (x + y) % 3 == 0 and (x - y) % 3 == 0:
            steps = max(x, y)
            best = min(best, steps)
    return best if best != float("inf") else -1
class Solution {
    int minimumMovesToEqualCandyBars(long A, long B, long C) {
        long best = Long.MAX_VALUE;
        long[][] targets = {{B, C}, {A, C}, {A, B}};
        for (long[] t : targets) {
            long x = t[0], y = t[1];
            if ((x + y) % 3 == 0 && ((x - y) % 3 + 3) % 3 == 0) {
                long steps = Math.max(x, y);
                if (steps < best) best = steps;
            }
        }
        return best == Long.MAX_VALUE ? -1 : (int) best;
    }
}
class Solution {
public:
    int minimumMovesToEqualCandyBars(long long A, long long B, long long C) {
        long long best = LLONG_MAX;
        vector<pair<long long, long long>> targets{{B, C}, {A, C}, {A, B}};
        for (auto& [x, y] : targets) {
            if ((x + y) % 3 == 0 && ((x - y) % 3 + 3) % 3 == 0) {
                long long steps = max(x, y);
                if (steps < best) best = steps;
            }
        }
        return best == LLONG_MAX ? -1 : (int) best;
    }
};
Pro 会员

解锁剩余 1 道 OA 真题

开通后立即解锁完整题面 + Python / Java / C++ 三语题解。 全站 165 家公司 · 1000+ 道 OA · 月度更新。