@arunkaly Well O(S*P)
arunkaly
@arunkaly
Posts made by arunkaly

RE: Simplest AC java sliding window solution with comments
clean simple and efficient O(N) solution

RE: C# two hashsets O(n) runtime
You dont need to check if one array is smaller or larger than the other. It does not matter whether your first hashset is made from the larger or smaller array. However, using the smaller array might take less space. Although seem to be doing the opposite.

RE: Replace two adjacent digits with larger one to find the smallest number that can be obtained
Here is a simple O(N) solution with no additional space. The key idea is to iteratively add each digit starting from the least significant and in each iteration there are two possibilities: 1) pick the bigger of current digit and previous digit and combine it with the rest of the number or 2) just append current digit to "current number" with already replaced digit. Pick the smallest of 1) and 2) and assign it to current number. but it did take me more than 30 mins to solve this.. so not sure how fast were they expecting to get this done.
Works only for num >=0.
public static int getMinimumAdjacentReplaced(int num) { int prevDigit = num%10; int nextDigit = 0; int remainingNum = num/10; int multiplier = 1; int prevAllDigits = 0; int curReplacedNum = prevDigit; while(remainingNum > 0) { nextDigit = remainingNum%10; int biggerDigit = nextDigit; if (prevDigit > nextDigit) biggerDigit = prevDigit; int replacedNum1 = biggerDigit*multiplier + prevAllDigits; int replacedNum2 = curReplacedNum + (nextDigit*multiplier); curReplacedNum = replacedNum2; if (replacedNum1 < replacedNum2) { curReplacedNum = replacedNum1; } prevAllDigits = prevAllDigits + prevDigit*multiplier; prevDigit = nextDigit; multiplier *= 10; remainingNum = remainingNum / 10; } return curReplacedNum; }