LeetCode-Swift icon indicating copy to clipboard operation
LeetCode-Swift copied to clipboard

[Solution] 303. Range Sum Query - Immutable

Open iosconstantine opened this issue 2 years ago • 0 comments

/**

  • Question Link: https://leetcode.com/problems/range-sum-query-immutable/
  • Primary idea: Calculate the sum of the elements of nums between indices left and right
  • Time Complexity: O(n), Space Complexity: O(1)

*/

class NumArray {
	var sums: [Int] = []

	init(_ nums: [Int]) {
		var currentSum = 0

		for num in nums {
			currentSum += num
			sums.append(currentSum)
		}
		print(sums)
	}

	func sumRange(_ left: Int, _ right: Int) -> Int {
		if left == 0 {
			return sums[right]
		}
		return sums[right] - sums[left-1]
	}
}

iosconstantine avatar Jun 28 '22 21:06 iosconstantine