|
| 1 | +/** |
| 2 | + * 1089 |
| 3 | + * Duplicate Zeros |
| 4 | + ** |
| 5 | + * Given a fixed-length integer array arr, |
| 6 | + * duplicate each occurrence of zero, |
| 7 | + * shifting the remaining elements to the right. |
| 8 | + * |
| 9 | + * Note that elements beyond the length of the original array are not written. |
| 10 | + * Do the above modifications to the input array in place and do not return anything. |
| 11 | + * |
| 12 | + * Example 1: |
| 13 | + * Input: arr = [1,0,2,3,0,4,5,0] |
| 14 | + * Output: [1,0,0,2,3,0,0,4] |
| 15 | + * Explanation: |
| 16 | + * After calling your function, |
| 17 | + * the input array is modified to: [1,0,0,2,3,0,0,4] |
| 18 | + * |
| 19 | + * Example 2: |
| 20 | + * Input: arr = [1,2,3] |
| 21 | + * Output: [1,2,3] |
| 22 | + * Explanation: |
| 23 | + * After calling your function, |
| 24 | + * the input array is modified to: [1,2,3] |
| 25 | + * |
| 26 | + * Constraints: |
| 27 | + * • 1 <= arr.length <= 10^4 |
| 28 | + * • 0 <= arr[i] <= 9 |
| 29 | + * |
| 30 | + * Hint 1: |
| 31 | + * This is a great introductory problem |
| 32 | + * for understanding and working with the concept of in-place operations. |
| 33 | + * The problem statement clearly states that we are to modify the array in-place. |
| 34 | + * That does not mean we cannot use another array. |
| 35 | + * We just don't have to return anything. |
| 36 | + * |
| 37 | + * Hint 2: |
| 38 | + * A better way to solve this would be without using additional space. |
| 39 | + * The only reason the problem statement allows you to make modifications in place |
| 40 | + * is that it hints at avoiding any additional memory. |
| 41 | + * |
| 42 | + * Hint 3: |
| 43 | + * The main problem with not using additional memory |
| 44 | + * is that we might override elements |
| 45 | + * due to the zero duplication requirement of the problem statement. |
| 46 | + * How do we get around that? |
| 47 | + * |
| 48 | + * Hint 4: |
| 49 | + * If we had enough space available, |
| 50 | + * we would be able to accommodate all the elements properly. |
| 51 | + * The new length would be the original length of the array plus the number of zeros. |
| 52 | + * Can we use this information somehow to solve the problem? |
| 53 | + ** |
| 54 | + * https://leetcode.com/problems/duplicate-zeros/ |
| 55 | +***/ |
| 56 | + |
| 57 | +namespace Problems; |
| 58 | + |
| 59 | +public class DuplicateZerosSolution |
| 60 | +{ |
| 61 | + public void DuplicateZeros( int[] arr ) |
| 62 | + { |
| 63 | + int left = 0; |
| 64 | + int right = arr.Length - 1; |
| 65 | + int shiftCount = 0; |
| 66 | + |
| 67 | + while ( left + shiftCount < arr.Length ) |
| 68 | + { |
| 69 | + if ( arr[left] == 0 ) |
| 70 | + { |
| 71 | + arr[right] = 0; |
| 72 | + |
| 73 | + shiftCount++; |
| 74 | + right--; |
| 75 | + } |
| 76 | + |
| 77 | + left++; |
| 78 | + } |
| 79 | + |
| 80 | + left--; |
| 81 | + |
| 82 | + while ( left >= 0 && shiftCount > 0 ) |
| 83 | + { |
| 84 | + if ( arr[left] == 0 ) |
| 85 | + { |
| 86 | + shiftCount--; |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + arr[left + shiftCount] = arr[left]; |
| 91 | + arr[left] = 0; |
| 92 | + } |
| 93 | + |
| 94 | + left--; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments