Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/main/java/g0301_0400/s0307_range_sum_query_mutable/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
307\. Range Sum Query - Mutable

Medium

Given an integer array `nums`, handle multiple queries of the following types:

1. **Update** the value of an element in `nums`.
2. Calculate the **sum** of the elements of `nums` between indices `left` and `right` **inclusive** where `left <= right`.

Implement the `NumArray` class:

* `NumArray(int[] nums)` Initializes the object with the integer array `nums`.
* `void update(int index, int val)` **Updates** the value of `nums[index]` to be `val`.
* `int sumRange(int left, int right)` Returns the **sum** of the elements of `nums` between indices `left` and `right` **inclusive** (i.e. `nums[left] + nums[left + 1] + ... + nums[right]`).

**Example 1:**

**Input**

["NumArray", "sumRange", "update", "sumRange"]
[[[1, 3, 5]], [0, 2], [1, 2], [0, 2]]

**Output:** \[null, 9, null, 8\]

**Explanation:**

NumArray numArray = new NumArray([1, 3, 5]);
numArray.sumRange(0, 2); // return 1 + 3 + 5 = 9
numArray.update(1, 2); // nums = [1, 2, 5]
numArray.sumRange(0, 2); // return 1 + 2 + 5 = 8

**Constraints:**

* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
* `-100 <= nums[i] <= 100`
* `0 <= index < nums.length`
* `-100 <= val <= 100`
* `0 <= left <= right < nums.length`
* At most <code>3 * 10<sup>4</sup></code> calls will be made to `update` and `sumRange`.
1 change: 0 additions & 1 deletion src/test/java/com_github_leetcode/ArrayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;

public class ArrayUtils {

private ArrayUtils() {}

public static List<List<Integer>> getLists(int[][] expected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class SolutionTest {
@Test
public void trappingRainWater() {
public void trap() {
assertThat(new Solution().trap(new int[] {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}), equalTo(6));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class SolutionTest {
@Test
public void multiplyStrings() {
public void multiply() {
assertThat(new Solution().multiply("2", "3"), equalTo("6"));
}
}
2 changes: 0 additions & 2 deletions src/test/java/g0001_0100/s0048_rotate_image/SolutionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ public class SolutionTest {
public void rotate() {
int[][] expected = new int[][] {{7, 4, 1}, {8, 5, 2}, {9, 6, 3}};
int[][] input = new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

new Solution().rotate(input);

assertThat(input, equalTo(expected));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ public void groupAnagrams() {
expected.add(Arrays.asList("eat", "tea", "ate"));
expected.add(Arrays.asList("bat"));
expected.add(Arrays.asList("tan", "nat"));

List<List<String>> actual =
new Solution()
.groupAnagrams(new String[] {"eat", "tea", "tan", "ate", "nat", "bat"});

assertThat(actual, equalTo(expected));
}
}
2 changes: 1 addition & 1 deletion src/test/java/g0001_0100/s0050_powx_n/SolutionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class SolutionTest {
@Test
public void powxN() {
public void myPow() {
assertThat(new Solution().myPow(2.00000, 10), equalTo(1024.00000));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

public class SolutionTest {
@Test
public void mergeIntervals() {
public void merge() {
int[][] input = {{1, 3}, {2, 6}, {8, 10}, {15, 18}};
int[][] expected = {{1, 6}, {8, 10}, {15, 18}};
int[][] actual = new Solution().merge(input);

assertThat(actual, equalTo(expected));
assertThat(new Solution().merge(input), equalTo(expected));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

public class SolutionTest {
@Test
public void insertInterval() {
public void insert() {
int[][] expected = {{1, 5}, {6, 9}};
int[][] actual = new Solution().insert(new int[][] {{1, 3}, {6, 9}}, new int[] {2, 5});

assertThat(actual, equalTo(expected));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
public class SolutionTest {
@Test
public void generateMatrix() {
int[][] actual = new Solution().generateMatrix(3);
int[][] expected = {{1, 2, 3}, {8, 9, 4}, {7, 6, 5}};
assertThat(actual, equalTo(expected));
assertThat(new Solution().generateMatrix(3), equalTo(expected));
}
}
3 changes: 1 addition & 2 deletions src/test/java/g0001_0100/s0061_rotate_list/SolutionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

public class SolutionTest {
@Test
public void rotateList() {
public void rotateRight() {
ListNode headActual = new ListNode(1);
headActual.next = new ListNode(2);
headActual.next.next = new ListNode(3);
headActual.next.next.next = new ListNode(4);
headActual.next.next.next.next = new ListNode(5);

assertThat(new Solution().rotateRight(headActual, 2).toString(), equalTo("4, 5, 1, 2, 3"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class SolutionTest {
@Test
public void minimumPathSum() {
public void minPathSum() {
int[][] expected = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}};
assertThat(new Solution().minPathSum(expected), equalTo(7));
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/g0001_0100/s0069_sqrtx/SolutionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class SolutionTest {
@Test
public void sqrtx() {
public void mySqrt() {
assertThat(new Solution().mySqrt(4), equalTo(2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
public class SolutionTest {
@Test
public void searchMatrix() {
assertThat(
new Solution()
.searchMatrix(
new int[][] {{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 60}}, 3),
equalTo(true));
int[][] input = {{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 60}};
assertThat(new Solution().searchMatrix(input, 3), equalTo(true));
}
}
10 changes: 2 additions & 8 deletions src/test/java/g0001_0100/s0079_submissions/SolutionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@
public class SolutionTest {
@Test
public void exist() {
assertThat(
new Solution()
.exist(
new char[][] {
{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}
},
"ABCCED"),
equalTo(true));
char[][] input = {{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}};
assertThat(new Solution().exist(input, "ABCCED"), equalTo(true));
}
}