Skip to content

Commit ee30d47

Browse files
committed
Correct some problems
1 parent 3c83329 commit ee30d47

File tree

5 files changed

+255
-26
lines changed

5 files changed

+255
-26
lines changed

Arrays/Add One To Number.cpp

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,52 @@ Q : Can the output have 0’s before the most significant digit? Or in other wor
3030
A : For the purpose of this question, NO. Even if the input has zeroes before the most significant digit.
3131
3232
*/
33+
vector<int> newSolution(vector<int> &A);
34+
vector<int> initialSolution(vector<int> &A);
3335

3436
vector<int> Solution::plusOne(vector<int> &A) {
37+
38+
// return initialSolution(A);
39+
return newSolution(A);
40+
}
41+
42+
// Much cleaner code but requires additional O(n) space
43+
vector<int> newSolution(vector<int> &A){
44+
// Reverse the digits
45+
reverse(A.begin(),A.end());
46+
vector<int> ans;
47+
// Initial carry = 1 i.e. 1 is to be added
48+
int carry = 1;
49+
int n = A.size();
50+
int sum;
51+
52+
// add carry
53+
for(int i=0;i<n;i++){
54+
sum = A[i] + carry;
55+
ans.push_back(sum%10);
56+
carry = sum/10;
57+
}
58+
59+
// remaining carry
60+
if(carry) ans.push_back(carry);
61+
62+
// remove additional zeroes
63+
while(ans[ans.size()-1]==0 && ans.size()>1) ans.pop_back();
64+
65+
// reverse again to get answer
66+
reverse(ans.begin(),ans.end());
67+
return ans;
68+
}
69+
vector<int> initialSolution(vector<int> &A){
3570
int n = A.size();
36-
int carry=0;
71+
int carry=1;
3772
for(int i=n-1;i>=0;i--){
38-
if(i==n-1){
39-
if(A[i]==9){
40-
A[i]=0;
41-
carry=1;
42-
}
43-
else A[i]++;
44-
}
45-
else if(carry ==0)break;
46-
else{
47-
A[i]+=carry;
48-
carry=0;
49-
if(A[i]==10){
50-
A[i]=0;
51-
carry=1;
52-
}
53-
}
73+
if(carry==0)break;
74+
75+
A[i]+=carry%10;
76+
carry /=10;
5477
}
78+
// O(n) additional space only needed if another digit is to be added at start
5579
if(carry==1){
5680
int temp=1,newTemp;
5781
for(int i=0;i<n;i++){
@@ -61,17 +85,16 @@ vector<int> Solution::plusOne(vector<int> &A) {
6185
}
6286
A.push_back(temp);
6387
}
88+
89+
// Remove extra zeroes at start
6490
int i =0;
6591
while(A[i]==0)i++;
66-
6792
if(i>0){
6893
vector<int>::iterator it1,it2;
6994
it1=A.begin();
7095
it2 = it1 + i;
7196
A.erase(it1,it2);
72-
7397
}
7498

7599
return A;
76-
77-
}
100+
}

Binary Search/Matrix Median.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
InterviewBit : Matrix Median
3+
4+
Link : https://www.interviewbit.com/problems/matrix-median/
5+
6+
Given a N cross M matrix in which each row is sorted, find the overall median of the matrix. Assume N*M is odd.
7+
8+
For example,
9+
10+
Matrix=
11+
[1, 3, 5]
12+
[2, 6, 9]
13+
[3, 6, 9]
14+
15+
A = [1, 2, 3, 3, 5, 6, 6, 9, 9]
16+
17+
Median is 5. So, we return 5.
18+
Note: No extra memory is allowed.
19+
20+
*/
21+
int Solution::findMedian(vector<vector<int>> &A){
22+
23+
int max=INT_MIN,min=INT_MAX;// max element will be right extreme while min will be on leftmost col
24+
int row = A.size();
25+
int col = A[0].size();
26+
// Get maximum and minimum values
27+
for(int i=0;i<row;i++){
28+
if(max < A[i][col-1])
29+
max = A[i][col-1];
30+
if(min > A[i][0])
31+
min = A[i][0];
32+
}
33+
34+
// now we have max and min (why? because median will be somewhat around it)
35+
// median will be at position where exactly N/2 element will be less than it
36+
// so
37+
// we can find how much element less than than specified element with upper bound
38+
int desired = (row*col+1)/2;
39+
int mid,less_than;
40+
while(min < max){
41+
mid = min + (max-min)/2;
42+
less_than = 0;
43+
for(int i=0;i<row;i++){
44+
less_than += (upper_bound(A[i].begin(),A[i].end(),mid)-A[i].begin());
45+
}
46+
if(less_than < desired){
47+
min = mid+1;
48+
}
49+
else{
50+
max = mid;
51+
}
52+
}
53+
return min; // (why min? as we want just N/2 element for which it is greater than)
54+
55+
}

Linked Lists/Insertion Sort List.cpp

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,66 @@ Return 1 -> 2 -> 3
3030
* ListNode(int x) : val(x), next(NULL) {}
3131
* };
3232
*/
33+
ListNode* initialSolution(ListNode* A);
34+
ListNode* newSolution(ListNode* A);
35+
3336
ListNode* Solution::insertionSortList(ListNode* A) {
3437

3538
if(!A ||!A->next)return A;
3639

40+
// return initialSolution(A);
41+
return newSolution(A);
42+
}
43+
// Lesser memory required
44+
ListNode* newSolution(ListNode* A){
45+
ListNode* sorted = NULL;
46+
ListNode* list = A;
47+
ListNode* curr;
48+
49+
// Traverse over the list
50+
while(list){
51+
//get first node; this node will be inserted in sorted list
52+
curr = list;
53+
54+
// Detach this node from list
55+
list=list->next;
56+
curr->next=NULL;
57+
58+
// Sorted list has not been created || this value is smaller than first node in sorted list
59+
if(!sorted || sorted->val > curr->val){
60+
curr->next = sorted;
61+
sorted = curr;
62+
}
63+
// Node will be inserted inside list
64+
else{
65+
66+
// traversal pointer
67+
ListNode* s = sorted;
68+
// Traverse till correct insertion position is found
69+
while(s->next && s->next->val < curr->val) s=s->next;
70+
71+
// Insert the node in the list
72+
ListNode* temp = s->next;
73+
s->next = curr;
74+
curr->next =temp;
75+
}
76+
}
77+
return sorted;
78+
}
79+
ListNode* initialSolution(ListNode* A){
80+
81+
3782
ListNode* ans = new ListNode(0);
38-
// ListNode* trav = A;
3983
ListNode* trav = ans;
84+
4085
while(A){
4186
int val = A->val;
4287
trav = ans;
88+
// Find correct insertion place in sorted list
4389
while(trav->next && val>trav->next->val){
4490
trav=trav->next;
4591
}
92+
// Copy the node in sorted list
4693
ListNode *temp = trav->next;
4794
trav->next = new ListNode(val);
4895
trav = trav->next;

Strings/Longest Common Prefix.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,29 @@ The answer would be “a”.
2828
2929
*/
3030
string Solution::longestCommonPrefix(vector<string> &A) {
31+
32+
// Stores longest common prefix
3133
string ans;
34+
3235
int n = A.size();
36+
char c;
37+
38+
// Entire string is answer
3339
if(n==1)return A[0];
40+
3441
for(int i=0;i<A[0].length();i++){
35-
if(i>=A[0].length())return ans;
36-
char c = A[0][i];
37-
for(int j=1;j<n;j++){
42+
43+
// Traverse over the strings
44+
for(int j=0;j<n;j++){
45+
46+
// Length of string is shorter than required index
3847
if(i>=A[j].length())return ans;
39-
if(A[j][i]!=c)return ans;
48+
49+
// Get character at index i in first string
50+
if(j==0)c=A[0][i];
51+
52+
// Different character found
53+
if(j!=0 && A[j][i]!=c)return ans;
4054
}
4155
ans.append(1,c);
4256
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
3+
InterviewBit : Minimum Characters required to make a String Palindromic
4+
5+
Link : https://www.interviewbit.com/problems/minimum-characters-required-to-make-a-string-palindromic/
6+
7+
You are given a string. The only operation allowed is to insert characters in the beginning of the string. How many minimum characters are needed to be inserted to make the string a palindrome string
8+
9+
Example:
10+
Input: ABC
11+
Output: 2
12+
Input: AACECAAAA
13+
Output: 2
14+
15+
16+
*/
17+
18+
/** Problem is solved using KMP pattern matching
19+
20+
Answer to the problem is |S| - |LPP|.
21+
Where |S| is the length of the input string and |LPP| is the length of longest palindromic prefix of the input string.
22+
23+
Why? Let’s assume we have string of the form S = PR, where P is longest possible palindrome and R is the rest of the string.
24+
To transform S into palindrome we should insert R characters at the beginning of the string. So finally string will have form RPR.
25+
For example, let’s consider AACECAAAA string. Longest palindromic prefix is AACECAA. The rest of the characters are AA.
26+
So to make full string palindromic we should add AA at the beginning of the string, ie final string = AA + AACECAA + AA.
27+
28+
But how to find length of longest palindromic prefix?
29+
Let’s calculate LSP for string of the form S + ‘#’ + reversed S, for example: AACECAAAA#AAAACECAA.
30+
Last element of LSP is the length of longest prefix-suffix of the entire string (AACECAA in our case -> AACECAA|AA#AA|AACECAA).
31+
But it is also length of longest palindromic prefix of input string.
32+
First part of the combined string is input string and the second part is reversed string.
33+
So when some prefix is the suffix of the entire string it means that the prefix is palindromic.
34+
35+
Finally, we calculated |LPP|, so we can return answer as |S| - |LPP|.
36+
37+
38+
*/
39+
void computeKmpLps(int lps[],string x);
40+
int Solution::solve(string A) {
41+
42+
43+
int n = A.length();
44+
45+
string B = A + '$';
46+
reverse(A.begin(),A.end());
47+
48+
B+=A;
49+
50+
int lps[n*2 + 1];
51+
52+
computeKmpLps(lps,B);
53+
54+
return n - lps[n*2];
55+
}
56+
void computeKmpLps(int lps[], string x){
57+
int i=1,m=x.length(),len=0;
58+
59+
lps[0] = 0; // lps[0] is always 0
60+
61+
// the loop calculates lps[i] for i = 1 to M-1
62+
while (i < m)
63+
{
64+
if (x[i] == x[len])
65+
{
66+
len++;
67+
lps[i] = len;
68+
i++;
69+
}
70+
else // (str[i] != str[len])
71+
{
72+
// This is tricky. Consider the example.
73+
// AAACAAAA and i = 7. The idea is similar
74+
// to search step.
75+
if (len != 0)
76+
{
77+
len = lps[len-1];
78+
79+
// Also, note that we do not increment
80+
// i here
81+
}
82+
else // if (len == 0)
83+
{
84+
lps[i] = 0;
85+
i++;
86+
}
87+
}
88+
}
89+
90+
}

0 commit comments

Comments
 (0)