Skip to content

Commit 26b9e0c

Browse files
chore: add LeetCode daily solution
1 parent 8cf4ba3 commit 26b9e0c

5 files changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Furthest Point From Origin (Easy)
2+
3+
**Problem ID:** 2833
4+
**Date:** 2026-04-24
5+
**Link:** https://leetcode.com/problems/furthest-point-from-origin/
6+
7+
## Approach
8+
9+
To solve the problem of finding the furthest point from the origin based on the given movement string, we can follow a straightforward approach that involves counting possible moves in both directions.
10+
11+
### Problem Breakdown
12+
1. **Understanding Moves**:
13+
- The string consists of three types of characters:
14+
- 'L': Move left.
15+
- 'R': Move right.
16+
- '_': Move either left or right (flexible move).
17+
18+
2. **Goal**:
19+
- We want to maximize the distance from the origin (0) after interpreting the moves.
20+
21+
### Approach
22+
1. **Count Moves**:
23+
- Traverse the string and count the occurrences of 'L', 'R', and '_'.
24+
- Let:
25+
- `countL` = number of 'L'
26+
- `countR` = number of 'R'
27+
- `countUnderscore` = number of '_'
28+
29+
2. **Calculate Maximum Distance**:
30+
- The furthest point can be calculated as:
31+
- Move all 'L' left, which contributes `-countL` to the position.
32+
- Move all 'R' right, which contributes `+countR` to the position.
33+
- Use all '_' to extend either left or right.
34+
- The furthest distance from the origin can be expressed as:
35+
- `maxDistance = countL + countR + countUnderscore`
36+
- This formula accounts for moving all possible characters to maximize the distance from the origin.
37+
38+
### Complexity
39+
- **Time Complexity**: O(n), where n is the length of the string. We perform a single pass through the string to count the characters.
40+
- **Space Complexity**: O(1), as we are using a constant amount of space for counters.
41+
42+
### Conclusion
43+
By counting the moves and utilizing the flexible moves represented by '_', we can efficiently compute the maximum distance achievable from the origin. This approach ensures that we consider all possible configurations of moves while maintaining optimal performance.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int furthestDistanceFromOrigin(String moves) {
3+
int leftMoves = 0;
4+
int rightMoves = 0;
5+
int unknownMoves = 0;
6+
7+
for (char move : moves.toCharArray()) {
8+
if (move == 'L') {
9+
leftMoves++;
10+
} else if (move == 'R') {
11+
rightMoves++;
12+
} else if (move == '_') {
13+
unknownMoves++;
14+
}
15+
}
16+
17+
int maxDistance = Math.abs((leftMoves + unknownMoves) - rightMoves);
18+
maxDistance = Math.max(maxDistance, Math.abs(leftMoves - (rightMoves + unknownMoves)));
19+
20+
return maxDistance;
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var furthestDistanceFromOrigin = function(moves) {
2+
let leftMoves = 0;
3+
let rightMoves = 0;
4+
let underscoreCount = 0;
5+
6+
for (let move of moves) {
7+
if (move === 'L') {
8+
leftMoves++;
9+
} else if (move === 'R') {
10+
rightMoves++;
11+
} else if (move === '_') {
12+
underscoreCount++;
13+
}
14+
}
15+
16+
return Math.abs(leftMoves - rightMoves) + underscoreCount;
17+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def furthestDistanceFromOrigin(self, moves: str) -> int:
3+
left_moves = moves.count('L')
4+
right_moves = moves.count('R')
5+
underscore_moves = moves.count('_')
6+
7+
return left_moves + right_moves + underscore_moves

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
433433
- 2026-04-21 — [Minimize Hamming Distance After Swap Operations](https://leetcode.com/problems/minimize-hamming-distance-after-swap-operations/) (Medium) → `Medium/2026-04-21-1722-Minimize-Hamming-Distance-After-Swap-Operations`
434434
- 2026-04-22 — [Words Within Two Edits of Dictionary](https://leetcode.com/problems/words-within-two-edits-of-dictionary/) (Medium) → `Medium/2026-04-22-2452-Words-Within-Two-Edits-of-Dictionary`
435435
- 2026-04-23 — [Sum of Distances](https://leetcode.com/problems/sum-of-distances/) (Medium) → `Medium/2026-04-23-2615-Sum-of-Distances`
436+
- 2026-04-24 — [Furthest Point From Origin](https://leetcode.com/problems/furthest-point-from-origin/) (Easy) → `Easy/2026-04-24-2833-Furthest-Point-From-Origin`

0 commit comments

Comments
 (0)