-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2728-CountHousesInACircularStreet.go
More file actions
109 lines (95 loc) · 3.09 KB
/
2728-CountHousesInACircularStreet.go
File metadata and controls
109 lines (95 loc) · 3.09 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
package main
// 2728. Count Houses in a Circular Street
// You are given an object street of class Street that represents a circular street
// and a positive integer k which represents a maximum bound for the number of houses in that street (in other words, the number of houses is less than or equal to k).
// Houses' doors could be open or closed initially.
// Initially, you are standing in front of a door to a house on this street.
// Your task is to count the number of houses in the street.
// The class Street contains the following functions which may help you:
// void openDoor(): Open the door of the house you are in front of.
// void closeDoor(): Close the door of the house you are in front of.
// boolean isDoorOpen(): Returns true if the door of the current house is open and false otherwise.
// void moveRight(): Move to the right house.
// void moveLeft(): Move to the left house.
// Return ans which represents the number of houses on this street.
// Example 1:
// Input: street = [0,0,0,0], k = 10
// Output: 4
// Explanation: There are 4 houses, and all their doors are closed.
// The number of houses is less than k, which is 10.
// Example 2:
// Input: street = [1,0,1,1,0], k = 5
// Output: 5
// Explanation: There are 5 houses, and the doors of the 1st, 3rd, and 4th house (moving in the right direction) are open, and the rest are closed.
// The number of houses is equal to k, which is 5.
// Constraints:
// n == number of houses
// 1 <= n <= k <= 10^3
import "fmt"
type Street interface {
OpenDoor()
CloseDoor()
IsDoorOpen() bool
MoveRight()
MoveLeft()
}
type StreetService struct {
data []int
}
func NewStreet(arr []int) Street {
return Street{ data: arr}
}
/**
* Definition for a street.
* type Street interface {
* OpenDoor()
* CloseDoor()
* IsDoorOpen() bool
* MoveRight()
* MoveLeft()
* }
*/
func houseCount(street Street, k int) int {
if street.IsDoorOpen() {
street.CloseDoor()
}
res, i := 0, 0
for i < k {
street.MoveRight()
i++
if !street.IsDoorOpen() {
res = i
street.OpenDoor()
}
}
return res
}
func houseCount1(street Street, k int) int {
for i := 0; i < k; i++ {
street.CloseDoor()
street.MoveRight()
}
res := 1
street.OpenDoor()
street.MoveRight()
for !street.IsDoorOpen() {
street.MoveRight()
res++
}
return res
}
func main() {
// Example 1:
// Input: street = [0,0,0,0], k = 10
// Output: 4
// Explanation: There are 4 houses, and all their doors are closed.
// The number of houses is less than k, which is 10.
//fmt.Println(houseCount(NewStreet([]int{0,0,0,0}, 10))) // 4
// Example 2:
// Input: street = [1,0,1,1,0], k = 5
// Output: 5
// Explanation: There are 5 houses, and the doors of the 1st, 3rd, and 4th house (moving in the right direction) are open, and the rest are closed.
// The number of houses is equal to k, which is 5.
//fmt.Println(houseCount(NewStreet([]int{1,0,1,1,0}, 5))) // 5
fmt.Println()
}