0% found this document useful (0 votes)
9 views5 pages

Tournament Match Count Calculation

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Tournament Match Count Calculation

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

===================================================================================

========================
You are given an integer n, the number of teams in a tournament that has following
rules:

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.
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.
Return the number of matches played in the tournament until a winner is decided.

Example 1:

Input: n = 7
Output: 6
Explanation: Details of the tournament:
- 1st Round: Teams = 7, Matches = 3, and 4 teams advance.
- 2nd Round: Teams = 4, Matches = 2, and 2 teams advance.
- 3rd Round: Teams = 2, Matches = 1, and 1 team is declared the winner.
Total number of matches = 3 + 2 + 1 = 6.

Constraints:

1 <= n <= 200

Answer ==> n-1


===================================================================================
==========================

===================================================================================
==========================
Hercy wants to save money for his first car. He puts money in the Leetcode bank
every day.

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.
Given n, return the total amount of money he will have in the Leetcode bank at the
end of the nth day.

Example 1:
Input: n = 4
Output: 10
Explanation: After the 4th day, the total is 1 + 2 + 3 + 4 = 10.

Example 2:
Input: n = 20
Output: 96
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.

1 <= n <= 1000

Answer :
int totalMoney(int n) {
int w = n / 7;
int money = w * 28;
money += (7 * w * (w - 1)) / 2;

if (n % 7) {
int extra_days = n % 7;
int money_to_add = w + 1;
for (int i = 0; i < extra_days; ++i) {
money += money_to_add;
money_to_add += 1;
}
}
return money;
}
===================================================================================
========================

===================================================================================
========================
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.

A substring is a contiguous sequence of characters within a string.

Example 1:

Input: num = "52"


Output: "5"
Explanation: The only non-empty substrings are "5", "2", and "52". "5" is the only
odd number.
Example 2:

Input: num = "4206"


Output: ""
Explanation: There are no odd numbers in "4206".

Solution :
string largestOddNumber(string num)
{
for(int i=[Link]()-1;i>=0;--i)
{
if((num[i]-'0')%2)
{
return [Link](0,i+1);
}
}

return "";
}

===================================================================================
========================

===================================================================================
========================
Given two string arrays word1 and word2, return true if the two arrays represent
the same string, and false otherwise.
A string is represented by an array if the array elements concatenated in order
forms the string.

Example 1:

Input: word1 = ["ab", "c"], word2 = ["a", "bc"]


Output: true
Explanation:
word1 represents string "ab" + "c" -> "abc"
word2 represents string "a" + "bc" -> "abc"
The strings are the same, so return true.
Example 2:

Input: word1 = ["a", "cb"], word2 = ["ab", "c"]


Output: false

Solution:
bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2)
{
int word1CharPtr=0;
int word2CharPtr=0;
int word1StringPtr=0;
int word2StringPtr=0;

while(word1StringPtr<[Link]() && word2StringPtr<[Link]())


{
if(word1[word1StringPtr][word1CharPtr++]!=word2[word2StringPtr][word2CharPtr+
+])
{
return false;
}
if(word1CharPtr==word1[word1StringPtr].size())
{
word1CharPtr=0;
word1StringPtr++;
}
if(word2CharPtr==word2[word2StringPtr].size())
{
word2CharPtr=0;
word2StringPtr++;
}
}
return word1StringPtr==[Link]() && word2StringPtr==[Link]();
}

===================================================================================
========================

===================================================================================
========================
Given an integer array sorted in non-decreasing order, there is exactly one integer
in the array that occurs more than 25% of the time, return that integer.

Example 1:
Input: arr = [1,2,2,6,6,6,6,7,10]
Output: 6
Example 2:

Input: arr = [1,1]


Output: 1

Constraints:

1 <= [Link] <= 104


0 <= arr[i] <= 105

Solution :

int findSpecialInteger(vector<int>& arr)


{
int size = [Link]() / 4;
for (int i = 0; i < [Link]() - size; i++)
{
if (arr[i] == arr[i + size])
{
return arr[i];
}
}

return -1;
}

Explanation :
The answer block has a length greater than n / 4, and thus it must overlap at least

one of the following positions in the array:

A quarter of the way through at index n / 4.


Halfway through at index n / 2.
Three-quarters of the way through at index 3n / 4

int findSpecialInteger(vector<int>& arr)


{
int n = [Link]();
vector<int> candidates = {arr[n / 4], arr[n / 2], arr[3 * n / 4]};
int target = n / 4;

for (int candidate : candidates) {


int left = lower_bound([Link](), [Link](), candidate) -
[Link]();
int right = upper_bound([Link](), [Link](), candidate) -
[Link]() - 1;
if (right - left + 1 > target) {
return candidate;
}
}

return -1;
}
===================================================================================
========================

You might also like