sub-array

Count unique values in subarrays

坚强是说给别人听的谎言 提交于 2019-12-25 09:49:38
问题 Array a*b is given, which contains numbers up to 1e5, and we have to sum the count of unique numbers in every k*k subarray, there are a-k*b-k subarrays e.g 1 2 3 4 3 2 4 1 for k=2 subarrays are {1,2,3,2}(3distinct values) {2,3,2,4}(3distinct values) {3,4,4,1}(3distinct values) output is 9 Is there a faster approach than using a table which stores count of every number ocurrencies in an actaully processed k*k subarray(e.g at index 3 we store count of 3's in a subarray), moving a k*k window by

Get the sub array in a bidimensional array having a particular key/value pair

蹲街弑〆低调 提交于 2019-12-19 03:59:30
问题 I have a large PHP array, similar to: $list = array( array( 'id' = '3243' 'link' = 'fruits' 'lev' = '1' ), array( 'id' = '6546' 'link' = 'apple' 'lev' = '2' ), array( 'id' = '9348' 'link' = 'orange' 'lev' = '2' ) ) I want to get the sub-array which contains a particular id . Currently I use the following code: $id = '3243' foreach ($list as $link) { if (in_array($id, $link)) { $result = $link; } } It works but I hope there is a better way of doing this. 回答1: You can write $link['id']==$id

Split multidimensional array in its sub_arrays

杀马特。学长 韩版系。学妹 提交于 2019-12-13 21:27:09
问题 I have just had this issue. I have a multidimensional array ($varianti) that looks like this: Array ( [pa_taglia] => Array ( [0] => l [1] => m ) [pa_colore] => Array ( [0] => blu [1] => giallo [2] => rosso ) ) What I need is to get different arrays for each sub array so I need this result: Array ( [0] => l [1] => m ) Array ( [0] => blu [1] => giallo [2] => rosso ) The main problem is that I can get as many sub-arrays as needed (this is for my Woocommerce plugin to create product_variations

Swift Array instance method drop(at: Int)

可紊 提交于 2019-12-13 03:47:49
问题 Array in Swift has several instance methods for excluding elements, such as dropFirst(), dropLast(), drop(while:), etc. What about drop(at:) ? Note: I'd use remove(at:), but the array I'm working with is a let constant. 回答1: Note: I'd use remove(at:) , but the array I'm working with is a constant. I wouldn't let that stop you: extension Array { func drop(at:Int) -> [Element] { var temp = self temp.remove(at: at) return temp } } let numbers = [1, 2, 3, 4, 5] print(numbers.drop(at: 2)) // [1, 2

Problem with Divide and Conquer Maximum Consecutive Subarray (MCS)

浪尽此生 提交于 2019-12-12 20:47:37
问题 I want to find a nonempty, contiguous subarray for a given input array of integers, that can have duplicate values. I tried the divide and conquer method to find the maximum consecutive subarray of an array, which returns a different result as expected. Please find the code below. private static int maxSumRec(int[] a, int low, int high) { int leftSum = 0, rightSum = 0; int sum = 0; if (low == high) { // Base case return a[low]; } int mid = (low + high) >> 1; // (low + high) / 2 int maxLeftSum

Filter a 2D numpy array

家住魔仙堡 提交于 2019-12-12 07:28:28
问题 I want to have a sub array (between min and max) of a numpy 2D ndarray xy_dat = get_xydata() x_displayed = xy_dat[((xy_dat > min) & (xy_dat < max))] min and max are float in order to be compare with the first value of the array xy_dat xy_dat is a 2D numpy array : [[ 735964. 1020. ] [ 735964.04166667 1020. ] [ 735964.08333333 1020. ] ..., [ 736613.39722222 1095. ] [ 736613.40416667 1100. ] [ 736613.41111111 1105. ]] x_displayed is correctly filtered but I have lost the second value (it is now

Julia: does an Array contain a specific sub-array

a 夏天 提交于 2019-12-10 14:38:42
问题 In julia we can check if an array contains a value, like so: > 6 in [4,6,5] true However this returns false, when attempting to check for a sub-array in a specific order: > [4,6] in [4,6,5] false What is the correct syntax to verify if a specific sub-array exists in an array? 回答1: For the third condition i.e. vector [4,6] appears as a sub-vector of 4,6,5 the following function is suggested: issubvec(v,big) = any([v == slice(big,i:(i+length(v)-1)) for i=1:(length(big)-length(v)+1)]) For the

Extract two sub array values in mongodb by $elemMatch

余生长醉 提交于 2019-12-08 05:04:38
问题 Aggregate, $unwind and $group is not my solution as they make query very slow, there for I am looking to get my record by db.collection.find() method. The problem is that I need more then one value from sub array. For example from the following example I want to get the "type" : "exam" and "type" : "quiz" elements. { "_id" : 22, "scores" : [ { "type" : "exam", "score" : 75.04996547553947 }, { "type" : "quiz", "score" : 10.23046475899236 }, { "type" : "homework", "score" : 96.72520512117761 },

Extract two sub array values in mongodb by $elemMatch

心不动则不痛 提交于 2019-12-06 15:45:18
Aggregate, $unwind and $group is not my solution as they make query very slow, there for I am looking to get my record by db.collection.find() method. The problem is that I need more then one value from sub array. For example from the following example I want to get the "type" : "exam" and "type" : "quiz" elements. { "_id" : 22, "scores" : [ { "type" : "exam", "score" : 75.04996547553947 }, { "type" : "quiz", "score" : 10.23046475899236 }, { "type" : "homework", "score" : 96.72520512117761 }, { "type" : "homework", "score" : 6.488940333376703 } ] } I am looking something like db.students.find(

find minimum-length subarray that has all numbers

淺唱寂寞╮ 提交于 2019-12-04 23:49:42
问题 File input.txt consists of two lines: first has integer number N space then integer number K (1 ≤ N,K ≤ 250000). Second has N space-delimeted integers, where each integer is less than or equal to K. It is guaranteed that each integer from 1 to K is in the array. The task is to find subarray of minimum length, that contains all integers. And print its start and end. Note, that indexing starts from 1. Examples: Input Output 5 3 2 4 1 2 1 3 2 6 4 2 6 2 4 2 3 3 1 I had this task in a recent