aakarshmadhavan's blog

By aakarshmadhavan, history, 6 years ago, In English
Given an array arr[] consisting of 0’s and 1’s. A flip operation is one in which you turn 1 into 0 and a 0 into 1.You have to do atmost one “Flip” operation on a subarray. Then finally display maximum number of 1 you can have in the array.

I did not know this was a DP problem before I checked the tag. I am kind of confused how to proceed with the problem right now. Any ideas/hints are massively appreciated. So far I thought of some things which arent quite correct such as

  • DP[i] = Max # of 1's in subarray [0,...,i]
  • DP[i] = Max # of 1's in subarray [0,...,i] if you flip at index i

I am thinking second one might be an idea, but I can't find a optimal structure for "recursion."

Thanks

Full text and comments »

  • Vote: I like it
  • +4
  • Vote: I do not like it

By aakarshmadhavan, history, 6 years ago, In English

I am just trying to make a recursive solution for this but it is failing horribly. Here is the problem:

https://leetcode.com/problems/can-i-win/description/

In the "100 game," two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to reach or exceed 100 wins.

What if we change the game so that players cannot re-use integers?

For example, two players might take turns drawing from a common pool of numbers of 1..15 without replacement until they reach a total >= 100.

Given an integer maxChoosableInteger and another integer desiredTotal, determine if the first player to move can force a win, assuming both players play optimally.

You can always assume that maxChoosableInteger will not be larger than 20 and desiredTotal will not be larger than 300.

Here is my code so far:

class Solution {
    boolean [] used;
    int total = 0;
    int maxChoosableInteger = 0;
    
    public boolean canIWin(int maxChoosableInteger, int desiredTotal) {
        used = new boolean[maxChoosableInteger + 1];
        this.total = desiredTotal;
        this.maxChoosableInteger = maxChoosableInteger;
        int n = helper(true, total);
        System.out.println(n);
        return  n >= total;
    }
    
    public int helper(boolean turn, int sum){
        if(sum < total || sum >= 2*total) return 0;
        int cur = 0;
        if(!turn) cur = Integer.MAX_VALUE;
        for(int i = maxChoosableInteger; i >= 1; --i){
            if(used[i]) continue;
            used[i] = true;
            if(turn) cur = Math.max(cur, i + helper(false, sum + i));
            else cur = Math.min(cur, -i + helper(true, sum - i));
            used[i] = false;
        }
        return cur;
    }
}

It is not working for case maxChooseableInteger=4, desiredTotal=6. If you can help me that would be very much appreciated. I am struggling with these types of "minimax" problems. Thanks in advance.

Full text and comments »

  • Vote: I like it
  • -3
  • Vote: I do not like it

By aakarshmadhavan, history, 6 years ago, In English

I found an interesting question on another OJ. I'm very bad at these types of questions (any suggestions to improve are also welcome).

486

For you all here this will be easy cake, I appreciate any help you can provide for me.

I tried 2 pointers-greedy, but I realized that the players will not always take the largest values from each of the left/right pointers of the array. I'm very stuck and can't find a way.

Any ideas/hints/suggestions (not too revealing). I really don't want to look up the solution before getting it myself.

Thanks

Full text and comments »

  • Vote: I like it
  • +1
  • Vote: I do not like it

By aakarshmadhavan, history, 6 years ago, In English
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of W.

If there is no such window in S that covers all characters in T, return the empty string "".
If there are multiple such minimum-length windows, return the one with the left-most starting index.

Example - Input: 
S = "abcdebdde", T = "bde"
Example - Output: "bcde"

I have spent approximately 2 hours on this problem with no progress. I just peeked at the "TAGS" of this problem and it says dynamic programming. I spent 2 hours trying two-pointers but could not solve it.

I am hoping you all can help me with the dynamic programming approach. I saw the space complexity was O(Length(S)*Length(T)) so this hints at 2D-DP array-table but I am unsure where the motivation of this even comes from and how to proceed further.

My possible DP ideas were

Boolean DP[i][j] ==> T[0, i] is a subsequence of S[0, j]
Boolean DP[i][j] ==> T[0, i] is a subsequence of S[0, j] AND T[i] == S[j]

None of these are correct/working. Please help me out!

Thanks

Full text and comments »

  • Vote: I like it
  • +4
  • Vote: I do not like it