Q1) You are given a positive Integer N, your task is to find the next smallest integer greater than N that does not contain two identical consecutive digits
ex: N=1765 -> 1767 beause 1766 has two identical consecutive digits
N=54 -> 56 not 55 because two identical digits
Solved using brute force, could think of an optimal solution.
function sol(N){
while(true){
if(!checkConsecutive(`${N+1}`)){
return N+1;
}
N++
}
}
function checkConsecutive(N){
if(N.length == 1) return false;
let flag = false;
for(let i = 0;i<N.length-1;i++){
if(N[i] === N[i+1]){
flag = true
}
}
return flag
}
Q2) A patient needs rehabilitation in next N days(0, N-1), The rehabilitation consists of X sessions. for every rehabilitation session other than the last one session, the next session is exactly Y days later.
You are given an array A of N integers listing the costs of the individual rehab session on the N days, that is rehab on kth days costs A[k]
write a function
def solution (A, X,Y):that given the array A and the two integers X and Y return the minimum cost of rehabilitation. It is guaranteed that it is always possible to complete all rehab sessions
Ex:
A = [4,2,3,7] X = 2 Y = 2
o/p 7 (sum of cost of day 0 and 2);
A = [10, 3 ,4, 7] X = 2 Y = 3
o/p 17 (sum of day 0 and day 3)
A = [4,2,5,4,3,5,1,4,2,7] X = 3 Y = 2
6 (day 4, 6 , 8)
Wrote a recursive solution and It only passed 40% of testcases.
```
function solution(A, X, Y) {
let minCost = getMinCost(0, A, X, Y);
return minCost
}
//Recursive Solution
function getMinCost(day, A, X, Y) {
if (day >= A.length) return Infinity;
if (X === 1) return A[day];
let attend = getMinCost(day + Y, A, X - 1, Y);
if (attend === Infinity) return Infinity;
let costAttend = attend + A[day];
let skip = getMinCost(day + 1, A, X, Y);
return Math.min(skip, costAttend);}
```
this question looked really easy and I was able to come up with one other solution but had no time to implement ,
Another approach I tried to do was , when we get are at the last therapy session just loop through the remaining array and return the minimum from that range. tried this in local IDE and didnt work either.
Anyways this OA went really bad, both the questions were pretty difficult for me to solve especially the first one, I could have solved the 2nd question If I had more time, I spent more 1.4 hrs to find the optimal approach for 1st question. then 20 min for second question.
solved 300+ problems in leetcode and still I am bad.
I have no idea where I am making mistake in my learning path, I am able to solve medium-hard level quesion in leetcode with ease especially on graph, tree, heap,trie, LL and other advanced DS, but couldn't solve this OA.
I mostly suck at number theory kind of question and I got that for an OA.
Any advice on what my preparation strategy should be for next OA would be really helpful.