algorithm

Counting Primitive Operations & Calculating Big-O Notation

纵饮孤独 提交于 2021-02-11 09:44:05
问题 I've written a piece of java code that when given an array (arrayX), works out the prefix averages of that array and outputs them in another array (arrayA). I am supposed to count the primitive operations and calculate the Big-O notation (which i'm guessing is the overall number of calculations). I have included the java code and what I believe to be the number of primitive operations next to each line, but I am unsure whether I have counted them correctly. Thanks in advance and sorry for my

Arrangement of arrays such that maximum number of elements are greater in one array

本小妞迷上赌 提交于 2021-02-11 08:53:14
问题 Assuming I have two arrays A and B (Both with equal number of elements) int A[] = {40,50,70}; int B[] = {80,60,45}; I have to rearrange array A in such a way that maximum number of elements in Array A are greater than their respective elements in array B. In this case, rearranging A as {40,70,50} would yield the required result. What would be the most optimal way of going this? 回答1: I would use something like: std::vector<int> f(std::vector<int> A, const std::vector<int>& B) { std::vector<std

Algorithm for locating the closest destination(s) to source(s) and resolving conflicts in case a single destination is mapped to multiple sources

跟風遠走 提交于 2021-02-11 08:22:06
问题 Problem Statement: Given a matrix of people(denoted by small alphabets) and bikes(denoted by capital alphabets), find the nearest bike for a given person. How will you change your solution if you have to find bikes for a set of people? (assuming multiple bikes can be at the same distance from 1 person)? I know Dijkstra's algorithm and Bellman Ford's algorithm, but curious about the implementation here. Or can it be solved by BFS(Breadth First Search)? 回答1: To assign a single bike to a single

Algorithm for locating the closest destination(s) to source(s) and resolving conflicts in case a single destination is mapped to multiple sources

大兔子大兔子 提交于 2021-02-11 08:16:56
问题 Problem Statement: Given a matrix of people(denoted by small alphabets) and bikes(denoted by capital alphabets), find the nearest bike for a given person. How will you change your solution if you have to find bikes for a set of people? (assuming multiple bikes can be at the same distance from 1 person)? I know Dijkstra's algorithm and Bellman Ford's algorithm, but curious about the implementation here. Or can it be solved by BFS(Breadth First Search)? 回答1: To assign a single bike to a single

Calculating the complexity of algorithm to print all valid (i.e., properly opened and closed) combinations of n-pairs of parentheses

我的梦境 提交于 2021-02-11 07:50:48
问题 I would like your opinion on the time and space complexity of this algorithm I implemented (in Python) to calculate the complexity of algorithm to print all valid (i.e., properly opened and closed) combinations of n-pairs of parentheses (see all valid combinations of n-pair of parenthesis) def find_par_n(n): s = set(["()"]) for i in range(2, n + 1): set_to_add = set() for str_ in s: set_temp = set() ana = set() for j in range(len(str_) + 1): str_c = str_[0:j] + '(' + str_[j:] if str_c in ana:

Is there any data structure in C++ STL for performing insertion, searching and retrieval of kth element in log(n)?

好久不见. 提交于 2021-02-11 04:29:50
问题 I need a data structure in c++ STL for performing insertion, searching and retrieval of kth element in log(n) (Note: k is a variable and not a constant) I have a class like class myClass{ int id; //other variables }; and my comparator is just based on this id and no two elements will have the same id. Is there a way to do this using STL or I've to write log(n) functions manually to maintain the array in sorted order at any point of time? 回答1: Afaik, there is no such datastructure. Of course,

How to find all the possible permutations of a matrix in R?

喜欢而已 提交于 2021-02-10 23:20:01
问题 I have a matrix, for example, 5x5. [,1] [,2] [,3] [,4] [,5] [1,] 22 -2 -2 -2 2 [2,] -2 22 2 2 2 [3,] -2 2 22 2 2 [4,] -2 2 2 22 2 [5,] 2 2 2 2 22. As you can see, the matrix is symmetric. Above the main diagonal, there are 4+3+2+1=10 positions, and I find via combn all the possible (permutation) matrices, which have (-2) 3 times in these 10 positions. That means 10!/3!*7!=120 matrices. But some of them are equivalent. So,my problem is how to find the non-equivalent matrices from the 120. I am

How to find all the possible permutations of a matrix in R?

天大地大妈咪最大 提交于 2021-02-10 23:19:03
问题 I have a matrix, for example, 5x5. [,1] [,2] [,3] [,4] [,5] [1,] 22 -2 -2 -2 2 [2,] -2 22 2 2 2 [3,] -2 2 22 2 2 [4,] -2 2 2 22 2 [5,] 2 2 2 2 22. As you can see, the matrix is symmetric. Above the main diagonal, there are 4+3+2+1=10 positions, and I find via combn all the possible (permutation) matrices, which have (-2) 3 times in these 10 positions. That means 10!/3!*7!=120 matrices. But some of them are equivalent. So,my problem is how to find the non-equivalent matrices from the 120. I am

Tic Tac Toe AI Bugs

北慕城南 提交于 2021-02-10 22:27:42
问题 I'm trying to implement an AI for Tic Tac Toe that is smart enough to never lose. I've tried two different algorithms but the AI still makes mistakes. I started with this minimax alpha-beta pruning algorithm. Here's a live demo: http://iioengine.com/ttt/minimax.htm It runs without error, but if you take the bottom left corner first, then either of the other two squares on the bottom row - the AI doesn't see that coming. I'm sure this isn't a flaw in the minimax algorithm - can anyone see an

Tic Tac Toe AI Bugs

江枫思渺然 提交于 2021-02-10 22:26:33
问题 I'm trying to implement an AI for Tic Tac Toe that is smart enough to never lose. I've tried two different algorithms but the AI still makes mistakes. I started with this minimax alpha-beta pruning algorithm. Here's a live demo: http://iioengine.com/ttt/minimax.htm It runs without error, but if you take the bottom left corner first, then either of the other two squares on the bottom row - the AI doesn't see that coming. I'm sure this isn't a flaw in the minimax algorithm - can anyone see an