GeezerVison is showing github.com — 443_String_Compression and 453_Minimum_Moves_to_Equal_Array_Elements · qiyuangong/leetcode@46b842a
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: README.md +2Lines changed: 2 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -129,6 +129,8 @@ Remember solutions are only solutions to given problems. If you want full study |
129 | 129 | | 421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/421_Maximum_XOR_of_Two_Numbers_in_an_Array.py) | Check 0~32 prefix, check if there is x y in prefixes, where x ^ y = answer ^ 1, O(32n) and O(n) | |
130 | 130 | | 422 | [Valid Word Square](https://leetcode.com/problems/valid-word-square/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/422_Valid_Word_Square.py) | Compare row with column, O(n^2) | |
131 | 131 | | 434 | [Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/434_Number_of_Segments_in_a_String.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/434_Number_of_Segments_in_a_String.java) | 1. trim &split<br>2. Find segment in place | |
| 132 | +| 443 | [String Compression](https://leetcode.com/problems/string-compression/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/443_String_Compression.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/443_String_Compression.java) | Maintain curr, read, write and anchor (start of this char). | |
| 133 | +| 453 | [Number of Segments in a String](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/453_Minimum_Moves_to_Equal_Array_Elements.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/453_Minimum_Moves_to_Equal_Array_Elements.java) | Each move is equal to minus one element in array, so the answer is the sum of all elements after minus min. | |
132 | 134 | | 463 | [Island Perimeter](https://leetcode.com/problems/island-perimeter/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/463_Island_Perimeter.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/463_Island_Perimeter.java) | math, find the area, actual number, then find the digit | |
133 | 135 |
|
134 | 136 | | # | To Understand | |
|
Collapse file
Copy file name to clipboard +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +class Solution { |
| 2 | + public int compress(char[] chars) { |
| 3 | + // https://leetcode.com/problems/string-compression/solution/ |
| 4 | + int anchor = 0, write = 0; |
| 5 | + for (int read = 0; read < chars.length; read++) { |
| 6 | + if (read + 1 == chars.length || chars[read + 1] != chars[read]) { |
| 7 | + chars[write++] = chars[anchor]; |
| 8 | + if (read > anchor) { |
| 9 | + for (char c: ("" + (read - anchor + 1)).toCharArray()) { |
| 10 | + chars[write++] = c; |
| 11 | + } |
| 12 | + } |
| 13 | + anchor = read + 1; |
| 14 | + } |
| 15 | + } |
| 16 | + return write; |
| 17 | + } |
| 18 | +} |
Collapse file
Copy file name to clipboard +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +import java.util.Arrays; |
| 2 | +import java.util.Collections; |
| 3 | + |
| 4 | +class Solution { |
| 5 | + public int minMoves(int[] nums) { |
| 6 | + if (nums.length == 0) return 0; |
| 7 | + Arrays.sort(nums); |
| 8 | + int min_num = nums[0]; |
| 9 | + int ans = 0; |
| 10 | + for (int num : nums) { |
| 11 | + ans += num - min_num; |
| 12 | + } |
| 13 | + return ans; |
| 14 | + } |
| 15 | +} |
Collapse file
Copy file name to clipboard +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +class Solution(object): |
| 2 | + def compress(self, chars): |
| 3 | + """ |
| 4 | + :type chars: List[str] |
| 5 | + :rtype: int |
| 6 | + """ |
| 7 | + # https://leetcode.com/problems/string-compression/solution/ |
| 8 | + anchor = write = 0 |
| 9 | + for read, c in enumerate(chars): |
| 10 | + if read + 1 == len(chars) or chars[read + 1] != c: |
| 11 | + chars[write] = chars[anchor] |
| 12 | + write += 1 |
| 13 | + if read > anchor: |
| 14 | + for digit in str(read - anchor + 1): |
| 15 | + chars[write] = digit |
| 16 | + write += 1 |
| 17 | + anchor = read + 1 |
| 18 | + return write |
Collapse file
Copy file name to clipboard +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +class Solution(object): |
| 2 | + def minMoves(self, nums): |
| 3 | + """ |
| 4 | + :type nums: List[int] |
| 5 | + :rtype: int |
| 6 | + """ |
| 7 | + if nums is None or len(nums) == 0: |
| 8 | + return 0 |
| 9 | + min_num = min(nums) |
| 10 | + return sum([i - min_num for i in nums]) |