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
Programming Articles
Page 568 of 2547
Basic Calculator II in Python
A basic calculator evaluates mathematical expressions containing non-negative integers and operators (+, -, *, /). The calculator must handle operator precedence correctly, where multiplication and division are performed before addition and subtraction. For the input "3+2*2", the output should be 7 because multiplication has higher precedence than addition. Algorithm Approach We use a stack-based approach to handle operator precedence ? Remove spaces from the expression Use a stack to store intermediate results Process multiplication and division immediately Store addition and subtraction operands for later summation Implementation class Solution: ...
Read MoreBinary Tree Zigzag Level Order Traversal in Python
Binary tree zigzag level order traversal visits nodes level by level, alternating direction each level. The first level goes left to right, second level right to left, third level left to right, and so on. 3 9 20 15 7 ...
Read MoreKeys and Rooms in Python
The Keys and Rooms problem is a graph traversal challenge where we need to determine if all rooms can be visited starting from room 0. Each room contains keys to other rooms, and we need to check if we can reach every room using available keys. Problem Understanding Given N rooms numbered 0 to N-1, we start in room 0. Each room i contains a list of keys rooms[i], where each key opens a specific room. We need to return True if we can visit all rooms, False otherwise. Key constraints ? Initially, all rooms ...
Read MoreMaximum Swap in Python
The Maximum Swap problem asks us to find the maximum possible number by swapping at most two digits in a given non-negative integer. For example, given 2736, we can swap digits 2 and 7 to get 7236, which is the maximum possible value. Algorithm Overview The approach works by comparing the original number with its digits sorted in descending order. When we find the first position where they differ, we perform the optimal swap ? Convert the number into a list of digits Create a sorted version in descending order to find the target arrangement Find ...
Read MorePalindromic Substrings in Python
A palindromic substring is a substring that reads the same forwards and backwards. Given a string, we need to count all palindromic substrings where substrings with different start or end indices are counted separately, even if they contain the same characters. For example, in the string "aaa", there are 6 palindromic substrings: "a", "a", "a", "aa", "aa", "aaa". Brute Force Approach The simplest approach is to check every possible substring and verify if it's a palindrome ? class Solution: def countSubstrings(self, s): counter ...
Read MoreMinimum Moves to Equal Array Elements II in Python
Finding the minimum number of moves to make all array elements equal is a classic problem that can be solved efficiently using the median approach. A move consists of incrementing or decrementing any element by 1. Algorithm Overview The key insight is that the optimal target value is the median of the array. This minimizes the total distance (moves) needed to make all elements equal ? Steps to Solve Sort the array to find the median easily Find the median element (middle element of sorted array) ...
Read More4Sum II in Python
The 4Sum II problem asks us to find how many tuples (i, j, k, l) exist such that A[i] + B[j] + C[k] + D[l] equals zero, given four lists A, B, C, D of integers. This is an optimization problem that can be solved efficiently using a hash map approach. Problem Understanding Given four lists of integers with the same length N (0 ≤ N ≤ 500), we need to count tuples where the sum of elements at indices (i, j, k, l) from lists A, B, C, D respectively equals zero. For example, with A = ...
Read MoreHexspeak in Python
Hexspeak is a novelty form of spelling that uses hexadecimal digits to represent words. In Python, we can convert decimal numbers to Hexspeak by first converting to hexadecimal, then replacing specific digits with letters. Understanding Hexspeak Conversion The conversion process involves these steps: Convert decimal number to hexadecimal Replace digit 0 with letter 'O' and digit 1 with letter 'I' Keep hexadecimal letters A-F unchanged If any other digits (2-9) exist, return "ERROR" Valid Hexspeak characters are: {"A", "B", "C", "D", "E", "F", "I", "O"} Implementation Using Built-in hex() Function Here's a ...
Read MoreDiet Plan Performance in Python
A diet plan performance problem tracks a dieter's points based on their calorie consumption over consecutive k days. The dieter gains or loses points depending on whether their total calories fall below a lower bound, above an upper bound, or within the normal range. Problem Description Given an array calories[i] representing daily calorie consumption, we need to evaluate every consecutive sequence of k days and calculate points based on these rules: If total calories < lower bound: lose 1 point If total calories > upper bound: gain 1 point Otherwise: no change in points ...
Read MoreSingle-Row Keyboard in python
A single-row keyboard requires finger movement between keys to type characters. Given a keyboard layout string and a word, we need to calculate the total time to type the word, where time equals the absolute distance between character positions. Problem Understanding Consider a keyboard layout "abcdefghijklmnopqrstuvwxyz" and word "hello": Start at index 0 (position 'a') Move to 'h' (index 7): distance = |0 - 7| = 7 Move to 'e' (index 4): distance = |7 - 4| = 3 Move to 'l' (index 11): distance = |4 - 11| = 7 Stay at 'l' (index 11): ...
Read More