Posts

House Robber - Leetcode

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and   it will automatically contact the police if two adjacent houses were broken into on the same night . Given an integer array  nums  representing the amount of money of each house, return  the maximum amount of money you can rob tonight  without alerting the police .   Example 1: Input: nums = [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4. Example 2: Input: nums = [2,7,9,3,1] Output: 12 Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12.   Constraints: 1 <= nums.length <= 100 0 <= nums[i] <= 400a Approach This problem can not be solved by g...

Longest Substring Without Repeating Characters

  Given a string   s , find the length of the   longest substring   without repeating characters.   Example 1: Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. Example 4: Input: s = "" Output: 0   Constraints: 0 <= s.length <= 5 * 10 4 s  consists of English letters, digits, symbols and spaces. Approach: Create a HashSet of Characters. Take 2 indexes i (denotes the head of chars) and j (denotes the stretch of chars).   public int lengthOfLongestSubstring(String s) {                  if(s.isEmpty())             return 0;       ...

Matrix Rotation (In Memory)

Image
  Thinking Process: We have to iterate over the columns of the matrix and keep adding elements in a queue starting from the last row of each column. Now, simply empty the queue into the matrix. NOTE: By adding the elements into the queue in the above fashion, we are creating a queue which will effectively produce a rotated image (if updated in the matrix in a row-wise manner). Now if we look at the image closely, we will observe that each column (from bottom to up) becomes a row after rotation. This is the sequence we are creating in the queue. public void rotate ( int [][] matrix) { Queue<Integer> q = new LinkedList<>(); for ( int j= 0 ;j<matrix[ 0 ].length;j++){ //iterate over columns for ( int i=matrix.length -1 ;i>= 0 ;i--){ //last row to 0th row q.add(matrix[i][j]); } } while (!q.isEmpty()){ for ( int i= 0 ;i<matrix.length;i++){ for ( int j= 0 ;...

Linked List - Delete given node

Image
You are given a pointer/ reference to the node which is to be deleted from the linked list of  N  nodes. The task is to delete the node. Pointer/ reference to head node is not given.  Note:  No head reference is given to you. Input: First line of input contains number of testcases T. For each testcase, first line of input contains length of linked list and next line contains the data of the linked list. The last line contains the node to be deleted. Output: For each testcase, in a newline, print the linked list after deleting the given node. Your Task: This is a  function  problem. You only need to complete the  function deleteNode  that takes  reference  to the node that needs to be  deleted . The  printing  is done  automatically  by the  driver code . Expected Time Complexity  : O(n) Expected Auxilliary Space  : O(n) Constraints: 1 <= T <= 100 1 <= N <= 10 3 Example: Input : 2 2...