-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2833-FurthestPointFromOrigin.go
More file actions
89 lines (76 loc) · 3.09 KB
/
2833-FurthestPointFromOrigin.go
File metadata and controls
89 lines (76 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
package main
// 2833. Furthest Point From Origin
// You are given a string moves of length n consisting only of characters 'L', 'R', and '_'.
// The string represents your movement on a number line starting from the origin 0.
// In the ith move, you can choose one of the following directions:
// move to the left if moves[i] = 'L' or moves[i] = '_'
// move to the right if moves[i] = 'R' or moves[i] = '_'
// Return the distance from the origin of the furthest point you can get to after n moves.
// Example 1:
// Input: moves = "L_RL__R"
// Output: 3
// Explanation: The furthest point we can reach from the origin 0 is point -3 through the following sequence of moves "LLRLLLR".
// Example 2:
// Input: moves = "_R__LL_"
// Output: 5
// Explanation: The furthest point we can reach from the origin 0 is point -5 through the following sequence of moves "LRLLLLL".
// Example 3:
// Input: moves = "_______"
// Output: 7
// Explanation: The furthest point we can reach from the origin 0 is point 7 through the following sequence of moves "RRRRRRR".
// Constraints:
// 1 <= moves.length == n <= 50
// moves consists only of characters 'L', 'R' and '_'.
import "fmt"
func furthestDistanceFromOrigin(moves string) int {
mp := make(map[byte]int)
for i := range moves {
mp[moves[i]]++
}
if mp['R'] > mp['L'] {
return mp['_'] + mp['R'] - mp['L']
}
return mp['_'] + mp['L'] - mp['R']
}
func furthestDistanceFromOrigin1(moves string) int {
left, right, count := 0, 0, 0
for i := range moves {
if moves[i] == 'L' {
left++
} else if moves[i]=='R' {
right++
}else{
count++
}
}
if left >= right {
return left - right + count
}
return right - left + count
}
func main() {
// Example 1:
// Input: moves = "L_RL__R"
// Output: 3
// Explanation: The furthest point we can reach from the origin 0 is point -3 through the following sequence of moves "LLRLLLR".
fmt.Println(furthestDistanceFromOrigin("L_RL__R")) // 3
// Example 2:
// Input: moves = "_R__LL_"
// Output: 5
// Explanation: The furthest point we can reach from the origin 0 is point -5 through the following sequence of moves "LRLLLLL".
fmt.Println(furthestDistanceFromOrigin("_R__LL_")) // 5
// Example 3:
// Input: moves = "_______"
// Output: 7
// Explanation: The furthest point we can reach from the origin 0 is point 7 through the following sequence of moves "RRRRRRR".
fmt.Println(furthestDistanceFromOrigin("_______")) // 7
fmt.Println(furthestDistanceFromOrigin("RRRRRRRRR")) // 9
fmt.Println(furthestDistanceFromOrigin("LLLLLLLLL")) // 9
fmt.Println(furthestDistanceFromOrigin("_________")) // 9
fmt.Println(furthestDistanceFromOrigin1("L_RL__R")) // 3
fmt.Println(furthestDistanceFromOrigin1("_R__LL_")) // 5
fmt.Println(furthestDistanceFromOrigin1("_______")) // 7
fmt.Println(furthestDistanceFromOrigin1("RRRRRRRRR")) // 9
fmt.Println(furthestDistanceFromOrigin1("LLLLLLLLL")) // 9
fmt.Println(furthestDistanceFromOrigin1("_________")) // 9
}