
문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
배열 nums가 주어지고, 각 nums[i]가 배열에서 nums[i]보다 작은 숫자가 얼마나 많은지 찾아라. 각 nums[i]에 대해 j != i이고 nums[j] < nums[i]인 유효한 j의 개수를 세라.
#1
Input: nums = [8, 1, 2, 2, 3]
Output: [4, 0, 1, 1, 3]
Explanation:
nums[0] = 0보다 작은 수는 4개이다.
nums[1] = 1보다 작은 수는 없다.
nums[2] = 2보다 작은 수는 1개이다.
nums[3] = 2보다 작은 수는 1개이다.
nums[4] = 3보다 작은 수는 3개이다.
#2
Input: nums = [6, 5, 4, 8]
Output: [2, 1, 0, 3]
#3
Input: nums = [7, 7, 7, 7]
Output: [0, 0, 0, 0]
class Solution {
public int[] smallerNumbersThanCurrent(int[] nums) {
int[] allNums = new int[101];
int[] result = new int[nums.length];
for(int i = 0; i < nums.length; i++){
allNums[nums[i]]++;
}
for(int i = 1; i < 101; i++){
allNums[i] += allNums[i - 1];
}
for(int i = 0; i < nums.length; i++){
if(nums[i] == 0){
result[i] = 0;
}else{
result[i] = allNums[nums[i] - 1];
}
}
return result;
}
}