JOISC2017 Ticket Reservation Problem Solution

Problem Statement: Given positive integers $n$, $m$, and $m$ triplets $(l_i, r_i, c_i)$, we have an array $a_{1..n}$ initialized with zeros. For each operation $i = 1, ..., m$, perform the following steps: Choose any integer $k \in [0, c_i]$. Add $k$ to all elements $a_j$ where $j \in [l_i, r_i]$. Add $c_i - k$ to all elements $a_j$ where $j \ ...

Posted on Tue, 09 Jun 2026 17:52:16 +0000 by adunphy

Problem Solving Approaches for AtCoder Beginner Contest 045

Problem A: Trapezoid Area Given the upper base $a$, lower base $b$, and height $h$ of a trapezoid, the area is calculated using the formula: $$\text{Area} = \frac{(a + b) \times h}{2}$$ Problem B: Card Game for Three Three players, A, B, and C, each start with a string of cards. Starting with player A, they draw cards in a sequence. If a player ...

Posted on Tue, 09 Jun 2026 17:50:46 +0000 by Ben5on

Understanding Time and Space Complexity in Algorithms

Data Structures A data structure is a way of organizing and storing data in a computer so that it can be accessed and modified efficiently. It defines the relationship between elements within a collection. Algorithms An algorithm is a well-defined computational procedure that takes input values and produces output values. Essentially, it's a se ...

Posted on Tue, 09 Jun 2026 17:30:41 +0000 by billabong0202

Minimum Money for Guaranteed Win in Guessing Game

Problem Description We're playing a number guessing game with the following rules: I pick a number between 1 and n. You guess which number I picked. Each time you guess wrong, I tell you whether my number is higher or lower. When you guess a number x and it's wrong, you pay $x. You win when you guess the correct number. Given n ≥ 1, determine ...

Posted on Tue, 09 Jun 2026 16:59:45 +0000 by void

Generating Valid IP Addresses and Subsets from Given Inputs

Valid IP Address Generation Given a string containing only digits, geenrate all possbile valid IP address combinations. A valid IP address consists of four integers between 0-255 separated by dots, with no leading zeros. class IPGenerator { vector<string> validIPs; bool isValidSegment(const string& s, int start, int end) ...

Posted on Tue, 09 Jun 2026 16:46:05 +0000 by Coronet

Binary Search Techniques and Applications

This week's practice focuses on mastering binary search algorithms through LeetCode problems. The first problem is Binary Search. Below is the implementation: int search(int* nums, int numsSize, int target) { int left = 0; int right = numsSize - 1; while (left <= right) { int mid = left + (right - left) / 2; if ( ...

Posted on Mon, 08 Jun 2026 18:45:49 +0000 by canabatz

Validating Stack Pop Sequences with Capacity Constraints

Given a stack with a maximum capacity of M, and a sequence of numbers from 1 to N pushed in order, determine whether a given output sequence can be achieved through a series of push and pop operations. The key insight is to simulate the stack operations: push elements from 1 to N in order, and whenever the top of the stack matches the next expe ...

Posted on Mon, 08 Jun 2026 17:55:28 +0000 by riddlejk

Implementing an O(1) LFU Cache Algorithm

LRU vs. LFU Eviction PoliciesLeast Recently Used (LRU) and Least Frequently Used (LFU) are common cache eviction strategies. LRU tracks the time since last access, evicting the oldest entry when capacity is reached. LFU prioritizes access frequency, evicting entries with the lowest hit count. When multiple entries share the same minimum frequen ...

Posted on Sun, 07 Jun 2026 18:13:51 +0000 by jordan

Longest Common Substring Using Hashing

The problem involves finding the longest common substring between two strings composed solely of lowercase letters. The string lengths can reach up to 250,000. The solution uses binary search combined with hashing techniques to efficiently determine the maximum length of the shared substring. Let's denote the two input strings as $ s_1 $ and $ ...

Posted on Sat, 06 Jun 2026 17:48:41 +0000 by HuggyBear

Efficient Algorithms for GCD Pair Counting, Incremental Construction, and Circular Coloring

Counting GCD Pairs in Dynamic Multisets Maintain a multiset of positive integers with insertion and deletion operations. For each query, count unordered pairs (i, j) where i ≠ j and gcd(i, j) = k. Constraints: n, V ≤ 10^5. Conisder the change in answer count: $$\sum_{i=1}^{V}c_i[\gcd(i,x)=k]$$ Let x' = x/k: $$\sum_{i=1}^{\lfloor V/k \rfloor}c_{ ...

Posted on Sat, 06 Jun 2026 17:32:00 +0000 by dhruvasagar