algorithm

Math operations with long long in c

Deadly 提交于 2021-02-10 20:15:08
问题 Given long long x , long long y , and int z , how can trunc(( x + y + z )/3) be calculated using fully defined operations in C but excluding unsigned long long and wider arithmetic? (trunc( x ) is “rounding toward zero”: It is x if x is an integer and otherwise is the next integer toward zero.) 回答1: You can calculate the average of two integers x and y without the risk of overflow as follows (pseudocode): int average = (x and y have the same sign) ? x + (y - x) / 2 : (x + y) / 2; This is not

How to bin the sum of list values in Python?

好久不见. 提交于 2021-02-10 19:56:55
问题 I need some help in binning my data values. Need a histogram-like function, but I don't want to list the occurrences, just the sum of the values for each bin. In my example below I have a list with the number of Twitter followers for 30 days. Lets say I want 10 bins, then each bin would take the values of 30 / 10 = 3 days. For the first three days the value for bin 1 would be 1391 + 142 + 0 = 1533 for bin 2 12618, etc., up to bin 10. The number of bins as well as the duration could eventually

How to bin the sum of list values in Python?

可紊 提交于 2021-02-10 19:55:43
问题 I need some help in binning my data values. Need a histogram-like function, but I don't want to list the occurrences, just the sum of the values for each bin. In my example below I have a list with the number of Twitter followers for 30 days. Lets say I want 10 bins, then each bin would take the values of 30 / 10 = 3 days. For the first three days the value for bin 1 would be 1391 + 142 + 0 = 1533 for bin 2 12618, etc., up to bin 10. The number of bins as well as the duration could eventually

Algorithm for Permutation with Buckets

吃可爱长大的小学妹 提交于 2021-02-10 17:54:34
问题 I am looking for an algorithm which works like this permutateBuckets([A,B,C]) and gives the following result: [ [[A,B,C]], [[A,B],[C]], [[A,C],[B]], [[B,C],[A]], [[A],[B,C]], [[B],[A,C]], [[C],[A,B]], [[A],[B],[C]], [[A],[C],[B]], [[B],[A],[C]], [[B],[C],[A]], [[C],[A],[B]], [[C],[B],[A]] ] In general: The permutation for [1,2,...,n] should include any possible arrangements of 1 up to n buckets that contain the input values, order of values within buckets is not relevant (e.g. [1,2] equals [2

Finding the Peak number of an array in C

拟墨画扇 提交于 2021-02-10 16:26:37
问题 I am new to coding in C. I am trying to write a code that finds the peak number in an array, that is the numbers that are greater than the numbers before and after said number. This is my code. It runs without error but no output so I know I am doing something wrong. #include <stdio.h> int main() { int nums[14] = {1, 2, 3, 3, 2, 4, 1, 5, 6, 3, 1, 10, 2, 8}; int peaks[4]; for(int i = 0; i < nums[i]; i++){ if(nums[i] > nums[i-1] && nums[i] > nums[i+1]){ peaks == nums[i]; } return peaks; }

Are there any efficient ways to populate a balanced tree structure

ε祈祈猫儿з 提交于 2021-02-10 15:53:56
问题 I have a balanced binary tree structure: Node 0 at depth 0 is the root. The root's left child is 1 and right child is 2 , and so on. Please see image: The total depth of the tree is given as N . This N is the only parameter of the problem. Nodes at level N are designated as leaf nodes. I am storing this tree using the following node structure. struct node_s{ int n, depth, parent;//n is node number int nodescendents;//number of descendents of the current node std::vector<int> descendents;/

Are there any efficient ways to populate a balanced tree structure

这一生的挚爱 提交于 2021-02-10 15:52:20
问题 I have a balanced binary tree structure: Node 0 at depth 0 is the root. The root's left child is 1 and right child is 2 , and so on. Please see image: The total depth of the tree is given as N . This N is the only parameter of the problem. Nodes at level N are designated as leaf nodes. I am storing this tree using the following node structure. struct node_s{ int n, depth, parent;//n is node number int nodescendents;//number of descendents of the current node std::vector<int> descendents;/

Find the nXn submatrix with the highest number of 1's

谁说胖子不能爱 提交于 2021-02-10 14:53:22
问题 I tried to solve this question but without success: Given 3n X 3n boolean matrix, find the nXn submatrix with the highest number of 1's in O(n^2). I had an idea but it was O(n^3). The idea: count the 1's in the matrix begin with (0,0) and move to the right and check only the new col that should be added VS the col that should be deleted. And the same idea for down. Each submatrix calculation is O(n^2) and passing all the matrix is O(n) so it's too much. I don't see a way how to pass across

Time Complexity for binary permutation representation of n bits

拥有回忆 提交于 2021-02-10 14:48:24
问题 I have return a below code in java to produce the possible binary representation of n digits. public List<String> binaryRepresenation(int n){ List<String> list = new ArrayList<>(); if(n>0){ permuation(n, list, ""); } return list; } private void permuation(int n, List<String> list, String str){ if(n==0){ list.add(str); }else{ permuation(n-1, list, str+"0"); permuation(n-1, list, str+"1"); } } For n=3, it produces 001 001 010 011 100 101 110 111 combinations. Overall this function produces 2^n

Time Complexity for binary permutation representation of n bits

梦想与她 提交于 2021-02-10 14:47:19
问题 I have return a below code in java to produce the possible binary representation of n digits. public List<String> binaryRepresenation(int n){ List<String> list = new ArrayList<>(); if(n>0){ permuation(n, list, ""); } return list; } private void permuation(int n, List<String> list, String str){ if(n==0){ list.add(str); }else{ permuation(n-1, list, str+"0"); permuation(n-1, list, str+"1"); } } For n=3, it produces 001 001 010 011 100 101 110 111 combinations. Overall this function produces 2^n