Write a program to find all the possible paths from a starting point to dest point in a maze(2-D array).
ex: 1 0 1 0 1 1 1 1 0 1 0 1 0 0 1 1If there is a block it’s represented by 0.
If there is a path it’s represented by 1.
Random commentary about Machine Learning, BigData, Spark, Deep Learning, C++, STL, Boost, Perl, Python, Algorithms, Problem Solving and Web Search
ex: 1 0 1 0 1 1 1 1 0 1 0 1 0 0 1 1If there is a block it’s represented by 0.
k: Number of floors
n: Number of Eggs
eggDrop(n, k): Minimum number of trails needed to find the critical
floor in worst case.
1) If the egg breaks after dropping from xth floor, then we only need to check for floors lower than x with remaining eggs; so the problem reduces to x-1 floors and n-1 eggs
2) If the egg doesn’t break after dropping from the xth floor, then we only need to check for floors higher than x; so the problem reduces to k-x floors and n eggs.
eggDrop(n, k) = 1 + min{max(eggDrop(n - 1, x - 1), eggDrop(n, k - x)): x in {1, 2, ..., k}}
int findNthElementByInorder(Node *node, int &n)
{
if (node == null)
return -1;
findNthElementByInorder(node->left, n);
if (n == 0)
return node-> value;
else
n--;
findNthElementByInorder(node->right, n);
}
int findNthElementByInorder(Node *node, int &n)
{
if (node == null)
return -1;
findNthElementByInorder(node->left, n);
if (n == 0)
return node-> value;
else
n--;
findNthElementByInorder(node->right, n);
}
Node* reverseLL(Node* n) {
Node* curr = n;
Node* prev = null;
while(curr !=null) {
Node* temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
return prev;
}
void threeWayPartition(int data[], int size, int low, int high) {
int p = -1;
int q = size;
for (int i = 0; i < q;) {
if (data[i] < low) {
swap(data[i], data[++p]);
++i;
} else if (data[i] >= high) {
swap(data[i], data[--q]);
} else {
++i;
}
}
For each i = 1..length do
left = 0, right = length
While left < i and right > i do
if values[left] + values[i] + values[right] == 0
# Got solution here
if values[left] + values[i] + values[right] > 0
# means that right border should be moved
right--
else
# moving left border
left++
const unsigned MULTIPLY_BASE = 10;
void multiplyLittleEndian(const vector < unsigned > &a, const vector < unsigned > &b, vector < unsigned > &r)
{
r.assign(a.size()+b.size(), 0);
unsigned ri=0;
for(unsigned ai=0; ai < a.size(); ++ai)
{
unsigned cri = ri++, carry = 0, am = a[ai], cr;
for(unsigned bi=0; bi < b.size(); ++bi, ++cri)
{
cr = carry + am*b[bi] + r[cri];
r[cri] = cr % MULTIPLY_BASE;
carry = cr / MULTIPLY_BASE;
}
while(carry)
{
cr = carry + r[cri];
r[cri++] = cr % MULTIPLY_BASE;
carry = cr / MULTIPLY_BASE;
}
}
while((!r.empty()) && r.back() == 0) r.pop_back(); // trim result
}
mapping = {
'I' : 1,
'V' : 5,
'X' : 10,
'L' : 50,
'C' : 100,
'D' : 500,
'M' : 1000,
}
def roman(string):
prev = 0
totalsum = 0
for c in string:
if mapping[c] <= prev:
totalsum = totalsum + mapping[c]
else:
totalsum = totalsum - prev
totalsum = totalsum + mapping[c] - prev
prev = mapping[c]
print(totalsum)
roman("XCIX")