ss1073857's blog

By ss1073857, history, 3 months ago, In English

What will be the time complexity for this code?

public int coinChange(int[] coins, int amount){
    if(coins == null || coins.length == 0 || amount < 1) return 0;

    Deque<Integer> queue = new ArrayDeque<Integer>();
    Set<Integer> visited = new HashSet<Integer>();
    queue.addFirst(amount);
    visited.add(amount);
    int level = 0;

    while(!queue.isEmpty()){
      int size = queue.size();

      while(size-- > 0){
        int curr = queue.removeLast();
        if(curr == 0) return level;

        if(curr < 0) continue;

        for(int coin : coins){
          int next = curr - coin;
          if(next >= 0 && !visited.contains(next)){
            queue.addFirst(next);
            visited.add(next);
          }
        }
      }

      level++;
    }

    return -1;
  }

Read more »

 
 
 
 
  • Vote: I like it
  • 0
  • Vote: I do not like it

By ss1073857, history, 21 month(s) ago, In English

You are given n switches, each switch connected to a led. cost of switch of switch i is ci. intially all leds are on. When any switch is used, all leds in range [i-k,i+k] will get toggled off (on to off, off to on). Find minimum cost to switch off all leds.N<=10000, K<=1000, Ci<10^9

Read more »

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

By ss1073857, history, 22 months ago, In English

Number of ways to arrange people of type A and type B in a row with k available positions given less than n people of type A can sit consecutively and less than m people of type B can sit consecutively.

Read more »

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

By ss1073857, history, 23 months ago, In English

Given an array of N numbers with M distinct no . Find minimum no. of numbers such that on permuting those in any order , digit with same value come together. Ex: 1 3 1 5 6 Choosing indices 0 and 1 or 1 and 2 makes all the one’s together So ans: 2 ( two indices are chosen)

Read more »

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