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. 添加crontab为什么要重定向输出到/dev/null

    如果crontab不重定向输出,并且crontab所执行的命令有输出内容的话,是一件非常危险的事情.因为该输出内容会以邮件的形式发送给用户,内容存储在邮件文件 /var/spool/mail/$use ...

  2. 编写类String的构造函数、析构函数和赋值函数

    已知类String的原型为: class String {   public:  String(const char *str = NULL); // 普通构造函数  String(const Str ...

  3. C#判断字符串为空的几种方法和效率判断

    C#判断字符串为空的几种方法和效率判断 string定义 1.1 string str1="":会定义指针(栈),并在内存里划一块值为空的存储空间(堆),指针指向这个空间.1.2 ...

  4. Node Security

    发一个很早之前做的一个小东西-安全管理软件-可以对U盘进行管理,对后台程序进行扫描.分析!

  5. STL set容器的一点总结

    整理了一下set常用语句,参看这篇http://www.cnblogs.com/BeyondAnyTime/archive/2012/08/13/2636375.html -------------- ...

  6. POJ 2084 Game of Connections

    卡特兰数. #include<stdio.h> #include<string.h> ; ; void mul(__int64 a[],int len,int b) { int ...

  7. I.MX6 uSDHC SD card register

    /**************************************************************************** * I.MX6 uSDHC SD card ...

  8. 20160124.CCPP详解体系(0003天)

    程序片段(01):HelloCGI.c 内容概要:CGI_HelloWorld #include <stdio.h> //01.CGI程序的编写规范 // (1).HTML文本格式声明后面 ...

  9. 使用Jekyll搭建博客

    最近闲来无事,捣鼓了一下Git以及Github,尝试了一下基于Jekyll搭建个人博客的方法,现在把整个过程进行一个总结(部分内容转自互联网): <img src="http://up ...

  10. 集合框架null与size=0

    被QA人员一眼指出来的问题,唉,好丢人 上栗子