Posts

Showing posts from October, 2019

Dynamic Programming: Basics and a Practical Example

Introduction: Dynamic programming is a problem-solving technique that can seem quite intimidating at first glance. However, once you grasp its fundamentals and see it in action, you'll find it to be a powerful tool in your coding arsenal. In this blog post, we'll demystify the basics of dynamic programming and walk through a practical example. We'll provide you with clear explanations and a complete Python program, so you can understand and apply this essential concept with confidence. Understanding Dynamic Programming: At its core, dynamic programming is all about breaking down a complex problem into smaller, overlapping subproblems. It's a technique used to efficiently solve problems by storing and reusing solutions to these subproblems, rather than recomputing them. This leads to significant improvements in runtime complexity. Example: Fibonacci Sequence with Dynamic Programming Let's start by tackling a classic problem: calculating Fibonacci numbers. The Fibonac...

Longest Palindromic Substring | leetcode

  Question Source - Given a string  s , find the longest palindromic substring in  s Example 1: Input: s = "babad" Output: "bab" Explanation: "aba" is also a valid answer. Example 2: Input: s = "cbbd" Output: "bb"   Solution: JAVA class Solution {     public String longestPalindrome(String s) {         if(s.length() == 0) {             return s;         }                  int startIndex = 0;         int endIndex = 0;         int maxLength = 0;                  for(int i = 0; i < s.length(); i++) {                          int length = 1;             int j = i - 1, k = i +1;                       ...

Median of Two Sorted Arrays Solution | leetcode

Source Solution : class Solution {     public double findMedianSortedArrays(int[] nums1, int[] nums2) {                  int merge[] = new int[nums1.length + nums2.length];         int i = 0, j = 0, index = 0;                  while(i < nums1.length || j < nums2.length) {             if(i < nums1.length && j < nums2.length) {                 if(nums1[i] <= nums2[j]) {                    merge[index] = nums1[i];                     i++;                     index++;                     continue;                 } else { ...

leetcode | Longest Substring Without Repeating Characters Solution

Source Problem: Given a string, find the length of the  longest substring  without repeating the 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. Solution: class Solution {     public int lengthOfLongestSubstring(String s) {                  Set<Character> subStr = new HashSet<>();         int maxLength = 0;         int j = 0;         for(int i = 0; i < s.length(); i++) {                          if(subStr.contains(s.charAt(i))){ ...

Climbing the Leaderboard | Hackerrank | Solution

Source  Hackerrank Alice is playing an arcade game and wants to climb to the top of the leaderboard and wants to track her ranking. The game uses  Dense Ranking , so its leaderboard works like this: The player with the highest score is ranked number   1 on the leaderboard. Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number. For example, ranked = {100, 90, 90, 80} player = {70. 80. 105} The ranked players will have ranks  ,  ,  , and  , respectively. If the player's scores are  ,   and  , their rankings after each game are  ,   and  . Return  . Function Description Complete the  climbingLeaderboard  function in the editor below. climbingLeaderboard has the following parameter(s): int ranked[n] : the leaderboard scores int player[m] : the player's scores Input Format The first line co...