We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Median Updates
Median Updates
Sort by
recency
|
191 Discussions
|
Please Login in order to post a comment
solution of this problem in C language
include
include
int cmpfunc(const void *a, const void b) { return ((int*)a - (int)b); }
int lower_bound(int *arr, int n, int val) { int l = 0, r = n; while (l < r) { int mid = (l + r) / 2; if (arr[mid] < val) l = mid + 1; else r = mid; } return l; }
void printMedian(int *arr, int n) { if (n == 0) { printf("Wrong!\n"); return; } if (n % 2 == 1) { printf("%d\n", arr[n/2]); } else { long long sum = (long long)arr[n/2 - 1] + (long long)arr[n/2]; if (sum % 2 == 0) printf("%lld\n", sum/2); else printf("%.1f\n", sum/2.0); } }
void median(int N, char (*s)[3], int *x) { int *arr = malloc(N * sizeof(int)); int size = 0;
}
int main() { int N; scanf("%d", &N); char s[N][3]; int x[N]; for (int i = 0; i < N; i++) { scanf("%s %d", s[i], &x[i]); } median(N, s, x); return 0; }
For those that write the challenge in C#, there is no built-in .NET collection that: only stores keys, keeps them sorted, and allows duplicates.
The only way that I have managed to do it is implement my custom data structure.
`
class Node: def init(self, data): self.data = data self.left = None self.right = None self.height = 1 self.size = 1
class AVLTree: def init(self): self.root = None
def median(s, x): avlTree = AVLTree() for op, val in zip(s, x): if op == "a": avlTree.insert(val) elif op == "r": avlTree.delete(val)
Done it with the AVL Tree. Every node of the tree tracks not only height but also size which allows to calculate the index of the node and therefore the median.
` class AVL: def init(self): self.root = None
The rest of the AVL tree implementation you may find on geeksforgeeks.
i got this one
https://ideone.com/YWjB9B