LeetCode 第 315 题:计算右侧小于当前元素的个数

方法一:暴力解法(Brute Force)

Java 代码:

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

/**
 * @author liweiwei1419
 * @date 2019/10/2 12:13 下午
 */
public class Solution {

    /**
     * 暴力解法
     *
     * @param nums
     * @return
     */
    public List countSmaller(int[] nums) {
        int len = nums.length;
        List res = new ArrayList<>(len);
        if (len == 0) {
            return res;
        }
        for (int i = 0; i < len - 1; i++) {
            int count = 0;
            for (int j = i + 1; j < len; j++) {
                if (nums[i] > nums[j]) {
                    count++;
                }
            }
            res.add(count);
        }
        res.add(0);
        return res;
    }

    public static void main(String[] args) {
        int[] nums = {5, 2, 6, 1};
        Solution solution = new Solution();
        List res = solution.countSmaller(nums);
        System.out.println(res);
    }
}

方法二:插入排序

  • 从后向前将看到的数插入到一个临时数组中;
  • 将当前看到的数插入到合适的位置,即第 1 个大于等于当前看到的数的位置;
  • 这个位置的索引的值就是这个数的右边比它严格小的数的个数
  • 这种插入排序是不稳定的。

Java 代码:

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

/**
 * @author liweiwei1419
 * @date 2019/10/2 11:19 上午
 */
public class Solution2 {


    /**
     * 插入排序
     *
     * @param nums
     * @return
     */
    public List countSmaller(int[] nums) {
        int len = nums.length;
        if (len == 0) {
            return new ArrayList<>();
        }
        int[] res = new int[len];
        // 在 temp 数组中找到第 1 个大于等于待插入元素的索引
        int[] temp = new int[len];
        temp[0] = nums[len - 1];
        res[len - 1] = 0;
        // end 指向即将写入的那个元素的位置
        int end = 1;
        for (int i = len - 2; i >= 0; i--) {
            if (nums[i] > temp[end - 1]) {
                temp[end] = nums[i];
                res[i] = end;
            } else {
                // 插入排序
                int j = end;
                int tempVal = nums[i];
                while (j > 0 && temp[j - 1] >= tempVal) {
                    temp[j] = temp[j - 1];
                    j--;
                }
                temp[j] = tempVal;
                res[i] = j;
            }
            end++;
        }

        List result = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            result.add(res[i]);
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = {2, 0, 1};
        Solution2 solution2 = new Solution2();
        List res = solution2.countSmaller(nums);
        System.out.println(res);
    }
}

说明:“将当前看到的数插入到合适的位置,即第 1 个大于等于当前看到的数的位置;”这一步可以使用二分查找法。

Java 代码:

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

/**
 * @author liweiwei1419
 * @date 2019/10/2 11:19 上午
 */
public class Solution3 {


    /**
     * 插入排序 + 二分查找
     *
     * @param nums
     * @return
     */
    public List countSmaller(int[] nums) {
        int len = nums.length;
        if (len == 0) {
            return new ArrayList<>();
        }
        int[] res = new int[len];
        // 在 temp 数组中找到第 1 个大于等于待插入元素的索引
        int[] temp = new int[len];
        temp[0] = nums[len - 1];
        res[len - 1] = 0;
        // end 指向即将写入的那个元素的位置
        int end = 1;
        for (int i = len - 2; i >= 0; i--) {
            if (nums[i] > temp[end - 1]) {
                temp[end] = nums[i];
                res[i] = end;
            } else {

                // 先找到第 1 个大于等 target 的索引

                int left = 0;
                int right = end - 1;
                while (left < right) {
                    int mid = (left + right) >>> 1;
                    if (temp[mid] < nums[i]) {
                        left = mid + 1;
                    } else {
                        right = mid;
                    }
                }

                // 这个时候就可以写入结果集了
                res[i] = left;

                // 插入排序,把它之后的全部后移一位
                int j = end - 1;
                while (j >= left) {
                    temp[j + 1] = temp[j];
                    j--;
                }
                temp[left] = nums[i];
            }
            end++;
        }


        List result = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            result.add(res[i]);
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = {5, 2, 6, 1};
        Solution3 solution2 = new Solution3();
        List res = solution2.countSmaller(nums);
        System.out.println(res);
    }
}

方法三:归并排序

方法四:树状数组

你可能感兴趣的:(LeetCode 第 315 题:计算右侧小于当前元素的个数)