Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <math>
- class Solution {
- public:
- int movesToChessboard(vector<vector<int>>& board)
- {
- // base case of empty board
- string type1 = "";
- string type2 = "";
- vector<int> type1Locations;
- vector<int> type2Locations;
- bool rowsValid = true;
- bool moreOnes = false;
- bool moreZeros = false;
- // type1 is inverse of type2
- // type1Locations size = type2Locations size
- // type1 number of 0's and 1's are not more then 1 number apart
- for (int r = 0; r < board.size(); ++r)
- {
- string rowCode;
- for (int num : board[r])
- {
- rowCode += num + 48;
- }
- if (type1 == rowCode)
- {
- type1Locations.push_back(r);
- }
- else if (type2 == rowCode)
- {
- type2Locations.push_back(r);
- }
- else if (type1 == "")
- {
- type1 = rowCode;
- type1Locations.push_back(r);
- }
- else if (type2 == "")
- {
- type2 = rowCode;
- type2Locations.push_back(r);
- }
- else
- {
- rowsValid = false;
- break;
- }
- }
- if (rowsValid && type1Locations.size() == type2Locations.size())
- {
- int numOnes = 0;
- int numZeros = 0;
- for (int i = 0; i < type1.size(); ++i)
- {
- if (type1[i] == type2[i])
- {
- rowsValid = false;
- break;
- }
- if (type1[i] == '0')
- {
- numZeros++;
- }
- if (type1[i] == '1')
- {
- numOnes++;
- }
- }
- if (abs(numOnes - numZeros) > 1)
- {
- rowsValid = false;
- }
- if (numOnes > numZeros)
- {
- moreOnes = true;
- }
- if (numZeros > numOnes)
- {
- moreZeros = true;
- }
- }
- if (rowsValid == false)
- {
- return -1;
- }
- else
- {
- int numSwitches = 0;
- int numEvens = 0;
- int numOdds = 0;
- for (int rowLocation : type1RowLocations)
- {
- if (rowLocation % 2 == 0)
- numEvens++;
- else
- numOdds++;
- }
- numSwitches = abs(numEvens - numOdds);
- if (moreOnes)
- {
- for (int i = 0; i < type1.size(); ++i)
- {
- if (i % 2 == 1 && type1[i] == '1')
- {
- ++numSwitches;
- }
- }
- }
- else if (moreZeros)
- {
- for (int i = 0; i < type1.size(); ++i)
- {
- if (i % 2 == 1 && type1[i] == '0')
- {
- ++numSwitches;
- }
- }
- }
- else
- {
- }
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement