-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution599.go
More file actions
69 lines (63 loc) · 1.31 KB
/
solution599.go
File metadata and controls
69 lines (63 loc) · 1.31 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
package solution599
// ============================================================================
// 599. Minimum Index Sum of Two Lists
// URL: https://leetcode.com/problems/minimum-index-sum-of-two-lists/
// ============================================================================
/*
***** Defunc, maybe I'll revisit later...
func findRestaurant(list1 []string, list2 []string) []string {
output := []string{}
var maxval int = -1
for i, s1 := range list1 {
for j, s2 := range list2 {
if s1 == s2 {
val := i + j
if maxval == -1 && maxval < val {
maxval = val
}
if maxval > val {
break
}
if maxval != -1 && val == maxval {
output = append(output, s1)
}
}
}
}
return output
}
*/
func findRestaurant_first_attempt(list1 []string, list2 []string) []string {
count := len(list1)
if len(list2) > count {
count = len(list2)
}
m := make(map[string]int, count)
output := make([]string, 0, count)
var minval = 1<<63 - 1
for i, s1 := range list1 {
_, ok1 := m[s1]
if !ok1 {
m[s1] = -1
}
for j, s2 := range list2 {
switch s1 {
case s2:
m[s1] = 0
m[s1] += i
m[s2] += j
if m[s2] < minval {
minval = m[s2]
}
}
}
}
for k, v := range m {
if v != -1 {
if v == minval {
output = append(output, k)
}
}
}
return output
}