computer-science

Minimum Subarray which is larger than a Key

本秂侑毒 提交于 2019-12-09 12:40:52
问题 I have an array of integers (not necessarily sorted), and I want to find a contiguous subarray which sum of its values are minimum, but larger than a specific value K e.g. : input : array : {1,2,4,9,5} , Key value : 10 output : {4,9} I know it's easy to do this in O(n ^ 2) but I want to do this in O(n) My idea : I couldn't find anyway to this in O(n) but all I could think was of O(n^2) time complexity. 回答1: Let's assume that it can only have positive values. Then it's easy. The solution is

Database operation that can be applied repeatedly and produce the same results?

风格不统一 提交于 2019-12-08 16:27:14
问题 I'm drawing a blank, or as some would say, having a senior moment. I know there’s a formal definition and a name for the concept where a db operation (stored procedure) that runs in a database will yield the same results if run repeatedly. It's something in the genre of the Mathematician’s reflexive, symmetric, transitive, etc. 回答1: Do you mean "deterministic" - as in will always return the same result if called with the same input? Or maybe "idempotent", which also means that calling the

React recursive tree pass JSON path

烂漫一生 提交于 2019-12-08 13:21:53
问题 I'm currently creating a Recursive tree using React and have hit another road block. I am trying to basically pass the path from within my JSON structure. So for each child I want to pass a object prop that looks something like this... Level 1 child (Clicked on Fruit) {value: "Fruit"} Level 2 child (Clicked on Tropical) {value: "Fruit", nested_values: [{ value: 'Tropical'}] } Level 3 child (Clicked on Pineapple) {value: "Fruit", nested_values: [{ value: 'Tropical', nested_values:[{ value:

Find the regular expression for the language on E={a,b}

安稳与你 提交于 2019-12-08 13:11:36
问题 L = w : (na(w) - nb(w)) mod 3 /= 0 How can I go about finding the regular expression for this language? I understand that it means that the number of As minus the number of Bs cannot be a multiple of 3. So a - b cannot be 3,6,9,12, etc. But I am still having trouble putting it into a regular expression. I tried first making it a DFA or NFA but I couldn't do that either. Any help is appreciated! 回答1: I would go about it by dividing the list of words on {a,b} into three cases: L1 = w : (na(w) -

16 bit logic/computer simulation in Swift

冷暖自知 提交于 2019-12-08 10:54:09
问题 I’m trying to make a basic simulation of a 16 bit computer with Swift. The computer will feature An ALU 2 registers That’s all. I have enough knowledge to create these parts visually and understand how they work, but it has become increasingly difficult to make larger components with more inputs while using my current approach. My current approach has been to wrap each component in a struct . This worked early on, but is becoming increasingly difficult to manage multiple inputs while staying

Dynamic range search [duplicate]

半城伤御伤魂 提交于 2019-12-08 08:00:02
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Fast Algorithm to Quickly Find the Range a Number Belongs to in a Set of Ranges? Given a list of range of numbers that are non-overlapping and sorted (rangeList), and a number, write an efficient algorithm to find first whether this number exists in (some range in) rangeList and if it does exist, return the correct range. For example rangeList = [(-1, 200), (300, 2000), (2011, 2300)] Query 1: 1000 -> (True, (300

Write Allocate / Fetch on Write Cache Policy

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 03:22:24
问题 I couldn't find a source that explains how the policy works in great detail. The combinations of write policies are explained in Jouppi's Paper for the interested. This is how I understood it. A write request is sent from cpu to cache. Request results in a cache-miss. A cache block is allocated for this request in cache.(Write-Allocate) Write request block is fetched from lower memory to the allocated cache block.(Fetch-on-Write) Now we are able to write onto allocated and updated by fetch

Number of ways to create an AVL Tree with n nodes and L leaf node

南笙酒味 提交于 2019-12-08 02:07:48
问题 I'd like to know number of ways I can create a Balanced Binary Tree with n nodes and L leaf nodes . also I know that n must be ( 2*L - 1 ) . 回答1: A balanced binary tree is a tree such that given any node, the two subtrees of that node has their height differing by at most one. So the number of nodes is not necessarily 2^L -1. If a tree has 2^L-1 nodes, then it is by definition, a full binary tree. So to answer your question.. If order does matter.. there are (n choose 1) ways (or n ways) to

division and multiplication by power of 2

不问归期 提交于 2019-12-08 01:18:11
问题 I read in a paper that division and multiplication of a number by a power of 2 is a trivial process. I have searched a lot of internet for the explanation but doesn't get it. Can any one explain in easy words what does this actually meant to say. 回答1: It is trivial from the bit operations perspective. Multiplying by 2 is equivalent to a shift left by 1 bit, division is a right shift. similarly it is the same trivial to multiply and divide by any power of 2. int a = 123; // or in binary format

Modification to “Implementing an N process barrier using semaphores”

喜你入骨 提交于 2019-12-07 17:36:59
问题 Recently I see this problem which is pretty similar to First reader/writer problem. Implementing an N process barrier using semaphores I am trying to modify it to made sure that it can be reuse and work correctly. n = the number of threads count = 0 mutex = Semaphore(1) barrier = Semaphore(0) mutex.wait() count = count + 1 if (count == n){ barrier.signal()} mutex.signal() barrier.wait() mutex.wait() count=count-1 barrier.signal() if(count==0){ barrier.wait()} mutex.signal() Is this correct? I