Python Articles

Page 666 of 855

Partition Array for Maximum Sum in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

The Partition Array for Maximum Sum problem involves partitioning an array into contiguous subarrays of length at most K, where each subarray's values are replaced with the maximum value in that subarray. The goal is to maximize the total sum after partitioning. For example, with array [1, 15, 7, 9, 2, 5, 10] and K=3, the optimal partitioning creates subarrays [1, 15, 7], [9], and [2, 5, 10], which become [15, 15, 15], [9], and [10, 10, 10], giving sum 84. Algorithm Approach We use dynamic programming where dp[i] represents the maximum sum for the subarray ending ...

Read More

Smallest Integer Divisible by K in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 446 Views

Given a positive integer K, we need to find the smallest positive integer N such that N is divisible by K and N contains only the digit 1 (like 1, 11, 111, 1111, etc.). We return the length of such number N, or -1 if no such number exists. For example, if K = 3, the smallest number containing only 1s that is divisible by 3 is 111, so we return 3 (the length). Algorithm To solve this problem, we follow these steps: If K is even or divisible by 5, return -1 (no solution ...

Read More

Clumsy Factorial in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 2K+ Views

The clumsy factorial is a variation of the traditional factorial where we use a rotating pattern of operations: multiply (*), divide (/), add (+), and subtract (-) instead of just multiplication. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1. The operations follow normal arithmetic precedence rules: multiplication and division are performed before addition and subtraction, and operations of equal precedence are evaluated left to right. We use floor division to ensure integer results. Understanding the Pattern Let's trace through clumsy(10) step by ...

Read More

Minimum Add to Make Parentheses Valid in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 482 Views

When we have a string of parentheses, we need to find the minimum number of parentheses to add to make it valid. A valid parentheses string follows these rules ? It is the empty string It can be written as XY (X concatenated with Y), where X and Y are valid strings It can be written as (A), where A is a valid string For example, if the string is "()))((", we need to add 4 more parentheses to make it valid. Algorithm We use a stack-based approach to track unmatched parentheses ? ...

Read More

Find and Replace Pattern in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 263 Views

Finding and replacing patterns in Python involves identifying words that match a given character pattern structure. A word matches a pattern if there exists a character mapping where each unique character in the pattern maps consistently to characters in the word. For example, with pattern "abb" and words ["abc", "deq", "mee", "aqq", "dkd", "ccc"], the words "mee" and "aqq" match because they follow the same structure: first character is unique, second and third characters are the same. Algorithm Approach The solution converts each word and the pattern into a normalized format representing the character structure. Characters are ...

Read More

Construct Binary Tree from Preorder and Postorder Traversal in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 570 Views

Binary tree construction from preorder and postorder traversals is a fundamental algorithm problem. Given two traversal sequences, we can reconstruct the original binary tree using a stack-based approach. Understanding the Problem Given preorder traversal [1, 2, 4, 5, 3, 6, 7] and postorder traversal [4, 5, 2, 6, 7, 3, 1], we need to construct the binary tree. The preorder visits root first, while postorder visits root last. 1 2 3 ...

Read More

Decoded String at Index in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 257 Views

The Decoded String at Index problem involves finding a character at a specific position in a decoded string without actually creating the full decoded string. This approach saves memory when dealing with very long decoded strings. Problem Understanding Given an encoded string, we decode it using these rules ? If the character is a letter, write it to the tape. If the character is a digit, repeat the entire current tape digit − 1 more times. For example, "hello2World3" becomes "hellohelloWorldhellohelloWorldhellohelloWorld". Algorithm Overview Instead of creating the full decoded string, we use ...

Read More

Kth Smallest Element in a Sorted Matrix in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 519 Views

Finding the Kth smallest element in a sorted matrix is a classic problem that can be solved efficiently using binary search. In this problem, we have an n x n matrix where each row and column is sorted in increasing order, and we need to find the kth smallest element in the entire matrix. Problem Understanding Given a matrix where rows and columns are sorted in ascending order, we need to find the kth smallest element. For example, in the matrix [[1, 5, 9], [10, 11, 13], [12, 13, 15]], if k=8, the answer is 13 because when ...

Read More

Surrounded Regions in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 394 Views

The Surrounded Regions problem involves capturing all regions of 'O' that are completely surrounded by 'X' on a 2D board. A region is considered surrounded if all 'O' cells are enclosed by 'X' cells and cannot reach the board's border. Problem Understanding Given a 2D board with 'X' and 'O' characters, we need to capture surrounded regions by flipping all 'O' to 'X'. However, 'O' cells connected to the border should remain unchanged ? Input Board Output Board XXXX XOOX XXOX XOXX XXXX XXXX XXXX XOXX ...

Read More

Minimum Path Sum in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 842 Views

The minimum path sum problem involves finding a path from the top-left corner to the bottom-right corner of a matrix that minimizes the sum of all numbers along the path. You can only move down or right at any point. Problem Example Given this matrix: 1 3 1 1 5 1 4 2 1 The optimal path is: 1 → 3 → 1 → 1 → 1, giving a minimum sum of 7. Algorithm Steps The dynamic programming approach works as follows: Fill the ...

Read More
Showing 6651–6660 of 8,549 articles
« Prev 1 664 665 666 667 668 855 Next »
Advertisements