[코테 풀이] How Many Numbers Are Smaller Than the Current Number

시내·2024년 6월 12일

Q_1365) How Many Numbers Are Smaller Than the Current Number

출처 : https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/

Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. 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].

class Solution {
    public int[] smallerNumbersThanCurrent(int[] nums) {
    	int index = 0;
        int[] counts = new int[nums.length];
        
        for (int n = 0; n < nums.length; n++) {
            int count = 0;
            for (int m = 0; m < nums.length; m++) {
                if (nums[n] > nums[m]) {
                    count++;
                }
            }
            counts[index++] = count;
        }
        return counts;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글