Skip to content

Commit c365dec

Browse files
add 1641
1 parent b98de6c commit c365dec

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ _If you like this project, please leave me a star._ ★
3434
|1656|[Design an Ordered Stream](https://leetcode.com/problems/design-an-ordered-stream/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1656.java) ||Easy|Array, Design|
3535
|1652|[Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1652.java) ||Easy|Array|
3636
|1646|[Get Maximum in Generated Array](https://leetcode.com/problems/get-maximum-in-generated-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1646.java) ||Easy|Array|
37+
|1641|[Count Sorted Vowel Strings](https://leetcode.com/problems/count-sorted-vowel-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1641.java) ||Medium|Math, DP, Backtracking|
3738
|1640|[Check Array Formation Through Concatenation](https://leetcode.com/problems/check-array-formation-through-concatenation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1640.java) ||Easy|Array, Sort|
3839
|1637|[Widest Vertical Area Between Two Points Containing No Points](https://leetcode.com/problems/widest-vertical-area-between-two-points-containing-no-points/)|[Javascript](./javascript/_1637.js)| | Medium | Sort |
3940
|1636|[Sort Array by Increasing Frequency](https://leetcode.com/problems/sort-array-by-increasing-frequency/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1636.java) ||Easy|Array, Sort|
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
public class _1641 {
6+
public static class Solution1 {
7+
/**
8+
* I solved this problem using Math, no DP, recursion or backtracking techniques.
9+
* Time: beat 100% submission consistently since it's O(n), essentialy it's O(1) because the contraints in the problem state: 1 <= n <= 50
10+
* After writing out from n = 1 to 3, we can see the pattern.
11+
* Detailed reasoning to be seen in my youtube video on my channel: https://www.youtube.com/fishercoder.
12+
*/
13+
public int countVowelStrings(int n) {
14+
if (n == 1) {
15+
return 5;
16+
}
17+
int[] arr = new int[]{1, 1, 1, 1, 1};
18+
int sum = 5;
19+
for (int i = 2; i <= n; i++) {
20+
int[] copy = new int[5];
21+
for (int j = 0; j < arr.length; j++) {
22+
if (j == 0) {
23+
copy[j] = sum;
24+
} else {
25+
copy[j] = copy[j - 1] - arr[j - 1];
26+
}
27+
}
28+
arr = Arrays.copyOf(copy, 5);
29+
sum = 0;
30+
for (int j = 0; j < arr.length; j++) {
31+
sum += arr[j];
32+
}
33+
}
34+
sum = 0;
35+
for (int j = 0; j < arr.length; j++) {
36+
sum += arr[j];
37+
}
38+
return sum;
39+
}
40+
}
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1641;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1641Test {
10+
private static _1641.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1641.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(5, solution1.countVowelStrings(1));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(15, solution1.countVowelStrings(2));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(35, solution1.countVowelStrings(3));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(70, solution1.countVowelStrings(4));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(66045, solution1.countVowelStrings(33));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)