algorithm

Adjacent Neighbor Summation [closed]

柔情痞子 提交于 2021-01-27 14:56:08
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 8 years ago . Improve this question If I am given the numbers [1,2,1] and I want to find the sum of each number with it's adjacent neighbors (in a ring) repeated a number of times. I can use the following formula: base case: [x=1, y=2, z=1] repeat 7 times. staring with index 0 or (variable x): round 1 index 0: [(x+y+z), y, z]

Adjacency list with O(1) look up time using HashSet?

会有一股神秘感。 提交于 2021-01-27 14:32:17
问题 In my algorithms class I've been told that a draw back of Adjacency Lists for graph representation is the O(n) look up time for iterating through the array of adjacent nodes corresponding to each node. I implement my adjacency list by using a HashMap that maps nodes to a HashSet of their adjacent nodes, wouldn't that only take O(1) look up time? Is there something I'm missing? 回答1: As you know look up for value using key in HashMap is O(1). However, in adjacency list the value of the HashMap

Car Fueling Problem by Greedy Alogorithm (getting list index out of range)

孤人 提交于 2021-01-27 14:23:48
问题 I have a small problem solving the Car fueling problem using the Greedy Algorithm. Problem Introduction You are going to travel to another city that is located 𝑑 miles away from your home city. Your car can travel at most 𝑚 miles on a full tank and you start with a full tank. Along your way, there are gas stations at distances stop1 stop2 . . . ,stopN from your home city. What is the minimum number of refills needed? Input: 950 400 4 200 375 550 750 Output: 2 What I've tried as of now def car

binary search tree path list

僤鯓⒐⒋嵵緔 提交于 2021-01-27 13:47:36
问题 This is code to get all the root to leaf paths in a Binary Tree but it puts in all the paths concatenated into one path. What is going wrong with the recursive call? private void rec(TreeNode root,List<Integer> l, List<List<Integer>> lists) { if (root == null) return; if (root.left == null && root.right == null ) { l.add(root.val); lists.add(l); } if (root.left != null) { l.add(root.val); rec(root.left,l,lists); } if (root.right != null) { l.add(root.val); rec(root.right,l,lists); } } 回答1:

Detecting copied or similar text blocks

时光总嘲笑我的痴心妄想 提交于 2021-01-27 13:43:03
问题 I have a bunch of texts about programming in Markdown format. There is a build process that is capable of converting those texts into Word/HTML and also perform simple validation rules like spell checking or checking if document has required header structure. I would like to extend that build code to also check for copy-pasted or similar chunks within all texts. Is there any existing Java/Groovy library that can help me with that analysis? My first idea was to use PMD's CopyPasteDetector, but

Restart a loop in Fortran

落花浮王杯 提交于 2021-01-27 13:42:55
问题 I have an algorithm that looks like this: 10 WRITE (*,*) "Start" DO I = 1, 10 WRITE (*,*) "Step" IF(I .EQ. 5) then go to 10 END IF END DO I want to restart the loop, when that if statement executes. However, I don't want to have to use a go to, I tried this: 10 WRITE (*,*) "Start" DO I = 1, 10 WRITE (*,*) "Step" IF(I .EQ. 5) then I = 0; CYCLE END IF END DO But then I get the error that I can't redefine the I variable, inside a loop. So I'm not sure how to approach this. Any help would be much

Fast Algorithm to Factorize All Numbers Up to a Given Number

[亡魂溺海] 提交于 2021-01-27 13:19:25
问题 I am looking for an algorithm that could factorize numbers based on numbers it already factorized. In other words, I am searching for a fast algorithm to factorise all numbers up to a given number, and store them in a (I guess this is the easiest data structure to use) list / tuple of tuples. I am looking for an "up to n" algorithm because I need all numbers up to "n", and I guess it's faster than just checking one by one. I want this algorithm to work within a reasonable time (less than an

Complexity for Binary Search (Insertion) for an ordered list

家住魔仙堡 提交于 2021-01-27 13:18:31
问题 I understand that binary search cannot be done for an unordered array. I also understand that the complexity of a binary search in an ordered array is O(log(n)) . Can I ask what is the complexity for binary search(insertion) for an ordered array? I saw from a textbook, it stated that the complexity is O(n) . Why isn't it O(1) since, it can insert directly, just like linear search. Since binary search can't be done in unordered list, why is it possible to do insertion, with a complexity of O(N

Calculating How Many Balls in Bins Over Several Values Using Dynamic Programming [closed]

假如想象 提交于 2021-01-27 12:43:39
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . Improve this question Regarding the classic problem of putting N identical balls into M distinct bins and printing all the combinations: What if you would want to extend the problem by printing all cases 0< M, N The brute force method could be done something like this: for (int i

Algorithmic way to search a list of tuples for a matching substring?

断了今生、忘了曾经 提交于 2021-01-27 12:43:31
问题 I have a list of tuples, about 100k entries. Each tuple consists of an id and a string, my goal is to list the ids of the tuples, whose strings contain a substring from a given list of substrings. My current solution is through set comprehension, ids can repeat. tuples = [(id1, 'cheese trees'), (id2, 'freezy breeze'),...] vals = ['cheese', 'flees'] ids = {i[0] for i in tuples if any(val in i[1] for val in vals)} output: {id1} Is there an algorithm that would allow doing that quicker? I'm