Skip to content

Commit 68dac90

Browse files
committed
array partitionI and reshape matrix
1 parent 4606482 commit 68dac90

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

Array/Array.Lib/Array.Lib.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@
4040
<Reference Include="System.Xml" />
4141
</ItemGroup>
4242
<ItemGroup>
43+
<Compile Include="ArrayPartitionISln.cs" />
4344
<Compile Include="PascalsTriangle.cs" />
4445
<Compile Include="PascalsTriangleII.cs" />
4546
<Compile Include="PlusOneSln.cs" />
4647
<Compile Include="Properties\AssemblyInfo.cs" />
48+
<Compile Include="ReshapeMatrixSln.cs" />
4749
<Compile Include="SearchInsertPosition.cs" />
4850
<Compile Include="ThirdMaximumNumber.cs" />
4951
</ItemGroup>

Array/Array.Lib/ArrayPartitionISln.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Array.Lib
7+
{
8+
public class ArrayPartitionISln
9+
{
10+
// Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.
11+
12+
//Example 1:
13+
//Input: [1,4,3,2]
14+
15+
//Output: 4
16+
//Explanation: n is 2, and the maximum sum of pairs is 4.
17+
public int ArrayPairSum(int[] nums)
18+
{
19+
Array.Sort(nums);
20+
int rtn = 0;
21+
for (int i = 0; i < nums.Length; i = i + 2)
22+
rtn += nums[i];
23+
return rtn;
24+
}
25+
}
26+
}

Array/Array.Lib/ReshapeMatrixSln.cs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Array.Lib
7+
{
8+
// In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.
9+
10+
//You're given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.
11+
12+
//The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.
13+
14+
//If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
15+
16+
//Example 1:
17+
//Input:
18+
//nums =
19+
//[[1,2],
20+
// [3,4]]
21+
//r = 1, c = 4
22+
//Output:
23+
//[[1,2,3,4]]
24+
//Explanation:
25+
//The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.
26+
//Example 2:
27+
//Input:
28+
//nums =
29+
//[[1,2],
30+
// [3,4]]
31+
//r = 2, c = 4
32+
//Output:
33+
//[[1,2],
34+
// [3,4]]
35+
//Explanation:
36+
//There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.
37+
public class ReshapeMatrixSln
38+
{
39+
// i*c+j = si*dim1+sj;
40+
public int[,] MatrixReshape(int[,] nums, int r, int c)
41+
{
42+
int dim0 = nums.GetUpperBound(0);
43+
int dim1 = nums.GetUpperBound(1);
44+
int si = 0, sj = 0;
45+
if (r * c != (dim0+1) * (dim1+1)) return nums;
46+
int[,] rtn = new int[r, c];
47+
for (int i = 0; i < r; i++) {
48+
for (int j = 0; j < c; j++,sj++){
49+
if (sj > dim1){
50+
si++;
51+
sj = 0;
52+
}
53+
rtn[i, j] = nums[si, sj];
54+
}
55+
}
56+
return rtn;
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)