Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
pourWater
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { public int[] pourWater2(int[] heights, int V, int K) { int n = heights.length; int[] waters = new int[n]; while (V -- > 0) { int left = K - 1, right = K + 1; int target = K; int curHeight = heights[K] + waters[K]; // leetcode是找到最近的 // 这个是找到最远的 while (left >= 0) { if (heights[left] + waters[left] > heights[left+1] + waters[left+1]) { break; } left -= 1; } if (left + 1 > 0 && heights[left+1] + waters[left+1] < curHeight) { waters[left+1] += 1; continue; } while (right < n) { if (heights[right] + waters[right] > heights[right-1] + waters[right-1]) { break; } right += 1; } if (right-1 < n - 1 && heights[right-1] + waters[right-1] < curHeight) { waters[right-1] += 1; continue; } // System.out.println(left + "\t" + right); // !!如果两边墙高度不一样 if (left < 0 && right >= n) { break; } else if (left < 0 && heights[right-1] + waters[right-1] == curHeight) { break; } else if (right >= n && heights[left+1] + waters[left+1] == curHeight) { break; } waters[K] += 1; } print(heights, waters); /*for (int i = 0; i < n; i++) { heights[i] += waters[i]; }*/ return heights; } private void print(int[] height, int[] waters) { int maxHeight = 0, n = height.length; for (int i = 0; i < n; i++) { maxHeight = Math.max(height[i] + waters[i], maxHeight); } for (int h = maxHeight; h > 0; h--) { for (int i = 0; i < n; i++) { if (height[i] >= h) { System.out.print("#"); } else if (height[i] + waters[i] >= h) { System.out.print("w"); } else { System.out.print(" "); } } System.out.println(); } } public static void main(String[] args) { Rextester s = new Rextester(); // int[] height = {2,1,2,3,2,4}; int[] height = {2,0,2,0,0,3}; for (int i = 0; i < height.length; i++) { s.pourWater2(height, 100, i); System.out.println(" "); } } }
run
|
edit
|
history
|
help
0
4th question
fizzbuzz
3.c
3d
hondaa11
asdasd
Java
js
pk3
names