diff --git a/README.md b/README.md index 7db3592a..a89d4c65 100644 --- a/README.md +++ b/README.md @@ -408,3 +408,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 2287. Rearrange Characters to Make Target String | [Link](https://leetcode.com/problems/rearrange-characters-to-make-target-string/) | [Link](./lib/easy/2287_rearrange_characters_to_make_target_string.rb) | | 2293. Min Max Game | [Link](https://leetcode.com/problems/min-max-game/) | [Link](./lib/easy/2293_min_max_game.rb) | | 2299. Strong Password Checker II | [Link](https://leetcode.com/problems/strong-password-checker-ii/) | [Link](./lib/easy/2299_strong_password_checker_ii.rb) | +| 2303. Calculate Amount Paid in Taxes | [Link](https://leetcode.com/problems/calculate-amount-paid-in-taxes/) | [Link](./lib/easy/2303_calculate_amount_paid_in_taxes.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 75e7c512..0a6058d9 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -5,7 +5,7 @@ require 'English' ::Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.name = 'leetcode-ruby' - s.version = '5.4.7' + s.version = '5.4.8' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE] s.executable = 'leetcode-ruby' diff --git a/lib/easy/2303_calculate_amount_paid_in_taxes.rb b/lib/easy/2303_calculate_amount_paid_in_taxes.rb new file mode 100644 index 00000000..97a00cef --- /dev/null +++ b/lib/easy/2303_calculate_amount_paid_in_taxes.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/calculate-amount-paid-in-taxes/ +# @param {Integer[][]} brackets +# @param {Integer} income +# @return {Float} +def calculate_tax(brackets, income) + result = 0.0 + prev = 0 + brackets.each do |bracket| + upper = bracket.first + percent = bracket.last + + return result + ((income - prev) * percent / 100.0) if income < upper + + result += (upper - prev) * percent / 100.0 + prev = upper + end + + result +end diff --git a/test/easy/test_2303_calculate_amount_paid_in_taxes.rb b/test/easy/test_2303_calculate_amount_paid_in_taxes.rb new file mode 100644 index 00000000..cf39f937 --- /dev/null +++ b/test/easy/test_2303_calculate_amount_paid_in_taxes.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/easy/2303_calculate_amount_paid_in_taxes' +require 'minitest/autorun' + +class CalculateAmountPaidInTaxesTest < ::Minitest::Test + def test_default + assert_equal(2.65000, calculate_tax([[3, 50], [7, 10], [12, 25]], 10)) + assert_equal(0.25000, calculate_tax([[1, 0], [4, 25], [5, 50]], 2)) + assert_equal(0.00000, calculate_tax([[2, 50]], 0)) + end + + def test_additional + assert_equal(0.33000, calculate_tax([[1, 33]], 1)) + end +end