456:132 模式

This commit is contained in:
huangge1199 2021-04-25 10:25:33 +08:00
parent 119d05496a
commit c68b16ff80
2 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,94 @@
//给你一个整数数组 nums 数组中共有 n 个整数132 模式的子序列 由三个整数 nums[i]nums[j] nums[k] 组成并同时满足
//i < j < k nums[i] < nums[k] < nums[j]
//
// 如果 nums 中存在 132 模式的子序列 返回 true 否则返回 false
//
//
//
// 进阶很容易想到时间复杂度为 O(n^2) 的解决方案你可以设计一个时间复杂度为 O(n logn) O(n) 的解决方案吗
//
//
//
// 示例 1
//
//
//输入nums = [1,2,3,4]
//输出false
//解释序列中不存在 132 模式的子序列
//
//
// 示例 2
//
//
//输入nums = [3,1,4,2]
//输出true
//解释序列中有 1 132 模式的子序列 [1, 4, 2]
//
//
// 示例 3
//
//
//输入nums = [-1,3,2,0]
//输出true
//解释序列中有 3 132 模式的的子序列[-1, 3, 2][-1, 3, 0] [-1, 2, 0]
//
//
//
//
// 提示
//
//
// n == nums.length
// 1 <= n <= 104
// -109 <= nums[i] <= 109
//
// Related Topics
// 👍 503 👎 0
package leetcode.editor.cn;
import java.util.Stack;
import java.util.TreeMap;
//456:132 模式
public class One32Pattern {
public static void main(String[] args) {
//测试代码
Solution solution = new One32Pattern().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean find132pattern(int[] nums) {
int n = nums.length;
if (n < 3) {
return false;
}
int left = nums[0];
TreeMap<Integer, Integer> right = new TreeMap<>();
for (int k = 2; k < n; k++) {
right.put(nums[k], right.getOrDefault(nums[k], 0) + 1);
}
for (int j = 1; j < n - 1; j++) {
if (left < nums[j]) {
Integer next = right.ceilingKey(left + 1);
if (next != null && next < nums[j]) {
return true;
}
}
left = Math.min(left, nums[j]);
right.put(nums[j + 1], right.get(nums[j + 1]) - 1);
if (right.get(nums[j + 1]) == 0) {
right.remove(nums[j + 1]);
}
}
return false;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,44 @@
<p>给你一个整数数组 <code>nums</code> ,数组中共有 <code>n</code> 个整数。<strong>132 模式的子序列</strong> 由三个整数 <code>nums[i]</code><code>nums[j]</code><code>nums[k]</code> 组成,并同时满足:<code>i < j < k</code><code>nums[i] < nums[k] < nums[j]</code></p>
<p>如果 <code>nums</code> 中存在 <strong>132 模式的子序列</strong> ,返回 <code>true</code> ;否则,返回 <code>false</code></p>
<p> </p>
<p><strong>进阶:</strong>很容易想到时间复杂度为 <code>O(n^2)</code> 的解决方案,你可以设计一个时间复杂度为 <code>O(n logn)</code><code>O(n)</code> 的解决方案吗?</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,3,4]
<strong>输出:</strong>false
<strong>解释:</strong>序列中不存在 132 模式的子序列。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [3,1,4,2]
<strong>输出:</strong>true
<strong>解释:</strong>序列中有 1 个 132 模式的子序列: [1, 4, 2] 。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>nums = [-1,3,2,0]
<strong>输出:</strong>true
<strong>解释:</strong>序列中有 3 个 132 模式的的子序列:[-1, 3, 2]、[-1, 3, 0] 和 [-1, 2, 0] 。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= n <= 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<div><div>Related Topics</div><div><li></li></div></div>\n<div><li>👍 503</li><li>👎 0</li></div>