geeksforgeeks@ Maximum Index (Dynamic Programming)
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)的更多相关文章
- [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 ...
- [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [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 ...
- [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 ...
- [Optimization] Advanced Dynamic programming
这里主要是较为详细地理解动态规划的思想,思考一些高质量的案例,同时也响应如下这么一句口号: “迭代(regression)是人,递归(recursion)是神!” Video series for D ...
- [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 ...
- About Dynamic Programming
Main Point: Dynamic Programming = Divide + Remember + Guess 1. Divide the key is to find the subprob ...
- 【动态规划】Dynamic Programming
动态规划 一.动态规划 动态规划(Dynamic Programming)是一种设计的技巧,是解决多阶段决策过程最优化问题的通用方法. 基本思想:将待求解问题分解成若干个子问题,先求解子问题,然后从这 ...
- Dynamic Programming
We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...
随机推荐
- PDO数据库
<?php //定义数据源$dsn = "mysql:dbname=testa;host=localhost"; //造PDO对象$pdo = new PDO($dsn,&q ...
- how bootstrap fit into our website design?
在web相关技术中,HTML代表了content,structure, CSS则负责styling,决定内容是如何展示的,javascript则主要负责interactive, behaviour. ...
- Qt之密码框不可选中、复制、粘贴、无右键菜单等
简述 在做用户登录.修改密码的时候,往往会用到密码框,其中一些功能要求与普通的输入框不同,例如:不能选中.复制.粘贴.无右键菜单等功能,当然设置密码不可见是必须的! 下面介绍两种方式来实现相同的效果. ...
- 【分享】哪个OS X版本支持哪个Xcode的版本?
在安装Xcode时,会碰到跟OS X操作系统匹配的问题,对照下下面几个表,以免给自己带来编译不过或者奇怪的错误等问题 以下列表来自网络: Xcode 1.0 - Xcode 2.x (before i ...
- Uva 10382 (区间覆盖) Watering Grass
和 Uva 10020几乎是一样的,不过这里要把圆形区域转化为能够覆盖的长条形区域(一个小小的勾股定理) 学习一下别人的代码,练习使用STL的vector容器 这里有个小技巧,用一个微小量EPS来弥补 ...
- poj 1986 Distance Queries
好像是模板题 当作练习题 不错: 要求任意两点之间的距离.可以假设一个根节点,然后所有点到根节点的距离,然后求出任意两点多公共祖先: 距离就变成了 dis[u]+dis[v] - 2*dis[ ...
- 警惕VPS服务商常用的超售手段
任何商业企业都希望将利益最大化,这是可以理解的.但如果做的过火最终损害的还是自己的利益.VPS服务提供商也一样,为了将利益最大化,他们往往会实用技术手段对所出售的VPS进行“超售”. 那么何谓“超售” ...
- JBPM4之decision节点:1、好学生 | 坏学生
JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流程图的插件 JBPM4入门——3.JBPM4开发环境的搭建 JBPM4入门—— ...
- 抛弃EF,20分构建一个属于自己的ORM框架
Poiuyt_cyc 博客园首页新随笔联系订阅管理随笔 - 11 文章 - 0 评论 - 111 抛弃EF,20分构建一个属于自己的ORM框架 相信EF大家都不陌生了,因为数据库表跟程序实体是一一 ...
- AE+C# 图层中增加相应属性标注
原文 AE+C# 图层中增加相应属性标注 ) { IGeoFeatureLayer pGeoFeatureLayer; ILineLabelPosition pLineLabelPosition; I ...