Skip to content

Commit 375ad01

Browse files
committed
1688. Count of Matches in Tournament and 2264. Largest 3-Same-Digit Number in String
1 parent a59b2d4 commit 375ad01

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@
391391
| 1679 | Max Number of K-Sum Pairs | [Ruby](./algorithms/ruby/1679-max-number-of-k-sum-pairs.rb) | Medium |
392392
| 1680 | Concatenation of Consecutive Binary Numbers | [Ruby](./algorithms/ruby/1680-concatenation-of-consecutive-binary-numbers.rb) | Medium |
393393
| 1685 | Sum of Absolute Differences in a Sorted Array | [Ruby](./algorithms/ruby/1685-sum-of-absolute-differences-in-a-sorted-array.rb) | Medium |
394+
| 1688 | Count of Matches in Tournament | [Ruby](./algorithms/ruby/1688-count-of-matches-in-tournament.rb) | Easy |
394395
| 1697 | Checking Existence of Edge Length Limited Paths | [Ruby](./algorithms/ruby/1697-checking-existence-of-edge-length-limited-paths.rb) | Hard |
395396
| 1706 | Where Will the Ball Fall | [Ruby](./algorithms/ruby/1706-where-will-the-ball-fall.rb) | Medium |
396397
| 1721 | Swapping Nodes in a Linked List | [Ruby](./algorithms/ruby/1721-swapping-nodes-in-a-linked-list.rb) | Medium |
@@ -433,6 +434,7 @@
433434
| 2215 | Find the Difference of Two Arrays | [Ruby](./algorithms/ruby/2215-find-the-difference-of-two-arrays.rb) | Easy |
434435
| 2218 | Maximum Value of K Coins From Piles | [Ruby](./algorithms/ruby/2218-maximum-value-of-k-coins-from-piles.rb) | Hard |
435436
| 2251 | Number of Flowers in Full Bloom | [Ruby](./algorithms/ruby/2251-number-of-flowers-in-full-bloom.rb) | Hard |
437+
| 2264 | Largest 3-Same-Digit Number in String | [Ruby](./algorithms/ruby/2264-largest-3-same-digit-number-in-string.rb) | Easy |
436438
| 2265 | Count Nodes Equal to Average of Subtree | [Ruby](./algorithms/ruby/2265-count-nodes-equal-to-average-of-subtree.rb) | Medium |
437439
| 2272 | Substring With Largest Variance | [Ruby](./algorithms/ruby/2272-substring-with-largest-variance.rb) | Hard |
438440
| 2300 | Successful Pairs of Spells and Potions | [Ruby](./algorithms/ruby/2300-successful-pairs-of-spells-and-potions.rb) | Medium |
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# frozen_string_literal: true
2+
3+
# 1688. Count of Matches in Tournament
4+
# Easy
5+
# https://leetcode.com/problems/count-of-matches-in-tournament
6+
7+
=begin
8+
You are given an integer n, the number of teams in a tournament that has strange rules:
9+
* If the current number of teams is even, each team gets paired with another team. A total of n / 2 matches are played, and n / 2 teams advance to the next round.
10+
* If the current number of teams is odd, one team randomly advances in the tournament, and the rest gets paired. A total of (n - 1) / 2 matches are played, and (n - 1) / 2 + 1 teams advance to the next round.
11+
Return the number of matches played in the tournament until a winner is decided.
12+
13+
Example 1:
14+
Input: n = 7
15+
Output: 6
16+
Explanation: Details of the tournament:
17+
- 1st Round: Teams = 7, Matches = 3, and 4 teams advance.
18+
- 2nd Round: Teams = 4, Matches = 2, and 2 teams advance.
19+
- 3rd Round: Teams = 2, Matches = 1, and 1 team is declared the winner.
20+
Total number of matches = 3 + 2 + 1 = 6.
21+
22+
Example 2:
23+
Input: n = 14
24+
Output: 13
25+
Explanation: Details of the tournament:
26+
- 1st Round: Teams = 14, Matches = 7, and 7 teams advance.
27+
- 2nd Round: Teams = 7, Matches = 3, and 4 teams advance.
28+
- 3rd Round: Teams = 4, Matches = 2, and 2 teams advance.
29+
- 4th Round: Teams = 2, Matches = 1, and 1 team is declared the winner.
30+
Total number of matches = 7 + 3 + 2 + 1 = 13.
31+
32+
Constraints:
33+
1 <= n <= 200
34+
=end
35+
36+
# @param {Integer} n
37+
# @return {Integer}
38+
def number_of_matches(n)
39+
matches = 0
40+
teams_advancing = n
41+
42+
while teams_advancing > 1 do
43+
matches += teams_advancing / 2
44+
teams_advancing = (teams_advancing / 2.0).ceil
45+
end
46+
47+
matches
48+
end
49+
50+
# **************** #
51+
# TEST #
52+
# **************** #
53+
54+
require "test/unit"
55+
class Test_number_of_matches < Test::Unit::TestCase
56+
def test_
57+
assert_equal 6, number_of_matches(7)
58+
assert_equal 13, number_of_matches(14)
59+
end
60+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# frozen_string_literal: true
2+
3+
# 2264. Largest 3-Same-Digit Number in String
4+
# Easy
5+
# https://leetcode.com/problems/largest-3-same-digit-number-in-string
6+
7+
=begin
8+
You are given a string num representing a large integer. An integer is good if it meets the following conditions:
9+
* It is a substring of num with length 3.
10+
* It consists of only one unique digit.
11+
Return the maximum good integer as a string or an empty string "" if no such integer exists.
12+
13+
Note:
14+
* A substring is a contiguous sequence of characters within a string.
15+
* There may be leading zeroes in num or a good integer.
16+
17+
Example 1:
18+
Input: num = "6777133339"
19+
Output: "777"
20+
Explanation: There are two distinct good integers: "777" and "333".
21+
"777" is the largest, so we return "777".
22+
23+
Example 2:
24+
Input: num = "2300019"
25+
Output: "000"
26+
Explanation: "000" is the only good integer.
27+
28+
Example 3:
29+
Input: num = "42352338"
30+
Output: ""
31+
Explanation: No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
32+
33+
Constraints:
34+
3 <= num.length <= 1000
35+
num only consists of digits.
36+
=end
37+
38+
# @param {String} num
39+
# @return {String}
40+
def largest_good_integer(num)
41+
res = []
42+
ans = ""
43+
44+
for i in(0).upto(num.size - 1)
45+
if res.empty?
46+
res << num[i]
47+
elsif res[-1] != num[i]
48+
res = [num[i]]
49+
else
50+
res << num[i]
51+
end
52+
53+
if res.size >= 3 && (ans[-1].to_i <= res[-1].to_i)
54+
ans = res[0..2].join
55+
end
56+
end
57+
58+
ans
59+
end
60+
61+
# **************** #
62+
# TEST #
63+
# **************** #
64+
65+
require "test/unit"
66+
class Test_largest_good_integer < Test::Unit::TestCase
67+
def test_
68+
assert_equal "777", largest_good_integer("6777133339")
69+
assert_equal "000", largest_good_integer("2300019")
70+
assert_equal "", largest_good_integer("42352338")
71+
end
72+
end

0 commit comments

Comments
 (0)