Skip to content

Instantly share code, notes, and snippets.

@binhngoc17
Created November 23, 2014 04:44
Show Gist options
  • Save binhngoc17/6deedf7ad6bd1d607250 to your computer and use it in GitHub Desktop.
Save binhngoc17/6deedf7ad6bd1d607250 to your computer and use it in GitHub Desktop.
Matrix Flip Problem
import fileinput
inputs = fileinput.input()
input_vals = []
for n in inputs:
input_vals.append(n.replace('\n', ''))
numRows, numCols=[int(i) for i in input_vals[0].split(' ')]
countMaxWishes = {}
for i in range(numRows):
row = input_vals[i+1]
flipToP = []
flipToT = []
for j in range(0, len(row)):
if row[j] == 'T':
flipToP.append(j)
else:
flipToT.append(j)
# As we iterate from the beginning so tuple(data[i]['P']) will uniquely identify
# a set of flips (We don't need to sort them)
countMaxWishes[tuple(flipToT)] = countMaxWishes.get(tuple(flipToT), 0) + 1
countMaxWishes[tuple(flipToP)] = countMaxWishes.get(tuple(flipToP), 0) + 1
print max(countMaxWishes.values())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment