Guest User

Untitled

a guest
Feb 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. int kadane2d(int[][] m){
  2. int row = m.length;
  3. if(row==0)return 0;
  4. int col = m[0].length;
  5.  
  6. int ret = Integer.MIN_VALUE;
  7. for (int left = 0; left < col; left++) {
  8. int[] sum = new int[row]; // reuse this
  9. for (int right = left; right < col; right++) {
  10. for (int i = 0; i < row; i++) {
  11. sum[i] += m[i][right];
  12. }
  13.  
  14. int curMax=sum[0];
  15. int max=sum[0];
  16. for (int i = 1; i < row; i++) {
  17. curMax = Math.max(curMax+sum[i], sum[i]);
  18. max = Math.max(curMax, max);
  19. }
  20. ret=Math.max(ret, max);
  21. }
  22. }
  23.  
  24. return ret;
  25. }
Add Comment
Please, Sign In to add comment