Skip to content

Commit b59b37a

Browse files
add 481
1 parent 22c2f98 commit b59b37a

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Your ideas/fixes/algorithms are more than welcome!
3131
|592|[Fraction Addition and Subtraction](https://leetcode.com/problems/fraction-addition-and-subtraction/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_592.java) | O(nlogx) |O(n) | Medium | Math
3232
|591|[Tag Validator](https://leetcode.com/problems/tag-validator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_591.java) | O(n) |O(n) | Hard | Stack, String
3333
|588|[Design In-Memory File System](https://leetcode.com/problems/design-in-memory-file-system/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_588.java) | O(n) |O(h) | Hard | Trie, Design
34+
|587|[Erect the Fence](https://leetcode.com/problems/erect-the-fence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_587.java) | O(?) |O(?) | Hard | Geometry
3435
|583|[Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_583.java) | O(m*n) |O(m*n) could be optimized to O(n) | Medium | DP
3536
|582|[Kill Process](https://leetcode.com/problems/kill-process/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_582.java) | O(n) |O(h) | Medium | Stack
3637
|581|[Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_581.java) | O(n) |O(1) | Easy | Array, Sort
@@ -103,6 +104,7 @@ Your ideas/fixes/algorithms are more than welcome!
103104
|485|[Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/MaxConsecutiveOnes.java) | O(n) |O(1) | Easy| Array
104105
|484|[Find Permutation](https://leetcode.com/problems/find-permutation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_484.java) | O(n) |O(1) | Medium | Array, String, Greedy
105106
|482|[License Key Formatting](https://leetcode.com/problems/license-key-formatting/)|[Solution](../master/src/main/java/com/fishercoder/solutions/LicenseKeyFormatting.java) | O(n) |O(n) | Medium|
107+
|481|[Magical String](https://leetcode.com/problems/magical-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_481.java) | O(?) |O(?) | Medium|
106108
|480|[Sliding Window Median](https://leetcode.com/problems/sliding-window-median/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_480.java) | O(nlogk) |O(k) | Hard| Heap
107109
|477|[Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/TotalHammingDistance.java) | O(n) |O(1) | Medium| Bit Manipulation
108110
|476|[Number Complement](https://leetcode.com/problems/number-complement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/NumberComplement.java) | O(n) |O(1) | Easy| Bit Manipulation
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 481. Magical String
5+
*
6+
* A magical string S consists of only '1' and '2' and obeys the following rules:
7+
8+
The string S is magical because concatenating the number of contiguous occurrences of characters '1' and '2' generates the string S itself.
9+
10+
The first few elements of string S is the following: S = "1221121221221121122……"
11+
12+
If we group the consecutive '1's and '2's in S, it will be:
13+
14+
1 22 11 2 1 22 1 22 11 2 11 22 ......
15+
16+
and the occurrences of '1's or '2's in each group are:
17+
18+
1 2 2 1 1 2 1 2 2 1 2 2 ......
19+
20+
You can see that the occurrence sequence above is the S itself.
21+
22+
Given an integer N as input, return the number of '1's in the first N number in the magical string S.
23+
24+
Note: N will not exceed 100,000.
25+
26+
Example 1:
27+
Input: 6
28+
Output: 3
29+
Explanation: The first 6 elements of magical string S is "12211" and it contains three 1's, so return 3.
30+
*/
31+
public class _481 {
32+
33+
/**credit: https://discuss.leetcode.com/topic/74917/simple-java-solution-using-one-array-and-two-pointers
34+
* Algorithm:
35+
36+
1. Create an int array a and initialize the first 3 elements with 1, 2, 2.
37+
2. Create two pointers head and tail. head points to the number which will be used to generate new numbers.
38+
tail points to the next empty position to put the new number. Then keep generating new numbers until tail >= n.
39+
3. Need to create the array 1 element more than n to avoid overflow because the last round head might points to a number 2.
40+
4. A trick to flip number back and forth between 1 and 2: num = num ^ 3*/
41+
public int magicalString(int n) {
42+
if (n <= 0) return 0;
43+
if (n <= 3) return 1;
44+
45+
int[] a = new int[n + 1];
46+
a[0] = 1;
47+
a[1] = 2;
48+
a[2] = 2;
49+
50+
int head = 2;
51+
int tail = 3;
52+
int num = 1;
53+
int result = 1;
54+
55+
while (tail < n) {
56+
for (int i = 0; i < a[head]; i++) {
57+
a[tail] = num;
58+
if (num == 1 && tail < n) result++;
59+
tail++;
60+
}
61+
num = num ^ 3;
62+
head++;
63+
}
64+
65+
return result;
66+
}
67+
68+
}

0 commit comments

Comments
 (0)