This file contains hidden or 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
| class MovingAverage{ | |
| private Queue<Integer> queue; | |
| private double sum; | |
| private int maxSize; | |
| public MovingAverage(int size){ | |
| queue = new LinkedList<Integer>(); | |
| maxSize = size; | |
| sum = 0.0; | |
| } |
This file contains hidden or 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
| Explanation: | |
| 1)first we need to create one pointer which hold the head of the answer | |
| 2)next we need to get hold of the two pointer on the linked list | |
| 3)first, check whether the two linked list is pointing to null or not | |
| 4)if one of the linked list is pointing to null, then just return the other linked list | |
| 5)if both of the linked list is pointing to null, then, return a null pointer | |
| /** | |
| * Definition for singly-linked list. | |
| * public class ListNode { | |
| * int val; |
This file contains hidden or 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
| Explanation: | |
| 1) In this question, we learn a new data structure call set | |
| 2) a set is an array like structure which it will not contain any repeat element, all the elements inside a set is unique | |
| 3) so first, we need to loop throught one of the two array, to add into a set | |
| 4) after adding into a set, loop through the second array, check whether the element in second array is inside the set or not | |
| 5) if it is inside the set, then, add the elements inside an ArrayList called result, and remove the element from the set so that the result list will not contain any repeated elements | |
| 6) next, convert the arrayList into an array structure | |
| 7) improvement may be made by adding the smaller size of the array into a set | |
| class Solution { |
This file contains hidden or 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
| Explanation: | |
| 1) There is a edge case where the array is [-1, -1, -1, 0, 1, 1] | |
| 2) so the leftsum must be initialized to be a zero at first, so that when the pointer at position 0, which is -1, the leftsum is 0 and the right sum will be -1+-1+0+1+1= 0 as well | |
| 3) the return position will be 0 | |
| class Solution { | |
| public int pivotIndex(int[] nums) { | |
| int sum = 0; | |
| for(int i=0; i<nums.length; i++){ | |
| sum = sum + nums[i]; |
This file contains hidden or 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
| Explanation: | |
| 1) first, we need to find the minimum number in the array | |
| 2) second, loop through the array, find the difference of the element with the minimum number | |
| 3) sum up the result | |
| 4) return the result | |
| For example : 1,1,5,2 total is 5 steps | |
| 2,2,5,3 | |
| 3,3,5,4 | |
| 4,4,5,5 | |
| 5,5,5,6 |
This file contains hidden or 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
| Explanation: | |
| 1) First, we make a for loop for the Array Length to add up all the even numbers inside the array first | |
| 2) Then, make a second for loop for the queries length. If the position of the queries accessing is an even number, then, minus | |
| the even number out of the array, then, check if the value at the position after doing a new value is even or not, if it is even | |
| add back the value to the variable even. Assign this variable even to the answer[j] array. | |
| class Solution { | |
| public int[] sumEvenAfterQueries(int[] A, int[][] queries) { | |
| int[] answer = new int[queries.length]; | |
| int even = 0; |
This file contains hidden or 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
| Explanation: | |
| 1) First, we will have two pointers which is ptr1 and ptr2 and each of then pointed to the node of each linked list | |
| 2) Now, in order for us to point to the head of the answer listNode, we create a LinkedList call head | |
| 3) Then, we create another LinkedList called current which will be moving to append a new node to the answer LinkedList | |
| 4) Create a while loop to contnuous looping through the LinkedList 1 and 2, until two of the LinkedList reached the last node | |
| 5) At the starting of each loop, check whether the L1 and L2 are pointed to a null or not, if it is, then assigned the value of the LinkList pointed to null to 0 | |
| 6) Then, we need to add up val1 and val2 and the carry figure | |
| 7) update the carry | |
| 8) move the current pointer to next | |
| 9) move the L1 to next if it is not null |
This file contains hidden or 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
| //explanation: 1)intuitively, given an input [1,2,4,2], we cannot rob adjacent house, so we skip one house after every rob. | |
| but this solution is not correct, (counter example [1,2,4,6] where we will need to skip two houses instead of one | |
| to rob the maximum amount. | |
| 2)Hence, we can use dynamic programming to solve this type of problem. First, we need to handle the base case where | |
| the size of the array is 0 (null), 1 and 2. where we can straight away return the result of this three base case. | |
| 3)For an array size of 3 and bigger, we need to find the maximum of the previous array, and make a new array to store | |
| the maximum of the previous elements we currently inspecting. | |
| 4)This is done inside the for loop. | |
| 5)Finally, we return the last elements in the dp array. | |
| class Solution { |
This file contains hidden or 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
| //Brute-Force Solution | |
| class Solution { | |
| public int[] twoSum(int[] nums, int target) { | |
| int answer[]; | |
| answer = new int[2]; | |
| for(int i =0; i<=nums.length - 1; i++){ | |
| for(int j = i+1 ; j<= nums.length - 1; j++){ | |
| if((nums[i] + nums[j]) == target){ | |
| answer[0] = i; | |
| answer[1] = j; |
NewerOlder