-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
525 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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? | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
public class firth { | ||
public void rotate(int[][] matrix) { | ||
for(int i = 0; i<matrix.length; i++){ | ||
for(int j = i; j<matrix[0].length; j++){ | ||
int temp = matrix[i][j]; | ||
matrix[i][j] = matrix[j][i]; | ||
matrix[j][i] = temp; | ||
} | ||
} | ||
for(int i =0 ; i<matrix.length; i++){ | ||
for(int j = 0; j<matrix.length/2; j++){ | ||
int temp = matrix[i][j]; | ||
matrix[i][j] = matrix[i][matrix.length-1-j]; | ||
matrix[i][matrix.length-1-j] = temp; | ||
} | ||
} | ||
} | ||
} | ||
/* ideal:not used | ||
d7: so rotating clockwise is the same as inverting and then fliping over the x axis, this soution is likely not the | ||
fastest but it work with out strange math. | ||
Q:how fast do you want this to run? | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class fithteenth { | ||
long fact(int k) | ||
{ | ||
long product = 1; | ||
for(int i = 1; i <= k; i++){ | ||
product *= i; | ||
} | ||
|
||
return product; | ||
} | ||
public int uniquePaths(int m, int n) { | ||
int choices=n+m-2; | ||
if(m==1||n==1){ | ||
return 1; | ||
} | ||
long top=fact(choices); | ||
long bot=fact(m-1)*fact(n-1); | ||
long ret=top/bot; | ||
return (int) ret; | ||
} | ||
} | ||
/* so i did not use any of the solution methods as i saw this was just a simple combinitorics question | ||
the problem is i have no clue how big 100!*100! and its makeing it so i need to change to a diffrent date type and | ||
long is not big enough. so im likely dumb | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class fourteenth { | ||
public boolean searchMatrix(int[][] matrix, int target) { | ||
if(matrix.length==0) return false; | ||
return binarySearch(matrix,target,0, matrix.length-1,0, matrix[0].length-1); | ||
} | ||
public boolean binarySearch(int[][] array, int target, int loi, int hii,int loj,int hij) { | ||
if (loi>hii||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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
public class ninth { | ||
private class IntervalComparator implements Comparator<Interval> { | ||
@Override | ||
public int compare(Interval a, Interval b) { | ||
return a.start < b.start ? -1 : a.start == b.start ? 0 : 1; | ||
} | ||
} | ||
public List<Interval> merge(List<Interval> intervals) { | ||
Collections.sort(intervals,new IntervalComparator()); | ||
int i=0; | ||
while(i<intervals.size()-1){ | ||
if(overlap(intervals.get(i),intervals.get(i+1))){ | ||
intervals.set(i,combine(intervals.get(i),intervals.get(i+1))); | ||
intervals.remove(i+1); | ||
}else{ | ||
i++; | ||
} | ||
} | ||
return intervals; | ||
} | ||
private boolean overlap(Interval a, Interval b) { | ||
return a.start <= b.end && b.start <= a.end; | ||
} | ||
private Interval combine(Interval a, Interval b){ | ||
int min=Math.min(a.start,b.start); | ||
int max=Math.max(a.end,b.end); | ||
return new Interval(min,max); | ||
} | ||
} | ||
/* ideal not used | ||
d7: so i saw this problem as 3 things, first do they over lap, easy if start of one is with in start-end range of | ||
another then yes. | ||
second was how do i add them, also easy, lows start and the highest end assumeing they over lap | ||
and thrid and finaly was simple getting it in order and then you only need to add one to the next element if they | ||
overlap if they dont then move on to the element after the first and repeat | ||
Q:more a note, javas not great for this kotlin or any langaue with Lambda expressions would be cleaner and easier | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
class number8 { | ||
private char[][] grid; | ||
private int[][] land; | ||
private int num=0; | ||
public int numIslands(char[][] grid) { | ||
this.grid=grid; | ||
this.land=new int[grid.length][]; | ||
for(int i=0;i<grid.length;i++){ | ||
land[i]=new int[grid[i].length]; | ||
} | ||
for(int i=0;i<grid.length;i++) { | ||
for (int j = 0; j < grid[i].length; j++) { | ||
land[i][j]=-1; | ||
} | ||
} | ||
for(int i=0;i<grid.length;i++){ | ||
for(int j=0;j<grid[i].length;j++){ | ||
if(isNewLand(i,j)){ | ||
num++; | ||
mark(i,j,num); | ||
} | ||
} | ||
} | ||
return num; | ||
} | ||
private boolean isNewLand(int i, int j){ | ||
if(i<0||j<0||i>grid.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. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
class second { | ||
|
||
public List<String> generateParenthesis(int n) { | ||
ArrayList<String> ret = new ArrayList<>(); | ||
if(n==1){ | ||
ret.add("()"); | ||
return ret; | ||
} | ||
List<String> 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? | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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;i<tasks.length;i++){ | ||
map[tasks[i]-'A']++; | ||
} | ||
Arrays.sort(map); | ||
int time=0; | ||
while(map[25]>0){ | ||
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? | ||
*/ |
Oops, something went wrong.