盛最多水的容器
 

给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (iai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (iai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

说明:你不能倾斜容器,且 n 的值至少为 2。

图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。

示例:

输入: [1,8,6,2,5,4,8,3,7]
输出: 49
class Solution {
public:
int maxArea(vector<int>& a){
vector <int> v[20100];
int up = 0;
int mx = 0, mn = 2e9;
int ans = 0; int pos = 0;
for (auto u : a) v[u].push_back(++pos), up = max(up, u); for (int i = up; i; --i){
if (v[i].size() > 0){
for (auto u : v[i]){
mx = max(mx, u);
mn = min(mn, u);
}
ans = max(ans, (mx - mn) * i);
}
} return ans;
}
};

  

给定一个大小为 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。

你可以假设数组是非空的,并且给定的数组总是存在众数。

示例 1:

输入: [3,2,3]
输出: 3

示例 2:

输入: [2,2,1,1,1,2,2]
输出: 2
class Solution {
public:
int majorityElement(vector<int>& nums) {
int cnt = 0, now = 0;
for (auto u : nums){
if (cnt == 0) now = u, cnt = 1;
else cnt += (u == now ? 1 : -1);
}
return now;
}
};
LRU缓存机制
 

运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get和 写入数据 put 。

获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。
写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。

进阶:

你是否可以在 O(1) 时间复杂度内完成这两种操作?

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define MP make_pair
#define fi first
#define se second class LRUCache {
private:
int up;
int cnt;
list< pair<int, int>> queue;
unordered_map< int, list<pair<int, int>>::iterator> mp; public:
LRUCache(int capacity){
up = capacity;
mp.clear();
cnt = 0; } int get(int key){
int ret = -1;
auto p = mp.find(key);
if (p != mp.end()){
ret = p -> se -> se;
queue.erase(p -> se);
queue.push_front(MP(key, ret));
p -> se = queue.begin();
} return ret; } void put(int key, int value){
auto p = mp.find(key);
if (p != mp.end()){
queue.erase(p -> se);
} else if (cnt == up){
int delkey = queue.back().fi;
queue.pop_back();
mp.erase(delkey);
} else ++cnt; queue.push_front(MP(key, value));
mp[key] = queue.begin();
}
};

设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。

  • push(x) -- 将元素 x 推入栈中。
  • pop() -- 删除栈顶的元素。
  • top() -- 获取栈顶元素。
  • getMin() -- 检索栈中的最小元素。

示例:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> 返回 -3.
minStack.pop();
minStack.top(); --> 返回 0.
minStack.getMin(); --> 返回 -2.
class MinStack {
private:
stack <int> s, t;
public:
/** initialize your data structure here. */
MinStack(){ } void push(int x){
s.push(x);
if (t.empty() || x <= t.top()){
t.push(x);
} } void pop(){
int now = s.top();
s.pop();
if (!t.empty() && t.top() == now){
t.pop();
} } int top(){
return s.top(); } int getMin(){
return t.top(); }
};

最新文章

  1. delphi之TDataset
  2. 关于IONIC 报错 XX is not a function
  3. C++实现对lua访问的封装
  4. 一模 (2) day2
  5. 使用PHP得到所有的HTTP请求头_还有应答头
  6. Loading CSS without blocking render
  7. WordPress /wp-admin/includes/post.php user_ID 参数操作权限提升漏洞
  8. MapReduce分析明星微博数据
  9. Grid++Report 数据填充教程
  10. 在 Windows 下远程桌面连接 Linux - VNC 篇
  11. 使用EasyMock对Servlet进行简单的测试
  12. 基于Spring开发的一个BIO-RPC框架(对新人很友好)
  13. which framework or library is best to use WebRTC
  14. TsinsenA1489 抽奖 【期望】
  15. VSTO学习(三)——创建Excel解决方案
  16. 数组中的stdClass Object如何访问
  17. NOI.ac #8 小w、小j和小z LIS
  18. linux centOS6 nexus 开启自动启动
  19. 网易郑栋:数据采集与分析的那些事——从数据埋点到AB测试
  20. Hadoop HBase概念学习系列之模式设计(十)

热门文章

  1. 8.6 JavaScript之HTML的DOM(三)
  2. TCP学习
  3. phpinfo中敏感信息记录
  4. FFmpeg之av_register_all()
  5. Qt VS MFC
  6. MySQL(逻辑分层,存储引擎,sql优化,索引优化以及底层实现(B+Tree))
  7. 基于MybatisUtil工具类,完成CURD操作
  8. Django博客系统
  9. 初识消息中间件之 ==&gt; ActiveMQ
  10. CTF—攻防练习之Capture the Flag