L33t code 841, by David Schindler
							parent
							
								
									f826f2b2d3
								
							
						
					
					
						commit
						02693c7419
					
				| 
						 | 
				
			
			@ -0,0 +1,41 @@
 | 
			
		|||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	res := canVisitAllRooms([][]int{{1}, {2}, {}})
 | 
			
		||||
	fmt.Printf("%+v\n", res)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func canVisitAllRooms(rooms [][]int) bool {
 | 
			
		||||
	if len(rooms) > 0 && len(rooms[0]) == 0 {
 | 
			
		||||
			return false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	keys := getAllKeys(0, make(map[int]struct{}), rooms)
 | 
			
		||||
	keys[0] = struct{}{}    
 | 
			
		||||
 | 
			
		||||
	for i := 0; i < len(rooms); i++ {
 | 
			
		||||
			if _, ok := keys[i]; !ok {
 | 
			
		||||
					return false
 | 
			
		||||
			}
 | 
			
		||||
	}
 | 
			
		||||
	return true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getAllKeys(index int, seen map[int]struct{}, rooms [][]int) map[int]struct{} {
 | 
			
		||||
	keys := map[int]struct{}{}
 | 
			
		||||
 | 
			
		||||
	for j := 0; j < len(rooms[index]); j++ {
 | 
			
		||||
			if _, ok := keys[rooms[index][j]]; ok {
 | 
			
		||||
					continue
 | 
			
		||||
			}
 | 
			
		||||
			keys[rooms[index][j]] = struct{}{}
 | 
			
		||||
			insideKeys := getAllKeys(j, keys, rooms)
 | 
			
		||||
			for k := range insideKeys {
 | 
			
		||||
					keys[k] = struct{}{}
 | 
			
		||||
			}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	return keys
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue