Two contestants can also enter the top 100, hhh
Jianxin 01. Delete linked list nodes at intervals
Give you the head node of a linked list
head
, delete another node every other node (requires to keep the head node).
Please return the head node of the final linked list.Example 1:
enter:
head = [1,2,3,4]
output:
[1,3]
Explanation:
The blue nodes are deleted nodes
Example 2:
enter:
head = [5,1,8,6,1]
output:
[5,8,1]
hint:
- The number of nodes in the linked list is
[1, 5000]
within range.1 <= Node.val <= 10000
speed pass
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteListNode(self, head: ListNode) -> ListNode:
cur=head
while cur and cur.next:
cur.next=cur.next.next
cur=cur.next
return head
CCB 02. Histogram Analysis
There are N columns in a certain histogram, and the height of each column is recorded
heights
in according to the sequence. Assuming thatcnt
any number of columns can form a column group, please find the column group with the smallest difference between the maximum height and the minimum height among all possible column groups, and return the column group in ascending order of height. If there are multiple column groups that meet the condition, return the column group with the smallest element.Example 1:
enter:
heights = [3,2,7,6,1,8], cnt = 3
output:
[1,2,3]
Explanation: [1,2,3] and [6,7,8] all meet the condition that the difference between the maximum height and the minimum height is the smallest in all column groups, and the first element with the smallest [1, 2,3] returns.
Example 2:
enter:
heights = [4,6,1,8,4,10], cnt = 4
output:
[4,4,6,8]
Explanation: The column group [4,4,6,8] satisfies the condition that the difference between the maximum height and the minimum height is the minimum.
hint:
2 <= cnt < heights.length <= 10^5
0 <= heights[i] <= 10^6
Fixed-length sliding window, easy
class Solution:
def analysisHistogram(self, heights: List[int], cnt: int) -> List[int]:
heights.sort()
n=len(heights)
i,j=0,cnt-1
res=(0,float('inf'))
while j<n:
tmp=heights[j]-heights[i]
if res[1]>tmp:
res=(i,tmp)
i+=1
j+=1
idx=res[0]
return heights[idx:idx+cnt]