{"id":307,"date":"2020-09-26T10:23:16","date_gmt":"2020-09-26T02:23:16","guid":{"rendered":"http:\/\/39.96.58.60\/?p=307"},"modified":"2022-10-18T16:43:37","modified_gmt":"2022-10-18T08:43:37","slug":"leetcode-1357-%e6%af%8f%e9%9a%94-n-%e4%b8%aa%e9%a1%be%e5%ae%a2%e6%89%93%e6%8a%98","status":"publish","type":"post","link":"https:\/\/www.yatenglg.cn\/blog\/?p=307","title":{"rendered":"Leetcode 1357. \u6bcf\u9694 n \u4e2a\u987e\u5ba2\u6253\u6298"},"content":{"rendered":"<p>\u8d85\u5e02\u91cc\u6b63\u5728\u4e3e\u884c\u6253\u6298\u6d3b\u52a8\uff0c\u6bcf\u9694&nbsp;<code>n<\/code>&nbsp;\u4e2a\u987e\u5ba2\u4f1a\u5f97\u5230 <code>discount<\/code>&nbsp;\u7684\u6298\u6263\u3002<\/p>\n<p>\u8d85\u5e02\u91cc\u6709\u4e00\u4e9b\u5546\u54c1\uff0c\u7b2c&nbsp;<code>i<\/code>&nbsp;\u79cd\u5546\u54c1\u4e3a&nbsp;<code>products[i]<\/code>&nbsp;\u4e14\u6bcf\u4ef6\u5355\u54c1\u7684\u4ef7\u683c\u4e3a&nbsp;<code>prices[i]<\/code>&nbsp;\u3002<\/p>\n<p>\u7ed3\u8d26\u7cfb\u7edf\u4f1a\u7edf\u8ba1\u987e\u5ba2\u7684\u6570\u76ee\uff0c\u6bcf\u9694&nbsp;<code>n<\/code>&nbsp;\u4e2a\u987e\u5ba2\u7ed3\u8d26\u65f6\uff0c\u8be5\u987e\u5ba2\u7684\u8d26\u5355\u90fd\u4f1a\u6253\u6298\uff0c\u6298\u6263\u4e3a&nbsp;<code>discount<\/code>&nbsp;\uff08\u4e5f\u5c31\u662f\u5982\u679c\u539f\u672c\u8d26\u5355\u4e3a&nbsp;<code>x<\/code>&nbsp;\uff0c\u90a3\u4e48\u5b9e\u9645\u91d1\u989d\u4f1a\u53d8\u6210&nbsp;<code>x - (discount * x) \/ 100<\/code>&nbsp;\uff09\uff0c\u7136\u540e\u7cfb\u7edf\u4f1a\u91cd\u65b0\u5f00\u59cb\u8ba1\u6570\u3002<\/p>\n<p>\u987e\u5ba2\u4f1a\u8d2d\u4e70\u4e00\u4e9b\u5546\u54c1\uff0c&nbsp;<code>product[i]<\/code>&nbsp;\u662f\u987e\u5ba2\u8d2d\u4e70\u7684\u7b2c&nbsp;<code>i<\/code>&nbsp;\u79cd\u5546\u54c1\uff0c&nbsp;<code>amount[i]<\/code>&nbsp;\u662f\u5bf9\u5e94\u7684\u8d2d\u4e70\u8be5\u79cd\u5546\u54c1\u7684\u6570\u76ee\u3002<\/p>\n<p>\u8bf7\u4f60\u5b9e\u73b0&nbsp;<code>Cashier<\/code>&nbsp;\u7c7b\uff1a<\/p>\n<ul>\n<li><code>Cashier(int n, int discount, int[] products, int[] prices)<\/code>&nbsp;\u521d\u59cb\u5316\u5b9e\u4f8b\u5bf9\u8c61\uff0c\u53c2\u6570\u5206\u522b\u4e3a\u6253\u6298\u9891\u7387&nbsp;<code>n<\/code>&nbsp;\uff0c\u6298\u6263\u5927\u5c0f <code>discount<\/code>&nbsp;\uff0c\u8d85\u5e02\u91cc\u7684\u5546\u54c1\u5217\u8868 <code>products<\/code>&nbsp;\u548c\u5b83\u4eec\u7684\u4ef7\u683c <code>prices<\/code>&nbsp;\u3002<\/li>\n<li><code>double&nbsp;getBill(int[] product, int[] amount)<\/code>&nbsp;\u8fd4\u56de\u8d26\u5355\u7684\u5b9e\u9645\u91d1\u989d\uff08\u5982\u679c\u6709\u6253\u6298\uff0c\u8bf7\u8fd4\u56de\u6253\u6298\u540e\u7684\u7ed3\u679c\uff09\u3002\u8fd4\u56de\u7ed3\u679c\u4e0e\u6807\u51c6\u7b54\u6848\u8bef\u5dee\u5728&nbsp;<code>10^-5<\/code>&nbsp;\u4ee5\u5185\u90fd\u89c6\u4e3a\u6b63\u786e\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165<\/strong>\n\n[\"Cashier\",\"getBill\",\"getBill\",\"getBill\",\"getBill\",\"getBill\",\"getBill\",\"getBill\"]\n\n[[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]]]\n\n<strong>\u8f93\u51fa<\/strong>\n\n[null,500.0,4000.0,800.0,4000.0,4000.0,7350.0,2500.0]\n\n<strong>\u89e3\u91ca<\/strong>\n\nCashier cashier = new Cashier(3,50,[1,2,3,4,5,6,7],[100,200,300,400,300,200,100]);\n\ncashier.getBill([1,2],[1,2]);                        \/\/ \u8fd4\u56de 500.0, \u8d26\u5355\u91d1\u989d\u4e3a = 1 * 100 + 2 * 200 = 500.\n\ncashier.getBill([3,7],[10,10]);                      \/\/ \u8fd4\u56de 4000.0\n\ncashier.getBill([1,2,3,4,5,6,7],[1,1,1,1,1,1,1]);    \/\/ \u8fd4\u56de 800.0 \uff0c\u8d26\u5355\u539f\u672c\u4e3a 1600.0 \uff0c\u4f46\u7531\u4e8e\u8be5\u987e\u5ba2\u662f\u7b2c\u4e09\u4f4d\u987e\u5ba2\uff0c\u4ed6\u5c06\u5f97\u5230 50% \u7684\u6298\u6263\uff0c\u6240\u4ee5\u5b9e\u9645\u91d1\u989d\u4e3a 1600 - 1600 * (50 \/ 100) = 800 \u3002\n\ncashier.getBill([4],[10]);                           \/\/ \u8fd4\u56de 4000.0\n\ncashier.getBill([7,3],[10,10]);                      \/\/ \u8fd4\u56de 4000.0\n\ncashier.getBill([7,5,3,1,6,4,2],[10,10,10,9,9,9,7]); \/\/ \u8fd4\u56de 7350.0 \uff0c\u8d26\u5355\u539f\u672c\u4e3a 14700.0 \uff0c\u4f46\u7531\u4e8e\u7cfb\u7edf\u8ba1\u6570\u518d\u6b21\u8fbe\u5230\u4e09\uff0c\u8be5\u987e\u5ba2\u5c06\u5f97\u5230 50% \u7684\u6298\u6263\uff0c\u5b9e\u9645\u91d1\u989d\u4e3a 7350.0 \u3002\n\ncashier.getBill([2,3,5],[5,3,2]);                    \/\/ \u8fd4\u56de 2500.0\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= n &lt;= 10^4<\/code><\/li>\n<li><code>0 &lt;= discount &lt;= 100<\/code><\/li>\n<li><code>1 &lt;= products.length &lt;= 200<\/code><\/li>\n<li><code>1 &lt;= products[i] &lt;= 200<\/code><\/li>\n<li><code>products<\/code>&nbsp;\u5217\u8868\u4e2d&nbsp;<strong>\u4e0d\u4f1a<\/strong>&nbsp;\u6709\u91cd\u590d\u7684\u5143\u7d20\u3002<\/li>\n<li><code>prices.length == products.length<\/code><\/li>\n<li><code>1 &lt;= prices[i] &lt;= 1000<\/code><\/li>\n<li><code>1 &lt;= product.length &lt;= products.length<\/code><\/li>\n<li><code>product[i]<\/code>&nbsp;\u5728&nbsp;<code>products<\/code>&nbsp;\u51fa\u73b0\u8fc7\u3002<\/li>\n<li><code>amount.length == product.length<\/code><\/li>\n<li><code>1 &lt;= amount[i] &lt;= 1000<\/code><\/li>\n<li>\u6700\u591a\u6709&nbsp;<code>1000<\/code> \u6b21\u5bf9&nbsp;<code>getBill<\/code>&nbsp;\u51fd\u6570\u7684\u8c03\u7528\u3002<\/li>\n<li>\u8fd4\u56de\u7ed3\u679c\u4e0e\u6807\u51c6\u7b54\u6848\u8bef\u5dee\u5728&nbsp;<code>10^-5<\/code>&nbsp;\u4ee5\u5185\u90fd\u89c6\u4e3a\u6b63\u786e\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u8bbe\u8ba1\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a172 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e86100.00% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a20.3 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e86100.00% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    Cashier __init__ \u4e2d\uff0c\n        \u4f7f\u7528\u5b57\u5178\u4fdd\u5b58\u5546\u54c1\u53ca\u5176\u4ef7\u683c\n        \u521d\u59cb\u5316\u987e\u5ba2\u8ba1\u7b97\u4e3a0\uff0c\u4e3a\u4fdd\u6301\u8ba1\u6570\u6570\u503c\u4e0d\u8d85\u9884\u671f\uff0c\u8ba1\u6570\u6570\u503c\u4fdd\u5b58\u5f53\u524d\u987e\u5ba2\/3\u7684\u4f59\u6570\n    getBill \u4e2d\uff0c\n        \u987e\u5ba2\u6570\u7d2f\u52a0\uff0c\u5e76\u66f4\u65b0\n        \u8ba1\u7b97\u662f\u5426\u8be5\u6253\u6298\n        \u4ece\u5546\u54c1\u5b57\u5178\u4e2d\u83b7\u53d6\u5546\u54c1\u5bf9\u5e94\u4ef7\u683c\u5e76\u8ba1\u7b97\u603b\u4ef7\n\"\"\"\nclass Cashier:\n\n    def __init__(self, n: int, discount: int, products: List[int], prices: List[int]):\n        self.n = n\n        self.discount = discount\n        self.products_prices_dict = dict(zip(products, prices))\n\n        self.num = 0\n\n    def getBill(self, product: List[int], amount: List[int]) -&gt; float:\n        self.num = (self.num + 1) % self.n\n        price = 0\n        for prod, amou in zip(product, amount):\n            price += self.products_prices_dict[prod] * amou\n        if self.num == 0:\n            price = price - self.discount \/ 100 * price\n\n        return price\n\n# Your Cashier object will be instantiated and called as such:\n# obj = Cashier(n, discount, products, prices)\n# param_1 = obj.getBill(product,amount)<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u8d85\u5e02\u91cc\u6b63\u5728\u4e3e\u884c\u6253\u6298\u6d3b\u52a8\uff0c\u6bcf\u9694&nbsp;n&nbsp;\u4e2a\u987e\u5ba2\u4f1a\u5f97\u5230 discount&nbsp;\u7684\u6298\u6263\u3002 \u8d85\u5e02&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/307"}],"collection":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=307"}],"version-history":[{"count":1,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/307\/revisions"}],"predecessor-version":[{"id":308,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/307\/revisions\/308"}],"wp:attachment":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}