Skip to content

Commit aa2beea

Browse files
committed
Add solutions
1716. Calculate Money in Leetcode Bank 1903. Largest Odd Number in String
1 parent 375ad01 commit aa2beea

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@
394394
| 1688 | Count of Matches in Tournament | [Ruby](./algorithms/ruby/1688-count-of-matches-in-tournament.rb) | Easy |
395395
| 1697 | Checking Existence of Edge Length Limited Paths | [Ruby](./algorithms/ruby/1697-checking-existence-of-edge-length-limited-paths.rb) | Hard |
396396
| 1706 | Where Will the Ball Fall | [Ruby](./algorithms/ruby/1706-where-will-the-ball-fall.rb) | Medium |
397+
| 1716 | Calculate Money in Leetcode Bank | [Ruby](./algorithms/ruby/1716-calculate-money-in-leetcode-bank.rb) | Easy |
397398
| 1721 | Swapping Nodes in a Linked List | [Ruby](./algorithms/ruby/1721-swapping-nodes-in-a-linked-list.rb) | Medium |
398399
| 1727 | Largest Submatrix With Rearrangements | [Ruby](./algorithms/ruby/1727-largest-submatrix-with-rearrangements.rb) | Medium |
399400
| 1732 | Find the Highest Altitude | [Ruby](./algorithms/ruby/1732-find-the-highest-altitude.rb) | Easy |
@@ -413,6 +414,7 @@
413414
| 1870 | Minimum Speed to Arrive on Time | [Ruby](./algorithms/ruby/1870-minimum-speed-to-arrive-on-time.rb) | Medium |
414415
| 1877 | Minimize Maximum Pair Sum in Array | [Ruby](./algorithms/ruby/1877-minimize-maximum-pair-sum-in-array.rb) | Medium |
415416
| 1887 | Minimize Maximum Pair Sum in Array | [Ruby](./algorithms/ruby/1887-reduction-operations-to-make-the-array-elements-equal.rb) | Medium |
417+
| 1903 | Largest Odd Number in String | [Ruby](./algorithms/ruby/1903-largest-odd-number-in-string.rb) | Easy |
416418
| 1921 | Eliminate Maximum Number of Monsters | [Ruby](./algorithms/ruby/1921-eliminate-maximum-number-of-monsters.rb) | Medium |
417419
| 1926 | Nearest Exit from Entrance in Maze | [Ruby](./algorithms/ruby/1926-nearest-exit-from-entrance-in-maze.rb) | Medium |
418420
| 1930 | Unique Length-3 Palindromic Subsequences | [Ruby](./algorithms/ruby/1930-unique-length-3-palindromic-subsequences.rb) | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
# 1716. Calculate Money in Leetcode Bank
4+
# Easy
5+
# https://leetcode.com/problems/calculate-money-in-leetcode-bank
6+
7+
=begin
8+
Hercy wants to save money for his first car. He puts money in the Leetcode bank every day.
9+
He starts by putting in $1 on Monday, the first day. Every day from Tuesday to Sunday, he will put in $1 more than the day before. On every subsequent Monday, he will put in $1 more than the previous Monday.
10+
Given n, return the total amount of money he will have in the Leetcode bank at the end of the nth day.
11+
12+
Example 1:
13+
Input: n = 4
14+
Output: 10
15+
Explanation: After the 4th day, the total is 1 + 2 + 3 + 4 = 10.
16+
17+
Example 2:
18+
Input: n = 10
19+
Output: 37
20+
Explanation: After the 10th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4) = 37. Notice that on the 2nd Monday, Hercy only puts in $2.
21+
22+
Example 3:
23+
Input: n = 20
24+
Output: 96
25+
Explanation: After the 20th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4 + 5 + 6 + 7 + 8) + (3 + 4 + 5 + 6 + 7 + 8) = 96.
26+
27+
Constraints:
28+
1 <= n <= 1000
29+
=end
30+
31+
# @param {Integer} n
32+
# @return {Integer}
33+
def total_money(n)
34+
k, x = n / 7, n % 7
35+
(7 * k * (k + 7) / 2) + (x * (2 * k + x + 1) / 2)
36+
end
37+
38+
# **************** #
39+
# TEST #
40+
# **************** #
41+
42+
require "test/unit"
43+
class Test_total_money < Test::Unit::TestCase
44+
def test_
45+
assert_equal 10, total_money(4)
46+
assert_equal 37, total_money(10)
47+
assert_equal 96, total_money(20)
48+
end
49+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
3+
# 1903. Largest Odd Number in String
4+
# Easy
5+
# https://leetcode.com/problems/largest-odd-number-in-string
6+
7+
=begin
8+
You are given a string num, representing a large integer. Return the largest-valued odd integer (as a string) that is a non-empty substring of num, or an empty string "" if no odd integer exists.
9+
A substring is a contiguous sequence of characters within a string.
10+
11+
Example 1:
12+
Input: num = "52"
13+
Output: "5"
14+
Explanation: The only non-empty substrings are "5", "2", and "52". "5" is the only odd number.
15+
16+
Example 2:
17+
Input: num = "4206"
18+
Output: ""
19+
Explanation: There are no odd numbers in "4206".
20+
21+
Example 3:
22+
Input: num = "35427"
23+
Output: "35427"
24+
Explanation: "35427" is already an odd number.
25+
26+
Constraints:
27+
1 <= num.length <= 105
28+
num only consists of digits and does not contain any leading zeros.
29+
=end
30+
31+
# @param {String} num
32+
# @return {String}
33+
def largest_odd_number(num)
34+
i = num.length - 1
35+
while i >= 0
36+
case num[i]
37+
when "1", "3", "5", "7", "9" then return num[0..i]
38+
end
39+
i -= 1
40+
end
41+
42+
""
43+
end
44+
45+
# **************** #
46+
# TEST #
47+
# **************** #
48+
49+
require "test/unit"
50+
class Test_largest_odd_number < Test::Unit::TestCase
51+
def test_
52+
assert_equal "5", largest_odd_number("52")
53+
assert_equal "", largest_odd_number("4206")
54+
assert_equal "35427", largest_odd_number("35427")
55+
end
56+
end

0 commit comments

Comments
 (0)