Fading Coder

One Final Commit for the Last Sprint

Reconstructing a Binary Tree from Preorder and Inorder Traversal Arrays

The problem requires constructing a binary tree given two integer arrays, preorder and inorder, representing the tree's preorder and inorder traversals, respectively, and returning the root node. The solution leverages the distinct properties of these traversal orders. In a preorder traversal, nodes...

SMU Summer 2023 Contest Round 7 Solutions

A. Two Rival Students The maximum possible dsitance between two students in a line of n positions is n - 1. Given their initial positions a and b, and up to x swaps, the achievable distance is the minimum of n - 1 and |a - b| + x. #include <bits/stdc++.h> using namespace std; int main() { ios:...

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...

Generate Unique Permutations with Backtracking

Given an array of integers that may contain duplicates, ganerate all unique permutations of the array. This problem extends the classic permutation generation by requiring the elimination of duplicate sequences that arise from identical elements. Key Insight When elements repeat, naive backtracking...

Finding Integers with Sequential Digits in a Range

Problem DefinitionAn integer is defined as having sequential digits if every digit in the number is exactly one greater than the previous digit. For example, 123 and 3456 satisfy this condition, while 135 or 121 do not.Given two integers, low and high, the task is to return a sorted list of all inte...

Hash Table and Two-Pointer Techniques for K-Sum Problems

4Sum II: Pair Summation with Hash Maps Given four integer arrays of equal length, determine the number of tuple combinations (i, j, k, l) where the sum of corresponding elements equals zero. Unlike single-array k-sum problems requiring deduplication, this scenario involves four independent collectio...

Adding Two Numbers Represented by Linked Lists

Problem Description Given two non-empty linked lists representing non-negative integres in reverse digit order, combine the numbers and return the result as a linked list. Basic Iterative Approach class Solution { public ListNode addTwoNumbers(ListNode first, ListNode second) { ListNode dummyHead =...

Python Algorithm Practice for Blue Bridge Cup Competition - Set 2

1. Minefield Crossing Approach: BFS traversal using a queue. import sys n = int(input()) grid = [input().split() for _ in range(n)] visited = [[0] * n for _ in range(n)] directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] queue = [] end_x, end_y = 0, 0 for i in range(n): for j in range(n): if grid[i][j]...

Heuristic Merging and Tree Heuristic Merging: A Comprehensive Guide

Heuristic Merging and Tree Heuristic Merging: A Comprehensive Guide Core Concepts Fundamental Knowledge: Heuristic Merging (DSU) Heuristic algorithms are optimizations based on human experience and intuition. The classic example of heuristic merging is the union-find data structure's union by size/r...

Solution to AtCoder Beginner Contest 446

D - Max Straight This problem can be solved efficient using a hash map. Initially, I overcomplicated it by thinking about sorting and finding the longest increasing subsequence. However, a simple approach using a map workss perfectly. #include <bits/stdc++.h> using namespace std; int main() {...
First 1 2 3 4 5 6 7 8 9 Next Last