Блог пользователя RoUge

Автор RoUge, история, 7 лет назад, По-английски

n = 6 a[] = {1, 1, 5, 2, 5, 5} answer = 1 ( swap a[0] with a[4] or a[5] )

n = 8 a[] = {1, 5, 5, 1, 4, 6, 1, 1} answer = 2 (swap a[0] with a[1] and a[5] with a[6] )

n = 8 a[] = {3, 1, 1, 5, 3, 3, 5, 5} answer = 2 (swap a[0] with a[1] and a[5] with a[6] )

indexing from zero in above examples.

  • Проголосовать: нравится
  • +9
  • Проголосовать: не нравится

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone answer this?

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

You can solve it using brute force if the limit is smaller than 1e5

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    What if n up to 1e9 ?

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Do you know dp ? I think you can solve it using dp , but I'm not sure . I will think about it, nice problem : ).

      • »
        »
        »
        »
        6 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        I know DP , But DP Work if limit is short , It won't work

        • »
          »
          »
          »
          »
          6 лет назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          Sad :') if n is 1e5 will it work ? right?

          • »
            »
            »
            »
            »
            »
            6 лет назад, # ^ |
              Проголосовать: нравится 0 Проголосовать: не нравится

            I Think Also no , It won't work with dp

          • »
            »
            »
            »
            »
            »
            6 лет назад, # ^ |
            Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

            Do you know dp better than n^2?

            Most things better than n^2 will work for 1e5

            • »
              »
              »
              »
              »
              »
              »
              6 лет назад, # ^ |
              Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

              right? , I didn't solve problems using dp.

              • »
                »
                »
                »
                »
                »
                »
                »
                6 лет назад, # ^ |
                  Проголосовать: нравится 0 Проголосовать: не нравится

                I don't Know dp will solve this problem better than n^2

              • »
                »
                »
                »
                »
                »
                »
                »
                6 лет назад, # ^ |
                  Проголосовать: нравится 0 Проголосовать: не нравится

                If You solve it using DP

                using memoization you need to loop to all who is adjacent is same element as them then Swap it and make answer will be minimum of all recurrences.

                In Worst Case it will be O(n^2)

              • »
                »
                »
                »
                »
                »
                »
                »
                6 лет назад, # ^ |
                  Проголосовать: нравится 0 Проголосовать: не нравится

                First make boolean array its name is vis after that ,We Can make Memoization Function Starting From First Element that its Adjacent element has same value and inside Function we will loop to adjacent elements that have same value and if we found element then increase answer by one and vis[i] = true , and swap element and make recursion on it.

                The Complexity of This in worst Case is O(n^2)

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  3 года назад, # ^ |
                  Rev. 2   Проголосовать: нравится -8 Проголосовать: не нравится

                  Bakry Hi, I am not able to understand how memoization is working here, could you please explain it in a little more detail please? Thank You in advance :)

                  Btw, this question was asked on an onsight google interview recently.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  3 года назад, # ^ |
                    Проголосовать: нравится 0 Проголосовать: не нравится

                  my comment is from 3 years, I don't remember any details and I think my solution was wrong.

              • »
                »
                »
                »
                »
                »
                »
                »
                6 лет назад, # ^ |
                  Проголосовать: нравится 0 Проголосовать: не нравится

                Your Welcome :)

                I will ask also Tomorrow about solution that will work well if n equal 10 ^ 9

              • »
                »
                »
                »
                »
                »
                »
                »
                6 лет назад, # ^ |
                  Проголосовать: нравится 0 Проголосовать: не нравится

                I didn't Find Solution For This Problem.

                Did you find any solution ?

            • »
              »
              »
              »
              »
              »
              »
              6 лет назад, # ^ |
                Проголосовать: нравится 0 Проголосовать: не нравится

              No I can't answer it with dp better than n^2