-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1820-MaximumNumberOfAcceptedInvitations.go
More file actions
133 lines (122 loc) · 4.03 KB
/
1820-MaximumNumberOfAcceptedInvitations.go
File metadata and controls
133 lines (122 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
// 1820. Maximum Number of Accepted Invitations
// There are m boys and n girls in a class attending an upcoming party.
// You are given an m x n integer matrix grid, where grid[i][j] equals 0 or 1.
// If grid[i][j] == 1, then that means the ith boy can invite the jth girl to the party.
// A boy can invite at most one girl, and a girl can accept at most one invitation from a boy.
// Return the maximum possible number of accepted invitations.
// Example 1:
// Input: grid = [[1,1,1],
// [1,0,1],
// [0,0,1]]
// Output: 3
// Explanation: The invitations are sent as follows:
// - The 1st boy invites the 2nd girl.
// - The 2nd boy invites the 1st girl.
// - The 3rd boy invites the 3rd girl.
// Example 2:
// Input: grid = [[1,0,1,0],
// [1,0,0,0],
// [0,0,1,0],
// [1,1,1,0]]
// Output: 3
// Explanation: The invitations are sent as follows:
// -The 1st boy invites the 3rd girl.
// -The 2nd boy invites the 1st girl.
// -The 3rd boy invites no one.
// -The 4th boy invites the 2nd girl.
// Constraints:
// grid.length == m
// grid[i].length == n
// 1 <= m, n <= 200
// grid[i][j] is either 0 or 1.
import "fmt"
func maximumInvitations(grid [][]int) int {
// 每行每列最多只能保留1个1,返回最多的1的个数
// 类似八皇后,回溯
// row从上到下,一个colMap保存已经有1的行回溯
// 尝试每个行的1,加上剪枝即可,每行也可以不加1,那就有2^m次方,越界了
// 固定一个起始行,往下走?好像也会越界 m <= 200
mp := make(map[int]int)
res, m := 0, len(grid)
var match func(i int, visited map[int]bool) bool
match = func(i int, visited map[int]bool) bool {
if _, ok := visited[i]; ok { return false }
visited[i] = true
for j := 0; j < len(grid[i]); j++ {
if grid[i][j] != 1 { continue }
if _, ok := mp[j]; !ok || match(mp[j], visited) {
mp[j] = i
return true
}
}
return false
}
for i := 0; i < m; i++ {
visited := make(map[int]bool)
if match(i, visited) {
res++
}
}
return res
}
func maximumInvitations1(grid [][]int) int {
res, m, n := 0, len(grid), len(grid[0])
myBoy := make([]int, n)
for i := range myBoy { myBoy[i] = -1 }
var findGirlForBoy func(boy int, visited []bool) bool
findGirlForBoy = func(boy int, visited []bool) bool {
for girl := 0; girl < n; girl++ {
if !visited[girl] && grid[boy][girl] == 1 {
visited[girl] = true
if myBoy[girl] == -1 || findGirlForBoy(myBoy[girl], visited) {
myBoy[girl] = boy
return true
}
}
}
return false
}
for i := 0; i < m; i++ {
visited := make([]bool, n)
if findGirlForBoy(i, visited) { res++ }
}
return res
}
func main() {
// Example 1:
// Input: grid = [[1,1,1],
// [1,0,1],
// [0,0,1]]
// Output: 3
// Explanation: The invitations are sent as follows:
// - The 1st boy invites the 2nd girl.
// - The 2nd boy invites the 1st girl.
// - The 3rd boy invites the 3rd girl.
grid1 := [][]int{
{1,1,1},
{1,0,1},
{0,0,1},
}
fmt.Println(maximumInvitations(grid1)) // 3
// Example 2:
// Input: grid = [[1,0,1,0],
// [1,0,0,0],
// [0,0,1,0],
// [1,1,1,0]]
// Output: 3
// Explanation: The invitations are sent as follows:
// -The 1st boy invites the 3rd girl.
// -The 2nd boy invites the 1st girl.
// -The 3rd boy invites no one.
// -The 4th boy invites the 2nd girl.
grid2 := [][]int{
{1,0,1,0},
{1,0,0,0},
{0,0,1,0},
{1,1,1,0},
}
fmt.Println(maximumInvitations(grid2)) // 3
fmt.Println(maximumInvitations1(grid1)) // 3
fmt.Println(maximumInvitations1(grid2)) // 3
}