764. Maximum plus sign - M

Posted by Mateobus on Sat, 10 Aug 2019 14:30:51 +0200

764. Maximum plus sign - M

label: Can you calculate dp?

In a 2D grid with sizes ranging from (0,0) to (N-1,N-1), each cell is 1 except for the element given in mines. What is the order of the largest axis alignment plus sign sign that contains 1 in the grid? Returns the order of the plus sign. If no plus sign is found, 0 is returned.

An "axisymmetric" plus sign with K "order 1" has a central grid grid grid[x][y] = 1, and four arms with a length of k-1 extending upward, downward, left and right from the center. An example of k-order "axisymmetric" plus sign is given below. Note that all grids with a plus sign are required to be 1, and other grids may be 0 or 1.

Examples of k-order axisymmetric plus sign:

Order 1:

000
010
000

Order 2:

00000
00100
01110
00100
00000

Order 3:

0000000
0001000
0001000
0111110
0001000
0001000
0000000

Example 1:

Input: N = 5, mines = [[4, 2]]
Output: 2
Explanation:

11111
11111
11111
11111
11011

In the above grid, the order of the maximum plus sign can only be 2. A sign has been marked in the figure.

Example 2:

Input: N = 2, mines = []
Output: 1

Explanation:

11
11

There is no second-order plus sign, there is first-order plus sign.

Example 3:

Input: N = 1, mines = [[0, 0]]
Output: 0
Explanation:

0

No plus sign, return 0.

Tips:

The range of integer N: [1,500].
The maximum length of mines is 5000.
mines[i] is a 2-length number consisting of two [0, N-1].
(In addition, programming with C, C++, or C# will be done with a slightly shorter time limit.)

Analysis:

  1. I didn't know much about dp, so I did it in the most clumsy way. DP records the distance from the nearest 0 on the left to the current position and from the nearest 0 on the top to the current position. The calculation on the back requires illusory traversal of the width on the right and the bottom. The last four take the smallest length, plus 1 on the center, to get the order.

  2. At that time, I thought that the cumulative number of left and up would be recorded with a and b, respectively, to minimize w = min (dp [i] [j] [a, DP [i] [j] [b), so that only DP [i] [j, min (N, j + w)] would be determined for right and down at [i,min(i+w)],[j,min(N,j+w)] [j]. a = I + K and DP [i] [j] [b] [i] [j] [b = I + k, (0 < K < w). Of course, this would be improved.

  3. This is to see other people's sauce operation, at first do not understand, later, too strong, a scan to determine all the order; first, because it is square, so the row length is equal, can simultaneously update a row of a row of a row of continuous length, and because it is the smaller one, so grid initialization to N, where In addition, for dp[i][j], there are two directions on the line, so the same column is compared twice (to right,to left). Another point is that the order of a row and a column is calculated in the time of one side length, as long as the order of N (k from left) is calculated. From 0 to N-1, all calculations are completed, so the total time complexity is O(N^ 2).

package main
import "fmt"

/*
Execution time: 1820 ms, beating 12.50% of all Go submissions
 Memory consumption: 7 MB, beating 100.00% of all Go submissions
*/
func orderOfLargestPlusSign_baoli2(N int, mines [][]int) int {
	grid:=make([][]int,N)
	for i:=0;i<N;i++{
		grid[i]=make([]int,N)
	}

	for i:=0;i<len(mines);i++{
		grid[mines[i][0]][mines[i][1]]=-1
	}
	for i:=0;i<N;i++{
		for j:=0;j<N;j++{
			grid[i][j]+=1
		}
	}
	ret:=0
	for i:=0;i<N;i++{
		for j:=0;j<N;j++{
			w,tmp:=0,0
			for k:=j-1;k>=0&&grid[i][k]>0;k--{//l
				if j-k>w{ w=j-k }
			}
			for k:=i-1;k>=0&&grid[k][j]>0;k--{//u
				if i-k>tmp{ tmp=i-k }
			}
			if w>tmp{ w=tmp }
			tmp=0
			for k:=j+1;k<N&&grid[i][k]>0;k++{//r
				if k-j>tmp{ tmp=k-j }
			}
			if w>tmp{ w=tmp }
			tmp=0
			for k:=i+1;k<N&&grid[k][j]>0;k++{//d
				if k-i>tmp{ tmp=k-i }
			}
			if w>tmp{ w=tmp }
			if grid[i][j]>0{ w+=1 }
			if ret<w{ ret=w }
		}
	}
	return ret
}

/*
Execution time: 232 ms, beating 37.50% of all Go submissions
 Memory consumption: 10.8 MB, beating 100.00% of all Go submissions
*/
type Node struct{
	a,b int
}
func orderOfLargestPlusSign_baoli1(N int, mines [][]int) int {
	dp:=make([][]Node,N)
	for i:=0;i<N;i++{
		dp[i]=make([]Node,N)
	}

	for i:=0;i<len(mines);i++{
		dp[mines[i][0]][mines[i][1]].a=-1
		dp[mines[i][0]][mines[i][1]].b=-1
	}
	for i:=0;i<N;i++{
		for j:=0;j<N;j++{
			dp[i][j].a+=1
			dp[i][j].b+=1
			if j>0&&dp[i][j].a>0{
				dp[i][j].a=dp[i][j-1].a+1
			}
			if i>0&&dp[i][j].b>0{
				dp[i][j].b=dp[i-1][j].b+1
			}
		}
	}
	ret:=0
	for i:=0;i<N;i++{
		for j:=0;j<N;j++{
			w:=dp[i][j].a-1
			if w>dp[i][j].b-1{ w=dp[i][j].b-1 }
			k:=j+w
			for ;k>j;k--{
				if k<N&&dp[i][k].a-dp[i][j].a==k-j{ break }
			}
			if w>k-j{ w=k-j } //Find the rightmost position
			k=i+w
			for ;k>i;k--{
				if k<N&&dp[k][j].b-dp[i][j].b==k-i{ break }
			}
			if w>k-i{ w=k-i }
			if ret<w+1{ ret=w+1 }
		}
	}
	return ret
}


/*
Execution time: 80 ms, beating 100.00% of all Go submissions
 Memory consumption: 7 MB, beating 100.00% of all Go submissions
*/
func orderOfLargestPlusSign(N int, mines [][]int) int {
	grid:=make([][]int,N)
	for i:=0;i<N;i++{
		grid[i]=make([]int,N)
	}
	for i:=0;i<N;i++{
		for j:=0;j<N;j++{
			grid[i][j]=N
		}
	}
	for i:=0;i<len(mines);i++{
		grid[mines[i][0]][mines[i][1]]=0
	}

	left,right,up,down:=0,0,0,0
	for k:=0;k<N;k++{
		left,right,up,down=0,0,0,0
		for i,j:=0,N-1;i<N;i,j=i+1,j-1{
			if grid[i][k]==0{ down=0 }else{ down+=1}
			if down<grid[i][k]{ grid[i][k]=down }
			if grid[k][i]==0{ left=0 }else{ left+=1 }
			if left<grid[k][i]{ grid[k][i]=left }
			if grid[j][k]==0{ up=0 }else{ up+=1 }
			if up<grid[j][k]{ grid[j][k]=up }
			if grid[k][j]==0{ right=0 }else{ right+=1 }
			if right<grid[k][j]{ grid[k][j]=right }
		}
	}
	ret:=0
	for i:=0;i<N;i++{
		for j:=0;j<N;j++{
			if grid[i][j]>ret{ ret=grid[i][j] }
		}
	}
	return ret
}

func main() {
	tables:=[][][]int{
		{{5},{4,2},},
		{{2},},
		{{1},{0,0}},
	}
	for _,v:=range tables{
		fmt.Println(orderOfLargestPlusSign(v[0][0],v[1:]))
	}
	
}

Topics: Programming