盛最多水的容器

给定 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              secondclass 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();}
};

转载于:https://www.cnblogs.com/cxhscst2/p/11446794.html

Leetcode 杂题相关推荐

  1. LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

    LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...

  2. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  3. LeetCode刷题记录13——705. Design HashSet(easy)

    LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...

  4. LeetCode刷题记录12——232. Implement Queue using Stacks(easy)

    LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...

  5. LeetCode刷题记录11——290. Word Pattern(easy)

    LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...

  6. LeetCode刷题记录10——434. Number of Segments in a String(easy)

    LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...

  7. LeetCode刷题记录9——58. Length of Last Word(easy)

    LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...

  8. LeetCode刷题记录8——605. Can Place Flowers(easy)

    LeetCode刷题记录8--605. Can Place Flowers(easy) 目录 LeetCode刷题记录8--605. Can Place Flowers(easy) 题目 语言 思路 ...

  9. LeetCode刷题记录7——824. Goat Latin(easy)

    LeetCode刷题记录7--824. Goat Latin(easy) 目录 LeetCode刷题记录7--824. Goat Latin(easy) 题目 语言 思路 后记 题目 题目需要将一个输 ...

最新文章

  1. 单例模式(单一实例)
  2. 如何在 Linux 中使用 find
  3. fedora操作系统优缺点_不同类型的操作系统的优缺点
  4. 分布式文档存储独角兽MongoDB——MongoDB常见命令(2)
  5. 【Flink】Flink Invalid timestamp -1 Timestamp should always be none-negative or null
  6. Python2 倒计时,还不快来掌握 Python3 酷炫的新特性? | 原力计划
  7. Vue3学习之第四节:setup()中使用watch、watchEffect 函数
  8. oracle获取用户名,Oracle 用户名详解
  9. 单点登录SSO的实现原理与方案详解
  10. webform(八)组合查询
  11. IntelliJ IDEA常用快捷键
  12. 新浪微博开放平台接口使用小结
  13. 保险业IT整体规划图
  14. wine android安装程序,Wine 4.0 发布- Ubuntu安装
  15. Java实现八皇后问题
  16. 网页游戏的项目设计方案分享
  17. 内存卡损坏怎么修复?分享实际经验
  18. 最优化路径和火车票退票
  19. 你究竟多想成功?(nba励志视频)
  20. python库——pandas

热门文章

  1. 0821Servlet基础
  2. Sqlserver2008相关配置问题
  3. Lucene中string docvalues使用utf-16的优化
  4. 我不够格,但我还是希望事情到此为止,继续工作罢
  5. 让全球数亿人拍摄到更美的照片,【北京三星研究院】招聘
  6. 重读 CenterNet,一个在Github有5.2K星标的目标检测算法
  7. DAS 2020 诚征论文及赞助!
  8. linux逻辑分区被删除了怎么办,找到了linux分区顺序错乱修复方法
  9. 那些用起来很爽,但用不好可能会被人打的Python骚操作
  10. 你可能不知道的10个Python Pandas的技巧和特性(下)