Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. package LIS;
  2.  
  3. import java.util.Vector;
  4.  
  5. public class GreedyLIS {
  6. /**
  7. * The greedy algorithm of LIS - doesn't work for any array
  8. * Complexity: O(n)
  9. */
  10. public static Vector<Integer> greedyLIS(int[] arr) {
  11. return greedyLIS(arr,0);
  12. }
  13.  
  14. private static Vector<Integer> greedyLIS(int[] arr,int start) {
  15. Vector<Integer> ans = new Vector<Integer>();
  16. ans.add(arr[start]);
  17. int max = arr[start];
  18. for (int i = start+1; i < arr.length; i++) {
  19. if(arr[i] > max) {
  20. ans.add(arr[i]);
  21. max = arr[i];
  22. }
  23. }
  24. return ans;
  25. }
  26.  
  27. /**
  28. * The improved greedy algorithm of LIS - check from any index - doesn't work for any array
  29. * Complexity: O(n^2)
  30. */
  31. public static Vector<Integer> greedyLISImproved(int[] arr) {
  32. Vector<Integer> ans = new Vector<Integer>();
  33. int maxLen = 0;
  34. for (int i = 0; i < arr.length; i++) {
  35. Vector<Integer> temp = greedyLIS(arr,i);
  36. if(temp.size() > maxLen) {
  37. maxLen = temp.size();
  38. ans = temp;
  39. }
  40. }
  41. return ans;
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement