当前位置:   article > 正文

LeetCode315_leetcode315 c语言

leetcode315 c语言

You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].

Example:

Given nums = [5, 2, 6, 1]

To the right of 5 there are 2 smaller elements (2 and 1).
To the right of 2 there is only 1 smaller element (1).
To the right of 6 there is 1 smaller element (1).
To the right of 1 there is 0 smaller element.

Return the array [2, 1, 1, 0].

这道题如果使用O(N^2)的穷搜方法是绝对超时的。·但是从最右边元素开始处理的想法却是正确的。

使用什么方法的可以减少查询次数呢?最妙的做法是我在LeetCode 的solution里看到的,这个方法在处理每个元素的同时维护了一棵二叉查找树,这样我们每次处理一个新的元素时,只需要从根节点处对其进行插入,并且记录每次走右子树时的sum值(因为右子树值均大于左子树),而每个右子树节点都记录了其左子树的节点数(包括重复的值)。

因此,先定义节点类

  1. class Node {
  2. public Node(int dup,int val){
  3. this.dup = dup; //记录重复次数
  4. this.val = val;
  5. this.left = null;
  6. this.right = null;
  7. }
  8. public void incDup(){
  9. this.dup++;
  10. }
  11. public int getDup(){
  12. return this.dup;
  13. }
  14. private int dup;
  15. public int sum; //记录该节点左子树中节点总数
  16. public int val;
  17. public Node left;
  18. public Node right;
  19. }

再实现一个二叉查找树插入函数,具有更新节点内部值的功能。

  1. public Node insert(List<Integer> counts,Node node,int curvalue,int sum){
  2. if (node == null) {
  3. node = new Node(1,curvalue);
  4. counts.add(0,sum);
  5. }
  6. else if (node.val == curvalue) {
  7. node.incDup();
  8. counts.add(0,sum+node.sum);
  9. }
  10. else if (node.val > curvalue) {
  11. node.sum++;
  12. node.left = insert(counts,node.left,curvalue,sum);
  13. }
  14. else node.right = insert(counts,node.right,curvalue,sum+node.getDup()+node.sum);
  15. return node;
  16. }

注意node == null时的情况,实际上是长出一个新的节点。而insert 函数返回的一直是根节点。

完整的solution:

  1. class Node {
  2. public Node(int dup,int val){
  3. this.dup = dup;
  4. this.val = val;
  5. this.left = null;
  6. this.right = null;
  7. }
  8. public void incDup(){
  9. this.dup++;
  10. }
  11. public int getDup(){
  12. return this.dup;
  13. }
  14. private int dup;
  15. public int sum;
  16. public int val;
  17. public Node left;
  18. public Node right;
  19. }
  20. public class Solution {
  21. public List<Integer> countSmaller(int[] nums) {
  22. List<Integer> counts = new ArrayList<Integer>();
  23. Node root = null;
  24. for (int i = nums.length-1;i>=0;i--){
  25. root = insert(counts,root,nums[i],0);
  26. }
  27. return counts;
  28. }
  29. public Node insert(List<Integer> counts,Node node,int curvalue,int sum){
  30. if (node == null) {
  31. node = new Node(1,curvalue);
  32. counts.add(0,sum);
  33. }
  34. else if (node.val == curvalue) {
  35. node.incDup();
  36. counts.add(0,sum+node.sum);
  37. }
  38. else if (node.val > curvalue) {
  39. node.sum++;
  40. node.left = insert(counts,node.left,curvalue,sum);
  41. }
  42. else node.right = insert(counts,node.right,curvalue,sum+node.getDup()+node.sum);
  43. return node;
  44. }
  45. }




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/227638
推荐阅读
  

闽ICP备14008679号