Skip to content

Commit

Permalink
added Segment Trees
Browse files Browse the repository at this point in the history
  • Loading branch information
devangi2000 committed Jan 4, 2022
1 parent 73fbe34 commit 93d40e6
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Bit-Manipulation/NumberComplement476.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation.

// For example, The integer 5 is "101" in binary and its complement is "010" which is the integer 2.
// Given an integer num, return its complement.



// Example 1:

// Input: num = 5
// Output: 2
// Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
// Example 2:

// Input: num = 1
// Output: 0
// Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.


// Constraints:
// 1 <= num < 231


// Note: This question is the same as 1009: https://leetcode.com/problems/complement-of-base-10-integer/

class Solution {
public:
int findComplement(int num) {
if(num == 0) return 1;
int temp = num, bits = 1;
while(temp){
num = num ^ bits;
temp >>= 1;
bits <<= 1;
}
return num;
}
};

38 changes: 38 additions & 0 deletions Bit-Manipulation/NumberComplement476.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation.

// For example, The integer 5 is "101" in binary and its complement is "010" which is the integer 2.
// Given an integer num, return its complement.



// Example 1:

// Input: num = 5
// Output: 2
// Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
// Example 2:

// Input: num = 1
// Output: 0
// Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.


// Constraints:

// 1 <= num < 231


// Note: This question is the same as 1009: https://leetcode.com/problems/complement-of-base-10-integer/

class Solution {
public int findComplement(int num) {
if(num == 0) return 1;
int temp = num, bits = 1;
while(temp > 0){
num = num ^ bits;
temp >>= 1;
bits <<= 1;
}
return num;
}
}
108 changes: 108 additions & 0 deletions Segment Trees/RangeSumQueryMutable-307.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// 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:

// 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:

// 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.

struct SegmentTreeNode{
int start, end, sum;
SegmentTreeNode *left, *right;

SegmentTreeNode(int s, int e):
start(s), end(e), sum(0), left(NULL), right(NULL){}
};

class NumArray {
public:
SegmentTreeNode* root = NULL;
NumArray(vector<int>& nums) {
root = buildTree(nums, 0, nums.size() - 1);
}

SegmentTreeNode* buildTree(vector<int> &nums, int start, int end){
if(start > end) return NULL;
else{
SegmentTreeNode* node = new SegmentTreeNode(start, end);
if(start == end)
node->sum = nums[start];
else{
int mid = start + (end - start) / 2;
node->left = buildTree(nums, start, mid);
node->right = buildTree(nums, mid + 1, end);
node->sum = node->left->sum + node->right->sum;
}
return node;
}
}

void update(int index, int val) {
updateTree(root, index, val);
}

void updateTree(SegmentTreeNode* root, int pos, int val){
if(root->start == root->end)
root->sum = val;
else{
int mid = root->start + (root->end - root->start)/2;
if(pos <= mid)
updateTree(root->left, pos, val);
else
updateTree(root->right, pos, val);
root->sum = root->left->sum + root->right->sum;
}
}

int sumRange(int left, int right) {
return sumRangeHelper(root, left, right);
}

int sumRangeHelper(SegmentTreeNode* root, int start, int end){
if(root->start == start and root->end == end)
return root->sum;
else{
int mid = root->start + (root->end - root->start) / 2;
if(end <= mid)
return sumRangeHelper(root->left, start, end);
else if(start >= mid + 1)
return sumRangeHelper(root->right, start, end);
else
return sumRangeHelper(root->left, start, mid) + sumRangeHelper(root->right, mid + 1, end);
}
}
};

/**
* 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);
*/
116 changes: 116 additions & 0 deletions Segment Trees/RangeSumQueryMutable-307.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// 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:

// 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:

// 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.

class NumArray {

class SegmentTreeNode {
int start, end;
SegmentTreeNode left, right;
int sum;

public SegmentTreeNode(int start, int end) {
this.start = start;
this.end = end;
this.left = null;
this.right = null;
this.sum = 0;
}
}

SegmentTreeNode root = null;

public NumArray(int[] nums) {
root = buildTree(nums, 0, nums.length - 1);
}

private SegmentTreeNode buildTree(int[] nums, int start, int end){
if(start > end) return null;
else {
SegmentTreeNode node = new SegmentTreeNode(start, end);
if(start == end)
node.sum = nums[start];
else{
int mid = start + (end - start) / 2;
node.left = buildTree(nums, start, mid);
node.right = buildTree(nums, mid + 1, end);
node.sum = node.left.sum + node.right.sum;
}
return node;
}
}

public void update(int index, int val) {
updateTree(root, index, val);
}

public void updateTree(SegmentTreeNode root, int pos, int val){
if(root.start == root.end)
root.sum = val;
else{
int mid = root.start + (root.end - root.start)/2;
if(pos <= mid)
updateTree(root.left, pos, val);
else
updateTree(root.right, pos, val);

root.sum = root.left.sum + root.right.sum;
}
}

public int sumRange(int left, int right) {
return sumRangeHelper(root, left, right);
}

public int sumRangeHelper(SegmentTreeNode root, int start, int end){
if(root.end == end && root.start == start)
return root.sum;
else{
int mid = root.start + (root.end - root.start)/2;
if(end <= mid)
return sumRangeHelper(root.left, start, end);
else if(start >= mid + 1)
return sumRangeHelper(root.right, start, end);
else
return sumRangeHelper(root.left, start, mid) + sumRangeHelper(root.right, mid + 1, end);
}
}
}

/**
* 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 93d40e6

Please sign in to comment.