Skip to content

Commit 0fd8062

Browse files
committed
Day 69, 2 leet code problems
1 parent d400b2b commit 0fd8062

File tree

3 files changed

+113
-3
lines changed

3 files changed

+113
-3
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 96 |
10-
| Current Streak | 68 days |
11-
| Longest Streak | 68 ( August 17, 2015 - October 23, 2015 ) |
9+
| Total Problems | 98 |
10+
| Current Streak | 69 days |
11+
| Longest Streak | 69 ( August 17, 2015 - October 24, 2015 ) |
1212

1313
</center>
1414

@@ -173,3 +173,5 @@ Include contains single header implementation of data structures and some algori
173173
| Given an unsorted integer array, find the first missing positive integer.Example: [1,2,0] should return 3 and [3,4,-1,1] should return 2. Expected time complexity O(n) and solution should use constant space| [firstMissingPositiveNum.cpp](leet_code_problems/firstMissingPositiveNum.cpp)|
174174
|Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example: Given [100, 4, 200, 1, 3, 2]. The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Algorithm should run in O(n) complexity.| [longestConsecutiveSeq.cpp](leet_code_problems/longestConsecutiveSeq.cpp)|
175175
|Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.| [mergeArrays.cpp](leet_code_problems/mergeArrays.cpp)
176+
|Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example: <ul><li>A = [2,3,1,1,4], return true.</li></ul><ul><li>A = [3,2,1,0,4], return false.</li></ul>| [jumpGame.cpp](leet_code_problems/jumpGame.cpp)|
177+
|Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example 1 -> A, 2 -> B,...26 -> Z, 27 -> AA, 28 -> AB, ...705 -> AAC | [excelColSheetTitle.cpp](leet_code_problems/excelColSheetTitle.cpp)|
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Given a positive integer, return its corresponding column title as appear in an Excel sheet.
3+
*
4+
* For example:
5+
*
6+
* 1 -> A
7+
* 2 -> B
8+
* 3 -> C
9+
* ...
10+
* 26 -> Z
11+
* 27 -> AA
12+
* 28 -> AB
13+
*
14+
* Approach:
15+
* We need to convert the column value to effective a base 26 number, Imagine this is similar
16+
* to converting a decimal to hexadecimal value. However, we need to be cautious of that the
17+
* column numbers are not 0 indexed, they are indexed from 1. So every time our n % 26 is 0,
18+
* we reduce 1 from remaining number.
19+
*
20+
*/
21+
22+
#include <iostream>
23+
#include <string>
24+
#include <algorithm>
25+
26+
std::string convertToTitle(int n) {
27+
std::string result("");
28+
while ( n > 0) {
29+
int rem = n % 26;
30+
if ( rem == 0 ) {
31+
result += 'Z';
32+
n = (n/26) - 1;
33+
} else {
34+
result += char( rem-1 + 'A');
35+
n = n/26;
36+
}
37+
}
38+
std::reverse(result.begin(), result.end());
39+
return result;
40+
}
41+
42+
int main()
43+
{
44+
std::cout << "Enter the column index(1 based):";
45+
int col;
46+
std::cin >> col;
47+
std::cout << "Corresponding excel sheet title for column number "
48+
<< col << " is :" << convertToTitle(col) << std::endl;
49+
return 0;
50+
}

leet_code_problems/jumpGame.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Given an array of non-negative integers, you are initially positioned at the first index of the array.
3+
*
4+
* Each element in the array represents your maximum jump length at that position.
5+
*
6+
* Determine if you are able to reach the last index.
7+
*
8+
* For example:
9+
* A = [2,3,1,1,4], return true.
10+
*
11+
* A = [3,2,1,0,4], return false.
12+
*
13+
* Approach : Here we have to observe that array value represents *maximum* value, so we can always jump j index from index i as long as j < A[i].
14+
* i.e. if A[i] = 3, we can jump either 1, 2, or 3 steps.
15+
* Now, We will use dynamic programing to solve this problem. Our subproblem is at any index i, what is the farthest index I can jump. If in the end
16+
* if the max value index we have gathered so far is bigger than array length, we have reached the last index.
17+
*/
18+
19+
#include <iostream>
20+
#include <vector>
21+
22+
bool canReach( std::vector<int> & vec ) {
23+
int n = vec.size();
24+
if ( n == 0 ) {
25+
return false;
26+
}
27+
if ( n == 1 ) {
28+
return true;
29+
}
30+
int m = 0;
31+
for (int i = 0; i < n; ++i ) {
32+
if ( i <= m ) {
33+
m = std::max( m, vec[i] + i );
34+
if ( m >= n-1) {
35+
return true;
36+
}
37+
}
38+
}
39+
return false;
40+
}
41+
42+
void printRes( std::vector<int> & vec ) {
43+
std::cout << "VEC:";
44+
for ( auto v : vec ) {
45+
std::cout << v << " ";
46+
}
47+
std::cout << std::endl;
48+
std::cout << (canReach(vec) ? "True" : "False");
49+
std::cout << std::endl;
50+
}
51+
52+
int main() {
53+
std::vector<int> vec1{ 2,3,1,1,4 };
54+
std::vector<int> vec2{ 3,2,1,0,4 };
55+
printRes(vec1);
56+
printRes(vec2);
57+
return 0;
58+
}

0 commit comments

Comments
 (0)