Skip to content

Commit 8e85c7c

Browse files
committed
🚀 08-Aug-2020
1 parent 843c624 commit 8e85c7c

9 files changed

+222
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Given a column title as appear in an Excel sheet, return its corresponding column number.
2+
3+
For example:
4+
5+
A -> 1
6+
B -> 2
7+
C -> 3
8+
...
9+
Z -> 26
10+
AA -> 27
11+
AB -> 28
12+
...
13+
Example 1:
14+
15+
Input: "A"
16+
Output: 1
17+
Example 2:
18+
19+
Input: "AB"
20+
Output: 28
21+
Example 3:
22+
23+
Input: "ZY"
24+
Output: 701
25+
26+
27+
Constraints:
28+
29+
1 <= s.length <= 7
30+
s consists only of uppercase English letters.
31+
s is between "A" and "FXSHRXW".
32+
33+
34+
35+
36+
37+
38+
39+
40+
class Solution {
41+
public:
42+
int titleToNumber(string s) {
43+
int res=0, k=0;
44+
int n=s.length();
45+
for(int i=n-1;i>=0;i--){
46+
int ch=(s[i]-'A')+1;
47+
res+=ch*pow(26,k++);
48+
}
49+
return res;
50+
}
51+
};

Diff for: mathematical/catalan.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,47 @@
1+
/*
2+
Given a number N. The task is to find the nth Catalan number.
3+
The first few Catalan numbers for n = 0, 1, 2, 3, … are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, …
4+
5+
Input:
6+
The first line of input contains a single integer T which denotes the number of test cases. The first line of each test case contains a single integer N.
7+
8+
Output:
9+
For each test case, in a new line print the Catalan number at position N.
10+
Note: Positions start from 0 as shown above.
11+
12+
Expected Time Complexity: O(N).
13+
Expected Auxiliary Space: O(N).
14+
15+
Constraints:
16+
1 <= T <= 100
17+
1 <= N <= 100
18+
19+
Example:
20+
Input:
21+
3
22+
5
23+
4
24+
10
25+
26+
Output:
27+
42
28+
14
29+
16796
30+
Explanation:
31+
From the given first few Catalan's number we can easily the answer for first two test cases.
32+
*/
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
145
#include<bits/stdc++.h>
246
#include <boost/multiprecision/cpp_int.hpp>
347
using namespace boost::multiprecision;

Diff for: mathematical/check_if_prime.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,18 @@ int main(){
5050

5151
return 0;
5252
}
53+
54+
55+
56+
57+
/*
58+
Prime number is in form of 6p+1, 6p-1
59+
example
60+
6p divisible by 6
61+
6p+1
62+
6p+2 divisible by 2
63+
6p+3 divisible by 3
64+
6p+4 divisible by 4
65+
6p+5 = 6p+6-1 = 6(p+1)-1 = 6X-1
66+
6p+6 divisible by 6
67+
*/

Diff for: mathematical/extended_eucledian_algorithm.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
/*
2+
As seen above, x and y are results for inputs a and b,
3+
a.x + b.y = gcd ----(1)
4+
5+
And x1 and y1 are results for inputs b%a and a
6+
(b%a).x1 + a.y1 = gcd
7+
8+
When we put b%a = (b - (⌊b/a⌋).a) in above,
9+
we get following. Note that ⌊b/a⌋ is floor(b/a)
10+
11+
(b - (⌊b/a⌋).a).x1 + a.y1 = gcd
12+
13+
Above equation can also be written as below
14+
b.x1 + a.(y1 - (⌊b/a⌋).x1) = gcd ---(2)
15+
16+
After comparing coefficients of 'a' and 'b' in (1) and
17+
(2), we get following
18+
x = y1 - ⌊b/a⌋ * x1
19+
y = x1
20+
21+
*/
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
133
#include<bits/stdc++.h>
234
using namespace std;
335

Diff for: mathematical/gcd_a_b.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ int main(){
2020
}
2121
return 0;
2222
}
23+
24+
25+
26+
27+
28+
// TC : O(Log min(a, b))

Diff for: mathematical/x_power_n_negative_decimal.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
Implement pow(x, n), which calculates x raised to the power n (xn).
3+
4+
Example 1:
5+
6+
Input: 2.00000, 10
7+
Output: 1024.00000
8+
Example 2:
9+
10+
Input: 2.10000, 3
11+
Output: 9.26100
12+
Example 3:
13+
14+
Input: 2.00000, -2
15+
Output: 0.25000
16+
Explanation: 2-2 = 1/22 = 1/4 = 0.25
17+
*/
18+
19+
20+
21+
22+
23+
24+
125
#include<bits/stdc++.h>
226
using namespace std;
327

Diff for: mathematical/x_power_y.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/*
2+
Input : x = 2, n = 3
3+
Output : 8
4+
5+
Input : x = 7, n = 2
6+
Output : 49
7+
*/
8+
9+
10+
11+
12+
13+
114
#include<bits/stdc++.h>
215
using namespace std;
316

Diff for: matrix/rotate_matrix_90_anticlockwise.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,23 @@ int main(){
9090

9191
return 0;
9292
}
93+
94+
95+
96+
97+
/*
98+
Matrix rotation by 90 degrees can be done in
99+
100+
clockwise
101+
anticlockwise
102+
103+
To rotate a matrix clockwise by 90 degrees:
104+
105+
Find the transpose of the matrix.
106+
Reverse every rows of the matrix.
107+
108+
To rotate a matrix anticlockwise by 90 degrees,
109+
110+
Find the transpose of the matrix.
111+
Reverse every columns of the matrix.
112+
*/

Diff for: string/excel_sheet_column_number.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,20 @@ int main(){
6666

6767
return 0;
6868
}
69+
70+
71+
72+
73+
74+
75+
/* Reverse of above question
76+
int titleToNumber(string s) {
77+
int res=0, k=0;
78+
int n=s.length();
79+
for(int i=n-1;i>=0;i--){
80+
int ch=(s[i]-'A')+1;
81+
res+=ch*pow(26,k++);
82+
}
83+
return res;
84+
}
85+
*.

0 commit comments

Comments
 (0)