Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Dec 20, 2024
1 parent 13abe01 commit c410517
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
# Task

Given an array arr of 4 digits, find the latest 24-hour time that can be made using each digit exactly once.

24-hour times are formatted as "HH:MM", where HH is between 00 and 23, and MM is between 00 and 59. The earliest 24-hour time is 00:00, and the latest is 23:59.

Return the latest 24-hour time in "HH:MM" format. If no valid time can be made, return an empty string.

## Examples

### Example 1:

Example 1:

Input: arr = [1,2,3,4]
Output: "23:41"
Explanation: The valid 24-hour times are "12:34", "12:43", "13:24", "13:42", "14:23", "14:32", "21:34", "21:43", "23:14", and "23:41". Of these times, "23:41" is the latest.
Example 2:
Input: `arr = [1,2,3,4]`
Output: `23:41`
Explanation: The valid 24-hour times are "12:34", "12:43", "13:24", "13:42", "14:23", "14:32", "21:34", "21:43", "23:14", and "23:41". Of these times, "23:41" is the latest.

Input: arr = [5,5,5,5]
Output: ""
Explanation: There are no valid 24-hour times as "55:55" is not valid.
### Example 2:

Input: `arr = [5,5,5,5]`
Output: an empty string
Explanation: There are no valid 24-hour times as "55:55" is not valid.

Constraints:
## Constraints:

arr.length == 4
0 <= arr[i] <= 9
`arr.length == 4`
`0 <= arr[i] <= 9`
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
# Task

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

Update the value of an element in nums.
Calculate the sum of the elements of nums between indices left and right inclusive where left <= right.
Implement the NumArray class:
- Update the value of an element in nums.
- 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]).
`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]).

## Examples

Example 1:
### Example 1:

Input
["NumArray", "sumRange", "update", "sumRange"]
[[[1, 3, 5]], [0, 2], [1, 2], [0, 2]]
Output
[null, 9, null, 8]
**Input:**
`["NumArray", "sumRange", "update", "sumRange"]`
`[[[1, 3, 5]], [0, 2], [1, 2], [0, 2]]`

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
**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:
## Constraints:

1 <= nums.length <= 3 * 104
-100 <= nums[i] <= 100
0 <= index < nums.length
-100 <= val <= 100
0 <= left <= right < nums.length
At most 3 * 104 calls will be made to update and sumRange.
`1 <= nums.length <= 3 * 10^4`
`-100 <= nums[i] <= 100`
`0 <= index < nums.length`
`-100 <= val <= 100`
`0 <= left <= right < nums.length`
At most `3 * 10^4` calls will be made to update and sumRange.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
namespace LeetCode.Challenges.Assessments.Range_Sum_Query___Mutable;

// A NumArray object will be instantiated and called as such:
// - NumArray obj = new NumArray(nums);
// - obj.Update(index,val);
// - int param_2 = obj.SumRange(left,right);
public class NumArray
{
private readonly int[] segmentTree;
Expand All @@ -14,50 +18,50 @@ public NumArray(int[] nums)

private void BuildSegmentTree(int[] nums)
{
// Initialize leaves of the segment tree
for (int i = 0; i < this.n; i++)
// Initialize leaves of the segment tree.
for (var i = 0; i < this.n; i++)
{
this.segmentTree[this.n + i] = nums[i];
}

// Build the rest of the tree by calculating parents
for (int i = this.n - 1; i > 0; i--)
// Build the rest of the tree by calculating parents.
for (var i = this.n - 1; i > 0; i--)
{
this.segmentTree[i] = this.segmentTree[2 * i] + this.segmentTree[2 * i + 1];
}
}

public void Update(int index, int val)
{
// Update the value at the leaf
int pos = index + n;
segmentTree[pos] = val;
// Update the value at the leaf.
var pos = index + this.n;
this.segmentTree[pos] = val;

// Recalculate the sum for the parent nodes
// Recalculate the sum for the parent nodes.
while (pos > 1)
{
pos /= 2;
segmentTree[pos] = segmentTree[2 * pos] + segmentTree[2 * pos + 1];
this.segmentTree[pos] = this.segmentTree[2 * pos] + this.segmentTree[2 * pos + 1];
}
}

public int SumRange(int left, int right)
{
int sum = 0;
var sum = 0;
left += n;
right += n;

while (left <= right)
{
if (left % 2 == 1)
{
sum += segmentTree[left];
sum += this.segmentTree[left];
left++;
}

if (right % 2 == 0)
{
sum += segmentTree[right];
sum += this.segmentTree[right];
right--;
}

Expand All @@ -68,11 +72,3 @@ public int SumRange(int left, int right)
return sum;
}
}

/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* obj.Update(index,val);
* int param_2 = obj.SumRange(left,right);
*/

0 comments on commit c410517

Please sign in to comment.