matrix

Largest rectangular sub matrix with the number difference k

北城余情 提交于 2020-01-25 19:00:26
问题 Input: A 2-dimensional array NxN - Matrix - with numbers from 0 to 9. Output: Largest rectangular area where absolute value of number's difference in area is k . Possible input: int k=3; const int N=5; int matrix[N][N] = {{9, 3, 8, 2, 0}, {2, 7, 6, 8, 5}, {8, 5, 7, 7, 6}, {3, 0, 4, 0, 9}, {7, 2, 0, 4, 0}}; Is it related to find largest area in histogram problem? If it does how I can transform this matrix two binary matrix? And how to approach this kind of problems? Answer: largest area is 8

用JavaScript刷LeetCode的正确姿势

爷,独闯天下 提交于 2020-01-25 18:09:52
虽然很多人都觉得前端算法弱,但其实 JavaScript 也可以刷题啊!最近两个月断断续续刷完了 leetcode 前 200 的 middle + hard ,总结了一些刷题常用的模板代码。 走过路过发现 bug 请指出,拯救一个辣鸡(但很帅)的少年就靠您啦! 常用函数 包括打印函数和一些数学函数。 const _max = Math.max.bind(Math); const _min = Math.min.bind(Math); const _pow = Math.pow.bind(Math); const _floor = Math.floor.bind(Math); const _round = Math.round.bind(Math); const _ceil = Math.ceil.bind(Math); const log = console.log.bind(console); //const log = _ => {} log 在提交的代码中当然是用不到的,不过在调试时十分有用。但是当代码里面加了很多 log 的时候,提交时还需要一个个注释掉就相当麻烦了,只要将 log 赋值为空函数就可以了。 举一个简单的例子,下面的代码是可以直接提交的。 // 计算 1+2+...+n // const log = console.log.bind(console);

54#螺旋矩阵

孤街浪徒 提交于 2020-01-25 18:05:31
题目: 题解: public List < Integer > spiralOrder ( int [ ] [ ] matrix ) { if ( matrix . length == 0 ) return new ArrayList ( ) ; int m = matrix . length ; int n = matrix [ 0 ] . length ; ArrayList result = new ArrayList ( ) ; int north = 0 ; //北 int south = m - 1 ; //南 int west = 0 ; //西 int east = n - 1 ; //东 int count1 = 0 ; int count2 = 1 ; result . add ( matrix [ 0 ] [ 0 ] ) ; while ( result . size ( ) < m * n ) { if ( north < ( m + 1 ) / 2 ) { while ( west != east ) { result . add ( matrix [ count1 ] [ ++ west ] ) ; if ( result . size ( ) == m * n ) return result ; } east -- ; } if ( west >= (

Create a matrix with conditional sum in each row -R

梦想与她 提交于 2020-01-25 11:35:50
问题 I've created this matrix: > atr <- matrix(rnorm(18,50,3), nrow=9, ncol=2) > atr [,1] [,2] [1,] 49.1 46.3 [2,] 49.9 49.2 [3,] 52.3 51.6 [4,] 49.3 46.1 [5,] 54.3 51.8 [6,] 46.7 47.2 [7,] 46.6 57.6 [8,] 53.9 53.4 [9,] 46.6 53.1 How Can I create the same matrix with values equal to rnorm(18,50,3) but with the condition that the sum of values on each row must be less or equal than 100. 回答1: I would do something like this nrow <- 9 ncol <- 2 mat <- matrix(nrow = nrow, ncol = ncol) i <- 1 while (i <

Covariance Matrix Python - Omit -9999 Value

 ̄綄美尐妖づ 提交于 2020-01-25 11:02:25
问题 I'm trying to calculate the co-variance matrix of two completely overlapping images using python. The code for the same is: stacked = np.vstack((image1.ravel(),image2.ravel())) np.cov(stacked) The issue with using this method is that sometimes the images may contain a NoData value like -9999 signifying that the pixel value isn't present. In such a case the np.cov still considers the value causing the mean of the images to drastically vary giving the wrong covariance output. If I try to remove

How to find n largest elements in an array and make the other elements zero in matlab? [duplicate]

强颜欢笑 提交于 2020-01-25 09:30:08
问题 This question already has answers here : Get the indices of the n largest elements in a matrix (4 answers) Closed 5 years ago . Suppose I have a matrix A=[2 3 4; 6 1 2] I want to find 2 largest elements and make all the other elements zero. In this case A finally becomes A=[0 0 4; 6 0 0] 回答1: Your line of action should be: Sort the matrix in descending order and obtain the order of the indices of the sorted elements. Discard the first two indices and use the rest to zero out the corresponding

leetcode-螺旋矩阵

可紊 提交于 2020-01-25 07:57:05
class Solution { private List<Integer> retlist = new LinkedList<>(); private int rowlength; private int columnlength; public List<Integer> spiralOrder(int[][] matrix) { if(matrix.length==0){ return this.retlist; } this.rowlength = matrix.length; this.columnlength = matrix[0].length; int index = 0; int maxcount = matrix[0].length*matrix.length; this.dfs(0,0,matrix,1,maxcount,0); return this.retlist; } public void dfs(int row,int column,int[][] matrix,int order,int maxcount,int index){ this.retlist.add(matrix[row][column]); index+=1; if(maxcount==index){ return; } //接下来判断方向 //1是右,2是左,3是上,4是下 if

random matrices 2

不问归期 提交于 2020-01-25 07:28:09
问题 I would like to generate 100 random square 6x6 matrices A=[a_{ij}] with Gaussian noise (modification with standard deviation 0.5) satisfying the following characteristics: 1. multiplicative inverse: i.e., a_{ij}=1/a_{ji} for all i,j=1,2,...,6. 2. all entries are positive: i.e., a_{ij}>0 for all i,j=1,2,...,6. 3. all diagonal elements are 1: i.e, a_{ii}=1 for all i=1,2,..,6. 4. transitive: i.e., a_{ih}*a_{hj}=a_{ij} for all i,j,h=1,2,...,6. So far, I tried to use a matlab function randn(6)*0.5

C program that dynamically allocates and fills 2 matrices, verifies if the smaller one is a subset of the other, and checks a condition

此生再无相见时 提交于 2020-01-25 06:59:09
问题 This question was migrated from Stack Overflow because it can be answered on Code Review Stack Exchange. Migrated 4 days ago . Yesterday, I asked this question because I was asked to implement a software that used variably sized matrices with C89's limitation, so I had to practise dynamic pointer-to-pointer allocation. After the useful answers I got, I started working on a solution to a problem that I was assigned as an exercise. Here's the assignment specification: Implement a C program that

finding an element in an array by comparing it with another array

空扰寡人 提交于 2020-01-25 06:42:11
问题 I have a matrix a = [ 1 'cancer' 2 'cancer' 3 'cancer' 4 'noncancer' 5 'noncancer' ] I have another matrix with values b = [ 4 5 2 ] Now I have to compare the b matrix values with values of a and the output should be output = [ 4 'noncancer' 5 'noncancer' 2 'cancer'] How can I do this in matlab ? 回答1: You can use ismember: a = { 1 'cancer' 2 'cancer' 3 'cancer' 4 'noncancer' 5 'noncancer' }; b = [ 4 5 2 ]; a(ismember([a{:,1}], b),:) This results in ans = [2] 'cancer' [4] 'noncancer' [5]