Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1365. How Many Numbers Are Smaller Than the Current Number #78

Closed
twy30 opened this issue Nov 3, 2020 · 3 comments · Fixed by #90
Closed

1365. How Many Numbers Are Smaller Than the Current Number #78

twy30 opened this issue Nov 3, 2020 · 3 comments · Fixed by #90
Labels

Comments

@twy30
Copy link
Contributor

twy30 commented Nov 3, 2020

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

using System.Linq;

public class Solution
{
    public int[] SmallerNumbersThanCurrent(int[] nums)
    {
        // 「輸入的數字」(複數)
        var inputNumbers = nums;

        // 「最大的可能的『輸入的數字』」
        const int maxPossibleInputNumber = 100;

        // 「『輸入的數字』的數量」(複數)
        var inputNumberCounts = new int[maxPossibleInputNumber + 1];

        foreach (var i in inputNumbers)
        {
            ++inputNumberCounts[i];
        }

        // 「 smaller Number 的數量」(複數)
        var smallerNumberCounts = new int[maxPossibleInputNumber + 1];

        for (int i = 1; i < smallerNumberCounts.Length; ++i)
        {
            smallerNumberCounts[i] = smallerNumberCounts[i - 1] + inputNumberCounts[i - 1];
        }

        return inputNumbers
        .Select(number => smallerNumberCounts[number])
        .ToArray();
    }
}

請參考「刷 LeetCode 練習命名」 #69 😊

@LPenny-github
Copy link

@twy30 大大我又來了!
麻煩有空給予命名建議 orz。感激不盡!

using System.Linq;

public class Solution {
    public int[] SmallerNumbersThanCurrent(int[] nums) {

            // output = 升冪排列好的 input 位置索引
            // (若 input 裡有相同的數字,就取最靠近 [0] 的位置索引)

            // input =  [8,1,2,2,3]
            // 升冪排列 input = [1,2,2,3,8] 
            //         index = [0,1,2,3,4]
            // output = [4,0,1,1,3]

            int[] inputNumbers = nums;
            int[] sortedNumbers = new int[inputNumbers.Length];
            Array.Copy(inputNumbers, sortedNumbers, inputNumbers.Length);
            Array.Sort(sortedNumbers);

            return inputNumbers.Select(digit=>Array.IndexOf(sortedNumbers, digit)).ToArray();
        }
    }

@twy30
Copy link
Contributor Author

twy30 commented Dec 3, 2020

@LPenny-github 歡迎 😊


            int[] sortedNumbers = new int[inputNumbers.Length];

另一個可能性是 sortedInputNumbers


以下與命名沒有直接關係 😊

            int[] sortedNumbers = new int[inputNumbers.Length];
            Array.Copy(inputNumbers, sortedNumbers, inputNumbers.Length);

可以參考 Array.Clone() ( https://docs.microsoft.com/en-us/dotnet/api/system.array.clone?view=net-5.0 )

@LPenny-github
Copy link

感謝 @twy30 大大指導 orz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants