Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 136d3fb

Browse files
committed
1365_How_Many_Numbers_Are_Smaller_Than_the_Current_Number
1 parent d90e3fe commit 136d3fb

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ Also, there are open source implementations for basic data structs and algorithm
220220
| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1089_Duplicate_Zeros.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1089_Duplicate_Zeros.java) | 2 Pass, store last position and final move steps, O(n) and O(1) |
221221
| 1260 | [Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1260_Shift_2D_Grid.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1260_Shift_2D_Grid.java) | Final position of each element can be computed according to k, m and n, e.g., k == mn, then don't move, O(mn) and O(mn) |
222222
| 1337 | [The K Weakest Rows in a Matrix](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1337_The_K_Weakest_Rows_in_a_Matrix.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1337_The_K_Weakest_Rows_in_a_Matrix.java) | Check by row, from left to right, until encount first zero, O(mn) and O(1) |
223+
| 1365 | [How Many Numbers Are Smaller Than the Current Number](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1365_How_Many_Numbers_Are_Smaller_Than_the_Current_Number.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1365_How_Many_Numbers_Are_Smaller_Than_the_Current_Number.java) | 1. Sort and get position in sorted nums, O(nlogn) and O(n)<br>2. Fill count into 0-100, O(n) and O(1) |
223224

224225
| # | To Understand |
225226
|---| ----- |
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.Map;
2+
3+
class Solution {
4+
/* public int[] smallerNumbersThanCurrent(int[] nums) {
5+
Map<Integer, Integer> sortedIndex = new HashMap<>();
6+
int[] sortedNums = new int[nums.length];
7+
// sort and get position
8+
System.arraycopy(nums, 0, sortedNums, 0, nums.length);
9+
Arrays.sort(sortedNums);
10+
for (int i = 0; i < nums.length; i++) {
11+
if (sortedIndex.containsKey(sortedNums[i])) continue;
12+
sortedIndex.put(sortedNums[i], i);
13+
}
14+
for (int i = 0; i < nums.length; i++)
15+
sortedNums[i] = sortedIndex.get(nums[i]);
16+
return sortedNums;
17+
} */
18+
19+
public int[] smallerNumbersThanCurrent(int[] nums) {
20+
int[] countList = new int[101];
21+
int[] res = new int[nums.length];
22+
// count numbers
23+
for (int i = 0; i < nums.length; i++)
24+
countList[nums[i]]++;
25+
// compute numbers before current index
26+
for (int i = 1; i < 101; i++)
27+
countList[i] += countList[i-1];
28+
for (int i = 0; i < nums.length; i++) {
29+
if (nums[i] == 0) res[i] = 0;
30+
else res[i] = countList[nums[i]-1];
31+
}
32+
return res;
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
# def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
3+
# sorted_index = {}
4+
# # sort nums and store sorted position in hashmap
5+
# for pos, value in enumerate(sorted(nums)):
6+
# if value in sorted_index:
7+
# continue
8+
# sorted_index[value] = pos
9+
# res = []
10+
# for value in nums:
11+
# res.append(sorted_index[value])
12+
# return res
13+
14+
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
15+
count_list = [0] * 101
16+
# count numbers
17+
for v in nums:
18+
count_list[v] += 1
19+
# compute numbers before current index
20+
for i in range(1, 101):
21+
count_list[i] += count_list[i-1]
22+
res = []
23+
for v in nums:
24+
if v == 0:
25+
res.append(0)
26+
else:
27+
res.append(count_list[v-1])
28+
return res
29+
30+
# def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
31+
# count = collections.Counter(nums)
32+
# for i in range(1,101):
33+
# count[i] += count[i-1]
34+
# return [count[x-1] for x in nums]

0 commit comments

Comments
 (0)