Kth smallest element (in a sorted matrix ; In a BST tree ; In two sorted array)

包含了三個問題

1.Kth smallest element in a sorted matrix

2.Kth smallest element in a BST(Binary Search Tree)

3.Kth smallest element in two sorted array

/**
 * 包含了三個問題
 * 1.Kth smallest element in a sorted matrix
 * 2.Kth smallest element in a BST(Binary Search Tree)
 * 3.Kth smallest element in two sorted array
 * @author 健身小碼哥
 *
 */
public class KthSmallestElement {
	/**
	 * Kth smallest element in a sorted matrix
	 * 思路一:二分查找
	 * @param matrix
	 * @param k
	 * @return
	 */
	public int findKthSmallestElementInSortedMatrix(int matrix[][] , int k)
	{
		int n = matrix.length;
        	int startValue = matrix[0][0];
        	int endValue = matrix[n-1][n-1];
        
        	while (startValue < endValue) 
       		{
           		int midValue = (startValue + endValue) / 2;
            
			int smallerCount = 0;
            
			for (int i = 0; i < n; i++) 
			{
            			for(int j = 0 ; j < n ; j++)
            			{
            				if(matrix[i][j] <= midValue)
            				{
            					smallerCount++;
            				}
            			}
			}
			if (smallerCount < k) 
			{
				startValue = midValue + 1;
			} else 
			{
				endValue = midValue;    
			}
		}
		return startValue;
	}
	/**
	 * Kth smallest element in a BST(Binary Search Tree)
	 * @param root
	 * @param k
	 * @return
	 */
	public int findKthSmallestElementInBST(TreeNode root , int k)
	{
		if (root == null)
		{
			return -1;  
		}
		int leftSize = getTreeSize(root.left);  
		if (k == leftSize+1)
		{  
		    return root.value;  
		}else if (k <= leftSize)
		{  
		    return findKthElementInBST(root.left,k);  
		}else
		{  
		    return findKthElementInBST(root.right, k-leftSize-1);  
		}  
	}
	/**
	 * 查找兩個有序數組中第k小的數
	 * @param nums1			有序數組1
	 * @param startIndex1	數組1開始位置
	 * @param nums2			有序數組2
	 * @param startIndex2	數組2開始位置
	 * @param k				
	 * @return
	 */
	public int findKthSmallestElementInTwoSortArray(int nums1[] ,int startIndex1, int nums2[] , int startIndex2 , int k)
	{
		int m = nums1.length - startIndex1;
		int n = nums2.length - startIndex2;
		//確保數組2的長度較大,方便之後的操作
		if(m > n)
		{
			return findKthSmallestElementInTwoSortArray(nums2, startIndex2, nums1, startIndex1, k);
		}
		
		if(m == 0)
		{
			return nums2[k-1];
		}
		
		if(k <= 1)
		{
			return Math.min(nums1[startIndex1], nums2[startIndex2]);
		}
		
		int minMid = Math.min(k/2,m);
		int maxMid = k - minMid;
		
		if(nums1[minMid + startIndex1 - 1] < nums2[maxMid +startIndex2 -1])
		{
			return findKthSmallestElementInTwoSortArray(nums1, startIndex1+minMid, nums2, startIndex2, k - minMid);
		}else if(nums1[minMid + startIndex1 - 1] > nums2[maxMid+startIndex2 -1])
		{
			return findKthSmallestElementInTwoSortArray(nums1, startIndex1, nums2, startIndex2 + maxMid, k - maxMid);
		}else
		{
			return nums1[minMid+startIndex1 -1];
		}
		
	}
	/**
	 * 
	 * 獲取樹的大小(節點個數)
	 * @param root	根節點
	 * @return
	 */
	public int getTreeSize(TreeNode root)
	{  
		if (root == null)
		{
		    return 0;
		}
		return 1+getTreeSize(root.left) + getTreeSize(root.right);          
	}  
	
	public static void main(String[] args) {
		int nums1[] = {1,3,5,7,9};
		int nums2[] = {2,4,6,8,10};
		int s = new KthSmallestElement().findKthSmallestElementInTwoSortArray(nums1, 0, nums2, 0, 6);
		System.out.println(s);
	}
	
}
/**
 * 樹節點類
 * @author 健身小碼哥
 *
 */
class TreeNode
{
	TreeNode left;
	TreeNode right;
	int value;
}







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章