国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

LeetCode -- Count of Smaller Numbers After Self

2019-11-08 02:11:47
字體:
供稿:網(wǎng)友
題目描述: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].給定一個(gè)數(shù)組a[n],返回?cái)?shù)組b[n],每個(gè)元素對應(yīng)當(dāng)前元素a[i]右邊小于a[i]的個(gè)數(shù)。思路:本題比較直接的實(shí)現(xiàn)是創(chuàng)建二叉搜索樹。從右向左遍歷數(shù)組a[n],i∈[n-1,0],創(chuàng)建二叉樹。二叉樹的每個(gè)節(jié)點(diǎn)node存放a[i]以及小于當(dāng)前節(jié)點(diǎn)的元素個(gè)數(shù)。如果a[i]小于node.Value,node.Count++,向左走;走不動(dòng)時(shí)把a(bǔ)[i]創(chuàng)建為葉子;如果a[i]大于等于node.Value,當(dāng)前count++,向右走;走不動(dòng)時(shí)把a(bǔ)[i]創(chuàng)建為葉子返回當(dāng)前count作為結(jié)果。實(shí)現(xiàn)代碼:
public class Solution {    public IList<int> CountSmaller(int[] nums)     {        if (nums == null || nums.Length == 0){    		return new List<int>();    	}    	    	var result = new List<int>(){0 /*there is no value on the right of last number*/};    	var len = nums.Length;    	var root = new BTNode(nums[len - 1]);    	for (var i = len - 2; i >= 0; i--){    		// add the value of arr on the tree one by one    		// also update the count of smaller on the 'root' node    		var smallerCount = BuildAndCount(root, nums[i]);    		result.Add(smallerCount);    	}    	    	result.Reverse();    	    	return result;     }          public int BuildAndCount(BTNode current, int value){         	int countOfSmaller = 0;     	while(true){    		if(value > current.Value){    			    			// value bigger than current node , add current node's 'count of smaller' on this value    			countOfSmaller += current.Count + 1;    			// keep going right ,until reach leaf    			if(current.Right == null){    				current.Right = new BTNode(value);    				break;    			}else{    				current = current.Right;    			}    		}    		else{    			// value is smaller than current node value , increase the count of smaller on current node    			current.Count ++;    			// put value on left tree leaf    			if(current.Left == null){    				current.Left = new BTNode(value);    				break;    			}    			else{    				current = current.Left;    			}    		}    	}    	    	return countOfSmaller;     }          public class BTNode{     	public BTNode Left;    	public BTNode Right;    	public int Value;    	public int Count ;    	public BTNode (int v){    		Value = v;    		Count = 0;    	}     }}也可以考慮BIT的實(shí)現(xiàn):https://www.topcoder.com/community/data-science/data-science-tutorials/binary-indexed-trees/
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 大余县| 汤阴县| 彰化市| 隆子县| 瓦房店市| 准格尔旗| 中山市| 子长县| 陆川县| 白河县| 砀山县| 双桥区| 聂拉木县| 深泽县| 大方县| 武胜县| 涪陵区| 镶黄旗| 静宁县| 万山特区| 万源市| 韩城市| 台南市| 滨州市| 黄平县| 合水县| 盘锦市| 朝阳县| 乌拉特中旗| 河北区| 阜新| 孟州市| 柳林县| 通江县| 扎囊县| 西乌珠穆沁旗| 柳江县| 新竹县| 宁夏| 凤冈县| 湾仔区|