Skip to content

Commit 15f0331

Browse files
committed
Bitonic Sequence and Wiggle Subsequence
1 parent a5f71e8 commit 15f0331

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* A sequence of numbers is called a bitonic sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative.
3+
* A sequence with fewer than two elements is trivially a wiggle sequence.
4+
*/
5+
6+
public class BitonicSequence
7+
{
8+
public int getMaxLenBitonicSeq(int[] arr)
9+
{
10+
int[] lis = new int[arr.length];
11+
int[] lds = new int[arr.length];
12+
13+
for(int i = 0; i < arr.length; i++)
14+
{
15+
lis[i] = 1;
16+
lds[i] = 1;
17+
}
18+
19+
for(int i = 1; i < arr.length; i++)
20+
{
21+
for(int j = 0; j < i; j++)
22+
{
23+
if(arr[j] < arr[i])
24+
lis[i] = Math.max(lis[i], lis[j] + 1);
25+
}
26+
}
27+
28+
for(int i = arr.length - 2; i >= 0; i--)
29+
{
30+
for(int j = arr.length - 1; j > i; j--)
31+
{
32+
if(arr[i] > arr[j])
33+
lds[i] = Math.max(lds[i], lds[j] + 1);
34+
}
35+
}
36+
37+
int max = 0;
38+
for(int i = 0; i < arr.length; i++)
39+
{
40+
if(max < lis[i] + lds[i] - 1)
41+
max = lis[i] + lds[i] - 1;
42+
}
43+
44+
return max;
45+
}
46+
47+
public static void main(String[] args)
48+
{
49+
BitonicSequence bs = new BitonicSequence();
50+
int[] arr = {1,4,3,7,2,1,8,11,13,0};
51+
int[] arr2 = {1,7,4,9,2,5};
52+
System.out.println(bs.getMaxLenBitonicSeq(arr2));
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Given two strings, find the number of times the second string occurs in the first string, whether continuous or discontinuous.
3+
*
4+
* Examples:
5+
* Input:
6+
* string a = "GeeksforGeeks"
7+
* string b = "Gks"
8+
*
9+
* Output: 4
10+
*
11+
* Explanation:
12+
* The four strings are
13+
* 'G'ee'k''s'forGeeks
14+
* 'G'eeksforGee'k''s'
15+
* 'G'ee'k'sforGeek's'
16+
* Geeksfor'G'ee'k''s'
17+
*/
18+
19+
class Solution
20+
{
21+
22+
public static int count(String a, String b)
23+
{
24+
int m = a.length();
25+
int n = b.length();
26+
27+
int dp[][] = new int[m + 1][n + 1];
28+
29+
// If first string is empty
30+
for (int i = 0; i <= n; ++i)
31+
dp[0][i] = 0;
32+
33+
// If second string is empty
34+
for (int i = 0; i <= m; ++i)
35+
dp[i][0] = 1;
36+
37+
// Fill dp[][] in
38+
// bottom up manner
39+
for (int i = 1; i <= m; i++)
40+
{
41+
for (int j = 1; j <= n; j++)
42+
{
43+
// If last characters are
44+
// same, we have two options -
45+
// 1. consider last characters
46+
// of both strings in solution
47+
// 2. ignore last character
48+
// of first string
49+
if (a.charAt(i - 1) == b.charAt(j - 1))
50+
dp[i][j] = dp[i - 1][j - 1] +
51+
dp[i - 1][j];
52+
53+
else
54+
// If last character are
55+
// different, ignore last
56+
// character of first string
57+
dp[i][j] = dp[i - 1][j];
58+
}
59+
}
60+
61+
return dp[m][n];
62+
}
63+
64+
// Driver Code
65+
public static void main (String[] args)
66+
{
67+
Solution obj = new Solution();
68+
String a = "GeeksforGeeks";
69+
String b = "Gks";
70+
71+
System.out.println(obj.count(a, b));
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative.
3+
* The first difference (if one exists) may be either positive or negative.
4+
* A sequence with fewer than two elements is trivially a wiggle sequence.
5+
*
6+
* For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative.
7+
* In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, \
8+
* the first because its first two differences are positive and the second because its last difference is zero.
9+
*
10+
* Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence.
11+
* A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.
12+
* Example 1:
13+
* Input: [1,7,4,9,2,5]
14+
* Output: 6
15+
* Explanation: The entire sequence is a wiggle sequence.
16+
*
17+
* Example 2:
18+
* Input: [1,17,5,10,13,15,10,5,16,8]
19+
* Output: 7
20+
* Explanation: There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].
21+
*
22+
* Example 3:
23+
* Input: [1,2,3,4,5,6,7,8,9]
24+
* Output: 2
25+
*/
26+
27+
class Solution {
28+
public int wiggleMaxLength(int[] nums) {
29+
if(nums.length == 0)
30+
return 0;
31+
32+
/**
33+
* If nums[i] > nums[i-1], that means it wiggles up. the element before it must be a down position.
34+
* so up[i] = down[i-1] + 1; down[i] keeps the same with before.
35+
*
36+
* If nums[i] < nums[i-1], that means it wiggles down. the element before it must be a up position.
37+
* so down[i] = up[i-1] + 1; up[i] keeps the same with before.
38+
*
39+
* If nums[i] == nums[i-1], that means it will not change anything becasue it didn't wiggle at all. so both down[i] and up[i] keep the same.
40+
*/
41+
42+
int up = 1, down = 1;
43+
44+
for(int i = 1; i < nums.length; i++)
45+
{
46+
if(nums[i] > nums[i - 1])
47+
{
48+
up = down + 1;
49+
}
50+
else if (nums[i] < nums[i - 1])
51+
{
52+
down = up + 1;
53+
}
54+
}
55+
56+
return Math.max(up, down);
57+
}
58+
}

LeetCode/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@
267267
- [x] [Delete Operations for Two Strings](https://github.com/kalpak92/DataStructures_Algorithms/tree/master/LeetCode/583.%20Delete%20Operations%20for%20Two%20Strings)
268268
- [x] [Longest Increasing Subsequence](https://github.com/kalpak92/DataStructures_Algorithms/tree/master/LeetCode/300%20Longest%20Increasing%20Subsequence)
269269
- [x] [Shortest Common Supersequence](https://github.com/kalpak92/DataStructures_Algorithms/tree/master/LeetCode/1092.%20Shortest%20Common%20Supersequence)
270+
- [x] [Wiggle Subsequence]()
270271
- [x] [Word Break](https://github.com/kalpak92/DataStructures_Algorithms/tree/master/LeetCode/139.%20Word%20Break)
271272
- [x] [Decode Ways](https://github.com/kalpak92/DataStructures_Algorithms/tree/master/LeetCode/91.%20Decode%20Ways)
272273
- [x] [Minimum Path Sum](https://github.com/kalpak92/DataStructures_Algorithms/tree/master/LeetCode/64.%20Minimum%20Path%20Sum)

0 commit comments

Comments
 (0)