Posts

Showing posts from November, 2020

LeetCode - Flipping an Image Solution - The Coding Shala

Home >> LeetCode >> Flipping an Image In this post, we will learn how to solve the Flipping an Image problem and will implement Flipping an Image solution in Java. Flipping an Image Problem Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally means that each row of the image is reversed.  For example, flipping [1, 1, 0] horizontally results in [0, 1, 1]. To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0]. Example 1: Input: [[1,1,0],[1,0,1],[0,0,0]] Output: [[1,0,0],[0,1,0],[1,1,1]] Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].                      Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]] Example 2: Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]] Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]] Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,

LeetCode - Minimum Cost to Move Chips to The Same Position Solution - The Coding Shala

Home >> LeetCode >> Minimum Cost to Move Chips to The Same Position In this post, we will learn how to solve LeetCode's Minimum Cost to Move Chips to The Same Position problem and will implement its solution in Java. Minimum Cost to Move Chips to The Same Position Problem We have n chips, where the position of the ith chip is position[i]. We need to move all the chips to the same position. In one step, we can change the position of the ith chip from position[i] to: - position[i] + 2 or position[i] - 2 with cost = 0. - position[i] + 1 or position[i] - 1 with cost = 1. Return the minimum cost needed to move all the chips to the same position. Example 1: Input: position = [1,2,3]  Output: 1  Explanation: First step: Move the chip at position 3 to position 1 with cost = 0. Second step: Move the chip at position 2 to position 1 with cost = 1. The total cost is 1. Example 2: Input: position = [1,2,3]  Output: 1  Explanation: First step: Move the chip at position 3 to

LeetCode - Consecutive Characters Solution - The Coding Shala

Home >> LeetCode >> Consecutive Characters In this post, we will learn how to solve LeetCode's Consecutive Characters problem and will implement its Java Solution. Consecutive Characters Problem Given a string  s , the power of the string is the maximum length of a non-empty substring that contains only one unique character. Return  the power  of the string. Example 1: Input: s = "leetcode"  Output: 2  Explanation: The substring "ee" is of length 2 with the character 'e' only. Example 2: Input: s = "abbcccddddeeeeedcba"  Output: 5  Explanation: The substring "eeeee" is of length 5 with the character 'e' only. Practice this problem on LeetCode: Click Here Consecutive Characters Java Solution Approach 1: If two consecutive characters are the same then increase the count and return the maximum count of a char. Java Program: class Solution { public int maxPower ( String s ) { int ans =

Sort a linked list using insertion sort - The Coding Shala

Home >> Interview Questions >> sort a linked list using insertion sort  In this post, we will learn how to sort a linked list using insertion sort and will implement a Java solution. Sort a linked list using insertion sort in Java Sort a linked list using insertion sort. Example 1: Input: 4->2->1->3  Output: 1->2->3->4 Example 2: Input: -1->5->3->4->0  Output: -1->0->3->4->5 Java Program: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode insertionSortList ( ListNode head ) { ListNode dummy = new ListNode ( 0 ); ListNode curr = head , prevNode , nextNode ; while ( curr != null ) { //set the prevNode and nextNode at sta

LeetCode - Convert Binary Number in a Linked List to Integer Solution - The Coding Shala

Home >> LeetCode >> convert binary number in a linked list to integer  In this post, we will learn how to solve LeetCode's Convert Binary Number in a Linked List to Integer problem and will implement Java Solution. Convert Binary Number in a Linked List to Integer Problem Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number. Return the decimal value of the number in the linked list. Example 1: Input: head = [1,0,1] Output: 5 Explanation: (101) in base 2 = (5) in base 10 Example 2: Input: head = [0] Output: 0 Example 3: Input: head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0] Output: 18880 Convert Binary Number in a Linked List to Integer Java Solution Approach 1: We can solve this using extra memory. First will take all data from the linked list and move to ArrayList then we can change it to decimal. Java Program:  /** * Definition for sing