Skip to content

Instantly share code, notes, and snippets.

@senarukana
Created March 11, 2015 07:58
Show Gist options
  • Select an option

  • Save senarukana/9a02f563278e49d23f58 to your computer and use it in GitHub Desktop.

Select an option

Save senarukana/9a02f563278e49d23f58 to your computer and use it in GitHub Desktop.
young matrix
ef add(self, val):
if self.size >= self.m * self.n:
return False
i, j = self.m-1, self.n-1
self.mat[i][j] = val
while True:
ni, nj = i, j
if i > 0 and self.mat[i-1][j] > self.mat[ni][nj]:
ni, nj = i-1, j
if j > 0 and self.mat[i][j-1] > self.mat[ni][nj]:
ni, nj = i, j-1
if ni == i and nj == j:
break
else:
self.mat[i][j], self.mat[ni][nj] = \
self.mat[ni][nj], self.mat[i][j]
i, j = ni, nj
self.size += 1
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment