[GeeksForGeeks] Convert an array to reduced form

Given an array with n distinct elements, convert the given array to a form where all elements are in range from 0 to n-1.

The order of elements is same, i.e., 0 is placed in place of smallest element, 1 is placed for second smallest element, … n-1 is placed for largest element.

Solution 1. O(n^2) runtime

Do a linear scan to find the smallest element and replace its value with 0;

Repeat the same process n - 1 times for the 2nd, 3rd,.....nth smallest elements.

Solution 2. O(n * log n) runtime, O(n) space

1. make a copy of input array and sort this copy.

2. use the sorted copy to create a mapping between each element and its reduced number .

3. iterate the input array and replace each element with its reduced number from the mapping.

 1 public void convertToReducedForm(int[] arr) {
 2     if(arr == null || arr.length == 0) {
 3         return;
 4     }
 5     int[] temp = new int[arr.length];
 6     HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
 7     for(int i = 0; i < temp.length; i++) {
 8         temp[i] = arr[i];
 9     }
10     Arrays.sort(temp);
11     for(int i = 0; i < temp.length; i++) {
12         if(!map.containsKey(temp[i])) {
13             map.put(temp[i], i);
14         }
15     }
16     for(int i = 0; i < arr.length; i++) {
17         arr[i] = map.get(arr[i]);
18     }
19 }
				
时间: 2024-12-26 07:19:03

[GeeksForGeeks] Convert an array to reduced form的相关文章

[GeeksForGeeks] Sort an array in wave form

Given an unsorted array of integers, sort the array into a wave like array. An array arr[0...n-1] is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= .... Solution 1. O(n * logn) runtime, using sorting 1. sort the in

LeetCode OJ - Convert Sorted Array/List to Binary Search Tree

虚函数使用的时机 为什么虚函数不总是适用? 1. 虚函数有事会带来很大的消耗: 2. 虚函数不总是提供所需的行为: 3. 当我们不考虑继承当前类时,不必使用虚函数. 必须使用虚函数的情况: 1. 当你想删除一个表面上指向基类对象,实际却是指向派生类对象的指针,就需要虚析构函数. LeetCode OJ - Convert Sorted Array/List to Binary Search Tree,布布扣,bubuko.com LeetCode OJ - Convert Sorted Arra

leetcode 108 Convert Sorted Array to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Convert Sorted Array to Binary Search Tree Description Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /** * Definition f

39.1: Convert Sorted Array to Binary Search Tree

/************************************************************************/            /*       39.1:  Convert Sorted Array to Binary Search Tree                               */            /************************************************************

Convert Sorted Array to Binary Search Tree &amp; Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 方法:将有序数组转为平衡二叉树,我们知道平衡二叉树的中序遍历是有序数组,而且“中间”的元素可以作为根节点,那么构建树的时候,找出中间元素,构造根元素,然后继续重复上面的方法,构造左子树和右子树.代码如下: 1 /**

[leetcode]Convert Sorted Array to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉查找树,这颗二叉查找树要求是平衡的. 解题思路:由于要求二叉查找树是平衡的.所以我们可以选在数组的中间那个数当树根root,然后这个数左边的数组为左子树,右边的数组为右子树,分别递归产生左右子树就可以了. 代码: # Definition for a binary tree node # class

LeetCode OJ 108. Convert Sorted Array to Binary Search Tree DFS求解

题目链接:https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 108. Convert Sorted Array to Binary Search Tree My Submissions Question Total Accepted: 68378 Total Submissions: 187560 Difficulty: Medium Given an array where elements ar

Convert Sorted Array to Binary Search Tree leetcode java

题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题解: 先复习下什么是二叉搜索树(引自Wikipedia): 二叉查找树(Binary Search Tree),也称有序二叉树(ordered binary tree),排序二叉树(sorted binary tree),是指一棵空树或者具有下列性质的二叉树: 若任意节点的左子树不空,则左子树

[Leetcode][JAVA] Convert Sorted Array to Binary Search Tree &amp;&amp; Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 很简单的二分法,只要给出Array的开始和结束下标作为参数传入即可. 1 public TreeNode sortedArrayToBST(int[] num) { 2 return constructBST(num,