Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 95ea221

Browse files
minor edits
1 parent 15074d2 commit 95ea221

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/main/java/com/fishercoder/solutions/_396.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.fishercoder.solutions;
22

3-
/**Given an array of integers A and let n to be its length.
3+
/**
4+
* 396. Rotate Function
5+
*
6+
* Given an array of integers A and let n to be its length.
47
58
Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a "rotation function" F on A as follow:
69
@@ -21,6 +24,7 @@ Calculate the maximum value of F(0), F(1), ..., F(n-1).
2124
F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26
2225
2326
So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.*/
27+
2428
//F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1]
2529
public class _396 {
2630
public int maxRotateFunction(int[] A) {
@@ -54,7 +58,7 @@ private int[] rotate(int[] a) {
5458
}
5559

5660
//**credit : https://discuss.leetcode.com/topic/58459/java-o-n-solution-with-explanation
57-
public int maxRotateFunction_1(int[] A) {
61+
public int maxRotateFunctionV2(int[] A) {
5862
int allSum = 0;
5963
int len = A.length;
6064
int F = 0;
@@ -74,6 +78,6 @@ public static void main(String...strings){
7478
int[] nums = new int[]{4, 3, 2, 6};
7579
_396 test = new _396();
7680
System.out.println(test.maxRotateFunction(nums));
77-
System.out.println(test.maxRotateFunction_1(nums));
81+
System.out.println(test.maxRotateFunctionV2(nums));
7882
}
7983
}

src/main/java/com/fishercoder/solutions/_42.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 42. Trapping Rain Water
45
* Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
56
67
For example,
78
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
89
9-
10-
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
10+
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
1111
*/
12+
1213
public class _42 {
1314

1415
public int trap(int[] height) {
1516
int len = height.length;
16-
if(len == 0){
17+
if (len == 0) {
1718
return 0;
1819
}
1920
int res = 0;
2021

21-
//first use DP to calculate out left and right arrays
22+
//first use DP to calculate left and right arrays
2223
int[] left = new int[height.length];
2324
int[] right = new int[height.length];
2425

2526
left[0] = height[0];
26-
for(int i = 1; i < height.length; i++){
27-
left[i] = Math.max(left[i-1], height[i]);
27+
for (int i = 1; i < height.length; i++) {
28+
left[i] = Math.max(left[i - 1], height[i]);
2829
}
2930

30-
right[height.length-1] = height[height.length-1];
31-
for(int i = height.length-2; i >= 0; i--){
32-
right[i] = Math.max(right[i+1], height[i]);
31+
right[height.length - 1] = height[height.length - 1];
32+
for (int i = height.length - 2; i >= 0; i--) {
33+
right[i] = Math.max(right[i + 1], height[i]);
3334
}
3435

35-
for(int i = 1; i < height.length-1; i++){
36+
for (int i = 1; i < height.length - 1; i++) {
3637
res += Math.min(left[i], right[i]) - height[i];
3738
}
3839
return res;

0 commit comments

Comments
 (0)