Skip to content

Commit 32944fe

Browse files
authored
feat: add solutions to lc problems: No.2833~2836 (#1519)
* No.2833.Furthest Point From Origin * No.2834.Find the Minimum Possible Sum of a Beautiful Array * No.2835.Minimum Operations to Form Subsequence With Target Sum * No.2836.Maximize Value of Function in a Ball Passing Game
1 parent f81a32d commit 32944fe

File tree

34 files changed

+2244
-0
lines changed

34 files changed

+2244
-0
lines changed

solution/1400-1499/1483.Kth Ancestor of a Tree Node/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ $$
7373

7474
时间复杂度方面,初始化为 $O(n \times \log n)$,查询为 $O(\log n)$。空间复杂度 $O(n \times \log n)$。其中 $n$ 为树的节点数。
7575

76+
相似题目:
77+
78+
- [2836. 在传球游戏中最大化函数值](/solution/2800-2899/2836.Maximize%20Value%20of%20Function%20in%20a%20Ball%20Passing%20Game/README.md)
79+
7680
<!-- tabs:start -->
7781

7882
### **Python3**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# [2833. 距离原点最远的点](https://leetcode.cn/problems/furthest-point-from-origin)
2+
3+
[English Version](/solution/2800-2899/2833.Furthest%20Point%20From%20Origin/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个长度为 <code>n</code> 的字符串 <code>moves</code> ,该字符串仅由字符 <code>'L'</code>、<code>'R'</code> 和 <code>'_'</code> 组成。字符串表示你在一条原点为 <code>0</code> 的数轴上的若干次移动。</p>
10+
11+
<p>你的初始位置就在原点(<code>0</code>),第 <code>i</code> 次移动过程中,你可以根据对应字符选择移动方向:</p>
12+
13+
<ul>
14+
<li>如果 <code>moves[i] = 'L'</code> 或 <code>moves[i] = '_'</code> ,可以选择向左移动一个单位距离</li>
15+
<li>如果 <code>moves[i] = 'R'</code> 或 <code>moves[i] = '_'</code> ,可以选择向右移动一个单位距离</li>
16+
</ul>
17+
18+
<p>移动 <code>n</code> 次之后,请你找出可以到达的距离原点 <strong>最远</strong> 的点,并返回 <strong>从原点到这一点的距离</strong> 。</p>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong>示例 1:</strong></p>
23+
24+
<pre>
25+
<strong>输入:</strong>moves = "L_RL__R"
26+
<strong>输出:</strong>3
27+
<strong>解释:</strong>可以到达的距离原点 0 最远的点是 -3 ,移动的序列为 "LLRLLLR" 。
28+
</pre>
29+
30+
<p><strong>示例 2:</strong></p>
31+
32+
<pre>
33+
<strong>输入:</strong>moves = "_R__LL_"
34+
<strong>输出:</strong>5
35+
<strong>解释:</strong>可以到达的距离原点 0 最远的点是 -5 ,移动的序列为 "LRLLLLL" 。
36+
</pre>
37+
38+
<p><strong>示例 3:</strong></p>
39+
40+
<pre>
41+
<strong>输入:</strong>moves = "_______"
42+
<strong>输出:</strong>7
43+
<strong>解释:</strong>可以到达的距离原点 0 最远的点是 7 ,移动的序列为 "RRRRRRR" 。
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
48+
<p><strong>提示:</strong></p>
49+
50+
<ul>
51+
<li><code>1 &lt;= moves.length == n &lt;= 50</code></li>
52+
<li><code>moves</code> 仅由字符 <code>'L'</code>、<code>'R'</code> 和 <code>'_'</code> 组成</li>
53+
</ul>
54+
55+
## 解法
56+
57+
<!-- 这里可写通用的实现逻辑 -->
58+
59+
**方法一:贪心**
60+
61+
遇到字符 `'_'` 时,我们可以选择向左或向右移动,而题目需要我们求出离原点最远的点,因此,我们可以先进行一次遍历,贪心地把所有的 `'_'` 都移到左边,求出此时离原点最远的点,再进行一次遍历,贪心地把所有的 `'_'` 都移到右边,求出此时离原点最远的点,最后取两次遍历中的最大值即可。
62+
63+
进一步地,我们只需要统计出字符串中 `'L'``'R'` 的个数之差,再加上 `'_'` 的个数即可。
64+
65+
时间复杂度 $O(n)$,其中 $n$ 为字符串的长度。空间复杂度 $O(1)$。
66+
67+
<!-- tabs:start -->
68+
69+
### **Python3**
70+
71+
<!-- 这里可写当前语言的特殊实现逻辑 -->
72+
73+
```python
74+
class Solution:
75+
def furthestDistanceFromOrigin(self, moves: str) -> int:
76+
return abs(moves.count("L") - moves.count("R")) + moves.count("_")
77+
```
78+
79+
### **Java**
80+
81+
<!-- 这里可写当前语言的特殊实现逻辑 -->
82+
83+
```java
84+
class Solution {
85+
public int furthestDistanceFromOrigin(String moves) {
86+
return Math.abs(count(moves, 'L') - count(moves, 'R')) + count(moves, '_');
87+
}
88+
89+
private int count(String s, char c) {
90+
int cnt = 0;
91+
for (int i = 0; i < s.length(); ++i) {
92+
if (s.charAt(i) == c) {
93+
++cnt;
94+
}
95+
}
96+
return cnt;
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
int furthestDistanceFromOrigin(string moves) {
107+
auto cnt = [&](char c) {
108+
return count(moves.begin(), moves.end(), c);
109+
};
110+
return abs(cnt('L') - cnt('R')) + cnt('_');
111+
}
112+
};
113+
```
114+
115+
### **Go**
116+
117+
```go
118+
func furthestDistanceFromOrigin(moves string) int {
119+
count := func(c string) int { return strings.Count(moves, c) }
120+
return abs(count("L")-count("R")) + count("_")
121+
}
122+
123+
func abs(x int) int {
124+
if x < 0 {
125+
return -x
126+
}
127+
return x
128+
}
129+
```
130+
131+
### **TypeScript**
132+
133+
```ts
134+
function furthestDistanceFromOrigin(moves: string): number {
135+
const count = (c: string) => moves.split('').filter(x => x === c).length;
136+
return Math.abs(count('L') - count('R')) + count('_');
137+
}
138+
```
139+
140+
### **...**
141+
142+
```
143+
144+
```
145+
146+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, and <code>&#39;_&#39;</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] = &#39;L&#39;</code> or <code>moves[i] = &#39;_&#39;</code></li>
13+
<li>move to the right if <code>moves[i] = &#39;R&#39;</code> or <code>moves[i] = &#39;_&#39;</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>&nbsp;</p>
19+
<p><strong class="example">Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> moves = &quot;L_RL__R&quot;
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 &quot;LLRLLLR&quot;.
25+
</pre>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> moves = &quot;_R__LL_&quot;
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 &quot;LRLLLLL&quot;.
33+
</pre>
34+
35+
<p><strong class="example">Example 3:</strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> moves = &quot;_______&quot;
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 &quot;RRRRRRR&quot;.
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= moves.length == n &lt;= 50</code></li>
48+
<li><code>moves</code> consists only of characters <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code> and <code>&#39;_&#39;</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 -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
int furthestDistanceFromOrigin(string moves) {
4+
auto cnt = [&](char c) {
5+
return count(moves.begin(), moves.end(), c);
6+
};
7+
return abs(cnt('L') - cnt('R')) + cnt('_');
8+
}
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func furthestDistanceFromOrigin(moves string) int {
2+
count := func(c string) int { return strings.Count(moves, c) }
3+
return abs(count("L")-count("R")) + count("_")
4+
}
5+
6+
func abs(x int) int {
7+
if x < 0 {
8+
return -x
9+
}
10+
return x
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int furthestDistanceFromOrigin(String moves) {
3+
return Math.abs(count(moves, 'L') - count(moves, 'R')) + count(moves, '_');
4+
}
5+
6+
private int count(String s, char c) {
7+
int cnt = 0;
8+
for (int i = 0; i < s.length(); ++i) {
9+
if (s.charAt(i) == c) {
10+
++cnt;
11+
}
12+
}
13+
return cnt;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def furthestDistanceFromOrigin(self, moves: str) -> int:
3+
return abs(moves.count("L") - moves.count("R")) + moves.count("_")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function furthestDistanceFromOrigin(moves: string): number {
2+
const count = (c: string) => moves.split('').filter(x => x === c).length;
3+
return Math.abs(count('L') - count('R')) + count('_');
4+
}

0 commit comments

Comments
 (0)