Tag: java
-
Data Structures & Algorithms in Java – Dynamic Programming – Jump Game
Problem: Given an array of numbers which indicate how many positions maximum you can jump from that index position , find out if you can reach the end of the array from the beginning. For example: For the input [2,3,1,1,4] You can jump maximum 2 positions from index 0, 3 positions from index 1 ,…
-
Data Structures & Algorithms in Java – Dynamic Programming – House Robber II
Problem: A robber goes to rob in a series of houses built next to each other forming a circle. So the first house and last house are next to each other. The robber cannot steal from consecutive houses as it would trigger an alarm. Find the maximum money the robber can steal. Input: [2,3,2] Each…
-
Data Structures & Algorithms in Java – Dynamic Programming – House Robber
Problem: A robber goes to rob in a series of houses built next to each other. He can’t rob from two consecutive houses , this will trigger an alarm. How much maximum money can he steal ? Input: The array [1,2,3,1] represents money in each house starting from the first index. Output: Maximum amount the…
-
Data Structures & Algorithms in Java – Dynamic Programming – Longest Common Subsequence
Problem: Given two strings , find the longest common subsequence between them. For example , Consider the input: “abcde” and “ace” The longest common subsequence between them is “ace”. A subsequence is just a sequence of characters from the original string with any characters removed and the order of remaining elements unchanged. Try out the…
-
Data Structures & Algorithms – Dynamic Programming – Climbing Stairs
Problem: Given a step number in a stair case , find the number of ways you can reach that step in the staircase if you can make 1 or 2 steps at a time Input: 4 Output: 5 ways Since you can take any of the below sequence of steps: [1,1,1,1] , [1,2,1],[1,1,2] ,[2,1,1], [2,2]…
-
Data Structures & Algorithms in Java – Binary – Reverse Bits
Problem: Given a number , reverse the bits in that number Input: 00000010100101000001111010011100 Output: 00111001011110000010100101000000 Since in Java the above binary number will be represented as an integer , the output should be 964176192 which is the decimal equivalent of the above binary number Assumptions: The input is stored in 32 bits (which is the…
-
Data Structures & Algorithms in Java – Binary – Missing Number
Problem: Given an array with values between 1 to n , find the missing number between 1 to n that is not present in the array Input: [9,6,4,2,3,5,7,0,1] Output: 8 In the above array of length 9 we have all the numbers from 1 to 9 except 8 , so output is 8 Try out…
-
What is “effectively final” in Java?
Let’s say you don’t want a variable value to be changed once it is initialized. How to achieve this in Java? By declaring the variable as final. Now the variable becomes a constant and the compiler will complain if it is modified anywhere. There is one more concept of “final” in Java , which is…
-
Are we using mutable objects too much?
By default any object you create in java is mutable. And so we end up using mutable objects more often. But Java team themselves suggest to use Immutable objects more. From https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html : Maximum reliance on immutable objects is widely accepted as a sound strategy for creating simple, reliable code One reason why developers wouldn’t…