leetcode 2303. Calculate Amount Paid in Taxes（python）
持續創作，加速成長！這是我參與「掘金日新計劃 · 6 月更文挑戰」的第25天，點選檢視活動詳情
You are given a 0-indexed 2D integer array brackets where brackets[i] = [upperi, percenti] means that the ith tax bracket has an upper bound of upperi and is taxed at a rate of percenti. The brackets are sorted by upper bound (i.e. upperi-1 < upperi for 0 < i < brackets.length).
Tax is calculated as follows:
- The first upper0 dollars earned are taxed at a rate of percent0.
- The next upper1 - upper0 dollars earned are taxed at a rate of percent1.
- The next upper2 - upper1 dollars earned are taxed at a rate of percent2.
- And so on.
You are given an integer income representing the amount of money you earned. Return the amount of money that you have to pay in taxes. Answers within 10^-5 of the actual answer will be accepted.
Input: brackets = [[3,50],[7,10],[12,25]], income = 10 Output: 2.65000 Explanation: The first 3 dollars you earn are taxed at 50%. You have to pay $3 * 50% = $1.50 dollars in taxes. The next 7 - 3 = 4 dollars you earn are taxed at 10%. You have to pay $4 * 10% = $0.40 dollars in taxes. The final 10 - 7 = 3 dollars you earn are taxed at 25%. You have to pay $3 * 25% = $0.75 dollars in taxes. You have to pay a total of $1.50 + $0.40 + $0.75 = $2.65 dollars in taxes.
Input: brackets = [[1,0],[4,25],[5,50]], income = 2 Output: 0.25000 Explanation: The first dollar you earn is taxed at 0%. You have to pay $1 * 0% = $0 dollars in taxes. The second dollar you earn is taxed at 25%. You have to pay $1 * 25% = $0.25 dollars in taxes. You have to pay a total of $0 + $0.25 = $0.25 dollars in taxes.
Input: brackets = [[2,50]], income = 0 Output: 0.00000 Explanation: You have no income to tax, so you have to pay a total of $0 dollars in taxes.
1 <= brackets.length <= 100 1 <= upperi <= 1000 0 <= percenti <= 100 0 <= income <= 1000 upperi is sorted in ascending order. All the values of upperi are unique. The upper bound of the last tax bracket is greater than or equal to income.
根據題意，給定一個 0 索引的 2D 整數陣列 brackets ，其中 brackets[i] = [upperi, percenti] 表示第 i 個稅級的上限為 upperi，並按百分比稅率徵稅。 brackets 按上限排序。
- 賺取的 upper0 美元按 percent0 的稅率徵稅。
- 賺取的下一個 upper1 - upper0 美元按 percent1 徵稅。
- 接下來的 upper2 - upper1 美元按 percent2 徵稅。
給定一個整數 income 代表您賺取的金額。返回您必須繳納的稅款。
時間複雜度為 O(N) ，空間複雜度為 O(1) 。
class Solution(object): def calculateTax(self, brackets, income): """ :type brackets: List[List[int]] :type income: int :rtype: float """ result = 0 pre = 0 for (b, p) in brackets: if b <= income: result += (b - pre) * p / float(100) else: result += (income - pre) * p / float(100) break pre = b return result
225 / 225 test cases passed. Status: Accepted Runtime: 48 ms Memory Usage: 13.4 MB
- linux 安裝 ES 8.0 和 kibana 8.0 爬米共坑
- ES 8.0 及其生態配置過程一條龍服務
- 2022 年中紀
- leetcode 1048. Longest String Chain（python）
- leetcode 1354. Construct Target Array With Multiple Sums（python）
- leetcode 2311. Longest Binary Subsequence Less Than or Equal to K（python）
- leetcode 2304. Minimum Path Cost in a Grid（python）
- leetcode 2303. Calculate Amount Paid in Taxes（python）
- 帶你輕鬆入門 Bert
- leetcode 1332. Remove Palindromic Subsequences（python）
- leetcode 3. Longest Substring Without Repeating Characters （python）
- leetcode 2296. Design a Text Editor （python）
- leetcode 2293. Min Max Game（python）
- leetcode 1584. Min Cost to Connect All Points（python）
- leetcode 318. Maximum Product of Word Lengths（python）
- 沙灘 美女才是夏天的正確開啟方式
- leetcode 2275. Largest Combination With Bitwise AND Greater Than Zero （python）
- leetcode 2258. Escape the Spreading Fire（python）
- leetcode 2257. Count Unguarded Cells in the Grid（python）
- leetcode 2273. Find Resultant Array After Removing Anagrams（python）