Skip to content

Commit 0e29b93

Browse files
committed
Merge branch 'master' of github.com:jackzhenguo/leetcode-csharp
2 parents 197a728 + 23f8b80 commit 0e29b93

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

Math/Math.Lib/MinimumMovesSln.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace leetcodeTest
6+
{
7+
// Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.
8+
9+
//Example:
10+
11+
//Input:
12+
//[1,2,3]
13+
14+
// Output:
15+
//3
16+
17+
//Explanation:
18+
//Only three moves are needed(remember each move increments two elements):
19+
20+
//[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
21+
//[1,3,3] => [3,3,5] => [5,5,5]
22+
public class MinimumMovesSln
23+
{
24+
public int MinMoves(int[] nums)
25+
{
26+
//assume moving m times, so m*(n-1) + sum_init = n * (min+m) // min+m is the final number
27+
//sum = n*min + m => m = sum - n* min;
28+
int min = nums[0];
29+
int sum = 0;
30+
for (int i = 0; i < nums.Length; i++)
31+
{
32+
if (nums[i] < min) min = nums[i];
33+
sum += nums[i];
34+
}
35+
return sum - nums.Length * min;
36+
}
37+
38+
}
39+
}

README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
* Welcome to visit my CSDN blog(http://blog.csdn.net/daigualu)
33
* CSDN Column(http://blog.csdn.net/column/details/14761.html) where detail solutions are.
44
## `Today Update`
5-
### Tree
6-
#### 105 Construct Binary Tree from Preorder and Inorder Traversal
7-
* [Github:#105 Construct Binary Tree from Preorder and Inorder Traversal](/Tree/Tree.TreeLib/BuildTreeByPreAndInorder.cs)
8-
* [CSDN:#105 Construct Binary Tree from Preorder and Inorder Traversal](http://blog.csdn.net/daigualu/article/details/72127022)
5+
### Math
6+
#### 453 Minimum Moves to Equal Array Elements
7+
* [Github:#453 Minimum Moves to Equal Array Elements](/Math/Math.Lib/MinimumMovesSln.cs)
8+
* [CSDN:#453 Minimum Moves to Equal Array Elements](http://blog.csdn.net/daigualu/article/details/72354061)
99
* Tips:
10-
* the most important function in solving this issue is
11-
* private TreeNode bulidTree(int preStart, int inStart, int inEnd) ;
12-
* Plus, preStart index in preorder is the root index, which is also the separate point in inorder and it’s left is left subtree and right is right subtree.
10+
* using Math equation to solve this issue!
11+
1312
---
1413
---
1514

@@ -228,4 +227,11 @@ Tags are following:
228227
* apply 1: valid stack oepration: ((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))
229228
* apply 2: ![binary trees](/Tree/Tree.TreeLib/binarytrees.jpg)
230229
* apply 3: ![triangles](/Tree/Tree.TreeLib/triangles.jpg)
231-
* apply 4: ![stairs](/Tree/Tree.TreeLib/stairs.jpg)
230+
* apply 4: ![stairs](/Tree/Tree.TreeLib/stairs.jpg)
231+
#### 105 Construct Binary Tree from Preorder and Inorder Traversal
232+
* [Github:#105 Construct Binary Tree from Preorder and Inorder Traversal](/Tree/Tree.TreeLib/BuildTreeByPreAndInorder.cs)
233+
* [CSDN:#105 Construct Binary Tree from Preorder and Inorder Traversal](http://blog.csdn.net/daigualu/article/details/72127022)
234+
* Tips:
235+
* the most important function in solving this issue is
236+
* private TreeNode bulidTree(int preStart, int inStart, int inEnd) ;
237+
* Plus, preStart index in preorder is the root index, which is also the separate point in inorder and it’s left is left subtree and right is right subtree.

0 commit comments

Comments
 (0)