找关系在数组中
来源:互联网 发布:淘宝网中年女装秋装 编辑:程序博客网 时间:2024/04/29 20:07
You given an array:
3, 2, 1, 6, 5, 4, 9, 8, 7
you have to find a 3 tuple which has property a < b < c, also a is before b, b is before c in array.
Answer can have multiple tuples, you have to find any one.
In this array, answer will be 3, 6, 9
answer:
O(n) time algorithm: Use the same classic algorithm for Longest Increasing Subsequence, breaking when you find a subsequence of length 3.
-Using Patience Sort --- O(n) OR
-Using Dynamic Programming --- O(n*n)
make a binary search tree from a given array
try to get right side of node which full fill this condition
package com.zhuyu_deng.test;public class Test{static int a[] = new int[] { 3, 2, 1, 6, 5, 4, 9, 8, 7 };static int c[] = new int[a.length];public static void main(String args[]){int b[] = new int[a.length];for (int i = 0; i < b.length; ++i)b[i] = 1;for (int i = 1; i < a.length; ++i){int maxLen = 1;for (int j = 0; j < i; ++j){if (a[j] < a[i] && b[j] + 1 > maxLen){maxLen = b[j] + 1;b[i] = maxLen;c[i] = j;}if (maxLen >= 3)break;}if (maxLen >= 3){print(i);System.out.println();break;}}}private static void print(int x){if (x != 0){print(c[x]);System.out.print(a[x] + " ");} else{System.out.print(a[x] + " ");}}}
package com.zhuyu_deng.test;import java.util.ArrayList;import java.util.List;public class Test{static int[] array = new int[] { 3, 2, 1, 6, 5, 4, 9, 8, 7 };static String finalString = "";public static void main(String[] args){List<String> touples = new ArrayList<String>();for (int i = 0; i < array.length; i++){getAllTouples(touples, i, 0);}System.out.println(touples);}public static List<String> getAllTouples(List<String> touples, int index, Integer pos){int pivotElement = array[index];for (int i = index + 1; i < array.length; i++){if (pivotElement < array[i] && pos < i){finalString = finalString + pivotElement;if (finalString.length() == 2){finalString = finalString + array[i];touples.add(finalString);finalString = finalString.substring(0, finalString.length() - 2);pos = i;i = index;} else{getAllTouples(touples, i, pos);finalString = finalString.substring(0, finalString.length() - 1);}}}return touples;}}
- 找关系在数组中
- 在有序数组中,找一个数字
- 在大数组中找重复和丢失的整数
- 算法案例 -- 在数组中找数量最多的数
- 在旋转数组中找最小的值
- 在一个整型数组中找孤立的数字
- offer38-找数字在有序数组中出现的次数
- 有序数组中找中位数
- 数组中找最大差值
- 找数组中重复元素
- 有序数组中找中位数
- 在字符串中找匹配
- 百度面试 在一个int数组中找这样一种数
- 在一个数组中找一个三元组,使得它们的和等于指定值
- 在数组中 找左边都比其小右边都比其大的元素
- 相邻元素差的绝对值都是1,在这样的数组中找目标元素
- Two sum(在数组中找两个数,使其和为指定值)
- 宝典练习1之在排序数组中找指定元素出现次数
- 用命令行编译libjpeg.lib
- iOS SDK:iOS调试技巧
- 使用pt-query-digest分析mysql slow query log
- Struts2中的ModelDriven机制及其运用
- jquery实现 图片延迟加载
- 找关系在数组中
- Silverlight WCF RIA服务(十六)表示模型
- PMP项目管理之核心:项目经理角色定位
- 数学之路(3)-机器学习(3)-机器学习算法-神经网络[7]【感知器】视频教程
- POJ 2406 Power Strings
- Robotium环境搭建
- 【TODO】
- android简单实现页面滑动关闭功能
- Android SDK Training - Fragment coding