Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

smaller elements on right hand side leetcode


/*
	This is an implementation that demonstrates
	how to efficiently find the number of smaller
	elements to the right of every individual array
	element.
	Example: 
	Input: nums = [5,2,6,1]
	Output: [2,1,1,0]
	Explanation:
	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. 
	

	Time complexity: O(nlog2(n)) 
	Space complexity: O(n)
*/

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

public class SmallerNumbers {
	class Item {
		int val;
		int index;

		public Item(int v, int i) {
			val = v;
			index = i;
		}

	}

	public static void main(String[] args) {
		SmallerNumbers application = new SmallerNumbers();
		int[] nums = { 5, 2, 6, 1 };
		List<Integer> output = application.countSmaller(nums);
		System.out.println(output); // [2, 1, 1, 0]
	}

	public List<Integer> countSmaller(int[] nums) {
		int n = nums.length;
		Item[] items = new Item[n];

		for (int i = 0; i < n; i++) {
			items[i] = new Item(nums[i], i);
		}

		int[] count = new int[n];
		mergeSort(items, 0, n - 1, count);

		List<Integer> result = new ArrayList<>();

		for (int c : count) {
			result.add(c);
		}

		return result;

	}

	private void mergeSort(Item[] items, int lo, int hi, int[] count) {
		if (lo >= hi)
			return;
		int mid = lo + (hi - lo) / 2;
		mergeSort(items, lo, mid, count);
		mergeSort(items, mid + 1, hi, count);
		merge(items, lo, mid, mid + 1, hi, count);
	}

	private void merge(Item[] items, int lo, int loEnd, int hi, int hiEnd, int[] count) {
		int m = hiEnd - lo + 1;
		Item[] sorted = new Item[m];
		int index = 0;
		int loPtr = lo, hiPtr = hi;

		int rightCounter = 0;

		while (loPtr <= loEnd && hiPtr <= hiEnd) {
			if (items[hiPtr].val < items[loPtr].val) {
				rightCounter++;
				sorted[index++] = items[hiPtr++];
			} else {
				count[items[loPtr].index] += rightCounter;
				sorted[index++] = items[loPtr++];
			}
		}

		while (loPtr <= loEnd) {
			count[items[loPtr].index] += rightCounter;
			sorted[index++] = items[loPtr++];
		}

		while (hiPtr <= hiEnd) {
			sorted[index++] = items[hiPtr++];
		}

		System.arraycopy(sorted, 0, items, lo, m);

	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: java get specific element from arraylist 
Java :: spring execute code after variable injected 
Java :: Integer i = new Integer(257); byte x = i.byteValue(); System.out.print(x); 
Java :: textarea user select disable java swing 
Java :: execute application jar 
Java :: how to print the last element of an array 
Java :: java 8 remove spaces from string 
Java :: android java string to double 
Java :: plus one leetcode 
Java :: append to file in java 
Java :: how to decompose a string into words in Java 
Java :: convert list to map java 
Java :: count the number of words in a string java 
Java :: get last element of array java 
Java :: quotation marks in string java 
Java :: how to use base64.getdecoder() android 
Java :: java max from 3 parameters 
Java :: public static int to String java 
Java :: create map java 
Java :: java int to binary string 
Java :: how to preset a list java 
Java :: access main class from another class spigot 
Java :: how to extract decimal vqalue from float in android studio 
Java :: input array through scanner in java 
Java :: java read each lines in file 
Java :: jdk path ubuntu 
Java :: retrofit dependency in android studio 
Java :: simple date format 
Java :: android java convert double to 2 decimal places 
Java :: get logged-in user in Spring Security 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =