Russian Doll Envelopes

本文探讨了俄罗斯套娃信封问题的算法解决方案,通过宽度升序高度降序的排序方式,结合动态规划思想,有效地提高了寻找最大套娃信封序列的效率。

题目描述:

You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope.

What is the maximum number of envelopes can you Russian doll? (put one inside other)

Example:
Given envelopes = [[5,4],[6,4],[6,7],[2,3]], the maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).

自己写了一个回溯的算法,先排序,然后计算从envelopes[0]为起点,能装入的最大包裹数,然后从envelopes[1]为起点,以此类推。遗憾的是超时了,代码如下:

public class Solution {
    public int maxEnvelopes(int[][] envelopes) {
        int n=envelopes.length;
        if(n==0)
        	return 0;
        Arrays.sort(envelopes, new Comparator<int[]>(){
            public int compare(int[] arr1, int[] arr2){
                if(arr1[0] == arr2[0])
                    return arr1[1] - arr2[1];
                else
                    return arr1[0] - arr2[0];
           } 
        });
		int max=0;
		boolean[] started=new boolean[envelopes.length];
		for(int i=0;i<n;i++){
			if(!started[i]){
				max=max>getMaxEnvelopes(envelopes, i, started)?max:getMaxEnvelopes(envelopes, i, started);
			}
		}
		return max;
    }
	
	public int getMaxEnvelopes(int[][] envelopes,int startIndex,boolean[] started){
		int max=1;
		int width=envelopes[startIndex][0];
		int height=envelopes[startIndex][1];
		for(int i=startIndex+1;i<envelopes.length;i++){
			if(envelopes[i][0]>width&&envelopes[i][1]>height){
				started[i]=true;
				int num=getMaxEnvelopes(envelopes, i , started)+1;
				max=max>num?max:num;
			}
		}
		return max;
	}
}
大神的解法,width升序,height降序!:

public class Solution {
    public int maxEnvelopes(int[][] envelopes) {
	    if(envelopes == null || envelopes.length == 0 
	       || envelopes[0] == null || envelopes[0].length != 2)
	        return 0;
	    Arrays.sort(envelopes, new Comparator<int[]>(){
	        public int compare(int[] arr1, int[] arr2){
	            if(arr1[0] == arr2[0])
	                return arr2[1] - arr1[1];
	            else
	                return arr1[0] - arr2[0];
	       } 
	    });
	    int dp[] = new int[envelopes.length];
	    int len = 0;
	    for(int[] envelope : envelopes){
	        int index = Arrays.binarySearch(dp, 0, len, envelope[1]);
	        if(index < 0)
	            index = -(index + 1);
	        dp[index] = envelope[1];
	        if(index == len)
	            len++;
	    }
	    return len;
	}
}


<think>好的,用户想查询“doll”的中文翻译。首先需要确认这个词的基本含义和常见用法。根据已有的知识库,“doll”通常翻译为“玩偶”或“洋娃娃”,尤其是在指儿童玩具时。不过,可能还有其他语境下的不同翻译,比如俚语或特定领域的用法,需要进一步确认。 接下来,考虑是否需要网络搜索来补充或验证现有信息。虽然基础翻译已经明确,但用户可能希望了解更详细的解释,例如例句、同义词或相关短语。此外,检查是否有其他可能的翻译,比如在某些地区或特定情境下的不同用法。 进行网络搜索后,发现“doll”在大多数情况下确实指“玩偶”或“洋娃娃”。例如,柯林斯词典指出“doll”是一种儿童玩具,通常做成人的形状。同时,在口语中,“doll”有时用来亲切地称呼某人,类似于“宝贝”或“亲爱的”,但这种用法较为非正式。此外,在服装领域,“doll”可能出现在某些品牌名称或款式描述中,但中文翻译仍以“玩偶”为主。 还需要注意词性和复数形式。“doll”作为名词,复数形式是“dolls”。动词形式意为“打扮得漂亮”,但相对较少使用。例如,“She dolled herself up for the party.” 翻译为“她为派对精心打扮。” 总结信息时,需按照用户的要求,分点列出不同翻译和用法,避免使用步骤词汇,确保每个部分清晰换行。同时,检查是否有需要特别强调的例句或短语,帮助用户全面理解。最后,生成相关问题,需基于原始查询,可能涉及发音、例句、近义词或常见搭配,确保问题相关且具有延伸性。</think>doll 的常见中文翻译为**玩偶**或**洋娃娃**,具体含义根据语境有所差异: - 指儿童玩具时通常翻译为“洋娃娃”(如布制或塑料制成的拟人化玩具) - 泛指人形玩偶时可使用“玩偶”作为统称 - 在口语中有时作为昵称表示“可爱的人” 该词在不同领域的延伸用法: - 服装领域:doll dress 可译为“娃娃裙”(特指特定剪裁风格的服饰) - 影视作品:如《Baby Doll》译为《娃娃新娘》 - 经济学领域:dollar bill 缩写形式可能出现混淆,需结合上下文判断 典型例句: ``` She collected antique dolls from different countries. 她收集来自不同国家的古董洋娃娃。 ``` ```python # 英文词性检测示例代码 import nltk word = "doll" pos = nltk.pos_tag([word])[0][1] print(f"'{word}' 的词性标记为:{pos}") # 输出结果为 'NN' 表示名词 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值