Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Segment & Fenwick trees

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Segment & Fenwick trees

Range Sum Query 2D — Mutable

advancedlayer A6 · Segment & Fenwick trees5 tests

Build a NumMatrix class over a rectangular grid of numbers that supports point updates and rectangle-sum queries.

  • new NumMatrix(matrix) — takes a copy of the grid
  • update(row, col, val) — replaces that cell's value
  • sumRegion(row1, col1, row2, col2) — the sum of the rectangle with those inclusive corners, where row1 <= row2 and col1 <= col2
  • Both operations should be O(log rows * log cols)
  • Values may be negative

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen