Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f374117

Browse files
authored
Added tasks 187-188.
1 parent 518ae2c commit f374117

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g0101_0200.s0187_repeated_dna_sequences;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public class Solution {
8+
public List<String> findRepeatedDnaSequences(String s) {
9+
if (s.length() < 10) {
10+
return Collections.emptyList();
11+
}
12+
boolean[] seen = new boolean[1024 * 1024];
13+
boolean[] added = new boolean[1024 * 1024];
14+
char[] chars = s.toCharArray();
15+
int buf = 0;
16+
int[] map = new int[128];
17+
map['A'] = 0;
18+
map['C'] = 1;
19+
map['G'] = 2;
20+
map['T'] = 3;
21+
List<String> ans = new ArrayList<>(s.length() / 2);
22+
for (int i = 0; i < 10; i++) {
23+
buf = (buf << 2) + map[chars[i]];
24+
}
25+
seen[buf] = true;
26+
for (int i = 10; i < chars.length; i++) {
27+
buf = ((buf << 2) & 0xFFFFF) + map[chars[i]];
28+
if (seen[buf]) {
29+
if (!added[buf]) {
30+
ans.add(new String(chars, i - 9, 10));
31+
added[buf] = true;
32+
}
33+
} else {
34+
seen[buf] = true;
35+
}
36+
}
37+
return ans;
38+
}
39+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0101_0200.s0188_best_time_to_buy_and_sell_stock_iv;
2+
3+
public class Solution {
4+
public int maxProfit(int k, int[] prices) {
5+
int n = prices.length;
6+
int[] dp = new int[k + 1];
7+
int[] maxdp = new int[k + 1];
8+
for (int i = 0; i <= k; i++) {
9+
maxdp[i] = Integer.MIN_VALUE;
10+
}
11+
for (int i = 1; i <= n; i++) {
12+
maxdp[0] = Math.max(maxdp[0], dp[0] - prices[i - 1]);
13+
for (int j = k; j >= 1; j--) {
14+
maxdp[j] = Math.max(maxdp[j], dp[j] - prices[i - 1]);
15+
dp[j] = Math.max(maxdp[j - 1] + prices[i - 1], dp[j]);
16+
}
17+
}
18+
return dp[k];
19+
}
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g0101_0200.s0187_repeated_dna_sequences;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.util.Arrays;
7+
import org.junit.Test;
8+
9+
public class SolutionTest {
10+
@Test
11+
public void findRepeatedDnaSequences() {
12+
assertThat(
13+
new Solution().findRepeatedDnaSequences("AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"),
14+
equalTo(Arrays.asList("AAAAACCCCC", "CCCCCAAAAA")));
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package g0101_0200.s0188_best_time_to_buy_and_sell_stock_iv;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void maxProfit() {
11+
assertThat(new Solution().maxProfit(2, new int[] {2, 4, 1}), equalTo(2));
12+
assertThat(new Solution().maxProfit(2, new int[] {3, 2, 6, 5, 0, 3}), equalTo(7));
13+
}
14+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /