从最大三角形周长看动态规划
Posted TypeOVOcode
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从最大三角形周长看动态规划相关的知识,希望对你有一定的参考价值。
题目描述:给定一个数组,从中找出三个数,使其能够构成三角形并且三角形的周长最大。
public class triangle {
private int getpos(int arr[],int low,int high){
int pivotkey=arr[low];
while(low<high){
while(low<high&&arr[high]>=pivotkey){
--high;
}
arr[low]=arr[high];
while(low<high&&arr[low]<=pivotkey){
++low;
}
arr[high]=arr[low];
}
arr[low]=pivotkey;
return low;
}
public void quicksort(int arr[],int low,int high){
if(low<high){
int pivot=getpos(arr,low,high);
quicksort(arr,low,pivot-1);
quicksort(arr,pivot+1,high);
}
}
public int getangle(int arr[]){
int ans=0;
int len=arr.length;
quicksort(arr,0,len-1);
for(int i=len-3;i>=0;i--){
if(arr[i]+arr[i+1]>arr[i+2]){
ans=(arr[i]+arr[i+1]+arr[i+2]);
break;
}
}
return ans;
}
public static void main(String[] args) {
triangle tri=new triangle();
int[] arr={1,89,77,3,4,5,12,10,9};
int ans=tri.getangle(arr);
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println(ans);
}
}
//"C:\Program Files (x86)\Java\jdk1.8.0_121\bin\java.exe" "-javaagent:D:\Inteljj\IntelliJ IDEA 2018.1.6\lib\idea_rt.jar=39694:D:\Inteljj\IntelliJ IDEA 2018.1.6\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_121\jre\lib\rt.jar;D:\triangle\out\production\triangle" triangle
//1 3 4 5 9 10 12 77 89 31
//Process finished with exit code 0
按照题目要求,解法一:最先想到的无非是暴力求解,设立三个变量i,j,k,依次对比,时间复杂度O(n^3);
解法二:先排序,然后从后往前遍历,若arr[n-3]+arr[n-2]>arr[n-1]不满足条件,则arr[n-4]+arr[n-3]>arr[n-1]一定不满足。故此,按照从后往前的顺序,若arr[n-3]+arr[n-2]>arr[n-1]一旦满足,即为最大周长的三角形(动态规划)。时间复杂度O(nlogn+n)=o(nlogn)。
以上是关于从最大三角形周长看动态规划的主要内容,如果未能解决你的问题,请参考以下文章