From 1b4cec835ff3eb4f0bd9b3bc86820748e18579a0 Mon Sep 17 00:00:00 2001 From: npincus Date: Sat, 8 Dec 2018 14:26:51 -0800 Subject: [PATCH] Add files via upload --- eleventh.java | 23 +++++++++++++++ first.java | 30 ++++++++++++++++++++ firth.java | 23 +++++++++++++++ fithteenth.java | 25 +++++++++++++++++ fourteenth.java | 31 +++++++++++++++++++++ fourth.java | 63 +++++++++++++++++++++++++++++++++++++++++ ninth.java | 37 +++++++++++++++++++++++++ number8.java | 54 ++++++++++++++++++++++++++++++++++++ second.java | 28 +++++++++++++++++++ seventh.java | 21 ++++++++++++++ sixth.java | 32 +++++++++++++++++++++ tenth.java | 19 +++++++++++++ third.java | 28 +++++++++++++++++++ thridteen.java | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ twelve.java | 37 +++++++++++++++++++++++++ 15 files changed, 525 insertions(+) create mode 100644 eleventh.java create mode 100644 first.java create mode 100644 firth.java create mode 100644 fithteenth.java create mode 100644 fourteenth.java create mode 100644 fourth.java create mode 100644 ninth.java create mode 100644 number8.java create mode 100644 second.java create mode 100644 seventh.java create mode 100644 sixth.java create mode 100644 tenth.java create mode 100644 third.java create mode 100644 thridteen.java create mode 100644 twelve.java diff --git a/eleventh.java b/eleventh.java new file mode 100644 index 0000000..42aa628 --- /dev/null +++ b/eleventh.java @@ -0,0 +1,23 @@ +public class eleventh { + public ListNode detectCycle(ListNode head) { + ListNode slow = head; + ListNode fast = head; + while (fast!=null && fast.next!=null){ + fast = fast.next.next; + slow = slow.next; + if (fast == slow){ + ListNode slow2 = head; + while (slow2 != slow){ + slow = slow.next; + slow2 = slow2.next; + } + return slow; + } + } + return null; + } +} +/* so what i saw here is i had already done this one back in the thrid one + no d7 or ideal + Q: it was vary clear + */ \ No newline at end of file diff --git a/first.java b/first.java new file mode 100644 index 0000000..d489b8f --- /dev/null +++ b/first.java @@ -0,0 +1,30 @@ +class first { + public int[] productExceptSelf(int[] nums) { + int n = nums.length; + int[] dp = new int[n]; + dp[0] = 1; + + //shifts right and multiply [1,2,3,4]->[1,1,2,6] + for (int i = 1; i < n; i++) { + dp[i] = dp[i - 1] * nums[i - 1]; + } + int right=1;//composets from right from left + //shifts left[1,1,2,6]->[24,12,8,6] + for (int i = n - 1; i >= 0; i--) { + dp[i] *= right; + right *= nums[i];//composets after finding current value so dp is not inclueading it self in the sum + } + return dp; + } +} + +/* ideal: + how do i not multiply the current value when finding the product of the set with out removeing on this index + D7: + so a give number is lift*right sums is what i see instantly + first i think of 2 lists but here i realize i only need 1 list and an accumulator and the accumulator will only grow + after i find the current value, for index cleanness i shift the first list right 1 time as it will be n-1 in size + now its n and the first index is filled with 1 the multiplicative identity this just leads to less code needed and + not careing about index out of bounds problems + Q: why are we doing so much work just to avoid division? + */ \ No newline at end of file diff --git a/firth.java b/firth.java new file mode 100644 index 0000000..360a96f --- /dev/null +++ b/firth.java @@ -0,0 +1,23 @@ +public class firth { + public void rotate(int[][] matrix) { + for(int i = 0; ihii||loj>hij) { + return false; + } + if(loi==hii&&loj==hij){ + return array[hii][hij]==target; + } + + int midi = (loi+hii)/2; + int midj = (loj+hij)/2; + if (array[midi][midj] == target) { + return true; + } + + if (array[midi][midj] > target) { + return binarySearch(array, target, loi, midi,loj,midj); + } + return binarySearch(array, target, loi, midi,midj+1,hij) + || binarySearch(array, target, midi+1, hii,midj+1,hij) + || binarySearch(array, target, midi+1, hii,loj,midj); + } +} +/* ideal: not used + d7: idea was to split the matrix into 4 section based of the middle point 1 less 3 greater but all rectangular and + they are all of close to the same size and keep doing that leading to log(n)log(m)solution think i messed up my boundrys + */ \ No newline at end of file diff --git a/fourth.java b/fourth.java new file mode 100644 index 0000000..90783f6 --- /dev/null +++ b/fourth.java @@ -0,0 +1,63 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class fourth { + public int rob(TreeNode root) { + if(root==null){ + return 0; + } + TreeNode not=new TreeNode(0); + TreeNode taken=new TreeNode(0); + rob(root,not,taken); + return Math.max(not.val,taken.val); + } + public void rob(TreeNode rob,TreeNode not,TreeNode taken){ + if(rob.right==null && rob.left==null){ + taken.val=rob.val; + not.val=0; + }else if(rob.left==null){ + not.right=new TreeNode(0); + taken.right=new TreeNode(0); + + rob(rob.right,not.right,taken.right); + + taken.val=rob.val+not.right.val; + not.val=Math.max(taken.right.val,not.right.val); + }else if(rob.right==null){ + not.left=new TreeNode(0); + taken.left=new TreeNode(0); + + rob(rob.left,not.left,taken.left); + + taken.val=rob.val+not.left.val; + not.val=Math.max(taken.left.val,not.left.val); + + }else{ + not.right=new TreeNode(0); + not.left=new TreeNode(0); + taken.right=new TreeNode(0); + taken.left=new TreeNode(0); + + rob(rob.right,not.right,taken.right); + rob(rob.left,not.left,taken.left); + + taken.val=rob.val+not.left.val+not.right.val; + not.val=Math.max(taken.left.val,not.left.val) + +Math.max(taken.right.val,not.right.val); + + } + } +} + +/* ideal: did not seam useful here + d7: what i saw was whats that we need to remember at every point what happens from the 2 choices before. + so the question is do i rob or do i not is not done until you have every point in the tree stored some how + so i make 2 trees to follow the first tree and i build them from bottem to top, one storeing what happens if i take + and the second what if i dont. + */ \ No newline at end of file diff --git a/ninth.java b/ninth.java new file mode 100644 index 0000000..153f5fc --- /dev/null +++ b/ninth.java @@ -0,0 +1,37 @@ +public class ninth { + private class IntervalComparator implements Comparator { + @Override + public int compare(Interval a, Interval b) { + return a.start < b.start ? -1 : a.start == b.start ? 0 : 1; + } + } + public List merge(List intervals) { + Collections.sort(intervals,new IntervalComparator()); + int i=0; + while(igrid.length-1||j>grid[i].length-1){ + return false; + } + else return land[i][j] == -1 && grid[i][j] == '1'; + } + private void mark(int i, int j, int value){ + if(isNewLand(i,j)){ + land[i][j]=value; + mark(i-1,j,value); + mark(i+1,j,value); + mark(i,j-1,value); + mark(i,j+1,value); + } + } +} +/* ideal:not really useful + d7: so here i saw things as 2 ideas, first knowing if we have been somewhere already this is handled by isNewLand + and second is marking when we find land and all land attached to it, this is done by mark + isNewLand is really simple if the land is not marked and not water its true other wise its false, edge is also false + + mark is simple it just calls it self on all attached places to anywhere we know there is land and marks them the same + as the land we are currently on. + + numIslands is the controler and does everything else, most simple starts mark if we are on new land and counts up and + moves threw the matrix + Q: less a question and more a note that there could be many types of blockage other then water to expand this problem + into but the core is kind of the same. its intersting. + */ \ No newline at end of file diff --git a/second.java b/second.java new file mode 100644 index 0000000..0c78196 --- /dev/null +++ b/second.java @@ -0,0 +1,28 @@ +import java.util.ArrayList; +import java.util.List; + +class second { + + public List generateParenthesis(int n) { + ArrayList ret = new ArrayList<>(); + if(n==1){ + ret.add("()"); + return ret; + } + List sub=generateParenthesis(n-1); + for (String s:sub) { + for (int i = 0; i < s.length(); i++) { + String p=s.substring(0, i) + "()" + s.substring(i); + if(!ret.contains(p)) ret.add(p); + } + } + return ret; + } +} +/* this solution is nieave + ideal: what are the legal combos of parenthesis? + d7: so first i saw that there is only one solution of the form n=1 () and n=2 has 3 solutions but 2 are degenerate + and any one could be build buy adding in solution one into all the places legal n and then removeing the degenerate + solutions + Q:what is the cleaner solution as this is really slow? + */ \ No newline at end of file diff --git a/seventh.java b/seventh.java new file mode 100644 index 0000000..81431c8 --- /dev/null +++ b/seventh.java @@ -0,0 +1,21 @@ +public class seventh { + public void flatten(TreeNode root) { + if(root==null||(root.right==null&&root.left==null)) return; + TreeNode right = root.right; + flatten(right); + flatten(root.left); + root.right = root.left; + root.left = null; + TreeNode p = root; + while(p.right!=null){ + p = p.right; + } + p.right = right; + } +} +/* ideal:not used + d7: so the recurive solition pops out clearly when i first see this but basicly the base case is simple 1 node on + its own is flat, and if you flaten right and left on any other note and put left flat point to right flat you are done. + the question then becomes simply finding the end of left and watching out for null pointer problems. + Q:no question here seams clearly defined + */ \ No newline at end of file diff --git a/sixth.java b/sixth.java new file mode 100644 index 0000000..f4c7b29 --- /dev/null +++ b/sixth.java @@ -0,0 +1,32 @@ +import java.util.Arrays; + +class sixth { + public int leastInterval(char[] tasks, int n) { + int[] map =new int[26]; + for(int i=0;i0){ + int index=0; + while(index<=n){ + if(map[25]==0){break;} + if(index<26) map[25-index]--; + time++; + index++; + } + Arrays.sort(map); + } + return time; + } +} +/* ideal:not used + d7: what i saw here is that there where 26 types of tasks at max and they could then be stored as numbers in a array + of size 26 and that what given task you do does not matter so we simple take the type task with the largest number of + task in it. this can be found by sorting the array many times. next i see that we can start at the end of the list + and move to the start and if the last element is ever 0 we are done, also i see we can block n tasks together if + they are unique. this solution does not presurve a list of task you would do and in what order just simply how long + they would take. + Q:whats the cool down intended to represent? + */ \ No newline at end of file diff --git a/tenth.java b/tenth.java new file mode 100644 index 0000000..e53d940 --- /dev/null +++ b/tenth.java @@ -0,0 +1,19 @@ + +public class tenth { + public boolean canJump(int[] nums) { + int hitsEnd = nums.length - 1; + for (int i = nums.length - 1; i >= 0; i--) { + if (i + nums[i] >= hitsEnd) { + hitsEnd = i; + } + } + return hitsEnd == 0; + } +} +/* ideal:not used + d7: what i saw was we can go forward but its going to suck as we have to cheak ever place may times, mean its easy to + go backward, so it becomes simpler as its just a boolean array of the same size as nums and we mark if this point + reaches the end but then i saw i did not really need the array after i build it but simple the index of the most resent + element that hits the end and if the index ever hits 0 we made it threw the list and can jump + Q:would love to think about this in 2d + */ \ No newline at end of file diff --git a/third.java b/third.java new file mode 100644 index 0000000..2789885 --- /dev/null +++ b/third.java @@ -0,0 +1,28 @@ +public class third { + public int findDuplicate(int[] nums) { + int tortoise = nums[0]; + int hare = nums[0]; + + //finds a loop + do { + tortoise = nums[tortoise]; + hare = nums[nums[hare]]; + } while (tortoise != hare); + hare = nums[0]; + //finds the last element in the lead up + while (hare != tortoise) { + hare = nums[hare]; + tortoise = nums[tortoise]; + } + return tortoise; + } +} +/* + ideal: where is the duplicate? + d7: so first i saw that no value in nums is greater then the number of indexs and all are represented other then 0 + next i saw that with more space we could do this easly with one pass and just cheak if we have already seen this number + next i saw that this could be seen as a linked list where the value point to the index of the number to go to next + and this list will have a loop thanks to PPH so then question becomes how do i find a loop in a linked list and then + how do i navigate to the element before the loop starts and this is the 2 pointer problem + Q:what is the best order of solution in O time? + */ \ No newline at end of file diff --git a/thridteen.java b/thridteen.java new file mode 100644 index 0000000..d002b81 --- /dev/null +++ b/thridteen.java @@ -0,0 +1,74 @@ +import java.util.ArrayList; +import java.util.List; + +class thridteen { + public List letterCombinations(String digits) { + ArrayList old=new ArrayList(); + ArrayList cur=new ArrayList(); + old.add(""); + for(int i=0;inums2[k]){ + sorted[i]=nums2[k]; + k++; + }else{ + sorted[i]=nums1[j]; + j++; + } + }else if(j==nums1.length){ + sorted[i]=nums2[k]; + k++; + }else { + sorted[i]=nums1[j]; + j++; + } + if(i==sorted.length-1){ + if(temp%2==0) return (sorted[i]+sorted[i-1])/2.0; + else return (sorted[i]); + } + } + + return 0; + } +} +/* no ideal: + d7: so what i saw is that as the list are sorted you could just stick them together + and as you only need the middle element you only need to go threw n+m/2 elements + and then you simple add the lists and when you get to the middle element you need to know if the list was odd or + even but thats all you need + */ \ No newline at end of file