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
Server Side Programming Articles
Page 558 of 2109
Trapping Rain Water in Python
The trapping rain water problem is a classic algorithmic challenge where we calculate how much water can be trapped after raining on an elevation map represented by an array of heights. Each element represents the height of a bar with width 1. ...
Read MoreFirst Missing Positive in Python
The First Missing Positive problem asks us to find the smallest missing positive integer from an unsorted array. For example, given the array [4, -3, 1, -1], the result is 2 since 1 is present but 2 is missing. Algorithm Approach We use a cyclic sort approach to solve this efficiently: Add a 0 at the beginning to handle 1-based indexing Place each positive number at its correct index position Scan the array to find the first missing positive Example Implementation Here's the complete solution using cyclic sort ? class Solution: ...
Read MoreLongest Valid Parentheses in Python
Finding the longest valid parentheses substring is a common problem that can be solved efficiently using a stack-based approach. Given a string containing only '(' and ')' characters, we need to find the length of the longest valid (well-formed) parentheses substring. For example, in the string "))(())())", the longest valid parentheses substring is "(())())" with length 6. Algorithm Approach We use a stack to track indices of unmatched parentheses ? Initialize a stack with −1 to handle edge cases For each character, if it's '(', push its index onto the stack If it's ')', check ...
Read MoreMerge k Sorted Lists in Python
Merging k sorted lists is a classic algorithm problem. Given multiple sorted linked lists, we need to combine them into a single sorted list. Python's heapq module provides an efficient solution using a min-heap data structure. Problem Understanding Given k sorted linked lists like [1, 4, 5], [1, 3, 4], [2, 6], we need to merge them into one sorted list [1, 1, 2, 3, 4, 4, 5, 6]. Algorithm Steps Create a min-heap to store the smallest elements from each list Add the first node of each non-empty list to the heap Repeatedly extract ...
Read MoreInsert the string at the beginning of all items in a list in Python
In this tutorial, we'll learn how to insert a string at the beginning of all items in a list in Python. For example, if we have a string "Tutorials_Point" and a list containing elements like "1", "2", "3", we need to add "Tutorials_Point" in front of each element to get "Tutorials_Point1", "Tutorials_Point2", "Tutorials_Point3". Using List Comprehension with format() The most straightforward approach is using list comprehension with string formatting − sample_list = [1, 2, 3] result = ['Tutorials_Point{0}'.format(i) for i in sample_list] print(result) ['Tutorials_Point1', 'Tutorials_Point2', 'Tutorials_Point3'] Using map() with format() ...
Read MorePython - Insert list in another list
When working with lists in Python, you often need to insert one list into another. Python provides several methods to accomplish this: append(), extend(), insert(), and list concatenation with + operator. Using append() Method The append() method adds the entire second list as a single element ? first_list = [1, 2, 3, 4, 5] second_list = [6, 7, 8, 9, 10] first_list.append(second_list) print("Using append():", first_list) Using append(): [1, 2, 3, 4, 5, [6, 7, 8, 9, 10]] Using extend() Method The extend() method adds each element of the second ...
Read MorePython - Increasing alternate element pattern in list
This article demonstrates how to create an increasing alternate element pattern in a list where each original element is followed by a string of asterisks that increases in length. We'll use list comprehension with enumerate() to achieve this pattern efficiently. Understanding the Pattern The increasing alternate element pattern takes a list like [1, 2, 3] and transforms it to [1, '*', 2, '**', 3, '***']. Each element is followed by asterisks equal to its position (1-indexed). Using List Comprehension with enumerate() The enumerate() function adds a counter to each element, starting from 1. We use nested ...
Read MoreCheck if one list is subset of other in Python
Python provides various methods to check if one list is a subset of another. A subset means all elements of the smaller list exist in the larger list. We'll explore three effective approaches: all() function, issubset() method, and intersection() method. Using all() Function The all() function returns True if all elements in an iterable are true, otherwise False. We can combine it with a generator expression to check if every element of the sublist exists in the main list − # Define the main list and the sublist main_list = ['Mon', 'Tue', 5, 'Sat', 9] sub_list ...
Read MoreCompare Version Numbers in Python
Comparing version numbers is a common programming task. Python provides several ways to compare version strings like "1.0.1" and "1.2.3". When comparing versions, we return 1 if the first version is greater, -1 if it's smaller, and 0 if they're equal. Understanding Version Number Comparison Version numbers consist of numeric parts separated by dots. Each part represents a different level of revision ? Version "2.5" means the 5th second-level revision of the 2nd first-level revision Missing parts default to 0 (e.g., "1.2" is equivalent to "1.2.0.0...") Compare each part from left to right until finding a ...
Read MoreLongest Well-Performing Interval in Python
The Longest Well-Performing Interval problem requires finding the longest subarray where tiring days (hours > 8) outnumber non-tiring days. We solve this using a prefix sum approach with a hashmap to track cumulative balance efficiently. Understanding the Problem A tiring day occurs when hours worked > 8. A well-performing interval is a subarray where tiring days strictly outnumber non-tiring days. We transform each day into +1 (tiring) or -1 (non-tiring) and find the longest subarray with positive sum. Algorithm Approach We use a prefix sum technique with the following key insights: Convert hours to ...
Read More