Last active
September 24, 2023 22:03
-
-
Save themichaelyang/78797deef0e1124c8be715e59070bbcf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NumArray | |
def initialize(nums) | |
sum = 0 | |
@prefix_sums = nums.map do |x| | |
sum += x | |
end | |
@prefix_sums.prepend(0) | |
end | |
def sum_range(left, right) | |
@prefix_sums[right + 1] - @prefix_sums[left] | |
end | |
end |
Author
themichaelyang
commented
Sep 24, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment