当前位置:网站首页>Daily practice - February 13, 2022
Daily practice - February 13, 2022
2022-07-06 00:54:00 【Trabecula aixj】
List of articles
One , The envelope problem of Russian Dolls
1, Program Introduction
Here's a two-dimensional array of integers envelopes , among envelopes[i] = [wi, hi] , It means the first one i The width and height of each envelope .
When the width and height of another envelope are larger than this one , This envelope can be put into another envelope , Like Russian Dolls .
Please calculate How many at most Envelopes can form a group “ Russian Dolls ” The envelope ( You can put one envelope in another ).
Be careful :
- It is not allowed to rotate the envelope .
Example 1:
- Input :envelopes = [[5,4],[6,4],[6,7],[2,3]]
- Output :3
- explain : The maximum number of envelopes is
3, Combination for :
[2,3] => [5,4] => [6,7].
Example 2:
- Input :envelopes = [[1,1],[1,1],[1,1]]
- Output :1
Tips :
- 1 < = e n v e l o p e s . l e n g t h < = 5000 1 <= envelopes.length <= 5000 1<=envelopes.length<=5000
- e n v e l o p e s [ i ] . l e n g t h = = 2 envelopes[i].length == 2 envelopes[i].length==2
- 1 < = w i , h i < = 1 0 4 1 <= wi, hi <= 10^4 1<=wi,hi<=104
2, Program code
# -*- coding: utf-8 -*-
""" Created on Sun Feb 13 21:48:03 2022 Function: The envelope problem of Russian Dolls @author: Trabecula aixj """
class Solution:
def maxEnvelopes(self, envelopes) -> int:
""" :param envelopes: List[List[int]] :return: int """
n = len(envelopes)
if not n:
return 0
envelopes.sort(key=lambda x: (x[0], -x[1]))
dp = [1] * n
for i in range(n):
for j in range(i):
if envelopes[j][1] < envelopes[i][1]:
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)
Two , The most points on the line
1, Program Introduction
Give you an array points , among points[i] = [xi, yi] Express X-Y A point on the plane . Find out how many points are in the same line at most .
Example 1:
- Input :points = [[1,1],[2,2],[3,3]]
- Output :3
Example 2:
- Input :points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
- Output :4
Tips :
- 1 <= points.length <= 300
- points[i].length == 2
- − 1 0 4 < = x i , y i < = 1 0 4 -10^4 <= xi, yi <= 10^4 −104<=xi,yi<=104
- points All the points in Different from each other
2, Program code
# -*- coding: utf-8 -*-
""" Created on Sun Feb 13 21:48:24 2022 Function: The most points on the line @author: Trabecula aixj """
class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
if len(points) <= 2:
return len(points)
maxNum = 0
for i in range(len(points)):
maxNum = max(maxNum, self.find(points[i], points[:i] + points[i + 1 :]))
return maxNum + 1
def find(self, point, pointList):
memories = {
}
count = 0
inf = float("inf")
for curPoint in pointList:
if curPoint[1] == point[1] and curPoint[0] != point[0]:
memories[inf] = memories.get(inf, 0) + 1
elif curPoint[1] == point[1] and curPoint[0] == point[0]:
count += 1
else:
slope = (curPoint[0] - point[0]) / (curPoint[1] - point[1])
memories[slope] = memories.get(slope, 0) + 1
if memories:
return count + max(memories.values())
else:
return count
3、 ... and , Look for the minimum value in the rotation sort array II
1, Program Introduction
We know a length of n Array of , In ascending order in advance , Through 1 To n Time rotate after , Get the input array . for example , Original array nums = [0,1,4,4,5,6,7] After the change may get :
- If you rotate 4 Time , You can get [4,5,6,7,0,1,4]
- If you rotate 7 Time , You can get [0,1,4,4,5,6,7]
Be careful , Array [a[0], a[1], a[2], …, a[n-1]] Rotate once The result is an array [a[n-1], a[0], a[1], a[2], …, a[n-2]] .
Give you a chance to exist repeat An array of element values nums , It turns out to be an ascending array , According to the above situation, we have made many rotations . Please find and return the The smallest element .
Example 1:
- Input :nums = [1,3,5]
- Output :1
Example 2:
- Input :nums = [2,2,2,0,1]
- Output :0
Tips :
- n == nums.length
- 1 <= n <= 5000
- -5000 <= nums[i] <= 5000
- nums It turns out to be an ascending array , And carried on 1 to n Second rotation
Advanced :
- The question is Look for the minimum value in the rotation sort array (https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/description/) The extended topic of .
- Does allowing repetition affect the time complexity of the algorithm ? How will it affect , Why? ?
2, Program code
# -*- coding: utf-8 -*-
""" Created on Sun Feb 13 21:49:48 2022 Function: Look for the minimum value in the rotation sort array II @author: Trabecula aixj """
class Solution:
def findMin(self, nums: List[int]) -> int:
left = 0
right = len(nums) - 1
while left < right:
mid = left + (right - left) // 2
if nums[mid] > nums[right]:
left = mid + 1
elif nums[mid] == nums[right]:
right -= 1
else:
right = mid
return nums[left]
边栏推荐
- Uniapp development, packaged as H5 and deployed to the server
- Folding and sinking sand -- weekly record of ETF
- C language programming (Chapter 6 functions)
- Promise
- Study diary: February 13, 2022
- 关于#数据库#的问题:(5)查询库存表中每本书的条码、位置和借阅的读者编号
- Common API classes and exception systems
- Questions about database: (5) query the barcode, location and reader number of each book in the inventory table
- 视频直播源码,实现本地存储搜索历史记录
- The value of applet containers
猜你喜欢
Study diary: February 13, 2022
如何制作自己的机器人
Comment faire votre propre robot
看抖音直播Beyond演唱会有感
KDD 2022 | EEG AI helps diagnose epilepsy
Building core knowledge points
What is the most suitable book for programmers to engage in open source?
BiShe - College Student Association Management System Based on SSM
MIT doctoral thesis | robust and reliable intelligent system using neural symbol learning
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
随机推荐
[Online gadgets] a collection of online gadgets that will be used in the development process
《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
MYSQL---查询成绩为前5名的学生
Four commonly used techniques for anti aliasing
从 1.5 开始搭建一个微服务框架——调用链追踪 traceId
Leetcode Fibonacci sequence
Spark DF增加一列
激动人心,2022开放原子全球开源峰会报名火热开启
Comment faire votre propre robot
MYSQL GROUP_ The concat function realizes the content merging of the same ID
Data analysis thinking analysis methods and business knowledge - analysis methods (III)
[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)
Spark SQL null value, Nan judgment and processing
Convert binary search tree into cumulative tree (reverse middle order traversal)
RAID disk redundancy queue
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
Installation and use of esxi
NLP basic task word segmentation third party Library: ICTCLAS [the third party library with the highest accuracy of Chinese word segmentation] [Chinese Academy of Sciences] [charge]
Cannot resolve symbol error
C language programming (Chapter 6 functions)