牛客网 2018校招真题 爱奇艺 DNA序列

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32767041/article/details/86499434

Description

牛客网 2018校招真题 DNA序列
题目描述真的令人费解

Solving Ideas

题目中所说的DNA片段有:
长度为1的DNA片段:‘A’, ‘C’, ‘G’, ‘T’, 共Math.pow(4, 1)=4个
长度为2的DNA片段:‘AA’, ‘CC’, ‘GG’, ‘TT’, ‘AC’, ‘CA’, ‘AG’,‘GA’, ‘AT’, ‘TA’,‘CG’, ‘GC’, ‘CT’,‘TC’, ‘GC’, ‘CG’, 共Math.pow(4, 2)=16个

长度为n的DNA片段:…

求最短没有出现在字符串s中的DNA片段的长度

Time complexity : O ( n 2 ) O(n^2)
Space complexity : O ( n 2 ) O(n^2)

Solution

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author wylu
 */
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int[] counts = new int[26];
        for (char ch : br.readLine().toCharArray()) counts[ch - 'a']++;
        int k = Integer.parseInt(br.readLine());

        while ((k--) != 0) {
            //每次最大值减1,最终可使总价值达到最小
            int maxIdx = 0;
            for (int i = 1; i < 26; i++) {
                if (counts[i] > counts[maxIdx]) maxIdx = i;
            }
            counts[maxIdx]--;
        }

        int res = 0;
        for (int i = 0; i < 26; i++) res += counts[i] * counts[i];
        System.out.println(res);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_32767041/article/details/86499434
今日推荐