Posts

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...

Array Transformation | biweekly-contest-12 | leetcode | Java Solution

Question Source Java Solution: class Solution {     public List<Integer> transformArray(int[] arr) {                  while(true) {             boolean isChanged = false;             int []copy = arr.clone();                          for(int i = 1; i < arr.length-1; i++) {                                 if(arr[i-1] > arr[i] && arr[i+1] > arr[i]) {                      copy[i]++;                     isChanged = true;                 }else if(arr[i-1] < arr[i] && arr[i+1] < arr[i]) {               ...

Design A Leaderboard | biweekly-contest-12 | leetcode | Java solution

Question Source Java Solution: class User implements Comparable<User> {     int id;     int score;          public User(int id, int score) {         this.id = id;         this.score =score;     }          @Override     public int compareTo(User u) {         return u.score - this.score;     }          @Override     public String toString(){         return id +":"+score+" ";     } } class Leaderboard {     private Set<Integer> ids;     private List<User> users;          public Leaderboard() {         this.ids = new HashSet<>();         this.users = new ArrayList<>();     }          pub...

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))){ ...