Programmer growth path (Day 27)

Posted by ltbaggz on Sun, 12 Sep 2021 02:35:13 +0200

Learning content:

LintCode brush questions:

·Query course creation time and teacher nationality

        Extension - > (inner join of SQL)

Select courses.created_at as course_date, as teacher_country 
from courses inner join teachers on courses.teacher_id =;

·Exchange the nodes in the linked list

        Take the head in and directly return the head when the head or the next one is empty. Note that you have to check whether the head is empty first, because | directly skips the right when the condition on the left is wrong. Therefore, when the head is empty, will be abnormal. Define a new head, assign the next of the original old head to it, and define a new next. This next is a new linked list (after replacing the first two sequences), this next is also the old header of the original third and fourth, so put it in the method and return the new header after the second.

 * Definition for ListNode
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }

public class Solution {
    public ListNode swapPairs(ListNode head) {
        // If (head. Next = = null | head = = null) reports an exception
            return head;
        ListNode newhead =;
        ListNode next = swapPairs(;;;
        return newhead;

·Interval decimal

        First, create the node of the segment tree, and then create the construction method of the segment tree. The min of root uses Integer.Max_VALUE, and then keep putting this array into the segment tree. The min of root takes the minimum value of min of the left node and the right node (when start and end are not equal) , when equal, it is directly equal to the element in A. create a search method to find the min value of the root of the left node start and the right node end. First define mid as the midpoint of root, use the mid value to recurse the query method in that range, and finally use the class Interval to get the start and end values to traverse the incoming queries, use the incoming array a to create the root node, and call the search method .

import java.util.ArrayList;
import java.util.List;

class Interval {
    int start, end;

    Interval(int start, int end) {
        this.start = start;
        this.end = end;

class TreeNode {
    public int start, end, min;
    public TreeNode left, right;

    public TreeNode(int start, int end, int min) {
        this.end = end;
        this.start = start;
        this.min = min;

public class Solution {
    public TreeNode build (int start,int end,int[]A){
        if (start>end){
            return null;
        TreeNode root = new TreeNode(start,end,Integer.MAX_VALUE);
        if (start!=end){
            int mid =(start+end)/2;
            root.left =build(start,mid,A);
            root.min =Math.min(root.left.min,root.right.min);
            root.min =A[start];
        return root;
    public int query(TreeNode root,int start ,int end){
        if (start == root.start && root.end == end){
            return root.min;
        int leftMin =Integer.MAX_VALUE;
        int rightMin =Integer.MAX_VALUE;
        int mid = (root.start+root.end)/2;
        if (start<=mid){
            if (mid<end){
                leftMin =query(root.left,start,mid);
        if (mid<end){
            if (mid>=start){
                rightMin =query(root.right,mid+1,end);}
                rightMin =query(root.right,start,end);
            return Math.min(leftMin,rightMin);
    public List<Integer> intervalMinNumber(int[] A, List<Interval> queries) {
        TreeNode root = build(0,A.length-1,A);
        List<Integer> ans = new ArrayList<>();
        for (Interval interval:queries){
        return ans;

 · The number of subarrays whose maximum value is within the bounds

        Define a left pointer, a result, a qualified position in the array, and use the right pointer + + to traverse the array. When the elements in the array are in [L,R], you can put the result + = right pointer - left pointer + 1, because when the elements continuously meet [L,R] For this set, they satisfy that the number of subarrays = 1+2+3+4+...+n, and record the current position, because if the next element is smaller than this set, it can only be added after the previous element, and the number of subarrays can only be + 1. When the element is larger than this set, you need to reset windows and point the left pointer to the next element of the current element.

public class Solution {
    public int numSubarrayBoundedMax(int[] A, int L, int R) {
        int left = 0;
        int resultint = 0;
        int windows = 0;
        for(int right =0;right<A.length;right++){
            if (A[right]>=L&&A[right]<=R){
                resultint +=right-left+1;
                windows =right-left+1;
            }else if (A[right]<L)
            else {
        return resultint;

 · fleet

          According to the definition of TreeMap, it will be sorted automatically according to the passed Key value and the distance from the end point. Therefore, as long as the statistical speed is greater than the previous speed, you can know how many fleets there are.

public class Solution {

    public int carFleet(int target, int[] position, int[] speed) {
        // Write your code here
        TreeMap<Integer, Double> m = new TreeMap<>();
        for (int i = 0; i < position.length; ++i) {
            m.put(-position[i], (double)(target - position[i]) / speed[i]);
        int res = 0; double cur = 0;
        for (double time : m.values()) {
            if (time > cur) {
                cur = time;
        return res;


Study time:

2021-9-11 14: 00-17: 00,19: 30-20: 10

Learning output:

Brush questions * 6

Learning blog * 1

Topics: Java