Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <math>
  4.  
  5. class Solution {
  6. public:
  7.     int movesToChessboard(vector<vector<int>>& board)
  8.     {
  9.         // base case of empty board
  10.         string type1 = "";
  11.         string type2 = "";
  12.         vector<int> type1Locations;
  13.         vector<int> type2Locations;
  14.         bool rowsValid = true;
  15.         bool moreOnes = false;
  16.         bool moreZeros = false;
  17.        
  18.         // type1 is inverse of type2
  19.         // type1Locations size = type2Locations size
  20.         // type1 number of 0's and 1's are not more then 1 number apart
  21.        
  22.         for (int r = 0; r < board.size(); ++r)
  23.         {
  24.             string rowCode;
  25.             for (int num : board[r])
  26.             {
  27.                 rowCode += num + 48;
  28.             }
  29.             if (type1 == rowCode)
  30.             {
  31.                 type1Locations.push_back(r);
  32.             }
  33.             else if (type2 == rowCode)
  34.             {
  35.                 type2Locations.push_back(r);
  36.             }
  37.             else if (type1 == "")
  38.             {
  39.                 type1 = rowCode;
  40.                 type1Locations.push_back(r);
  41.             }
  42.             else if (type2 == "")
  43.             {
  44.                 type2 = rowCode;
  45.                 type2Locations.push_back(r);
  46.             }
  47.             else
  48.             {
  49.                 rowsValid = false;
  50.                 break;
  51.             }
  52.         }
  53.        
  54.         if (rowsValid && type1Locations.size() == type2Locations.size())
  55.         {
  56.             int numOnes = 0;
  57.             int numZeros = 0;
  58.             for (int i = 0; i < type1.size(); ++i)
  59.             {
  60.                 if (type1[i] == type2[i])
  61.                 {
  62.                     rowsValid = false;
  63.                     break;
  64.                 }
  65.                 if (type1[i] == '0')
  66.                 {
  67.                     numZeros++;
  68.                 }
  69.                 if (type1[i] == '1')
  70.                 {
  71.                     numOnes++;
  72.                 }
  73.             }
  74.            
  75.             if (abs(numOnes - numZeros) > 1)
  76.             {
  77.                 rowsValid = false;
  78.             }
  79.            
  80.             if (numOnes > numZeros)
  81.             {
  82.                 moreOnes = true;
  83.             }
  84.             if (numZeros > numOnes)
  85.             {
  86.                 moreZeros = true;
  87.             }
  88.         }
  89.        
  90.         if (rowsValid == false)
  91.         {
  92.             return -1;
  93.         }
  94.         else
  95.         {
  96.             int numSwitches = 0;
  97.             int numEvens = 0;
  98.             int numOdds = 0;
  99.            
  100.             for (int rowLocation : type1RowLocations)
  101.             {
  102.                 if (rowLocation % 2 == 0)
  103.                     numEvens++;
  104.                 else
  105.                     numOdds++;
  106.             }
  107.            
  108.             numSwitches = abs(numEvens - numOdds);
  109.            
  110.             if (moreOnes)
  111.             {
  112.                 for (int i = 0; i < type1.size(); ++i)
  113.                 {
  114.                     if (i % 2 == 1 && type1[i] == '1')
  115.                     {
  116.                         ++numSwitches;
  117.                     }
  118.                 }
  119.             }
  120.             else if (moreZeros)
  121.             {
  122.                 for (int i = 0; i < type1.size(); ++i)
  123.                 {
  124.                     if (i % 2 == 1 && type1[i] == '0')
  125.                     {
  126.                         ++numSwitches;
  127.                     }
  128.                 }
  129.             }
  130.             else
  131.             {
  132.                
  133.             }
  134.         }
  135.     }
  136. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement