Skip to content

Commit 6503142

Browse files
committed
leetcode
1 parent 18542f0 commit 6503142

File tree

4 files changed

+330
-0
lines changed

4 files changed

+330
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
3+
-* Excel Sheet Column Title *-
4+
5+
Given an integer columnNumber, return its corresponding
6+
column title as it appears in an
7+
Excel sheet.
8+
9+
For example:
10+
11+
A -> 1
12+
B -> 2
13+
C -> 3
14+
...
15+
Z -> 26
16+
AA -> 27
17+
AB -> 28
18+
...
19+
20+
21+
Example 1:
22+
23+
Input: columnNumber = 1
24+
Output: "A"
25+
Example 2:
26+
27+
Input: columnNumber = 28
28+
Output: "AB"
29+
Example 3:
30+
31+
Input: columnNumber = 701
32+
Output: "ZY"
33+
34+
35+
Constraints:
36+
37+
1 <= columnNumber <= 231 - 1
38+
39+
*/
40+
41+
class A {
42+
/**
43+
* Time Complexity: O(log26n), as we are using a loop and in each traversal, we decrement by floor division of 26.
44+
45+
Auxiliary Space: O(10000), as we are using extra space for the array.
46+
*/
47+
// Runtime: 512 ms, faster than 50.00% of Dart online submissions for Excel Sheet Column Title.
48+
// Memory Usage: 141.5 MB, less than 50.00% of Dart online submissions for Excel Sheet Column Title.
49+
String convertToTitle(int columnNumber) {
50+
List<int> arr = List.filled(10000, 0);
51+
int i = 0;
52+
53+
// Step 1: Converting to number assuming
54+
// 0 in number system
55+
while (columnNumber > 0) {
56+
arr[i] = columnNumber % 26;
57+
columnNumber = columnNumber ~/ 26;
58+
i++;
59+
}
60+
61+
// Step 2: Getting rid of 0, as 0 is
62+
// not part of number system
63+
for (int j = 0; j < i - 1; j++) {
64+
if (arr[j] <= 0) {
65+
arr[j] += 26;
66+
arr[j + 1] = arr[j + 1] - 1;
67+
}
68+
}
69+
String ans = '';
70+
for (int j = i; j >= 0; j--) {
71+
if (arr[j] > 0) ans += String.fromCharCode(65 + arr[j] - 1);
72+
}
73+
74+
return ans;
75+
}
76+
}
77+
78+
class B {
79+
// Runtime: 391 ms, faster than 50.00% of Dart online submissions for Excel Sheet Column Title.
80+
// Memory Usage: 140.1 MB, less than 50.00% of Dart online submissions for Excel Sheet Column Title.
81+
82+
String convertToTitle(int columnNumber) {
83+
String alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
84+
if (columnNumber < 26)
85+
return alpha[columnNumber - 1];
86+
else {
87+
int q = (columnNumber ~/ 26), r = columnNumber % 26;
88+
if (r == 0) {
89+
if (q == 1)
90+
return alpha[(26 + r - 1)];
91+
else
92+
return convertToTitle(q - 1) + alpha[(26 + r - 1)];
93+
} else
94+
return convertToTitle(q) + alpha[r - 1];
95+
}
96+
}
97+
}
98+
99+
class C {
100+
// Runtime: 490 ms, faster than 50.00% of Dart online submissions for Excel Sheet Column Title.
101+
// Memory Usage: 150.5 MB, less than 50.00% of Dart online submissions for Excel Sheet Column Title.
102+
String convertToTitle(int columnNumber) {
103+
if (columnNumber-- == 0) return "";
104+
return convertToTitle(columnNumber ~/ 26) +
105+
String.fromCharCode((columnNumber % 26) + 'A'.codeUnitAt(0));
106+
}
107+
}
108+
109+
class D {
110+
// Runtime: 305 ms, faster than 50.00% of Dart online submissions for Excel Sheet Column Title.
111+
// Memory Usage: 157.9 MB, less than 50.00% of Dart online submissions for Excel Sheet Column Title.
112+
String convertToTitle(int columnNumber) {
113+
StringBuffer result = StringBuffer();
114+
115+
while (columnNumber > 0) {
116+
columnNumber--;
117+
//result.write(0, ('A' + n % 26));
118+
result.writeCharCode(((columnNumber % 26) + 'A'.codeUnitAt(0)));
119+
columnNumber ~/= 26;
120+
}
121+
122+
return result.toString().split("").reversed.join("");
123+
}
124+
}
125+
126+
class E {
127+
// Runtime: 324 ms, faster than 50.00% of Dart online submissions for Excel Sheet Column Title.
128+
// Memory Usage: 140 MB, less than 50.00% of Dart online submissions for Excel Sheet Column Title.
129+
130+
String convertToTitle(int columnNumber) {
131+
return columnNumber == 0
132+
? ""
133+
: convertToTitle(--columnNumber ~/ 26) +
134+
String.fromCharCode((columnNumber % 26) + 'A'.codeUnitAt(0));
135+
}
136+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// func convertToTitle(n int) string {
6+
// result := ""
7+
// for n != 0 {
8+
// fmt.Println(n)
9+
// result = string('A'+(n-1)%26) + result
10+
// n = (n - 1) / 26
11+
// }
12+
// return result
13+
// }
14+
15+
func convertToTitle(n int) string {
16+
result := ""
17+
for n != 0 {
18+
fmt.Println(n)
19+
result = string(rune('A'+(n-1)%26)) + result
20+
n = (n - 1) / 26
21+
}
22+
return result
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# 🔥 Find K Closest Elements 🔥 || 5 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
The problem is similar to converting a decimal number to its binary representation but instead of a binary base system where we have two digits only 0 and 1, here we have 26 characters from A-Z.
6+
So, we are dealing with base 26 instead of base binary.
7+
That’s not where the fun ends, we don’t have zero in this number system, as A represents 1, B represents 2 and so on Z represents 26.
8+
To make the problem easily understandable, we approach the problem in two steps:
9+
10+
Convert the number to base 26 representation, considering we have 0 also in the system.
11+
Change the representation to the one without having 0 in its system.
12+
HOW? Here is an example
13+
14+
Step 1:
15+
Consider we have number 676, How to get its representation in the base 26 system? In the same way, we do for a binary system, Instead of division and remainder by 2, we do division and remainder by 26.
16+
17+
Base 26 representation of 676 is : 100
18+
Step2
19+
But Hey, we can’t have zero in our representation. Right? Because it’s not part of our number system. How do we get rid of zero? Well it’s simple, but before doing that let’s remind one simple math trick:
20+
21+
Subtraction:
22+
5000 - 9, How do you subtract 9 from 0 ? You borrow
23+
from next significant bit, right.
24+
In a decimal number system to deal with zero, we borrow 10 and subtract 1 from the next significant.
25+
In the Base 26 Number System to deal with zero, we borrow 26 and subtract 1 from the next significant bit.
26+
So Convert 10026 to a number system that does not have ‘0’, we get (25 26)26
27+
Symbolic representation of the same is: YZ
28+
29+
* Time Complexity: O(log26n), as we are using a loop and in each traversal, we decrement by floor division of 26.
30+
* Auxiliary Space: O(10000), as we are using extra space for the array.
31+
32+
```dart
33+
class Solution {
34+
// Runtime: 512 ms, faster than 50.00% of Dart online submissions for Excel Sheet Column Title.
35+
// Memory Usage: 141.5 MB, less than 50.00% of Dart online submissions for Excel Sheet Column Title.
36+
37+
String convertToTitle(int columnNumber) {
38+
List<int> arr = List.filled(10000, 0);
39+
int i = 0;
40+
41+
// Step 1: Converting to number assuming
42+
// 0 in number system
43+
while (columnNumber > 0) {
44+
arr[i] = columnNumber % 26;
45+
columnNumber = columnNumber ~/ 26;
46+
i++;
47+
}
48+
49+
// Step 2: Getting rid of 0, as 0 is
50+
// not part of number system
51+
for (int j = 0; j < i - 1; j++) {
52+
if (arr[j] <= 0) {
53+
arr[j] += 26;
54+
arr[j + 1] = arr[j + 1] - 1;
55+
}
56+
}
57+
String ans = '';
58+
for (int j = i; j >= 0; j--) {
59+
if (arr[j] > 0) ans += String.fromCharCode(65 + arr[j] - 1);
60+
}
61+
62+
return ans;
63+
}
64+
}
65+
```
66+
67+
## Solution - 2
68+
69+
We can use a recursive function which definitely reduces the time and increase the efficiency:
70+
71+
Alphabets are in sequential order like: ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’. You have experienced while using excel when you see columns and rows numbering are done in Alphabetical ways.
72+
73+
Here’s How I purposefully think about the logic of how it is arranged.
74+
75+
(In Mathematical terms, [a , b ] means from ‘a’ to ‘b’).
76+
77+
[1,26] = [A,Z] (Understand by ‘1’ stands for ‘A’ and ’26” stands for “Z”). For [27,52] ,it will be like [AA,AZ], For [57,78] it will be [BA,BZ]
78+
79+
Logic is to append an Alphabet sequentially whenever it ends up numbering at 26.
80+
81+
For example, if the number is ’27’ which is greater than ’26’, then we simply need to divide by 26, and we get the remainder as 1, We see “1” as “A” and can be recursively done.
82+
83+
we will be using python for this.
84+
85+
Algorithm is:
86+
87+
1. Take an array and Sort the letters from A to Z . (You can also use the import string and string function to get “A to Z” in uppercase.)
88+
89+
2. If the number is less than or equal to ’26’, simply get the letter from the array and print it.
90+
91+
3. If it is greater than 26, use the Quotient Remainder rule, if the remainder is zero, there are 2 possible ways, if the quotient is “1”, simply hash out the letter from the index [r-1]( ‘r’ is remainder), else call out the function from the num =(q-1) and append at the front to the letter indexing [r-1].
92+
93+
4. If the remainder is not equal to “0”, call the function for the num = (q) and append at the front to the letter indexing [r-1].
94+
Time Complexity: O(log26n), as we are using recursion and in each recursive call, we decrement by floor division of 26.
95+
96+
Auxiliary Space: O(1), as we are not using any extra space.
97+
98+
```dart
99+
class Solution {
100+
// Runtime: 391 ms, faster than 50.00% of Dart online submissions for Excel Sheet Column Title.
101+
// Memory Usage: 140.1 MB, less than 50.00% of Dart online submissions for Excel Sheet Column Title.
102+
103+
String convertToTitle(int columnNumber) {
104+
String alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
105+
if (columnNumber < 26)
106+
return alpha[columnNumber - 1];
107+
else {
108+
int q = (columnNumber ~/ 26), r = columnNumber % 26;
109+
if (r == 0) {
110+
if (q == 1)
111+
return alpha[(26 + r - 1)];
112+
else
113+
return convertToTitle(q - 1) + alpha[(26 + r - 1)];
114+
} else
115+
return convertToTitle(q) + alpha[r - 1];
116+
}
117+
}
118+
}
119+
```
120+
121+
## Solution - 3
122+
123+
```dart
124+
class Solution {
125+
// Runtime: 490 ms, faster than 50.00% of Dart online submissions for Excel Sheet Column Title.
126+
// Memory Usage: 150.5 MB, less than 50.00% of Dart online submissions for Excel Sheet Column Title.
127+
128+
String convertToTitle(int columnNumber) {
129+
if (columnNumber-- == 0) return "";
130+
return convertToTitle(columnNumber ~/ 26) +
131+
String.fromCharCode((columnNumber % 26) + 'A'.codeUnitAt(0));
132+
}
133+
}
134+
```
135+
136+
## Solution - 4
137+
138+
```dart
139+
class Solution {
140+
// Runtime: 305 ms, faster than 50.00% of Dart online submissions for Excel Sheet Column Title.
141+
// Memory Usage: 157.9 MB, less than 50.00% of Dart online submissions for Excel Sheet Column Title.
142+
143+
String convertToTitle(int columnNumber) {
144+
StringBuffer result = StringBuffer();
145+
146+
while (columnNumber > 0) {
147+
columnNumber--;
148+
result.writeCharCode(((columnNumber % 26) + 'A'.codeUnitAt(0)));
149+
columnNumber ~/= 26;
150+
}
151+
return result.toString().split("").reversed.join("");
152+
}
153+
}
154+
```
155+
156+
## Solution - 5
157+
158+
```dart
159+
class Solution {
160+
// Runtime: 324 ms, faster than 50.00% of Dart online submissions for Excel Sheet Column Title.
161+
// Memory Usage: 140 MB, less than 50.00% of Dart online submissions for Excel Sheet Column Title.
162+
163+
String convertToTitle(int columnNumber) {
164+
return columnNumber == 0
165+
? ""
166+
: convertToTitle(--columnNumber ~/ 26) +
167+
String.fromCharCode((columnNumber % 26) + 'A'.codeUnitAt(0));
168+
}
169+
}
170+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
5959
- [Intersection of Two Linked Lists](IntersectionOfTwoLinkedLists/intersection_of_two_linked_lists.dart)
6060
- [Missing Ranges](MissingRanges/missing_ranges.dart)
6161
- [Find K Closest Elements](FindKClosestElements/find_k_closest_elements.dart)
62+
- [Excel Sheet Column Title](ExcelSheetColumnTitle/excel_sheet_column_title.dart)
6263

6364
## Reach me via
6465

0 commit comments

Comments
 (0)