forked from srishilesh/Data-Structure-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1365_Numbers_smaller_than_current_number.java
47 lines (39 loc) · 1.1 KB
/
1365_Numbers_smaller_than_current_number.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/
// MY APPROACH
class Solution {
public int[] smallerNumbersThanCurrent(int[] nums) {
int count = 0;
int[] out = new int[nums.length];
for(int i=0;i<nums.length;i++)
{
count = 0;
for(int j=0;j<nums.length;j++)
{
if(j!=i && nums[j]<nums[i])
count++;
}
out[i] = count;
}
return out;
}
}
// BEST SOLUTION
class Solution {
public int[] smallerNumbersThanCurrent(int[] nums) {
int[] count = new int[101];
int[] res = new int[nums.length];
for (int i =0; i < nums.length; i++) {
count[nums[i]]++;
}
for (int i = 1 ; i <= 100; i++) {
count[i] += count[i-1];
}
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0)
res[i] = 0;
else
res[i] = count[nums[i] - 1];
}
return res;
}
}