Skip to content

Commit e1c0b26

Browse files
committed
Day 72 : One DP and one tree problem
1 parent ec753ac commit e1c0b26

File tree

3 files changed

+151
-3
lines changed

3 files changed

+151
-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 | 103 |
10-
| Current Streak | 71 days |
11-
| Longest Streak | 71 ( August 17, 2015 - October 26, 2015 ) |
9+
| Total Problems | 105 |
10+
| Current Streak | 72 days |
11+
| Longest Streak | 72 ( August 17, 2015 - October 27, 2015 ) |
1212

1313
</center>
1414

@@ -180,3 +180,5 @@ Include contains single header implementation of data structures and some algori
180180
|Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].| [moveZeroes.cpp](leet_code_problems/moveZeroes.cpp)|
181181
|Given an array of integers, find if the array contains any duplicates. Function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.| [containsDuplicate.cpp](leet_code_problems/containsDuplicate.cpp)|
182182
| Given a list, rotate the list to the right by k places, where k is non-negative. For example: <ul><li>Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL</li></ul>| [rotateList.cpp](leet_code_problems/rotateList.cpp)|
183+
| Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.). You have the following 3 operations permitted on a word:<ul><li>Insert a character</li></ul><ul><li>Delete a character.</li></ul><ul><li>Replace a character</li></ul>.|[editDistance.cpp](leet_code_problems/editDistance.cpp)|
184+
|Given a binary tree, Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL.You may only use constant extra space.You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).| [connectNextPointers.cpp](leet_code_problems/connectNextPointers.cpp)|
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
3+
*
4+
* Initially, all next pointers are set to NULL.
5+
*
6+
* Note:
7+
*
8+
* You may only use constant extra space.
9+
* You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
10+
*
11+
*/
12+
13+
14+
#include<iostream>
15+
#include<queue>
16+
17+
struct TreeLinkNode {
18+
int data;
19+
TreeLinkNode * left;
20+
TreeLinkNode * right;
21+
TreeLinkNode * next;
22+
TreeLinkNode( int d ) : data{ d }, left{ nullptr }, right{ nullptr }, next{ nullptr } {}
23+
};
24+
25+
void connect(TreeLinkNode *root) {
26+
if( root == nullptr) {
27+
return;
28+
}
29+
std::queue<TreeLinkNode*> qu;
30+
qu.push(root);
31+
qu.push(nullptr);
32+
TreeLinkNode * curr = nullptr;
33+
while(!qu.empty()) {
34+
curr = qu.front();
35+
qu.pop();
36+
if ( curr == nullptr ) {
37+
if (!qu.empty()) {
38+
qu.push(nullptr);
39+
}
40+
continue;
41+
}
42+
if (!qu.empty()) {
43+
curr->next = qu.front();
44+
} else {
45+
curr->next = nullptr;
46+
}
47+
if ( curr->left ) {
48+
qu.push(curr->left);
49+
}
50+
if ( curr->right ) {
51+
qu.push(curr->right);
52+
}
53+
}
54+
}
55+
56+
void printTree( TreeLinkNode * root ) {
57+
if (root) {
58+
std::cout << "Node value " << root->data << "-->";
59+
if ( root->next != nullptr ) {
60+
std::cout << root->next->data << std::endl;
61+
} else {
62+
std::cout << "nullptr" << std::endl;
63+
}
64+
printTree(root->left);
65+
printTree(root->right);
66+
}
67+
}
68+
69+
70+
int main() {
71+
TreeLinkNode * root = new TreeLinkNode(1);
72+
root->left = new TreeLinkNode(2);
73+
root->right = new TreeLinkNode(3);
74+
root->left->left = new TreeLinkNode(4);
75+
root->left->right = new TreeLinkNode(5);
76+
root->right->left = new TreeLinkNode(6);
77+
root->right->right = new TreeLinkNode(7);
78+
std::cout << "Before Connect:\n";
79+
printTree(root);
80+
connect(root);
81+
std::cout << "After Connect:\n";
82+
printTree(root);
83+
return 0;
84+
}

leet_code_problems/editDistance.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
3+
*
4+
* You have the following 3 operations permitted on a word:
5+
*
6+
* a) Insert a character
7+
* b) Delete a character
8+
* c) Replace a character
9+
*
10+
*/
11+
12+
#include <iostream>
13+
#include <string>
14+
15+
using std::string;
16+
17+
int min( int a, int b, int c ) {
18+
int minab = ( a < b ) ? a : b;
19+
return ( minab < c ) ? minab:c;
20+
}
21+
22+
int minEditDistance(string word1, string word2) {
23+
int len1 = word1.size();
24+
int len2 = word2.size();
25+
int minCost;
26+
int ** table = new int*[len1+1];
27+
for ( int i = 0;i <= len1; ++i ) {
28+
table[i] = new int[len2+1];
29+
}
30+
31+
for (int i = 0; i <= len1; ++i) {
32+
for (int j = 0; j <= len2; ++j ) {
33+
if ( i == 0 ) {
34+
table[i][j] = j;
35+
}
36+
else if ( j == 0 ) {
37+
table[i][j] = i;
38+
} else if ( word1[i-1] == word2[j-1]) {
39+
table[i][j] = table[i-1][j-1];
40+
} else {
41+
table[i][j] = 1+ min(table[i-1][j-1], table[i][j-1], table[i-1][j]);
42+
}
43+
}
44+
}
45+
minCost = table[len1][len2];
46+
for ( int i = 0; i <= len1; ++i ) {
47+
delete table[i];
48+
}
49+
delete[] table;
50+
return minCost;
51+
}
52+
53+
54+
int main() {
55+
std::string word1, word2;
56+
std::cout << "Enter word 1 : " ;
57+
std::cin >> word1;
58+
std::cout << "Enter word 2 : " ;
59+
std::cin >> word2;
60+
std::cout << "Min edit distance : " << minEditDistance(word1, word2) << std::endl;
61+
return 0;
62+
}

0 commit comments

Comments
 (0)