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 43c7782

Browse files
committed
Minor improvements
1 parent ccd625d commit 43c7782

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

‎src/main/java/com/rampatra/arrays/LongestConsecutiveSubsequence.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
public class LongestConsecutiveSubsequence {
1111

1212
/**
13-
* Given an array of integers, find the length of the longest sub-sequence such that
14-
* elements in the subsequence are consecutive integers, the consecutive numbers can
15-
* be in any order.
13+
* Given an array of distinct integers, find the length of the longest sub-sequence such that
14+
* elements in the subsequence are consecutive integers, the consecutive numbers can be in any order.
1615
* <p>
1716
* Examples:
1817
* Input: arr[] = {1, 9, 3, 10, 4, 20, 2};
@@ -24,14 +23,14 @@ public class LongestConsecutiveSubsequence {
2423
* Output: 5
2524
* The subsequence {36, 35, 33, 34, 32} is the longest subsequence
2625
* of consecutive elements.
27-
*
26+
* <p>
2827
* NOTE: You can also sort this array and check for consecutive elements. You can take this approach if interviewer
2928
* asks to solve with no additional space but do bear in mind that some sorting algorithms do require extra space.
3029
*
31-
* @param arr unsorted array of integers
30+
* @param arr unsorted array of non-repeating integers
3231
* @return the length of the longest consecutive subsequence
3332
*/
34-
private static int findLongestConsecutiveSubsequence(int[] arr) {
33+
private static int findLengthOfLongestConsecutiveSubsequence(int[] arr) {
3534
int longestSubseqCount = 0;
3635
int subseqCount;
3736
int currElem;
@@ -67,11 +66,15 @@ private static int findLongestConsecutiveSubsequence(int[] arr) {
6766
}
6867

6968
public static void main(String[] args) {
70-
System.out.println("{1, 9, 3, 10, 4, 20, 2}: " + findLongestConsecutiveSubsequence(new int[]{1, 9, 3, 10, 4, 20, 2}));
69+
70+
System.out.println("{1, 9, 3, 10, 4, 20, 2}: " +
71+
findLengthOfLongestConsecutiveSubsequence(new int[]{1, 9, 3, 10, 4, 20, 2}));
7172
System.out.println("{36, 41, 56, 35, 44, 33, 34, 92, 43, 32, 42}: " +
72-
findLongestConsecutiveSubsequence(new int[]{36, 41, 56, 35, 44, 33, 34, 92, 43, 32, 42}));
73-
System.out.println("{1}: " + findLongestConsecutiveSubsequence(new int[]{1}));
74-
System.out.println("{}: " + findLongestConsecutiveSubsequence(new int[]{}));
75-
System.out.println("{1,5,8,3}: " + findLongestConsecutiveSubsequence(new int[]{1, 5, 8, 3}));
73+
findLengthOfLongestConsecutiveSubsequence(new int[]{36, 41, 56, 35, 44, 33, 34, 92, 43, 32, 42}));
74+
System.out.println("{1,5,8,3}: " + findLengthOfLongestConsecutiveSubsequence(new int[]{1, 5, 8, 3}));
75+
76+
// corner cases
77+
System.out.println("{1}: " + findLengthOfLongestConsecutiveSubsequence(new int[]{1}));
78+
System.out.println("{}: " + findLengthOfLongestConsecutiveSubsequence(new int[]{}));
7679
}
7780
}

‎src/main/java/com/rampatra/linkedlists/DeleteAlternateNodes.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
import com.rampatra.base.SingleLinkedNode;
55

66
/**
7-
* Created by IntelliJ IDEA.
7+
* Delete alternate nodes in a single linked list.
88
*
99
* @author rampatra
1010
* @since 6/27/15
11-
* @time: 5:27 PM
1211
*/
1312
public class DeleteAlternateNodes {
1413

@@ -26,12 +25,12 @@ public static <E extends Comparable<E>> void deleteAlternateNodes(SingleLinkedNo
2625

2726
public static void main(String[] args) {
2827
SingleLinkedList<Integer> linkedList = new SingleLinkedList<>();
29-
linkedList.add(00);
30-
linkedList.add(11);
31-
linkedList.add(22);
32-
linkedList.add(33);
33-
linkedList.add(44);
34-
linkedList.add(55);
28+
linkedList.add(0);
29+
linkedList.add(1);
30+
linkedList.add(2);
31+
linkedList.add(3);
32+
linkedList.add(4);
33+
linkedList.add(5);
3534
linkedList.printList();
3635
deleteAlternateNodes(linkedList);
3736
linkedList.printList();

0 commit comments

Comments
(0)

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