当前位置:网站首页>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 .
边栏推荐
- Map introduction
- 16_ Redis_ Redis persistence
- Solution of Queen n problem
- 6.12 企业内部upp平台(Unified Process Platform)的关键一刻
- 10_ Redis_ geospatial_ command
- Learn the method code of using PHP to realize the conversion of Gregorian calendar and lunar calendar
- MySQL -- Index Optimization -- order by
- List set & UML diagram
- 20_Redis_哨兵模式
- 04_ Stack
猜你喜欢

15_Redis_Redis.conf详解

10_Redis_geospatial_命令

06_ Stack and queue conversion

08_ strand

03.golang初步使用

Download blender on Alibaba cloud image station

LeetCode刷题——验证二叉树的前序序列化#331#Medium

19_Redis_宕机后手动配置主机

vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)

Set set you don't know
随机推荐
07_ Hash
21_Redis_浅析Redis缓存穿透和雪崩
MySQL -- Index Optimization -- order by
Learn the method code example of converting timestamp to uppercase date using PHP
Practical debugging skills
Topology architecture of the minimum deployment of tidb cluster
LeetCode刷题——验证二叉树的前序序列化#331#Medium
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
Infra11199 database system
二叉树的遍历方式主要有:先序遍历、中序遍历、后序遍历、层次遍历。先序、中序、后序其实指的是父节点被访问的次序。若在遍历过程中,父节点先于它的子节点被访问,就是先序遍历;
The past and present lives of visual page building tools
Common English abbreviations for data analysis (I)
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
11_Redis_Hyperloglog_命令
. Net core logging system
Learn the method code of using PHP to realize the conversion of Gregorian calendar and lunar calendar
20_ Redis_ Sentinel mode
16_Redis_Redis持久化
JVM architecture, classloader, parental delegation mechanism