1。對於數組A[0,1,2,3,4,...,k],求得0<=i < j < k,且使得A[j] - A[i]為最大值。
最簡單也最容易想到的搜索兩遍,即可得到答案。i的位置從起始至倒數第二個位置,j的位置從末尾元素至i后一個位置,保存記錄最大的差值即可。
不過最簡單的方法復雜度為n的平方,其實令有一個時間復雜度很低的方法,及從前至后遍歷,添加一個保存當前訪問元素之前的最小的元素,最大值必定需要減去已訪問過元素的最小值才能夠獲得,這樣時間復雜度降至n。
class Solution {
public:
//i<j max(a[j]-a[i])
int arrayMaxGap(vector<int> &array) {
if(array.size() == 0 || array.size() == 1)
return -1;
if(array.size() == 2) {
return array[1] - array[0];
}
int maxres,premin;
maxres = array[1] - array[0];
premin = array[0];
for(int i = 1;i < array.size();i++) {
if (array[i] - premin > maxres)
maxres = array[i] - premin;
if (array[i] < premin)
premin = array[i];
}
return maxres;
}
}
2。對於遞增的數組A[0,1,2,3,4,...,k],數組B[0,1,2,3,4,...k'],對於0<=i<k,0<=j<k',對於計算出的A[i]+B[j],求其前k小個元素。
對於這個題目最先想到的思路,構造一個集合計算出所有的A[i]+B[j],然后求取前k小個元素就比較簡單了,但是如果k相對於構造出的集合比較小,這樣就比較浪費空間了。
對於任意一個元素A[i]+B[j],我們可以指導A[i+1]+B[j]和A[i]+B[j+1]是其接下來可確定的最近鄰的兩個大於其的元素,首先A[0]+B[0],接下來就是A[1]+B[0],A[0]+B[1]之中小的元素為第二小的元素,接下來繼續擴展第二小的元素得到A[2]+B[0],A[1]+B[1],或者A[0]+B[2],A[1]+B[1],所以如果用指針的話這樣無法保存,求取最小的結合不斷的膨脹,所以這里考慮利用最小堆每次得到多個元素中最小的元素。
//class for minKsum
struct Point {
int x;
int y;
int sum;
};
bool operator<(Point a,Point b){
return a.sum > b.sum;
}
class Solution {
public:
//array A,arrayB, obtain the minest k a[i]+b[j]. that a[0]+b[0] is the minest
vector<int> minKsum(vector<int> i_A,vector<int> i_B,int k) {
vector<int> res;
priority_queue<Point> priorityq;
if(i_A.size() == 0&&i_B.size() == 0) {
return res;
}
if(i_A.size() == 0) {
if (i_B.size() > k)
{
return res;
}
return vector<int>(i_B.begin(),i_B.begin()+k);
}
if(i_B.size() == 0) {
if(i_A.size() > k)
{
return res;
}
return vector<int>(i_A.begin(),i_A.begin()+k);
}
Point first;
first.x = 0;
first.y = 0;
first.sum = i_A[0] + i_B[0];
priorityq.push(first);
while (!priorityq.empty()&&res.size() != k)
{
Point tmpoutput = priorityq.top();
priorityq.pop();
res.push_back(tmpoutput.sum);
if(tmpoutput.y == 0) {
if (tmpoutput.x + 1 < i_A.size())
{
Point tmp;
tmp.x = tmpoutput.x + 1;
tmp.y = tmpoutput.y;
tmp.sum = i_A[tmpoutput.x + 1] + i_B[tmpoutput.y];
priorityq.push(tmp);
}
if (tmpoutput.y + 1 < i_B.size()) {
Point tmp;
tmp.x = tmpoutput.x;
tmp.y = tmpoutput.y + 1;
tmp.sum = i_A[tmpoutput.x] + i_B[tmpoutput.y + 1];
priorityq.push(tmp);
}
}
else {
if(tmpoutput.y + 1 < i_B.size()) {
Point tmp;
tmp.x = tmpoutput.x;
tmp.y = tmpoutput.y + 1;
tmp.sum = i_A[tmpoutput.x] + i_B[tmpoutput.y + 1];
priorityq.push(tmp);
}
}
}
return res;
}
}
3。實現帶有重復元素的二分查找,如果查找的元素重復,返回重復元素的起始位置。
類似二分查找,但是結束條件有些不同,結束條件為當前節點=目標元素並且當前節點為起始節點或者前一個節點不等與目標元素。
class Solution {
public:
int binarySearch(vector<int> &array, int target) {
if(array.size() == 1) {
if (array[0] == target)
return 0;
else
return -1;
}
int start = 0,end = array.size() - 1,mid;
while(start <= end) {
mid = (start + end)/2;
if(array[mid] == target && (mid == 0 || array[mid-1] != target)) {
return mid;
}
else if(target <= array[mid]) {
end = mid - 1;
}
else {
start = mid + 1;
}
}
return -1;
}
}
4。對於鏈表表示的完全二叉樹,給出根節點的指針,統計其節點的數量。
完全遍歷需要O(n)時間的復雜度。但是這樣沒有完全利用完全二叉樹的特性。
計算節點數量轉換為計算最后一層節點數量,計算最后一層節點數量可以這樣計算,左子樹,右子樹,如果深度相同,計算右子樹最后一層數量+滿的左子樹。如果深度不同,計算左子樹的最后一層節點數量,這樣問題規模可以直接減半。
#include <algorithm>
#include <time.h>
#include <iostream>
using namespace std;
struct TreeNode {
TreeNode *left;
TreeNode *right;
int value;
TreeNode(int v) {value = v;}
};
class Solution {
public:
int recurseTree(TreeNode *root) {
if (root == NULL)
return 0;
int tdepth = depth(root);
return (int)pow((double)2,tdepth-1) - 1 + recuseNode(root,tdepth);
}
//遞歸統計完全二叉樹的最后一層節點個數
int recuseNode(TreeNode *root,int depth) {
if(root == NULL)
return 0;
//最后一層才進行統計,depth == 1
if(root->left == NULL && root->right == NULL && depth == 1)
return 1;
return recuseNode(root->left,depth-1)+recuseNode(root->right,depth-1);
}
int dc(TreeNode *root) {
if(root == NULL)
return 0;
int treedepth = depth(root);
if(treedepth == 1)
return 1;
else
return (int)pow((double)2,treedepth-1) - 1 + devide_conquer(root);
}
//分治統計完全二叉樹的最后一層節點個數
int devide_conquer(TreeNode *root) {
int tdepth = depth(root);
//如果只有兩層,直接可以得到最后一層節點個數
if(tdepth == 2) {
if(root->right == NULL)
return 1;
return 2;
}
int leftdepth = depth(root->left);
int rightdepth = depth(root->right);
//right side
if (leftdepth == rightdepth) {
return (int)pow((double)2,tdepth-1-1) + devide_conquer(root->right);
}
//left side
else if(leftdepth > rightdepth) {
return devide_conquer(root->left);
}
else {
//error
}
}
//計算深度
int depth(TreeNode *root) {
int depth = 0;
TreeNode *cur = root;
while(cur != NULL) {
cur = cur->left;
depth++;
}
return depth;
}
};
int main(int argc,char **argv) {
TreeNode *_1root = new TreeNode(1);
_1root->left = NULL;
_1root->right = NULL;
TreeNode *_2root1 = new TreeNode(1);
TreeNode *_2root2 = new TreeNode(2);
TreeNode *_2root3 = new TreeNode(3);
TreeNode *_2root4 = new TreeNode(4);
TreeNode *_2root5 = new TreeNode(5);
TreeNode *_2root6 = new TreeNode(6);
TreeNode *_2root7 = new TreeNode(7);
TreeNode *_2root8 = new TreeNode(8);
TreeNode *_2root9 = new TreeNode(9);
TreeNode *_2root10 = new TreeNode(10);
TreeNode *_2root11 = new TreeNode(11);
TreeNode *_2root12 = new TreeNode(12);
_2root1->left = _2root2;
_2root1->right = _2root3;
_2root2->left = _2root4;
_2root2->right = _2root5;
_2root3->left = _2root6;
_2root3->right = _2root7;
_2root4->left = _2root8;
_2root4->right = _2root9;
_2root5->left = _2root10;
_2root5->right = _2root11;
_2root6->left = _2root12;
_2root6->right = NULL;
_2root7->left = NULL;
_2root7->right = NULL;
_2root8->left = NULL;
_2root8->right = NULL;
_2root9->left = NULL;
_2root9->right = NULL;
_2root10->left = NULL;
_2root10->right = NULL;
_2root11->left = NULL;
_2root11->right = NULL;
_2root12->left = NULL;
_2root12->right = NULL;
Solution s;
int s11,s12,s21,s22;
clock_t start,finish;
start = clock();
for(int i = 0;i < 1000000;i++)
s11 = s.dc(_1root);
finish = clock();
double t11 = double(finish - start);
start = clock();
for(int i = 0;i < 1000000;i++)
s12 = s.recurseTree(_1root);
finish = clock();
double t12 = double(finish - start);
start = clock();
for(int i = 0;i < 1000000;i++)
s21 = s.dc(_2root1);
finish = clock();
double t21 = double(finish - start);
start = clock();
for(int i = 0;i < 1000000;i++)
s22 = s.recurseTree(_2root1);
finish = clock();
double t22 = double(finish - start);
cout<<"s11:"<<s11<<"time:"<<t11<<"ms"<<endl;
cout<<"s12:"<<s12<<"time:"<<t12<<"ms"<<endl;
cout<<"s21:"<<s21<<"time:"<<t21<<"ms"<<endl;
cout<<"s22:"<<s22<<"time:"<<t22<<"ms"<<endl;
}
5。給出一串數字,判斷是否為有效二叉查找樹的后序遍歷序列(及是否能夠通過這串后序遍歷序列構造出二叉查找樹)。
最先想到的思路,將當前序列排序,有序的假設為中序遍歷序列,這樣可以類似還原BST。利用后序的序列不斷的分割中序。有序的序列是否能夠在后序中為一串連續的空間,如果連續即可構造出BST。這種方法的時間復雜度可以從O(n2)優化至O(nlgn),利用hash表。
有一個更好的思路,不利用中序,直接利用后序,首先利用最后的根節點,利用二分查找nlgn得到分割點,當前元素小於根,下一個元素大於根,考慮一些邊界情況。
然后,根節點之前的為左子樹,其上界為根節點;根節點之后的為右子樹,其下界為根節點,遞歸下去,遞歸至一個元素,如果符合上下界的條件,即可形成BST。
class Solution {
public:
//postorder sequece and checkout if it can be construct BST
bool validBST(vector<int> &postorder) {
return validBST_recurse(postorder, 0, postorder.size() - 1, INT_MIN, INT_MAX);
}
bool validBST_recurse(vector<int> &postorder, int start, int end, int min, int max) {
if(end < start) {
return true;
}
if(end == start) {
return postorder[end] > min && postorder[end] < max;
}
int partition = binaryPartition(postorder,start,end);
bool left = validBST_recurse(postorder, start, partition, min, postorder[end]);
bool right = validBST_recurse(postorder, partition+1, end-1, postorder[end], max);
return left&&right;
}
//partition to two part, postorder[p] < postorder[end] && portorder[p+1] > postorder[end]
int binaryPartition(vector<int> &postorder, int start, int end) {
int target = postorder[end];
int mid;
end = end - 1;
if(start == end)
return start;
while(start < end) {
mid = (start + end)/2;
if(postorder[mid] < target && (mid == end || postorder[mid+1] > target)) {
return mid;
}
else if(postorder[mid] > target && (mid == start || postorder[mid-1] < target)) {
return mid-1;
}
else if(postorder[mid] < target) {
start = mid + 1;
}
else {
end = mid - 1;
}
}
return -1;
}
}
