二、每隔 n 個顧客打折(Biweekly20)

題目描述:
超市裏正在舉行打折活動,每隔 n 個顧客會得到 discount 的折扣。

超市裏有一些商品,第 i 種商品爲 products[i] 且每件單品的價格爲 prices[i] 。

結賬系統會統計顧客的數目,每隔 n 個顧客結賬時,該顧客的賬單都會打折,折扣爲 discount (也就是如果原本賬單爲 x ,那麼實際金額會變成 x - (discount * x) / 100 ),然後系統會重新開始計數。

顧客會購買一些商品, product[i] 是顧客購買的第 i 種商品, amount[i] 是對應的購買該種商品的數目。

請你實現 Cashier 類:

Cashier(int n, int discount, int[] products, int[] prices) 初始化實例對象,參數分別爲打折頻率 n ,折扣大小 discount ,超市裏的商品列表 products 和它們的價格 prices 。
double getBill(int[] product, int[] amount) 返回賬單的實際金額(如果有打折,請返回打折後的結果)。返回結果與標準答案誤差在 10^-5 以內都視爲正確結果。

示例 1:

輸入
[“Cashier”,“getBill”,“getBill”,“getBill”,“getBill”,“getBill”,“getBill”,“getBill”]
[[3,50,[1,2,3,4,5,6,7],[100,200,300,400,300,200,100]],[[1,2],[1,2]],[[3,7],[10,10]],[[1,2,3,4,5,6,7],[1,1,1,1,1,1,1]],[[4],[10]],[[7,3],[10,10]],[[7,5,3,1,6,4,2],[10,10,10,9,9,9,7]],[[2,3,5],[5,3,2]]]
輸出
[null,500.0,4000.0,800.0,4000.0,4000.0,7350.0,2500.0]
解釋
Cashier cashier = new Cashier(3,50,[1,2,3,4,5,6,7],[100,200,300,400,300,200,100]);
cashier.getBill([1,2],[1,2]); // 返回 500.0, 賬單金額爲 = 1 * 100 + 2 * 200 = 500.
cashier.getBill([3,7],[10,10]); // 返回 4000.0
cashier.getBill([1,2,3,4,5,6,7],[1,1,1,1,1,1,1]); // 返回 800.0 ,賬單原本爲 1600.0 ,但由於該顧客是第三位顧客,他將得到 50% 的折扣,所以實際金額爲 1600 - 1600 * (50 / 100) = 800 。
cashier.getBill([4],[10]); // 返回 4000.0
cashier.getBill([7,3],[10,10]); // 返回 4000.0
cashier.getBill([7,5,3,1,6,4,2],[10,10,10,9,9,9,7]); // 返回 7350.0 ,賬單原本爲 14700.0 ,但由於系統計數再次達到三,該顧客將得到 50% 的折扣,實際金額爲 7350.0 。
cashier.getBill([2,3,5],[5,3,2]); // 返回 2500.0

提示:

1 <= n <= 10^4
0 <= discount <= 100
1 <= products.length <= 200
1 <= products[i] <= 200
products 列表中 不會 有重複的元素。
prices.length == products.length
1 <= prices[i] <= 1000
1 <= product.length <= products.length
product[i] 在 products 出現過。
amount.length == product.length
1 <= amount[i] <= 1000
最多有 1000 次對 getBill 函數的調用。
返回結果與標準答案誤差在 10^-5 以內都視爲正確結果。

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/apply-discount-every-n-orders
著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。

手速題

class Solution {
    public int[] sortByBits(int[] arr) {
              Map<Integer,List<Integer>> map = new HashMap<> ();
        for (int i = 0; i < arr.length; i++) {
            int i1 = Integer.bitCount (arr[i]);
            if(map.get (i1) == null){
                map.put (i1,new ArrayList<> ());
            }
            map.get (i1).add (arr[i]);
        }

        int index = 0;
        for (Map.Entry<Integer, List<Integer>> entry : map.entrySet ()) {
            System.out.println (entry.getKey ());
            List<Integer> value = entry.getValue ();
            Collections.sort (value);
            for (int i = 0; i <= value.size () - 1; i++) {
                arr[index ++ ] = value.get (i);
            }
        }
        return arr;

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章