Array(9) -- Container With Most Water,Insert Delete GetRandom O(1) I, II

Container With Most Water

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container and n is at least 2.

这种求和求积的问题基本都是靠双指针去解决,在移动时选择较小的指针移动,以为如果想获得比现在更大的面积,只有移动小指针才有可能。

    int maxArea(vector<int>& height) {
        int left = 0, right = height.size() - 1;
    	int maxArea = 0;
    
    	while (left < right) {
    		maxArea = max(maxArea, min(height[left], height[right]) * (right - left));
    		if (height[left] < height[right])
    			left++;
    		else
    			right--;
    	}
    	return maxArea;
    }


Insert Delete GetRandom O(1)


使用vector来进行角标访问实现O(1)的random;删除与vector队尾元素互换后再进行删除,这样每次不需移动其他数。

unordered_map + vector,对于非顺序访问的数据结构,迭代器只有 itr++这种操作,不能itr = itr + 1。

开始的版本随机数使用了srand() + rand()的方法,没有ac; 发现它的expected answer默认的是不使用srand, 因此每次产生的随机数都是一样的,这里就不计较了。

class RandomizedSet {
public:

    /** Initialize your data structure here. */
    RandomizedSet() {
        
    }
    
    /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
    bool insert(int val) {
        if(um.find(val) != um.end()) return false;
        else {
            vec.push_back(val);
            um[val] = vec.size() - 1; 
            return true;
        }
    }
    
    /** Removes a value from the set. Returns true if the set contained the specified element. */
    bool remove(int val) {
        if(um.find(val) == um.end()) return false;
        else {
            int pos = um[val];
            swap(vec[pos], vec[vec.size()-1]);
            um[vec[pos]] = pos;
            vec.pop_back();
            um.erase(val);
            return true;
        }
    }
    
    /** Get a random element from the set. */
    int getRandom() {
        return vec[rand() % vec.size()];
    }
    
private:
    unordered_map<int, int> um;
    vector<int> vec;
};

Insert Delete GetRandom O(1) - Duplicates allowed

使用一个set来记录相同数字在vector中的角标,需要注意的一点是

如果要交换的元素和尾部元素相同,则不需要进行元素交换,直接将队尾的元素pop_back()然后修改set即可

class RandomizedCollection {
public:
    /** Initialize your data structure here. */
    RandomizedCollection() {
        
    }
    
    /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
    bool insert(int val) {
        bool unfound = false;
        if(um.find(val) == um.end()) unfound = true;
        vec.push_back(val);
        um[val].insert(vec.size() - 1);
        return unfound;
    }
    
    /** Removes a value from the collection. Returns true if the collection contained the specified element. */
    bool remove(int val) {
        if(um.find(val) == um.end()) return false;
        int pos = *(um[val].begin());
        //如果要交换的元素和尾部元素相同,则不需要进行元素交换,直接将队尾的元素pop_back()然后修改set即可

        if(vec[pos] != vec[vec.size()-1]){
            swap(vec[pos], vec[vec.size()-1]);
            um[vec[pos]].erase(vec.size()-1);
            um[vec[pos]].insert(pos);
        }
        um[val].erase(pos);
        vec.pop_back();
        if(um[val].empty()) um.erase(val);
        return true;
    }
    
    /** Get a random element from the collection. */
    int getRandom() {
        if(vec.empty()) return NULL;
        return vec[rand() % vec.size()];
    }
private:
    unordered_map<int, unordered_set<int> > um;
    vector<int> vec;
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值