刷题-单调栈

柱状图中的最大矩形-leetcode87

思路

采用单调递增栈,元素前面的就是元素的左边界,将元素弹出的就是元素的右边界,

复杂度分析

时间复杂度:o(n)

空间复杂度:O(n)

代码

class Solution {
    public int largestRectangleArea(int[] heights) {

        int[] left = new int[heights.length];
        int[] right = new int[heights.length];
        Arrays.fill(right, heights.length);

        Deque<Integer> heightDeque = new ArrayDeque<>();
        Deque<Integer> indexDeque = new ArrayDeque<>();

    for (int i = 0; i < heights.length; i++) {
        while (heightDeque.peek() != null && heightDeque.peek() >= heights[i]) {
            heightDeque.pop();
            right[indexDeque.pop()] = i;
        }
        left[i] = indexDeque.peek() == null ? -1 : indexDeque.peek();
        heightDeque.push(heights[i]);
        indexDeque.push(i);
    }
    int result = 0;

    for (int i = 0; i < right.length; i++) {
        result = Math.max(result, heights[i] * (right[i] - left[i] - 1));
    }
    return result;
        
    }
}

快速排序

最好时间复杂度:O(n*logn)

最坏时间复杂度:O(n^2)

平均:O(n*logn)

public static void quickSort(int[] arr, int start, int end) {
        if (start >= end) {
            return;
        }
        int pivot = arr[start];
        int left = start + 1;
        int right = end;

        while (left <= right) {
            while (left <= end && arr[left] < pivot) {
                left++;
            }
            while (right > start && arr[right] >= pivot) {
                right--;
            }
            if (right > left) {
                int temp = arr[right];
                arr[right] = arr[left];
                arr[left] = temp;
            }
        }
        arr[start] = arr[right];
        arr[right] = pivot;

        quickSort(arr, right + 1, end);
        quickSort(arr, start, right - 1);

    }

堆排序

最好最坏以及平均的时间复杂度都是O(logn * n)

空间复杂度是O(1)

非稳定排序

重点在于构造大堆

public static void heapSort(int[] arr) {
        for (int i = arr.length / 2 - 1; i >= 0; i--) {
            heapify(arr, i, arr.length);
        }

        for (int i = 0; i < arr.length; i++) {
            sweap(arr, 0, arr.length - i - 1);
            heapify(arr, 0, arr.length - i - 1);

        }
    }

    public static void sweap(int[] arr, int left, int right) {
        int temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;
    }

    public static void heapify(int[] arr, int i, int length) {
        int left = i * 2 + 1;
        int right = i * 2 + 2;
        int largest = i;

        if (left < length && arr[left] > arr[largest]) {
            largest = left;
        }
        if (right < length && arr[right] > arr[largest]) {
            largest = right;
        }

        if (largest != i) {
            int temp = arr[i];
            arr[i] = arr[largest];
            arr[largest] = temp;
            heapify(arr, largest, length);
        }


    }

复习一下双重校验锁

public class Singleton {
    // 使用volatile关键字确保多线程环境下的可见性
    private static volatile Singleton instance;

    // 私有构造函数,防止外部实例化
    private Singleton() {
        // 初始化代码(如果有)
    }

    /​**​
     * 获取单例实例的方法
     *
     * @return 单例实例
     */
    public static Singleton getInstance() {
        // 第一次检查,避免不必要的同步
        if (instance == null) {
            // 同步块,确保只有一个线程可以进入
            synchronized (Singleton.class) {
                // 第二次检查,确保实例未被其他线程创建
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }

    // 其他方法
    public void doSomething() {
        System.out.println("Singleton instance is doing something.");
    }
}

使用volatile的原因是防止指令重排

死锁

代码

package com.wll;

public class DeadLock {

    private static final DeadLock d1 = new DeadLock();
    private static final DeadLock d2 = new DeadLock();



    public static void main(String[] args) {
        deadLock();

    }

    public static void deadLock() {
        Thread t = new Thread(() -> {

            synchronized (d1) {
                try {
                    System.out.println("T获得D1");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (d2) {
                    System.out.println("t等待D2");
                }
            }



        });

        Thread t1 = new Thread(() -> {

            synchronized (d2) {
                try {
                    System.out.println("T1获得D2");
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (d1) {
                    System.out.println("T1等待d1");
                }
            }



        });

        t.start();
        t1.start();
    }
}

Thread的run方法,是一个方法,调用这个方法的时候,并不会新起一个线程。

start方法会新起一个线程,不能多次调用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值