Fading Coder

One Final Commit for the Last Sprint

Mastering Recursion Through Three Classic Problems

Tower of Hanoi: A Recursive Challenge The Tower of Hanoi is a classic puzzle involving three pegs and a number of disks of different sizes. The objective is to move the entire stack to another peg, following these rules: only one disk can be moved at a time, and a larger disk cannot be placed on top...

Using Union-Find with Path Compression and Dynamic Programming for Truth-Teller Identification

Problem Context An island has two types of inhabitants: truth-tellers, who always tell the truth, and liars, who always lie. Each inhabitent has a unique integer identifier. You are allowed n questions. Each question must be direcetd to one inhabitant, asking whether another inhabitant is a truth-te...

Essential LeetCode Problems for Algorithm Practice

Longest Increasing Subsequence To determine the length of the longest strictly increasing subsequence, dynamic programming is applied where dp[i] represents the length of the longest subsequence ending at index i. class Solution { public: int lengthOfLIS(vector<int>& sequence) { int size =...

Algorithmic Problem Solving: Contest Round Solutions

Problem A: Symmetric Pair Removal Given a binary string of length n, repeatedly remove a pair of characters from opposite ends if they differ (one is '0' and the other is '1'). Calculate the remaining length after no more valid pairs can be removed. A two-pointer technique efficiently solves this pr...

Competitive Programming Problem Set and Solutions

L1-1: Print Greeting Print a fixed greeting message. print("yuan shen, qi dong!") L1-2: Value Comparison Given a real number x, compare it to 1 and print the appropriate relational operator. #include <iostream> using namespace std; int main() { double val; cin >> val; if(val &l...

Solving Road Construction Problem Using Dynamic Programming Approach

Problem Analysis The road construction problem involves determining the minimum time required to complete paving operations across multiple segments. While algorithm tags suggest greedy approaches and binary indexed trees, a dynamic programming solution provides an elegant and efficeint implementati...

Efficient Problem Solutions for Programming Contests

Contest Preparation Algorithm This solution handles two special cases before applying the general formulla: #include <iostream> using namespace std; int main() { int n, m; cin >> n >> m; if (n == 0) { cout << 0 << endl; } else if (n <= m) { cout << 2 << e...

Dynamic Programming Patterns: Linear DP, Knapsack Variants and Optimization Techniques

Longest Monotonic Subsequence via Greedy Array Core Concept The "pseudo-monotonic stack" approach for finding longest monotonic subsequences uses a dynamic array that maintains potential candidates for optimal sequence construction. This method efficiently builds the solution in O(n log n)...

Algorithmic Solutions for the YsOI2023 Contest

Task 1: Bitwise Manipulation Strategy The objective involves transforming an integer based on specific bitwise properties. The core logic requires isolating the odd component of the input value by shifting out trailing zeros. Once the odd base is identified, it is shifted left by a count derived fro...

Solving Inversion Counting, Permutation Optimization, and Other Competitive Programming Challenges

Interval Inversion Count Given an array of length n with elements bounded by a small constant (≤ 50), we need to answer m queries of the form: count the number of inversions inside subarray [l, r]. A naive O(n2) per query is too slow, and a Mo’s algorithm solution, while feasible for moderate constr...