找关系在数组中

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;
	}
}


可以使用快速选择算法来数组中第 k 个小的元素。 快速选择算法的基本思想是,选择一个基准元素,将数组中小于基准元素的元素放在基准元素的左边,将大于基准元素的元素放在基准元素的右边,然后比较基准元素的位置和 k 的大小关系,如果基准元素的位置等于 k,就到了第 k 个小的元素,如果基准元素的位置大于 k,就在基准元素的左边继续查,如果基准元素的位置小于 k,就在基准元素的右边继续查。 以下是使用快速选择算法数组中第 k 个小的元素的 Python 代码: ```python def quick_select(nums, k): pivot = nums[0] left = [x for x in nums if x < pivot] right = [x for x in nums if x > pivot] mid = [x for x in nums if x == pivot] if k <= len(left): return quick_select(left, k) elif k <= len(left) + len(mid): return pivot else: return quick_select(right, k - len(left) - len(mid)) ``` 在这个函数中,我们首先选择数组中的第一个元素作为基准元素 pivot。然后,我们将数组分成三个部分:小于 pivot 的元素放在 left 数组中,大于 pivot 的元素放在 right 数组中,等于 pivot 的元素放在 mid 数组中。接下来,我们比较基准元素的位置和 k 的大小关系,如果基准元素的位置等于 k,就返回基准元素的值,如果基准元素的位置大于 k,就在 left 数组中继续查第 k 个小的元素,如果基准元素的位置小于 k,就在 right 数组中继续查第 k - len(left) - len(mid) 个小的元素。 这个函数的时间复杂度为 O(n),其中 n 是数组的长度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值