当前位置:网站首页>Leetcode skimming -- incremental ternary subsequence 334 medium
Leetcode skimming -- incremental ternary subsequence 334 medium
2022-07-02 15:28:00 【Fire breathing dragon and water arrow turtle】
Discussion and source code of increasing ternary subsequence
The title of the increasing ternary subsequence is shown in the figure below , This question belongs to greedy class and array type , It mainly investigates the use of search methods and the understanding of array structure . The title of this article, the author thought 2 Methods , They are two-way traversal method and binary search method , The bidirectional traversal method uses Java Compiling , The binary search method uses Python Compiling , Of course, this may not be the optimal solution , I also hope you guys can give a faster algorithm .
I think this problem can be solved by using the idea of two-way traversal , First calculate the length of the array , If the length of the array is less than 3, Then return directly False Result . Then initialize an integer array , Make the first element of the new array equal to the first element of the original array . Start looping through the original array , Assign the minimum value of the current element of the original array and the previous element of the new array to the current element of the new array . Then initialize an integer array , Make the last element of the new array equal to the last element of the original array . Loop through the original array from the back to the front , Assign the maximum value of the current element of the original array and the previous element of the new array to the current element of the new array . Finally, traverse three arrays , When the condition is met, the current element is larger than the previous element of the left array and smaller than the next element of the right array True Result , Otherwise, it will return at the end of traversal False result . Then according to this idea, our Java The code is as follows :
# Fire breathing dragon and water arrow turtle
class Solution {
public boolean increasingTriplet(int[] nums) {
int numLen = nums.length;
if (numLen < 3) {
return false;
}
int[] leftMin = new int[numLen];
leftMin[0] = nums[0];
for (int ir = 1; ir < numLen; ir++) {
leftMin[ir] = Math.min(leftMin[ir - 1], nums[ir]);
}
int[] rightMax = new int[numLen];
rightMax[numLen - 1] = nums[numLen - 1];
for (int ir = numLen - 2; ir >= 0; ir--) {
rightMax[ir] = Math.max(rightMax[ir + 1], nums[ir]);
}
for (int ir = 1; ir < numLen - 1; ir++) {
if (nums[ir] > leftMin[ir - 1] && nums[ir] < rightMax[ir + 1]) {
return true;
}
}
return false;
}
}

obviously , We can see that the effect of the two-way search method is ok , At the same time, it can also be solved by binary search . Initialize first , Assign the minimum value to two user-defined variables, the minimum value variable and the intermediate value variable , Then start traversing the array , If the current element is less than or equal to the minimum , Assign the current element to the minimum value . Otherwise, if the current element is less than or equal to the intermediate value , Assign the current element to the intermediate value , Otherwise, go straight back to True Result . If the traversal has not returned to the end , Go straight back False Result . So according to this idea, we can solve , Here is Python Code :
# Fire breathing dragon and water arrow turtle
class Solution:
def increasingTriplet(self, nums: List[int]) -> bool:
smallNum = inf
middleNum = inf
for ij in nums:
if(ij <= smallNum):
smallNum = ij
elif(ij <= middleNum):
middleNum = ij
else:
return True
return False

As a result Java The efficiency of the version of the two-way search method is relatively poor , and Python The speed of the binary search method of version is ok , But there should be more ways to further speed up , I hope friends can give me more advice , Thank you very much .
边栏推荐
- Tidb data migration tool overview
- 13_Redis_事务
- How to test tidb with sysbench
- 18_ Redis_ Redis master-slave replication & cluster building
- MySQL -- Index Optimization -- order by
- 哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
- How to write sensor data into computer database
- The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
- TiDB 集群最小部署的拓扑架构
- Summary of the first three passes of sqli Labs
猜你喜欢
随机推荐
TiDB混合部署拓扑
Tidb data migration tool overview
Infra11199 database system
数据分析思维分析方法和业务知识——业务指标
21_ Redis_ Analysis of redis cache penetration and avalanche
03_線性錶_鏈錶
HUSTPC2022
Deploy tidb cluster with tiup
TiDB 环境与系统配置检查
2021-2022學年編譯原理考試重點[華僑大學]
06_栈和队列转换
原则、语言、编译、解释
Real estate market trend outlook in 2022
Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
如何用 Sysbench 测试 TiDB
语义分割学习笔记(一)
LeetCode_ String_ Simple_ 412.Fizz Buzz
Steps for Navicat to create a new database
党史纪实主题公益数字文创产品正式上线
SQL transaction









