Skip to content

Commit 6bd8336

Browse files
add 1365
1 parent 44358cf commit 6bd8336

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ _If you like this project, please leave me a star._ ★
99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
1111
|1367|[Linked List in Binary Tree](https://leetcode.com/problems/linked-list-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1367.java) | |Medium||DP, Linked List, Tree
12+
|1365|[How Many Numbers Are Smaller Than the Current Number](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1365.java) | |Easy||Array, HashTable
1213
|1362|[Closest Divisors](https://leetcode.com/problems/closest-divisors/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1362.java) | |Medium||Math
1314
|1361|[Validate Binary Tree Nodes](https://leetcode.com/problems/validate-binary-tree-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1361.java) | |Medium||Graph
1415
|1360|[Number of Days Between Two Dates](https://leetcode.com/problems/number-of-days-between-two-dates/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1360.java) | |Easy||
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* 1365. How Many Numbers Are Smaller Than the Current Number
9+
*
10+
* Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it.
11+
* That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i].
12+
* Return the answer in an array.
13+
*
14+
* Example 1:
15+
* Input: nums = [8,1,2,2,3]
16+
* Output: [4,0,1,1,3]
17+
* Explanation:
18+
* For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
19+
* For nums[1]=1 does not exist any smaller number than it.
20+
* For nums[2]=2 there exist one smaller number than it (1).
21+
* For nums[3]=2 there exist one smaller number than it (1).
22+
* For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
23+
*
24+
* Example 2:
25+
* Input: nums = [6,5,4,8]
26+
* Output: [2,1,0,3]
27+
*
28+
* Example 3:
29+
* Input: nums = [7,7,7,7]
30+
* Output: [0,0,0,0]
31+
*
32+
* Constraints:
33+
* 2 <= nums.length <= 500
34+
* 0 <= nums[i] <= 100
35+
* */
36+
public class _1365 {
37+
public static class Solution1 {
38+
public int[] smallerNumbersThanCurrent(int[] nums) {
39+
int[] result = new int[nums.length];
40+
int[] tmp = Arrays.copyOf(nums, nums.length);
41+
Arrays.sort(tmp);
42+
List<Integer> list = new ArrayList<>();
43+
for (int i : tmp) {
44+
list.add(i);
45+
}
46+
for (int i = 0; i < nums.length; i++) {
47+
result[i] = list.indexOf(nums[i]);
48+
}
49+
return result;
50+
}
51+
}
52+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1365;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1365Test {
10+
private static _1365.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1365.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[]{8, 1, 2, 2, 3};
21+
assertArrayEquals(new int[]{4, 0, 1, 1, 3}, solution1.smallerNumbersThanCurrent(nums));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
nums = new int[]{6, 5, 4, 8};
27+
assertArrayEquals(new int[]{2, 1, 0, 3}, solution1.smallerNumbersThanCurrent(nums));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
nums = new int[]{7, 7, 7, 7};
33+
assertArrayEquals(new int[]{0, 0, 0, 0}, solution1.smallerNumbersThanCurrent(nums));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)