Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 687 of 855
Convert Sorted Array to Binary Search Tree in Python
Converting a sorted array to a height-balanced binary search tree (BST) is a common algorithmic problem. A height-balanced BST is a binary tree where the depth of the two subtrees of every node never differs by more than 1. Algorithm The key insight is to use the middle element as the root to ensure balance. The algorithm works as follows − If the array is empty, return None Find the middle element and make it the root Recursively build the left subtree from elements before the middle Recursively build the right subtree from elements after the ...
Read MoreMaximum Depth of Binary Tree in Python
A binary tree's maximum depth is the number of nodes along the longest path from the root to any leaf node. This is a fundamental tree traversal problem that can be solved efficiently using recursion. 1 2 2 3 4 3 ...
Read MoreMerge Sorted Array in Python
When working with two sorted arrays, we often need to merge them into a single sorted array. Python provides several approaches to accomplish this task efficiently. For example, if we have A = [1, 2, 4, 7] and B = [1, 3, 4, 5, 6, 8], the merged result should be [1, 1, 2, 3, 4, 4, 5, 6, 7, 8]. Using Two Pointers Approach The most efficient approach uses two pointers to compare elements from both arrays ? def merge_sorted_arrays(arr1, arr2): merged = [] i = ...
Read MoreSqrt(x) in Python
Finding the square root of a number without using library functions is a common programming challenge. We need to implement our own function that returns the integer part of the square root, truncating any decimal digits. For example, if x = 4, the result is 2. If x = 8, the result is also 2 (since √8 = 2.828... but we take only the integer part). Algorithm Overview We use binary search to find the square root efficiently ? Initialize low = 1, high = x + 1, and answer = 0 While high > ...
Read MorePlus One in Python
The Plus One problem involves incrementing a large number represented as an array of digits. Given an array like [5, 3, 2, 4] representing 5324, we need to add 1 and return [5, 3, 2, 5] representing 5325. Method 1: String Conversion Approach Convert the array to a string, then to integer, add 1, and convert back to array ? def plus_one_string(digits): # Convert digits to string num_str = "" for digit in digits: num_str += ...
Read MoreMaximum Subarray in Python
The Maximum Subarray Problem involves finding a contiguous subarray within an array that has the largest sum. This is a classic problem that can be efficiently solved using Kadane's Algorithm, which uses dynamic programming principles. For example, given the array [-2, 1, -3, 4, -1, 2, 1, -5, 4], the maximum subarray is [4, -1, 2, 1] with sum 6. Algorithm Overview The dynamic programming approach works as follows ? Create a DP array of the same size as input array Initialize dp[0] = nums[0] ...
Read MoreCount and Say in Python
The Count and Say sequence is a fascinating pattern where each term describes the previous term by counting consecutive identical digits. Let's understand how this sequence works and implement it in Python. Understanding the Sequence The Count and Say sequence starts with "1" and each subsequent term describes the previous term ? Term 1: "1" Term 2: "11" (one 1) Term 3: "21" (two 1s) Term 4: "1211" (one 2, one 1) Term 5: "111221" (one ...
Read MoreImplement strStr() in Python
The strStr() function finds the first occurrence of a substring within a string and returns its index. This is similar to the strstr() function in C programming. If the substring is not found, it returns -1. Algorithm To implement strStr(), we use a two-pointer approach with the following steps ? Initialize i = 0 (for main string), j = 0 (for substring) If substring is empty, return 0 While i < length of string and remaining characters >= substring length: If characters match, compare the entire substring If complete match found, return starting index Otherwise, ...
Read MoreMerge Two Sorted Lists in Python
Merging two sorted lists is a common programming problem where we combine two ordered sequences into a single sorted sequence. Python provides several approaches including recursion, iteration, and built-in methods like heapq.merge(). For example, if A = [1, 2, 4, 7] and B = [1, 3, 4, 5, 6, 8], the merged result will be [1, 1, 2, 3, 4, 4, 5, 6, 7, 8]. Using Recursion with Linked Lists The recursive approach works by comparing the first elements of both lists and selecting the smaller one ? class ListNode: def ...
Read MoreLongest Common Prefix in Python
Finding the longest common prefix among a set of strings is a common programming problem. The longest common prefix is the initial substring that appears at the beginning of all strings in the array. If no common prefix exists, we return an empty string. For example, given the array ["school", "schedule", "scotland"], the longest common prefix is "sc" since it appears at the start of all three strings. Algorithm Approach We start by taking the first string as our reference. Then we iterate through each remaining string, comparing characters one by one. When we find a mismatch, ...
Read More