Sort by

recency

|

1931 Discussions

|

  • + 0 comments

    This is the solution for Java

        public static void minimumBribes(List<Integer> q) {
            int n = q.size();
            int bribes = 0;
    
            for(int i = n; i >= 1; i--) {
                // quick exit if number is in the right place
                if (q.get(i - 1) == i) {
                    continue;
                }
    
                // case where expected element is only one
                // places ahead
                if(q.get(i - 2) == i) {
                    bribes++;
                    Collections.swap(q, i-1, i-2);
                }
    
                // case where expected element has moved two
                // places away
                if(q.get(i - 3) == (i)) {
                    bribes += 2;
                    Collections.rotate(q.subList(i - 3, i), -1);
                }
    
                // case where expected element has moved ahead
                // further places
                else {
                    System.out.println("Too chaotic");
                    return;
                }
            }
            System.out.printf("%d\n",bribes);
        }
            }
            System.out.printf("%d\n",bribes);
        }
    
  • + 0 comments

    Solution for c#

    public static void minimumBribes(List<int> q)
    {
    	int totalBribes = 0;
    
    	for (int i = q.Count - 1; i >= 0; i--)
    	{
    		// if the current person moves than 2 spots
    		if (q[i] - (i + 1) > 2)
    		{
    			Console.WriteLine("Too chaotic");
    			return;
    		}
    
    		// Sum up the bribes
    		for (int j = Math.Max(0, q[i] - 2); j < i; j++)
    		{
    			if (q[j] > q[i])
    			{
    					totalBribes++;
    			}
    		}
    	}
    	Console.WriteLine(totalBribes);
    }
         
    
  • + 0 comments

    Solution for c#

    public static void minimumBribes(List<int> q)
    {
    	int totalBribes = 0;
    
    	for (int i = q.Count - 1; i >= 0; i--)
    	{
    			// if the current person moves than 2 spots
    			if (q[i] - (i + 1) > 2)
    			{
    					Console.WriteLine("Too chaotic");
    					return;
    			}
    
    			// Sum up the bribes
    			for (int j = Math.Max(0, q[i] - 2); j < i; j++)
    			{
    					if (q[j] > q[i])
    					{
    							totalBribes++;
    					}
    			}
    	}
    	Console.WriteLine(totalBribes);
    }
         
    
  • + 0 comments

    I have same question.

    But when i try to check manually this is what i found: 12345678 - original 12354678 - bribe 1 12534678 - bribe 2 12534768 - bribe 3 12537468 - bribe 4 12537486 - bribe 5 12537846 - bribe 6 12537864 - bribe 7

  • + 0 comments

    There is one thing I don't understand, can a biber be bribed? for instance * 1 2 3 4 5 * 2 1 3 4 5 * 1 2 3 4 5