This set of tasks evaluates array-based greedy reasoning for circular route feasibility, stack-based parsing for bracket matching, and grid/graph reachability under resource constraints, testing algorithmic problem solving, state management, and implementation accuracy.
You are given three separate coding tasks (solve each independently). Provide an algorithm and implement it.
You are given two integer arrays gas[0..n-1] and cost[0..n-1] describing a circular route of n stations.
i
, you can add
gas[i]
units of fuel.
i
to station
(i+1) mod n
consumes
cost[i]
units of fuel.
Task: Return an index s such that starting at station s allows you to complete one full loop and return to s without the fuel ever going negative. If it is impossible, return -1. If multiple answers exist, returning any one is acceptable.
Constraints (typical): 1 <= n <= 2e5, 0 <= gas[i], cost[i] <= 1e9.
Given a string s consisting only of the characters '(', ')', '{', '}', '[', ']'.
Task: Determine whether s is a valid bracket sequence:
Return true if valid, otherwise false.
Constraints (typical): 0 <= |s| <= 2e5.
You are given:
G
= initial fuel in the tank.
R x C
with cells of the following types:
S
: start cell
T
: target cell
.
: free cell (no fuel gained)
#
: obstacle (cannot enter)
'0'
–
'9'
: a refuel cell; when you enter this cell, your fuel increases by that digit value (fuel gain happens upon entering).
Movement rules:
Task: Return whether it is possible to reach T from S.
Clarifications to make explicit in your solution:
Constraints (typical): 1 <= R,C <= 200, 0 <= G <= 1e5.