当前位置:网站首页>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 .
边栏推荐
- 03_線性錶_鏈錶
- 损失函数与正负样本分配:YOLO系列
- 基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
- vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
- Table responsive layout tips
- 如何用 Sysbench 测试 TiDB
- 06_栈和队列转换
- 21_ Redis_ Analysis of redis cache penetration and avalanche
- Pytorch 保存tensor到.mat文件
- 语义分割学习笔记(一)
猜你喜欢

搭建自己的语义分割平台deeplabV3+

18_Redis_Redis主从复制&&集群搭建

15_ Redis_ Redis. Conf detailed explanation

I made an istio workshop. This is the first introduction

How to choose a third-party software testing organization for automated acceptance testing of mobile applications

Solve the problem that El radio group cannot be edited after echo

Yolov5 code reproduction and server operation

Oracle primary key auto increment

Base64 coding can be understood this way

04_ 栈
随机推荐
學習使用php實現公曆農曆轉換的方法代碼
18_Redis_Redis主从复制&&集群搭建
Solve the problem of frequent interruption of mobaxterm remote connection
5. Practice: jctree implements the annotation processor at compile time
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
Force deduction solution summary 2029 stone game IX
4. Data splitting of Flink real-time project
NBA player analysis
. Net core logging system
FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
Map introduction
19_ Redis_ Manually configure the host after downtime
损失函数与正负样本分配:YOLO系列
TiDB跨数据中心部署拓扑
Steps for Navicat to create a new database
XML Configuration File
04.进入云原生后的企业级应用构建的一些思考
Common English abbreviations for data analysis (I)
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
The past and present lives of visual page building tools