leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 6cb05c1fb3abea9ff6efc61065bae148092394d9
parent d085e18b05173ffc4bd1ee5776c77b9bb561b59b
Author: Jack Sangdahl <jack@pngu.org>
Date:   Thu, 23 Apr 2026 21:05:05 -0600

2833. furthest point from origin

Diffstat:
Afurthest_point_from_origin.c | 35+++++++++++++++++++++++++++++++++++
Afurthest_point_from_origin.py | 22++++++++++++++++++++++
2 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/furthest_point_from_origin.c b/furthest_point_from_origin.c @@ -0,0 +1,35 @@ +// 2833. Furthest Point From Origin +#include "leetcode.h" + +/* + * 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. in the i'th move, you can choose one of the following directions: + * move to the left if 'moves[i] = L' or 'moves[i] = _', and move to the right + * 'moves[i] = R' or 'moves[i] = _'. return the distance from the origin of the + * furthest point you can get to after 'n' moves. + */ + +int furthestDistanceFromOrigin(char *moves) { + int l = 0, r = 0, n = strlen(moves); + for (int i = 0; i < n; i++) { + l += (moves[i] == 'R') - (moves[i] == 'L'); + r += moves[i] == '_'; + } + return abs(l) + r; +} + +int main() { + char *m1 = "L_RL__R"; + char *m2 = "_R__LL_"; + char *m3 = "_______"; + int r1 = furthestDistanceFromOrigin(m1); + int r2 = furthestDistanceFromOrigin(m2); + int r3 = furthestDistanceFromOrigin(m3); + printf("%d\n", r1); + assert(r1 == 3); + printf("%d\n", r2); + assert(r2 == 5); + printf("%d\n", r3); + assert(r3 == 7); +} diff --git a/furthest_point_from_origin.py b/furthest_point_from_origin.py @@ -0,0 +1,22 @@ +# 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. in the i'th move, you can choose one of the following directions: +move to the left if 'moves[i] = L' or 'moves[i] = _', and move to the right +'moves[i] = R' or 'moves[i] = _'. return the distance from the origin of the +furthest point you can get to after 'n' moves. +""" + + +class Solution: + def furthestDistanceFromOrigin(self, moves: str) -> int: + return abs(sum((c == "R") - (c == "L") for c in moves)) + moves.count("_") + + +if __name__ == "__main__": + obj = Solution() + print(obj.furthestDistanceFromOrigin(moves="L_RL__R")) + print(obj.furthestDistanceFromOrigin(moves="_R__LL_")) + print(obj.furthestDistanceFromOrigin(moves="_______"))