当前位置:网站首页>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 .
边栏推荐
- CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
- 二叉树的遍历方式主要有:先序遍历、中序遍历、后序遍历、层次遍历。先序、中序、后序其实指的是父节点被访问的次序。若在遍历过程中,父节点先于它的子节点被访问,就是先序遍历;
- Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
- Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
- [C language] explain the initial and advanced levels of the pointer and points for attention (1)
- Force deduction solution summary 2029 stone game IX
- Apprendre le Code de la méthode de conversion du calendrier lunaire grégorien en utilisant PHP
- Mavn builds nexus private server
- Huffman tree: (1) input each character and its weight (2) construct Huffman tree (3) carry out Huffman coding (4) find hc[i], and get the Huffman coding of each character
- 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
猜你喜欢
学习使用php将时间戳转换为大写日期的方法代码示例
19_Redis_宕机后手动配置主机
08_ 串
5. Practice: jctree implements the annotation processor at compile time
Case introduction and problem analysis of microservice
FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
MySQL calculate n-day retention rate
Mavn builds nexus private server
16_Redis_Redis持久化
Steps for Navicat to create a new database
随机推荐
Solve the problem that El radio group cannot be edited after echo
二叉树的遍历方式主要有:先序遍历、中序遍历、后序遍历、层次遍历。先序、中序、后序其实指的是父节点被访问的次序。若在遍历过程中,父节点先于它的子节点被访问,就是先序遍历;
07_ Hash
Evaluation of embedded rz/g2l processor core board and development board of Feiling
How to conduct TPC-C test on tidb
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
03_ Linear table_ Linked list
How to solve the problem of database content output
Oracle primary key auto increment
08_ strand
数据分析常见的英文缩写(一)
16_Redis_Redis持久化
Learn the method code of using PHP to realize the conversion of Gregorian calendar and lunar calendar
LeetCode刷题——去除重复字母#316#Medium
06_栈和队列转换
Kibana basic operation
yolo格式数据集处理(xml转txt)
Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
XML配置文件
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!