|
| 1 | +# [2833. Furthest Point From Origin](https://leetcode.com/problems/furthest-point-from-origin) |
| 2 | + |
| 3 | +[中文文档](/solution/2800-2899/2833.Furthest%20Point%20From%20Origin/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>You are given a string <code>moves</code> of length <code>n</code> consisting only of characters <code>'L'</code>, <code>'R'</code>, and <code>'_'</code>. The string represents your movement on a number line starting from the origin <code>0</code>.</p> |
| 8 | + |
| 9 | +<p>In the <code>i<sup>th</sup></code> move, you can choose one of the following directions:</p> |
| 10 | + |
| 11 | +<ul> |
| 12 | + <li>move to the left if <code>moves[i] = 'L'</code> or <code>moves[i] = '_'</code></li> |
| 13 | + <li>move to the right if <code>moves[i] = 'R'</code> or <code>moves[i] = '_'</code></li> |
| 14 | +</ul> |
| 15 | + |
| 16 | +<p>Return <em>the <strong>distance from the origin</strong> of the <strong>furthest</strong> point you can get to after </em><code>n</code><em> moves</em>.</p> |
| 17 | + |
| 18 | +<p> </p> |
| 19 | +<p><strong class="example">Example 1:</strong></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | +<strong>Input:</strong> moves = "L_RL__R" |
| 23 | +<strong>Output:</strong> 3 |
| 24 | +<strong>Explanation:</strong> The furthest point we can reach from the origin 0 is point -3 through the following sequence of moves "LLRLLLR". |
| 25 | +</pre> |
| 26 | + |
| 27 | +<p><strong class="example">Example 2:</strong></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<strong>Input:</strong> moves = "_R__LL_" |
| 31 | +<strong>Output:</strong> 5 |
| 32 | +<strong>Explanation:</strong> The furthest point we can reach from the origin 0 is point -5 through the following sequence of moves "LRLLLLL". |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p><strong class="example">Example 3:</strong></p> |
| 36 | + |
| 37 | +<pre> |
| 38 | +<strong>Input:</strong> moves = "_______" |
| 39 | +<strong>Output:</strong> 7 |
| 40 | +<strong>Explanation:</strong> The furthest point we can reach from the origin 0 is point 7 through the following sequence of moves "RRRRRRR". |
| 41 | +</pre> |
| 42 | + |
| 43 | +<p> </p> |
| 44 | +<p><strong>Constraints:</strong></p> |
| 45 | + |
| 46 | +<ul> |
| 47 | + <li><code>1 <= moves.length == n <= 50</code></li> |
| 48 | + <li><code>moves</code> consists only of characters <code>'L'</code>, <code>'R'</code> and <code>'_'</code>.</li> |
| 49 | +</ul> |
| 50 | + |
| 51 | +## Solutions |
| 52 | + |
| 53 | +<!-- tabs:start --> |
| 54 | + |
| 55 | +### **Python3** |
| 56 | + |
| 57 | +```python |
| 58 | +class Solution: |
| 59 | + def furthestDistanceFromOrigin(self, moves: str) -> int: |
| 60 | + return abs(moves.count("L") - moves.count("R")) + moves.count("_") |
| 61 | +``` |
| 62 | + |
| 63 | +### **Java** |
| 64 | + |
| 65 | +```java |
| 66 | +class Solution { |
| 67 | + public int furthestDistanceFromOrigin(String moves) { |
| 68 | + return Math.abs(count(moves, 'L') - count(moves, 'R')) + count(moves, '_'); |
| 69 | + } |
| 70 | + |
| 71 | + private int count(String s, char c) { |
| 72 | + int cnt = 0; |
| 73 | + for (int i = 0; i < s.length(); ++i) { |
| 74 | + if (s.charAt(i) == c) { |
| 75 | + ++cnt; |
| 76 | + } |
| 77 | + } |
| 78 | + return cnt; |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +### **C++** |
| 84 | + |
| 85 | +```cpp |
| 86 | +class Solution { |
| 87 | +public: |
| 88 | + int furthestDistanceFromOrigin(string moves) { |
| 89 | + auto cnt = [&](char c) { |
| 90 | + return count(moves.begin(), moves.end(), c); |
| 91 | + }; |
| 92 | + return abs(cnt('L') - cnt('R')) + cnt('_'); |
| 93 | + } |
| 94 | +}; |
| 95 | +``` |
| 96 | +
|
| 97 | +### **Go** |
| 98 | +
|
| 99 | +```go |
| 100 | +func furthestDistanceFromOrigin(moves string) int { |
| 101 | + count := func(c string) int { return strings.Count(moves, c) } |
| 102 | + return abs(count("L")-count("R")) + count("_") |
| 103 | +} |
| 104 | +
|
| 105 | +func abs(x int) int { |
| 106 | + if x < 0 { |
| 107 | + return -x |
| 108 | + } |
| 109 | + return x |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +### **TypeScript** |
| 114 | + |
| 115 | +```ts |
| 116 | +function furthestDistanceFromOrigin(moves: string): number { |
| 117 | + const count = (c: string) => moves.split('').filter(x => x === c).length; |
| 118 | + return Math.abs(count('L') - count('R')) + count('_'); |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +### **...** |
| 123 | + |
| 124 | +``` |
| 125 | +
|
| 126 | +``` |
| 127 | + |
| 128 | +<!-- tabs:end --> |
0 commit comments