Java基础练习05--数组中最长递增子序列长度

小猴子下山,沿着下山的路有一排桃树,每棵树都结了一些桃子。小猴子想摘桃子,但是有一些条件需要遵守,小猴子只能沿着下 山的方向走,不能回头,每颗树最多摘一个,而且一旦摘了一棵树的桃子,就不能再摘比这棵树结的桃子少的树上的桃子。那么小 猴子最多能摘到几颗桃子呢?
举例说明,比如有5棵树,分别结了10,4,5,12,8颗桃子,
那么小猴子最多能摘3颗桃子,来 自于结了4,5,8颗桃子的桃树

其实,这就是给出了一个整型数组,让你求出其中最长的递增子序列长度。

代码:

package cn.drc.programming.test;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 猴子摘桃
 * @author drc
 */
public class MaxAmountOfPeaches {
    public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int trees = sc.nextInt(); // 几棵树
	int[] treePeaches = new int[trees]; // 树上的桃子数
	for(int x=0; x peaches[y] && subSeqLen[y]+1 > subSeqLen[x]) {
		    subSeqLen[x] = subSeqLen[y] + 1;
		}
	    }
	}
        Arrays.sort(subSeqLen);
	return subSeqLen[subSeqLen.length-1];
    }
}

 

你可能感兴趣的:(Java)