maximum-gap(经过了提示)

本文介绍了一种改进的桶排序算法实现,通过计算最大间隔来找出整数数组中最大值与最小值之间的最大间隔。该算法首先确定数组中的最大值和最小值,然后计算合适的桶间隔,使用两个数组记录每个桶内的最小值和最大值,最后遍历这些值来找到最大的间隔。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面的分桶个数做的不太好,原来的解法是用的 

int gap = (big - small) / vlen;
        if (gap == 0) {
            gap = 1;
        }

下面是现在的Java解法:

package com.company;

import java.util.*;

class Solution {
    public int maximumGap(int[] nums) {
        if (nums.length < 2) {
            return 0;
        }

        // 用 Radix 排序
        int small = Integer.MAX_VALUE;
        int big = 0;
        for (int num : nums) {
            if (num < small) {
                small = num;
            }
            if (num > big) {
                big = num;
            }
        }
        System.out.println("big is " + big + " small " + small);
        int gap = (big - small - 1) / (nums.length - 1) + 1;
        if (gap == 0) {
            return 0;
        }
        int gap_num = (big - small) / gap + 1;
        int[] first = new int[gap_num];
        int[] second = new int[gap_num];
        // [ )
        System.out.println("gap is " + gap + " len is " + nums.length + "big is " + big + " small " + small);
        for (int num : nums) {
            int index = (num - small) / gap;
            if (first[index] == 0 || num < first[index]) {
                first[index] = num;
            }
            if (second[index] == 0 || num > second[index]) {
                second[index] = num;
            }
        }
        int ret = -1;
        int last = -1;
        for (int i=0; i<gap_num; i++) {
            if (first[i] == 0) {
                continue;
            }
            if (last == -1) {
                last = second[i];
                ret = last - first[i];
            }
            else {
                if (first[i] - last > ret) {
                    ret = first[i] - last;
                }
                if (second[i] - first[i] > ret) {
                    ret = second[i] - first[i];
                }
                last = second[i];
            }

        }
        return ret;
    }
}

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello!");
        Solution solution = new Solution();

        int[] nums = {1,1,1,1,1,5,5,5,5,5};

        int ret = solution.maximumGap(nums);
        System.out.printf("Get ret: %s\n", ret);

        /*Iterator<List<Integer>> iterator = ret.iterator();
        while (iterator.hasNext()) {
            Iterator iter = iterator.next().iterator();
            while (iter.hasNext()) {
                System.out.printf("%d,", iter.next());
            }
            System.out.println();
        }*/

        System.out.println();

    }
}

 

seqkit seq "$file" -s --id ">$seq_id" -w 100 > output.fasta Error: unknown flag: --id Usage: seqkit seq [flags] Flags: -k, --color colorize sequences - to be piped into "less -R" -p, --complement complement sequence, flag '-v' is recommended to switch on --dna2rna DNA to RNA -G, --gap-letters string gap letters to be removed with -g/--remove-gaps (default "- \t.") -h, --help help for seq -l, --lower-case print sequences in lower case -M, --max-len int only print sequences shorter than or equal to the maximum length (-1 for no limit) (default -1) -R, --max-qual float only print sequences with average quality less than this limit (-1 for no limit) (default -1) -m, --min-len int only print sequences longer than or equal to the minimum length (-1 for no limit) (default -1) -Q, --min-qual float only print sequences with average quality greater or equal than this limit (-1 for no limit) (default -1) -n, --name only print names/sequence headers -i, --only-id print IDs instead of full headers -q, --qual only print qualities -b, --qual-ascii-base int ASCII BASE, 33 for Phred+33 (default 33) -g, --remove-gaps remove gaps letters set by -G/--gap-letters, e.g., spaces, tabs, and dashes (gaps "-" in aligned sequences) -r, --reverse reverse sequence --rna2dna RNA to DNA -s, --seq only print sequences -u, --upper-case print sequences in upper case -v, --validate-seq validate bases according to the alphabet Global Flags: --alphabet-guess-seq-length int length of sequence prefix of the first FASTA record based on which seqkit guesses the sequence type (0 for whole seq) (default 10000) --compress-level int compression level for gzip, zstd, xz and bzip2. type "seqkit -h" for the range and default value for each format (default -1) --id-ncbi FASTA head is NCBI-style, e.g. >gi|110645304|ref|NC_002516.2| Pseud... --id-regexp string regular expression for parsing ID (default "^(\\S+)\\s?") -X, --infile-list string file of input files list (one file per line), if given, they are appended to files from cli arguments -w, --line-width int line width when outputting FASTA format (0 for no wrap) (default 60) -o, --out-file string out file ("-" for stdout, suffix .gz for gzipped out) (default "-") --quiet be quiet and do not show extra information -t, --seq-type string sequence type (dna|rna|protein|unlimit|auto) (for auto, it automatically detect by the first sequence) (default "auto") -j, --threads int number of CPUs. can also set with environment variable SEQKIT_THREADS) (default 4)什么意思
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值