min

Is there an easy way to make a min heap in C++?

ε祈祈猫儿з 提交于 2019-12-17 22:40:07
问题 I'm very new to C++, and I was wondering if there was a way to make a min heap in C++ from the standard library. 回答1: Use make_heap() and friends, defined in <algorithm> , or use priority_queue , defined in <queue> . The priority_queue uses make_heap and friends underneath. #include <queue> // functional,iostream,ctime,cstdlib using namespace std; int main(int argc, char* argv[]) { srand(time(0)); priority_queue<int,vector<int>,greater<int> > q; for( int i = 0; i != 10; ++i ) q.push(rand()%10

Can we validate Min and Max Value for a floating Number using RegExp?

こ雲淡風輕ζ 提交于 2019-12-17 21:35:29
问题 Hi I know that we can validate Min and Max Length of a number using Regex. But can we validate Min and Max Value for a floating point number using the same? Min Value : 0.00 Max Value :100,000,000.00 Could anyone please just apply Min and Max Value to following Regex: ^(?=.*\d)(?!.*?\.[^.\n]*,)\d*(,\d*,?)*(\.\d*)?$ Above Regex matches a floating number with optional decimal point and commas. 回答1: Regex is for strings. You try to compare floats. It's just not the right tool. It's worse than

Get object with minimum value using extension method min()

雨燕双飞 提交于 2019-12-17 20:51:51
问题 list.Min() gets me the min value as integer. I want to get the object in List which has the min value for a certain property X. How can I do that? 回答1: Have a look at MinBy in MoreLINQ - or I believe Reactive Extensions has something similar in System.Interactive : var cheapestProduct = products.MinBy(p => p.Price); If more than one item has the lowest value, the earliest one in the sequence will be returned. 来源: https://stackoverflow.com/questions/6414052/get-object-with-minimum-value-using

Getting the minimum of the rows in a data frame

微笑、不失礼 提交于 2019-12-17 20:07:16
问题 I am working with a dataframe that has 65 variables in it. The first variable catalogs a person, and the next 64 variables indicate the geographic distance that person is from each of 64 locations. Using R, I would like to create a new variable that catalogs the shortest distance for each person to one of those 64 locations. For example: if person X is 35, 50, 79, 100, 450...miles away from the locations, I would like the new variable to automatically assign them a 35, because this is the

Java - limit number between min and max

僤鯓⒐⒋嵵緔 提交于 2019-12-17 16:45:11
问题 I want to return the number as long as it falls within a limit, else return the maximum or minimum value of the limit. I can do this with a combination of Math.min and Math.max . public int limit(int value) { return Math.max(0, Math.min(value, 10)); } I'm wondering if there's an existing limit or range function I'm overlooking. 3rd party libraries welcome if they are pretty common (eg: Commons or Guava) 回答1: As of version 21, Guava includes Ints.constrainToRange() (and equivalent methods for

Returning all maximum or minimum values that can be multiple

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 10:00:51
问题 Enumerable#max_by and Enumerable#min_by return one of the relevant elements (presumably the first one) when there are multiple max/min elements in the receiver. For example, the following: [1, 2, 3, 5].max_by{|e| e % 3} returns only 2 (or only 5 ). Instead, I want to return all max/min elements and in an array. In the example above, it would be [2, 5] (or [5, 2] ). What is the best way to get this? 回答1: arr = [1, 2, 3, 5] arr.group_by{|a| a % 3} # => {1=>[1], 2=>[2, 5], 0=>[3]} arr.group_by{

Can min/max of moving window achieve in O(N)?

橙三吉。 提交于 2019-12-17 03:19:02
问题 I have input array A A[0], A[1], ... , A[N-1] I want function Max(T,A) which return B represent max value on A over previous moving window of size T where B[i+T] = Max(A[i], A[i+T]) By using max heap to keep track of max value on current moving windows A[i] to A[i+T], this algorithm yields O(N log(T)) worst case. I would like to know is there any better algorithm? Maybe an O(N) algorithm 回答1: O(N) is possible using Deque data structure. It holds pairs (Value; Index). at every step: if (!Deque

Remove NA values from a vector

痞子三分冷 提交于 2019-12-17 02:19:07
问题 I have a huge vector which has a couple of NA values, and I'm trying to find the max value in that vector (the vector is all numbers), but I can't do this because of the NA values. How can I remove the NA values so that I can compute the max? 回答1: Trying ?max , you'll see that it actually has a na.rm = argument, set by default to FALSE . (That's the common default for many other R functions, including sum() , mean() , etc.) Setting na.rm=TRUE does just what you're asking for: d <- c(1, 100,

Getting the index of the returned max or min item using max()/min() on a list

这一生的挚爱 提交于 2019-12-16 22:24:09
问题 I'm using Python's max and min functions on lists for a minimax algorithm, and I need the index of the value returned by max() or min() . In other words, I need to know which move produced the max (at a first player's turn) or min (second player) value. for i in range(9): newBoard = currentBoard.newBoardWithMove([i / 3, i % 3], player) if newBoard: temp = minMax(newBoard, depth + 1, not isMinLevel) values.append(temp) if isMinLevel: return min(values) else: return max(values) I need to be

C++'s min_element() not working for array [closed]

℡╲_俬逩灬. 提交于 2019-12-14 03:29:54
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I am trying to find the minimal element of a subset of an array in c++ as follows: #include <iostream> #include <algorithm> using std::cin; using std::cout; using std::min_element; void load_values(int n); unsigned *w; int main() { int n, a, b; cin >> n >> a >> b; w = new unsigned[n]; load_values(n