http://www.practice.geeksforgeeks.org/problem-page.php?pid=129

Maximum Index

Given an array A of integers, find the maximum of j - i subjected to the constraint of A[i] <= A[j].

Example :

A : [3 5 4 2]

Output : 2 
for the pair (3, 4)

Input:

The first line contains an integer T, depicting total number of test cases. 
Then following T lines contains an integer N depicting the size of array and next line followed by the value of array.
Output:

Print the maximum difference of the indexes i and j in a separtate line.
Constraints:

1 ≤ T ≤ 30
1 ≤ N ≤ 1000
0 ≤ A[i] ≤ 100
Example:

Input
1
2
1 10
Output
1

import java.util.*;
import java.lang.*;
import java.io.*; class GFG { public static int func(int[] arr) { int n = arr.length;
int[] dp = new int[n];
int rs = 0; dp[0] = 0;
for(int i=1; i<n; ++i) {
for(int pre=0; pre<i; ++pre) {
if(arr[i] >= arr[pre]) {
dp[i] = Math.max(dp[i], dp[pre] + i - pre);
rs = Math.max(rs, dp[i]);
}
}
}
return rs;
} public static void main (String[] args) {
Scanner in = new Scanner(System.in);
int times = in.nextInt(); while(times > 0) {
--times; int n = in.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; ++i) {
arr[i] = in.nextInt();
} System.out.println(func(arr));
}
}
}

geeksforgeeks@ Maximum Index (Dynamic Programming)的更多相关文章

  1. [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  2. [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  3. [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. [Optimization] Advanced Dynamic programming

    这里主要是较为详细地理解动态规划的思想,思考一些高质量的案例,同时也响应如下这么一句口号: “迭代(regression)是人,递归(recursion)是神!” Video series for D ...

  6. [LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  7. About Dynamic Programming

    Main Point: Dynamic Programming = Divide + Remember + Guess 1. Divide the key is to find the subprob ...

  8. 【动态规划】Dynamic Programming

    动态规划 一.动态规划 动态规划(Dynamic Programming)是一种设计的技巧,是解决多阶段决策过程最优化问题的通用方法. 基本思想:将待求解问题分解成若干个子问题,先求解子问题,然后从这 ...

  9. Dynamic Programming

    We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...

随机推荐

  1. 简单理解Hibernate三种状态的概念及互相转化

    本文描述了Hibernate三种状态的概念及互相转化.Java对象的生命周期中有三种状态,而且互相转化.它们分别是临时状态,持久化状态,以及游离状态. AD:WOT2015 互联网运维与开发者大会 热 ...

  2. 函数fsp_fill_free_list

    /**********************************************************************//** Puts new extents to the ...

  3. 基础组件_panel面板

    面板作为承载其它内容的容器.这是构建其他组件的基础(比如:layout,tabs,accordion等).它还提供了折叠.关闭.最大化.最小化和自定义行为.面板可以很容易地嵌入到web页面的任何位置. ...

  4. 【笨嘴拙舌WINDOWS】tagTEXTMETRIC结构

    tagTEXTMETRIC用于定义在window输出文字时字的大小,其结构如下: 我在窗体上写了两句话,来详细解剖该结构(在MM_TEXT模式下输出) tmHeight表示一行文字的高度.改例中值为1 ...

  5. 【量化】docker

    查看docker docker ps docker ps -a 删除docker docker stop 8809752ca95a docker rm 8809752ca95a 打包fly cd ~/ ...

  6. mysql无法插入中文字符解决

    1. 基于可维护的角度,虽然latin1没什么问题,但是还是尽量换成utf8或者gb系列 2. 出现乱码时: SHOW VARIABLES LIKE 'character%'SHOW VARIABLE ...

  7. Mac 上安装MySQL

    http://blog.neten.de/posts/2014/01/27/install-mysql-using-homebrew/ http://www.wkii.org/mac-os-x-ins ...

  8. ecshop 分类树全部显示

    1.在page_header.lbi文件中加入 $GLOBALS['smarty']->assign('categories',get_categories_tree()); // 保证首页页面 ...

  9. 修改Chrome默认搜索引擎为Google.com

    在使用Chrome的时候,Google为增强本地化搜索,或将默认的Google搜索引擎转换为本地语言,如在中国会自动转到google.com.hk,日本会会自动转到google.co.jp,如果你是一 ...

  10. 【转】使用Xcode中的iOS SDK给iphone开发出第一个App程序

    之前已经折腾过用Xcode开发OS X的程序了,现在继续折腾,用iOS SDK开发移动设备(iphone/ipad/ipod touch)的程序. 1.从iOS Developer Library中找 ...