|
| 1 | +// Runtime: 4 ms (Top 100.0%) | Memory: 61.60 MB (Top 46.55%) |
| 2 | + |
1 | 3 | class Solution {
|
2 |
| - /* |
3 |
| - 1. let's assume we have an array with 2 * n size |
4 |
| - from [2,3,1,4,0] to [2,3,1,4,0,2,3,1,4,0] |
5 |
| - 2. loop through every element from left to right |
6 |
| - index 0 1 2 3 4 5 6 7 8 9 |
7 |
| - [2,3,1,4,0,2,3,1,4,0] |
8 |
| - 3. we can find the different value between index and element, we only keep value >= 0 |
9 |
| - index 0 1 2 3 4 5 6 7 8 9 |
10 |
| - [2,3,1,4,0,2,3,1,4,0] |
11 |
| - value 1 4 3 3 6 4 9 |
12 |
| - 4. we perform sliding window(size n) on this pattern |
13 |
| - l = left pointer of window, r = current right pointer |
14 |
| - index 0 1 2 3 4 5 6 7 8 9 |
15 |
| - [2,3,1,4,0,2,3,1,4,0] |
16 |
| - value 1 4 3 3 6 4 9 : if left pointer pass this value, this value cannot be counted in result |
17 |
| - l r |
18 |
| - value : if left pointer smaller than or equal to 1, we can keep it in window |
19 |
| - (largest left pointer index to keep this element in our window) |
20 |
| - 5. we use variable "sum" to keep tracking current count of valid elements |
21 |
| - but we need to remove invalid value from "sum" |
22 |
| - 6. in order to remove invalid left pointer / value, we need count[] array to mark element count at that index |
23 |
| - before moving left pointer point to next index, we need to use sum - count[left] |
24 |
| - example |
25 |
| - index 0 1 2 3 4 5 6 7 8 9 |
26 |
| - [2,3,1,4,0,2,3,1,4,0] |
27 |
| - value 1 4 3 3 6 4 9 |
28 |
| - left = 0, right = 2, sum = 1, count = {{1:1}} |
29 |
| - left = 0, right = 4, sum = 2, count = {{1:1}, {4:1}} |
30 |
| - left = 1, right = 5, sum = 3, count = {{1:1}, {4:1}, {3:1}} |
31 |
| - after calculate max / possible result, we need to remove count[left] from sum |
32 |
| - left = 1, right = 5, sum = (3 - count[left]) = 2, count = {{1:1}, {4:1}, {3:1}} |
33 |
| - left = 2, right = 6, sum = 3, count = {{1:1}, {4:1}, {3:2}} |
34 |
| - left = 3, right = 7, sum = 4, count = {{1:1}, {4:1}, {3:2}, {6:1}} |
35 |
| - after calculate max / possible result, we need to remove count[left] from sum |
36 |
| - left = 3, right = 7, sum = (4 - count[left]) = 2, count = {{1:1}, {4:1}, {3:2}, {6:1}} |
37 |
| - left = 4, right = 8, sum = 3, count = {{1:1}, {4:2}, {3:2}, {6:1}} |
38 |
| - after calculate max / possible result, we need to remove count[left] from sum |
39 |
| - left = 4, right = 8, sum = (3 - count[left]) = 1, count = {{1:1}, {4:2}, {3:2}, {6:1}} |
40 |
| - .... |
41 |
| - |
42 |
| - |
43 |
| - */ |
44 | 4 | public int bestRotation(int[] nums) {
|
45 |
| - int n = nums.length; |
46 |
| - int[] cnt = new int[2 * n]; |
47 |
| - int max = 0; |
48 |
| - int res = 0; |
49 |
| - for(int r = 0, l = 0, sum = 0; r < 2 * n; r++) { |
50 |
| - int v = r - nums[r % n]; |
51 |
| - if(v >= 0) { |
52 |
| - cnt[Math.min(2 * n - 1, v)]++; |
53 |
| - if(v >= l) { |
54 |
| - sum++; |
55 |
| - } |
56 |
| - } |
57 |
| - if(r - l == n - 1) { |
58 |
| - if(sum > max) { |
59 |
| - max = sum; |
60 |
| - res = l; |
61 |
| - } |
62 |
| - sum -= cnt[l]; |
63 |
| - l++; |
| 5 | + final int size = nums.length; |
| 6 | + int[] rsc = new int[size]; |
| 7 | + for(int i = 0; i < size - 1; i++) { |
| 8 | + int value = nums[i]; |
| 9 | + int downPos = (i + 1 + size - value) % size; |
| 10 | + rsc[downPos]--; |
| 11 | + } |
| 12 | + int value = nums[size-1]; |
| 13 | + if( value != 0 ) rsc[size - value]--; |
| 14 | + int bestk = 0; |
| 15 | + int bestscore = rsc[0]; |
| 16 | + int score = rsc[0]; |
| 17 | + for(int i = 1; i < nums.length; i++) { |
| 18 | + score += rsc[i] + 1; |
| 19 | + if( score > bestscore ) { |
| 20 | + bestk = i; |
| 21 | + bestscore = score; |
64 | 22 | }
|
65 | 23 | }
|
66 |
| - return res; |
| 24 | + return bestk; |
67 | 25 | }
|
68 | 26 | }
|
0 commit comments