1,889 questions
-4
votes
0
answers
49
views
Do Python variable references get stored on the stack or the heap? [duplicate]
Where does the reference to a variabl e.g. x = 3 get stored in Python?
Sources on the web say that the references are stored on the stack. ex:
Stack Memory: Stack memory stores temporary data, ...
0
votes
1
answer
148
views
can newlib-nano printf function work without heap allocation
i was looking at this heap implementation, and i was wondering if it is necessary to implement heap for printf even if i'm calling setvbuf(stdout, NULL, _IONBF, 0) before calling printf.
i looked at ...
1
vote
2
answers
83
views
Why defining string within a function scope allocates it on heap instead of a stack? [duplicate]
According this awesome article there is an interesting case with two given examples:
A:
B:
And there is an explanation for A:
Unfortunately you have not taken into account that such Strings will ...
0
votes
1
answer
49
views
How does a microcontroller keep track of heap free()?
In a microcontroller without any OS, how does the microcontroller keep track of where a malloc will point to in the heap?
char *x;
char *y;
char *z;
x=(char*)malloc(10);
y=(char*)malloc(10);
free(x);
...
2
votes
4
answers
190
views
How can I efficiently maintain median in a dynamic data stream with support for deletions?
I'm working on a problem where I need to maintain the median of a data stream, but unlike typical implementations that only support insertions, I also need to support deletions of arbitrary values.
...
1
vote
1
answer
213
views
EIGEN: Advice needed for memory allocation
I am trying to write an efficient multistep solver in C++ using the Eigen library. To do so, I need a few variables that keep track of the history. One of them is of type Eigen::VectorX<Eigen::...
4
votes
1
answer
106
views
gdb: get references to value returned by the find command
I'm trying to port a program I wrote in windows with syscalls to linux. The program reads the memory of another running process and displays certain values as they change. To get started and figure ...
1
vote
1
answer
102
views
Does the call instruction write something onto the stack? For example, things like environment variables?
I am currently running a C++ program in which I have used the heap to simulate the stack and employed assembly language to place relevant information (such as memory addresses) into registers, thereby ...
0
votes
1
answer
81
views
Data streaming - is this the right way to build CSV file using ByteArrayOutputStream?
I do have this code.
@RequestMapping("/test")
fun getData(): ResponseEntity<ByteArray> {
val items = repository.getItems()
val outputStream = ByteArrayOutputStream()
...
1
vote
3
answers
138
views
Why is the heap in heapsort "the wrong way"?
In heapsort, the heap is max-heap where each time we extract the maximum element from index 0 and place it at the right side of the array. I'm now wondering why we don't build the max-heap in reverse, ...
2
votes
1
answer
188
views
Which option has precendence if I enable and disable FrontEndHeapDebugOptions at the same time?
The undocumented (I can't find a MSDN reference) FrontEndHeapDebugOptions Registry key has two flags:
Bit 2 (0x04) for disabling the Segment Heap, thus forcing NT Heap
Bit 3 (0x08) for enabling the ...
1
vote
1
answer
129
views
Min Pairing Heap - How to increase key faster than O(logn)?
I know that a min pairing heap can decrease a key faster than O(logn). However, is there any way to make the increase key operate also faster than decrease key to the top, remove, than insert new ...
0
votes
1
answer
60
views
Why heapq.heappush is an O(log n) operation?
import heapq
minHeap = [4, 7, 2, 8, 1, 3]
heapq.heapify(minHeap) # O(n log n) operation
print(minHeap) # [1, 4, 2, 8, 7, 3]
heapq.heappush(minHeap, 1) # O(log n) operation?
print(minHeap) # ...
1
vote
0
answers
49
views
use a priority queue to do hierarchical clustering without import heapq
I am using priority queue to do the hierarchical clustering(can not import heapq), and want to use the complete-link method, but I don't know what is the problem of my code, the reason is far from ...
5
votes
0
answers
111
views
Why does the last element in a range need to fulfill the heap property for pop_heap?
The C++ standard explains how std::pop_heap is supposed to work:
Effects: Swaps the value in the location first with the value in the location last - 1 and makes
[first, last - 1) into a heap with ...
0
votes
1
answer
125
views
How a pointer to pointer pointing to a NULL pointer works in C?
I am trying to understand merging two sorted lists into one sorted output. Got the below code from internet. trav's value will have to be the address of mergedHead but here, trav is holding the value ...
1
vote
1
answer
47
views
Empty Encoder Map but the Huffman tree is generating
So I am making a data compression tool using Huffman Encoding and decoding, I am at the stage where I have built the Huffman Tree and it is a success but I am feeding that tree in a GenerateCodes ...
1
vote
0
answers
167
views
Is My Understanding of C Program Memory Layout Correct? (Text, Data, Heap, Stack Sections)
I'm currently working on an assignment that requires me to analyze the memory layout of a given C program. The task is to identify which parts of the program are stored in different memory sections: ...
4
votes
4
answers
199
views
Find shortest subarrays A[0:L], B[0:L] where M different elements in A are bigger than M different elements in B (time complexity)
I need to find minimum subarray of size L, A[0:L], B[0:L] such that there are M different elements in A that are bigger than M different elements in B. Like A[i] > B[j] counts but I cannot use A[i] ...
-1
votes
1
answer
251
views
Haskell, Python speed comparison (Heap // priority-queue)
I have a problem to solve. Details of it basically irrelevant & I have two valid solutions for it: in python and Haskell.
Python code:
import heapq
_, volume, *ppl = map(int, open("input.txt&...
-4
votes
1
answer
493
views
Can anyone explain how this creates a max heap in C#?
I know this is a min heap
var minHeap = new PriorityQueue<int, int>();
But why does this comparer result in a max heap?
var maxHeap = new PriorityQueue<int, int>(Comparer<int>....
1
vote
1
answer
130
views
Heapsort Implementation Using a Min-Heap (Ternary Heap) — Feedback on Correctness and Optimization
I’m working on implementing a heapsort algorithm using a min-heap, specifically a ternary heap, where each node can have up to three children. I know that my solution is inefficient, and I'm looking ...
1
vote
0
answers
108
views
Heap vs Red-Black Tree
Why would we ever use a heap over a red-black tree?
Take a scenario where we want a min-heap. The time complexities of a mean-heap are as follows.
Peek: O(1) average/worst
Delete First: O(log(n)) ...
2
votes
1
answer
143
views
Finding the K-th largest element using heap
I am trying to solve the leetcode problem: kth-largest-element-in-an-array
I know a way to solve this is by using a heap. However, I wanted to implement my own heapify method for practice, and here is ...
1
vote
1
answer
118
views
re-heapify an array-(min)heap after modifying current minimum and a few other elements
I have a question similar to question-1 and question-2. Q-1 describes an approach for modifying a single element of array and re-sorting. Q-2 talks about deleting an element from a heap and re-...
0
votes
1
answer
66
views
For boost::fibonacci_heap, does pop() API automatically update the heap? Or is it necessary to follow it with update()?
I am new to using boost::fibonacci_heap and had some questions regarding usage. I have read the user manual, the answer is not clear: "https://www.boost.org/doc/libs/1_51_0/doc/html/boost/heap/...
2
votes
1
answer
114
views
Why is it impossible to convert a Min Heap to a Binary Search Tree (BST) in O(n) time?
I'm working on formally proving that it's impossible to convert a Min Heap into a BST in O(n) time complexity.
My reasoning is that any algorithm attempting this conversion would need to perform ...
0
votes
1
answer
91
views
How to determine the Optimal Approach for Inserting Multiple Elements into a Binary Heap?
I have a max binary heap implemented based on a complete binary tree. I need to insert multiple elements into the heap, where the current heap array has a length of n and the new elements array has a ...
1
vote
1
answer
115
views
Definition of heap used by std::is_heap and std::make_heap
According to this page, there a two definitions of a heap (used by std::make_heap, std::is_heap, etc..) :
Until C++20 :
A random access range [first, last) is a heap with respect to a
comparator comp ...
0
votes
1
answer
268
views
Time Complexity for finding k smallest elements in an array of size n
I came across 3 approaches for the problem.
Sort the array and find the k elements O(nlog(n))
Using minHeap, heapify in O(n) time and extract k elements O(klog(n)); total = O(n + klog(n))
Using ...
0
votes
1
answer
170
views
Using Python heapq.heapify(Array)
I want to use heapq.heapify(Arr) but everytime i want a slice of array to be used without creating new memory allocation for the new array. I want the rest of the array to be as is. example: heapq....
0
votes
0
answers
15
views
(datastructure)Binary heap delete min big theta time question [duplicate]
In binary heap, these time complexities are given for the delete-min operation:
best: Θ(1)
average: Θ(log𝑛)
amortised: Θ(log𝑛)
worst: Θ(log𝑛)
I don't understand the best case: Θ(1)
Do we have to ...
0
votes
1
answer
80
views
Max Product Finder with heap
task:
Max Product Finder
Create a maxProductFinderK() function that takes in a list of numbers and an integer k, and returns the largest product that can be attained from any k integers in the list. ...
0
votes
3
answers
160
views
Data Structure studying. Max heap. Sorting part, necessity of the second loop
We were given the code:
internal class Program
{
static void Main(string[] args)
{
int[] arr = { 10, 40, 30, 20, 10, 5, 8 };
int n = arr.Length;
int i;
...
0
votes
0
answers
65
views
building a max heap from class Items
i am building a max heap with class item which has (name ,price ,category) one time by comparing the name and one time by comparing the price but there is a problem with the name comparing
here is the ...
1
vote
2
answers
535
views
How does heapify maintain max-heap property when both children are larger than the root and their children are also greater than their parents?
I am currently learning about heap sort and I am having trouble understanding the heapify process, particularly when both children of the root are larger than the root itself, and the sub-children (...
0
votes
1
answer
63
views
Im using minheap for solving my question but heap is placing few elements that are larger 1st and i cant understand why
Ok so min heap place small value 1st in heap but in my case 5.xxx is being placed before 3.xxx and i dont know how to solve this issue when im using sort its giving correct answer but i want to do it ...
0
votes
1
answer
60
views
Facing issues while implementing heap sort in Java
I have written the following program to implement heap sort (both ascending and descending) in Java. It has the following steps actually.
Create an array with a pre-specified size and initialise it ...
1
vote
1
answer
88
views
To build a max Heap from Array. implementation. How to dynamically manage size of Array. I am supposed to increae arr size when I use Insert method
To build a max Heap from Array. In implementation part. How to dynamically manage size of Array?. I am supposed to increase arr size when I use Insert method. Tried but it does not return desired heap ...
1
vote
1
answer
214
views
Given an array that has a max-heap of unknown size, find heap size
I'm given an n sized array that houses a max-heap in it's first x elements (x is unknown). After those x elements, each element has value of infinity. My task is to find x in log(x) time complexity.
...
0
votes
1
answer
72
views
Hire K workers test case fail
Problem
You are given a 0-indexed integer array costs where costs[i] is the cost of hiring the ith worker.
You are also given two integers k and candidates. We want to hire exactly k workers according ...
2
votes
1
answer
328
views
Efficient list sorting: Using heap instead of standard sorting is slower
I'm trying to create a more efficient way to sort lists and dictionaries in python and came across Efficient data structure keeping objects sorted on multiple keys. There the suggested solution was to ...
0
votes
0
answers
46
views
Implementing a max heap in python using heapq for a custom class object with custom comparators in Python? [duplicate]
Python's heapq seems to only support min heaps. If the heap were just made of numbers or an object thats compared using numbers, you could simply multiple each number by -1 to create a max heap but ...
1
vote
0
answers
54
views
Sorting sums of two numbers in an effective way
I am working on the following problem. Assume you have a sorted array of numbers:
a[1] > a[2] > ... > a[n]
and a threshold T. You want to determine all pairs (i,j) such that
a[i] + a[j] > ...
0
votes
1
answer
121
views
An algorithm to find the shortest path based on 2 criteria
We start on node 0 and need to get to node n-1 while using as less steps as possible. At the same time each step affects our temperature, some steps add 1 degree and some subtract 1.
The input is in ...
3
votes
2
answers
1k
views
At which levels in a max-heap might the `k`-th largest element reside?
Consider a max-heap containing n elements. I'm interested in determining the levels within the heap where the k-th largest element could be located, where 2 <= k <= floor(n/2). It's assumed that ...
1
vote
1
answer
85
views
Incorrect Result Order with PHP Heaps
I'm running in to an issue where none of the PHP heap classes store the data in the correct order.
Examples given are trivial, but the error with this small dataset makes a project I'm working on fail ...
2
votes
0
answers
109
views
Efficient Implementation of Priority Queue with Constant-Time Extraction of the Minimum Element
I am working on a project that requires a priority queue with the following characteristics:
Constant-Time Extraction: I need to efficiently extract the minimum element from the priority queue in ...
2
votes
1
answer
256
views
Why we should use sink to construct the heap in heapsort rather than swim?
In 《Algorithms 4th edition》 2.4. It mentioned that In heap construction, to proceed from right to left, using sink() to make subheaps is more efficient. But why?
I think it is the same to proceed from ...
0
votes
1
answer
264
views
Sliding Window Median Two Heaps Method in Python, TLE Error
Im having trouble solving the Sliding Window Median problem using two heaps method as i keep encountering Time Limit Exceeded Error on the test case where K = 50000 and the input an array of 100000 ...