Finding Intervals for each StoreHow to detect a loop in a linked list?Easy interview question got harder: given numbers 1..100, find the missing number(s) given exactly k are missingFind an integer not among four billion given onesFinding maximum for every window of size k in an arrayFinding maximum number k such that for all combinations of k pairs, we have k different elements in each combinationHow to find time complexity of an algorithmFinding a maximal sorted subsequenceDynamic (time-indexed) Maximum Flow - Ford-FulkersonFast way to find the maximum value within a rectangle region of a 2d arrayRange minimum query, dynamic array, interval tree, treap

What to call a small, open stone or cement reservoir that supplies fresh water from a spring or other natural source?

Is presenting a play showing Military characters in a bad light a crime in the US?

Why does an injection from a set to a countable set imply that set is countable?

Gambler's Fallacy Dice

How would a physicist explain this starship engine?

Parse a C++14 integer literal

Is it wise to pay off mortgage with 401k?

How to play vs. 1.e4 e5 2.Nf3 Nc6 3.Bc4 d6?

Existence of a model of ZFC in which the natural numbers are really the natural numbers

How to prove the emptiness of intersection of two context free languages is undecidable?

Will this series of events work to drown the Tarrasque?

How do you cope with rejection?

Is my company merging branches wrong?

How to say "they didn't leave him a penny"?

Why was Houston selected as the location for the Manned Spacecraft Center?

Keeping the dodos out of the field

pwaS eht tirsf dna tasl setterl fo hace dorw

Difference in 1 user doing 1000 iterations and 1000 users doing 1 iteration in Load testing

How do we explain the use of a software on a math paper?

Does a windmilling propeller create more drag than a stopped propeller in an engine out scenario?

Are there historical examples of audiences drawn to a work that was "so bad it's good"?

Are there any nuances between "dismiss" and "ignore"?

If you attack a Tarrasque while swallowed, what AC do you need to beat to hit it?

Why "strap-on" boosters, and how do other people say it?



Finding Intervals for each Store


How to detect a loop in a linked list?Easy interview question got harder: given numbers 1..100, find the missing number(s) given exactly k are missingFind an integer not among four billion given onesFinding maximum for every window of size k in an arrayFinding maximum number k such that for all combinations of k pairs, we have k different elements in each combinationHow to find time complexity of an algorithmFinding a maximal sorted subsequenceDynamic (time-indexed) Maximum Flow - Ford-FulkersonFast way to find the maximum value within a rectangle region of a 2d arrayRange minimum query, dynamic array, interval tree, treap






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















You have n stores in a line, each with an associated amount of money $c_i$. At time 1, you start at store 1. At each subsequent time, you can choose to visit store i-1, stay at store i, or visit store i+1. At each time step, you can collect $c_i$ money assuming you’re at store i, regardless of how many times you already visited that store.



Given a list of times $t_j$, find the maximum amount of money that can be collected given you can collect money for $t_j$ time steps.



I’m having trouble coming up with a way to preprocess the data. My idea is that for each booth, I want to find a time interval for which it is optimal to rush straight to that booth and stay there, collecting money at each time step. I’m not sure of a correct way to do this efficiently in either O(n) time or O(nlogn) time.



Edit: What I’m currently doing is for each booth, I have an expression as a function of time that represents the maximum amount of money I can collect if I go straight to the booth and stay there. Then for each time, I take the max of all these functions. This takes linear time to process each time query. I’d like to have a logarithmic procedure for doing so.










share|improve this question
























  • Hello and welcome to Stack Overflow, you are expected to show your attempt first and show where you are encountering a problem, please go through How to ask and How to create a minimum complete verifiable example

    – anand_v.singh
    Mar 21 at 3:50











  • Do you know the c_i before hand or do you need to traverse through the shops to know?

    – Perplexabot
    Mar 21 at 5:16











  • The c_i are known before hand.

    – J Chen
    Mar 21 at 5:18











  • Is c_i independent of t_j? In other words does a c_i value for a specific shop change with t_j?

    – Perplexabot
    Mar 21 at 5:21












  • The c_i are fixed and independent of t_j.

    – J Chen
    Mar 21 at 5:22

















0















You have n stores in a line, each with an associated amount of money $c_i$. At time 1, you start at store 1. At each subsequent time, you can choose to visit store i-1, stay at store i, or visit store i+1. At each time step, you can collect $c_i$ money assuming you’re at store i, regardless of how many times you already visited that store.



Given a list of times $t_j$, find the maximum amount of money that can be collected given you can collect money for $t_j$ time steps.



I’m having trouble coming up with a way to preprocess the data. My idea is that for each booth, I want to find a time interval for which it is optimal to rush straight to that booth and stay there, collecting money at each time step. I’m not sure of a correct way to do this efficiently in either O(n) time or O(nlogn) time.



Edit: What I’m currently doing is for each booth, I have an expression as a function of time that represents the maximum amount of money I can collect if I go straight to the booth and stay there. Then for each time, I take the max of all these functions. This takes linear time to process each time query. I’d like to have a logarithmic procedure for doing so.










share|improve this question
























  • Hello and welcome to Stack Overflow, you are expected to show your attempt first and show where you are encountering a problem, please go through How to ask and How to create a minimum complete verifiable example

    – anand_v.singh
    Mar 21 at 3:50











  • Do you know the c_i before hand or do you need to traverse through the shops to know?

    – Perplexabot
    Mar 21 at 5:16











  • The c_i are known before hand.

    – J Chen
    Mar 21 at 5:18











  • Is c_i independent of t_j? In other words does a c_i value for a specific shop change with t_j?

    – Perplexabot
    Mar 21 at 5:21












  • The c_i are fixed and independent of t_j.

    – J Chen
    Mar 21 at 5:22













0












0








0








You have n stores in a line, each with an associated amount of money $c_i$. At time 1, you start at store 1. At each subsequent time, you can choose to visit store i-1, stay at store i, or visit store i+1. At each time step, you can collect $c_i$ money assuming you’re at store i, regardless of how many times you already visited that store.



Given a list of times $t_j$, find the maximum amount of money that can be collected given you can collect money for $t_j$ time steps.



I’m having trouble coming up with a way to preprocess the data. My idea is that for each booth, I want to find a time interval for which it is optimal to rush straight to that booth and stay there, collecting money at each time step. I’m not sure of a correct way to do this efficiently in either O(n) time or O(nlogn) time.



Edit: What I’m currently doing is for each booth, I have an expression as a function of time that represents the maximum amount of money I can collect if I go straight to the booth and stay there. Then for each time, I take the max of all these functions. This takes linear time to process each time query. I’d like to have a logarithmic procedure for doing so.










share|improve this question
















You have n stores in a line, each with an associated amount of money $c_i$. At time 1, you start at store 1. At each subsequent time, you can choose to visit store i-1, stay at store i, or visit store i+1. At each time step, you can collect $c_i$ money assuming you’re at store i, regardless of how many times you already visited that store.



Given a list of times $t_j$, find the maximum amount of money that can be collected given you can collect money for $t_j$ time steps.



I’m having trouble coming up with a way to preprocess the data. My idea is that for each booth, I want to find a time interval for which it is optimal to rush straight to that booth and stay there, collecting money at each time step. I’m not sure of a correct way to do this efficiently in either O(n) time or O(nlogn) time.



Edit: What I’m currently doing is for each booth, I have an expression as a function of time that represents the maximum amount of money I can collect if I go straight to the booth and stay there. Then for each time, I take the max of all these functions. This takes linear time to process each time query. I’d like to have a logarithmic procedure for doing so.







algorithm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 21 at 4:16







J Chen

















asked Mar 21 at 3:40









J ChenJ Chen

11




11












  • Hello and welcome to Stack Overflow, you are expected to show your attempt first and show where you are encountering a problem, please go through How to ask and How to create a minimum complete verifiable example

    – anand_v.singh
    Mar 21 at 3:50











  • Do you know the c_i before hand or do you need to traverse through the shops to know?

    – Perplexabot
    Mar 21 at 5:16











  • The c_i are known before hand.

    – J Chen
    Mar 21 at 5:18











  • Is c_i independent of t_j? In other words does a c_i value for a specific shop change with t_j?

    – Perplexabot
    Mar 21 at 5:21












  • The c_i are fixed and independent of t_j.

    – J Chen
    Mar 21 at 5:22

















  • Hello and welcome to Stack Overflow, you are expected to show your attempt first and show where you are encountering a problem, please go through How to ask and How to create a minimum complete verifiable example

    – anand_v.singh
    Mar 21 at 3:50











  • Do you know the c_i before hand or do you need to traverse through the shops to know?

    – Perplexabot
    Mar 21 at 5:16











  • The c_i are known before hand.

    – J Chen
    Mar 21 at 5:18











  • Is c_i independent of t_j? In other words does a c_i value for a specific shop change with t_j?

    – Perplexabot
    Mar 21 at 5:21












  • The c_i are fixed and independent of t_j.

    – J Chen
    Mar 21 at 5:22
















Hello and welcome to Stack Overflow, you are expected to show your attempt first and show where you are encountering a problem, please go through How to ask and How to create a minimum complete verifiable example

– anand_v.singh
Mar 21 at 3:50





Hello and welcome to Stack Overflow, you are expected to show your attempt first and show where you are encountering a problem, please go through How to ask and How to create a minimum complete verifiable example

– anand_v.singh
Mar 21 at 3:50













Do you know the c_i before hand or do you need to traverse through the shops to know?

– Perplexabot
Mar 21 at 5:16





Do you know the c_i before hand or do you need to traverse through the shops to know?

– Perplexabot
Mar 21 at 5:16













The c_i are known before hand.

– J Chen
Mar 21 at 5:18





The c_i are known before hand.

– J Chen
Mar 21 at 5:18













Is c_i independent of t_j? In other words does a c_i value for a specific shop change with t_j?

– Perplexabot
Mar 21 at 5:21






Is c_i independent of t_j? In other words does a c_i value for a specific shop change with t_j?

– Perplexabot
Mar 21 at 5:21














The c_i are fixed and independent of t_j.

– J Chen
Mar 21 at 5:22





The c_i are fixed and independent of t_j.

– J Chen
Mar 21 at 5:22












1 Answer
1






active

oldest

votes


















0














Your revenue function that returns the amount of money by going straight to shop i and staying there for a given total time t is



r_i(t) = sum(j from 1 to i - 1: c_j) + c_i * (t - i + 1)


The sums can be calculated incrementally for each i and you get a neat linear function:



r_i(t) = a_i + b_i * t


with a_i = sum as above + (1 - i) * c_i and b_i = c_i.



Now, for each time t, you want to find the maximum of all the r_i(t). For this, we can use an ordering of the functions. Specifically, we can order them such that a function f1 appears earlier than f2 in an ordered sequence if function f1 is greater than f2 before their intersection point. Then, if we have the available functions for a given point t (which are all r_i that are reachable in t), we just need to step through the ordered sequence. The first element will be the best function for the starting time. Then, we can check the intersection with the next function. If t is after that intersection, we continue to this function as this will be the new maximum for the current interval. And so on. This can be done incrementally for each query if you sort the queries by time. While doing this process, you also need to add new functions that become available. Using an appropriate data structure (e.g. a binary search tree), you can do the insertion in O(log n).



So, how can we compare two functions f1(t) = a1 + t * b1 and f2(t) = a2 + t * b2? The intersection point is ti = (a2 - a1) / (b1 - b2). And f1 is greater than f2 before that point if b1 < b2.



Here is the overall algorithm:



input: n stores, q queries Q
sort queries by time - O(q log q)
create empty BST
currentBestFunction = empty //pointer to element in BST
nextQuery = Q.first
sumCi = 0 //temporary variable for incremental sum
for t = 1 to maxQ
if t <= n
// we can reach a new store
a = sumCi + (1 - t) * c_t
b = c_t
sumCi += c_t
insert function (a, b) into BST - O(log n)
if currentBestFunction = empty
currentBestFunction = the inserted function
while currentBestFunction is not the last function in BST
successor = find successor function to currentBestFunction in BST
calculate intersection ti = (successor.a - currentBestFunction.a) / (currentBestFunction.b - successor.b)
if ti > t
break //we are in the interval for the current best function
else
currentBestFunction = successor
loop
if nextQuery == t
answer query by evaluating currentBestFunction at t
advance nextQuery
if no more queries then stop





share|improve this answer

























  • What is the runtime for answering each query using this? And does this still apply if the queries can be bigger than n?

    – J Chen
    Mar 21 at 19:40











  • The runtime of answering all queries will be O(q (log q + log n)). If there are queries beyond the number of stores (sounds reasonable, didn't think of that - will update the answer accordingly), you will continue the loop but you won't add new entries to the BST.

    – Nico Schertler
    Mar 21 at 22:05











  • Could you also clarify what you mean by successor function?

    – J Chen
    Mar 21 at 22:21











  • How is the BST sorted?

    – J Chen
    Mar 21 at 23:22











  • The BST contains functions. Hence, the successor function is the function that is right behind the current function in the BST ordering. The BST is ordered by the ordering relation described in the text.

    – Nico Schertler
    Mar 22 at 2:25












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55273448%2ffinding-intervals-for-each-store%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Your revenue function that returns the amount of money by going straight to shop i and staying there for a given total time t is



r_i(t) = sum(j from 1 to i - 1: c_j) + c_i * (t - i + 1)


The sums can be calculated incrementally for each i and you get a neat linear function:



r_i(t) = a_i + b_i * t


with a_i = sum as above + (1 - i) * c_i and b_i = c_i.



Now, for each time t, you want to find the maximum of all the r_i(t). For this, we can use an ordering of the functions. Specifically, we can order them such that a function f1 appears earlier than f2 in an ordered sequence if function f1 is greater than f2 before their intersection point. Then, if we have the available functions for a given point t (which are all r_i that are reachable in t), we just need to step through the ordered sequence. The first element will be the best function for the starting time. Then, we can check the intersection with the next function. If t is after that intersection, we continue to this function as this will be the new maximum for the current interval. And so on. This can be done incrementally for each query if you sort the queries by time. While doing this process, you also need to add new functions that become available. Using an appropriate data structure (e.g. a binary search tree), you can do the insertion in O(log n).



So, how can we compare two functions f1(t) = a1 + t * b1 and f2(t) = a2 + t * b2? The intersection point is ti = (a2 - a1) / (b1 - b2). And f1 is greater than f2 before that point if b1 < b2.



Here is the overall algorithm:



input: n stores, q queries Q
sort queries by time - O(q log q)
create empty BST
currentBestFunction = empty //pointer to element in BST
nextQuery = Q.first
sumCi = 0 //temporary variable for incremental sum
for t = 1 to maxQ
if t <= n
// we can reach a new store
a = sumCi + (1 - t) * c_t
b = c_t
sumCi += c_t
insert function (a, b) into BST - O(log n)
if currentBestFunction = empty
currentBestFunction = the inserted function
while currentBestFunction is not the last function in BST
successor = find successor function to currentBestFunction in BST
calculate intersection ti = (successor.a - currentBestFunction.a) / (currentBestFunction.b - successor.b)
if ti > t
break //we are in the interval for the current best function
else
currentBestFunction = successor
loop
if nextQuery == t
answer query by evaluating currentBestFunction at t
advance nextQuery
if no more queries then stop





share|improve this answer

























  • What is the runtime for answering each query using this? And does this still apply if the queries can be bigger than n?

    – J Chen
    Mar 21 at 19:40











  • The runtime of answering all queries will be O(q (log q + log n)). If there are queries beyond the number of stores (sounds reasonable, didn't think of that - will update the answer accordingly), you will continue the loop but you won't add new entries to the BST.

    – Nico Schertler
    Mar 21 at 22:05











  • Could you also clarify what you mean by successor function?

    – J Chen
    Mar 21 at 22:21











  • How is the BST sorted?

    – J Chen
    Mar 21 at 23:22











  • The BST contains functions. Hence, the successor function is the function that is right behind the current function in the BST ordering. The BST is ordered by the ordering relation described in the text.

    – Nico Schertler
    Mar 22 at 2:25
















0














Your revenue function that returns the amount of money by going straight to shop i and staying there for a given total time t is



r_i(t) = sum(j from 1 to i - 1: c_j) + c_i * (t - i + 1)


The sums can be calculated incrementally for each i and you get a neat linear function:



r_i(t) = a_i + b_i * t


with a_i = sum as above + (1 - i) * c_i and b_i = c_i.



Now, for each time t, you want to find the maximum of all the r_i(t). For this, we can use an ordering of the functions. Specifically, we can order them such that a function f1 appears earlier than f2 in an ordered sequence if function f1 is greater than f2 before their intersection point. Then, if we have the available functions for a given point t (which are all r_i that are reachable in t), we just need to step through the ordered sequence. The first element will be the best function for the starting time. Then, we can check the intersection with the next function. If t is after that intersection, we continue to this function as this will be the new maximum for the current interval. And so on. This can be done incrementally for each query if you sort the queries by time. While doing this process, you also need to add new functions that become available. Using an appropriate data structure (e.g. a binary search tree), you can do the insertion in O(log n).



So, how can we compare two functions f1(t) = a1 + t * b1 and f2(t) = a2 + t * b2? The intersection point is ti = (a2 - a1) / (b1 - b2). And f1 is greater than f2 before that point if b1 < b2.



Here is the overall algorithm:



input: n stores, q queries Q
sort queries by time - O(q log q)
create empty BST
currentBestFunction = empty //pointer to element in BST
nextQuery = Q.first
sumCi = 0 //temporary variable for incremental sum
for t = 1 to maxQ
if t <= n
// we can reach a new store
a = sumCi + (1 - t) * c_t
b = c_t
sumCi += c_t
insert function (a, b) into BST - O(log n)
if currentBestFunction = empty
currentBestFunction = the inserted function
while currentBestFunction is not the last function in BST
successor = find successor function to currentBestFunction in BST
calculate intersection ti = (successor.a - currentBestFunction.a) / (currentBestFunction.b - successor.b)
if ti > t
break //we are in the interval for the current best function
else
currentBestFunction = successor
loop
if nextQuery == t
answer query by evaluating currentBestFunction at t
advance nextQuery
if no more queries then stop





share|improve this answer

























  • What is the runtime for answering each query using this? And does this still apply if the queries can be bigger than n?

    – J Chen
    Mar 21 at 19:40











  • The runtime of answering all queries will be O(q (log q + log n)). If there are queries beyond the number of stores (sounds reasonable, didn't think of that - will update the answer accordingly), you will continue the loop but you won't add new entries to the BST.

    – Nico Schertler
    Mar 21 at 22:05











  • Could you also clarify what you mean by successor function?

    – J Chen
    Mar 21 at 22:21











  • How is the BST sorted?

    – J Chen
    Mar 21 at 23:22











  • The BST contains functions. Hence, the successor function is the function that is right behind the current function in the BST ordering. The BST is ordered by the ordering relation described in the text.

    – Nico Schertler
    Mar 22 at 2:25














0












0








0







Your revenue function that returns the amount of money by going straight to shop i and staying there for a given total time t is



r_i(t) = sum(j from 1 to i - 1: c_j) + c_i * (t - i + 1)


The sums can be calculated incrementally for each i and you get a neat linear function:



r_i(t) = a_i + b_i * t


with a_i = sum as above + (1 - i) * c_i and b_i = c_i.



Now, for each time t, you want to find the maximum of all the r_i(t). For this, we can use an ordering of the functions. Specifically, we can order them such that a function f1 appears earlier than f2 in an ordered sequence if function f1 is greater than f2 before their intersection point. Then, if we have the available functions for a given point t (which are all r_i that are reachable in t), we just need to step through the ordered sequence. The first element will be the best function for the starting time. Then, we can check the intersection with the next function. If t is after that intersection, we continue to this function as this will be the new maximum for the current interval. And so on. This can be done incrementally for each query if you sort the queries by time. While doing this process, you also need to add new functions that become available. Using an appropriate data structure (e.g. a binary search tree), you can do the insertion in O(log n).



So, how can we compare two functions f1(t) = a1 + t * b1 and f2(t) = a2 + t * b2? The intersection point is ti = (a2 - a1) / (b1 - b2). And f1 is greater than f2 before that point if b1 < b2.



Here is the overall algorithm:



input: n stores, q queries Q
sort queries by time - O(q log q)
create empty BST
currentBestFunction = empty //pointer to element in BST
nextQuery = Q.first
sumCi = 0 //temporary variable for incremental sum
for t = 1 to maxQ
if t <= n
// we can reach a new store
a = sumCi + (1 - t) * c_t
b = c_t
sumCi += c_t
insert function (a, b) into BST - O(log n)
if currentBestFunction = empty
currentBestFunction = the inserted function
while currentBestFunction is not the last function in BST
successor = find successor function to currentBestFunction in BST
calculate intersection ti = (successor.a - currentBestFunction.a) / (currentBestFunction.b - successor.b)
if ti > t
break //we are in the interval for the current best function
else
currentBestFunction = successor
loop
if nextQuery == t
answer query by evaluating currentBestFunction at t
advance nextQuery
if no more queries then stop





share|improve this answer















Your revenue function that returns the amount of money by going straight to shop i and staying there for a given total time t is



r_i(t) = sum(j from 1 to i - 1: c_j) + c_i * (t - i + 1)


The sums can be calculated incrementally for each i and you get a neat linear function:



r_i(t) = a_i + b_i * t


with a_i = sum as above + (1 - i) * c_i and b_i = c_i.



Now, for each time t, you want to find the maximum of all the r_i(t). For this, we can use an ordering of the functions. Specifically, we can order them such that a function f1 appears earlier than f2 in an ordered sequence if function f1 is greater than f2 before their intersection point. Then, if we have the available functions for a given point t (which are all r_i that are reachable in t), we just need to step through the ordered sequence. The first element will be the best function for the starting time. Then, we can check the intersection with the next function. If t is after that intersection, we continue to this function as this will be the new maximum for the current interval. And so on. This can be done incrementally for each query if you sort the queries by time. While doing this process, you also need to add new functions that become available. Using an appropriate data structure (e.g. a binary search tree), you can do the insertion in O(log n).



So, how can we compare two functions f1(t) = a1 + t * b1 and f2(t) = a2 + t * b2? The intersection point is ti = (a2 - a1) / (b1 - b2). And f1 is greater than f2 before that point if b1 < b2.



Here is the overall algorithm:



input: n stores, q queries Q
sort queries by time - O(q log q)
create empty BST
currentBestFunction = empty //pointer to element in BST
nextQuery = Q.first
sumCi = 0 //temporary variable for incremental sum
for t = 1 to maxQ
if t <= n
// we can reach a new store
a = sumCi + (1 - t) * c_t
b = c_t
sumCi += c_t
insert function (a, b) into BST - O(log n)
if currentBestFunction = empty
currentBestFunction = the inserted function
while currentBestFunction is not the last function in BST
successor = find successor function to currentBestFunction in BST
calculate intersection ti = (successor.a - currentBestFunction.a) / (currentBestFunction.b - successor.b)
if ti > t
break //we are in the interval for the current best function
else
currentBestFunction = successor
loop
if nextQuery == t
answer query by evaluating currentBestFunction at t
advance nextQuery
if no more queries then stop






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 21 at 22:06

























answered Mar 21 at 16:31









Nico SchertlerNico Schertler

26k42452




26k42452












  • What is the runtime for answering each query using this? And does this still apply if the queries can be bigger than n?

    – J Chen
    Mar 21 at 19:40











  • The runtime of answering all queries will be O(q (log q + log n)). If there are queries beyond the number of stores (sounds reasonable, didn't think of that - will update the answer accordingly), you will continue the loop but you won't add new entries to the BST.

    – Nico Schertler
    Mar 21 at 22:05











  • Could you also clarify what you mean by successor function?

    – J Chen
    Mar 21 at 22:21











  • How is the BST sorted?

    – J Chen
    Mar 21 at 23:22











  • The BST contains functions. Hence, the successor function is the function that is right behind the current function in the BST ordering. The BST is ordered by the ordering relation described in the text.

    – Nico Schertler
    Mar 22 at 2:25


















  • What is the runtime for answering each query using this? And does this still apply if the queries can be bigger than n?

    – J Chen
    Mar 21 at 19:40











  • The runtime of answering all queries will be O(q (log q + log n)). If there are queries beyond the number of stores (sounds reasonable, didn't think of that - will update the answer accordingly), you will continue the loop but you won't add new entries to the BST.

    – Nico Schertler
    Mar 21 at 22:05











  • Could you also clarify what you mean by successor function?

    – J Chen
    Mar 21 at 22:21











  • How is the BST sorted?

    – J Chen
    Mar 21 at 23:22











  • The BST contains functions. Hence, the successor function is the function that is right behind the current function in the BST ordering. The BST is ordered by the ordering relation described in the text.

    – Nico Schertler
    Mar 22 at 2:25

















What is the runtime for answering each query using this? And does this still apply if the queries can be bigger than n?

– J Chen
Mar 21 at 19:40





What is the runtime for answering each query using this? And does this still apply if the queries can be bigger than n?

– J Chen
Mar 21 at 19:40













The runtime of answering all queries will be O(q (log q + log n)). If there are queries beyond the number of stores (sounds reasonable, didn't think of that - will update the answer accordingly), you will continue the loop but you won't add new entries to the BST.

– Nico Schertler
Mar 21 at 22:05





The runtime of answering all queries will be O(q (log q + log n)). If there are queries beyond the number of stores (sounds reasonable, didn't think of that - will update the answer accordingly), you will continue the loop but you won't add new entries to the BST.

– Nico Schertler
Mar 21 at 22:05













Could you also clarify what you mean by successor function?

– J Chen
Mar 21 at 22:21





Could you also clarify what you mean by successor function?

– J Chen
Mar 21 at 22:21













How is the BST sorted?

– J Chen
Mar 21 at 23:22





How is the BST sorted?

– J Chen
Mar 21 at 23:22













The BST contains functions. Hence, the successor function is the function that is right behind the current function in the BST ordering. The BST is ordered by the ordering relation described in the text.

– Nico Schertler
Mar 22 at 2:25






The BST contains functions. Hence, the successor function is the function that is right behind the current function in the BST ordering. The BST is ordered by the ordering relation described in the text.

– Nico Schertler
Mar 22 at 2:25




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55273448%2ffinding-intervals-for-each-store%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript