跟随,学习,进步

Acm

The Ultimate Computer Technology Blog | The Knowledgebase of Computing

https://helloacm.com/
转到作者网站

How to Find the Largest Unique Number in Array?

Given an array of integers A, return the largest integer that only occurs once. If no integer occurs once, return -1. Example 1: Input: Output: 8 Explanation: The maximum integer in the array is 9... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


How to Find the K-diff Pairs in an Array with Two Pointer or Hash Map?

Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


String Algorithm: Reverse the first k characters for every 2k characters

Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


How to Generate Parentheses using Bruteforce or Depth First Search (Backtracking) Algorithms?

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: We can use the following two algorithms:... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


Algorithm to Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. You may assume that duplicates do not exist in the tree. For example, given preorder = inorder = Return the following... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


The 24 Game Algorithm using Depth First Search

You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through *, /, +, -, (, ) to get the value of 24. Example 1: Input: Output: True Explanation:... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


How to Count the Path Sum from a Binary Tree using Depth First Search Algorithm?

You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root or a leaf, but it must... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


How to Multiply Two Matrices in C++?

Given two sparse matrices A and B, return the result of AB. You may assume that A’s column number is equal to B’s row number. Example: Input: A = , ] B = , , ] Output: | 1 0 0... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


How to Compute the Number of Equivalent Domino Pairs?

Given a list of dominoes, dominoes = is equivalent to dominoes = if and only if either (a==c and b==d), or (a==d and b==c) – that is, one domino can be rotated to be equal to another domino.... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


The Maximum Average Subtree of a Binary Tree

Given the root of a binary tree, find the maximum average value of any subtree of that tree. (A subtree of a tree is any node of that tree plus all its descendants. The average value of a tree is the... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


How to Design a Snake Game?

Design a Snake game that is played on a device with screen size = width x height. Play the game online if you are not familiar with the game. The snake is initially positioned at the top left corner... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


How to Design a Two-Sum Data Structure?

Design and implement a TwoSum class. It should support the following operations: add and find. add – Add the number to an internal data structure. find – Find if there exists any pair of... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


The MySQL If and Case Statement Exercise: How to Swap Elements in MySQL?

Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all f values to m and vice versa) with a single update statement and no... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


Compute the Number of Ways to Paint the House via Dynamic Programming Algorithm

There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Return the total... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


How to Paint The Houses using Minimal Costs via Dynamic Programming Algorithm?

There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


Relative Sort Array Algorithm: Sort Array Based on Predefined Sequence

Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1. Sort the elements of arr1 such that the relative ordering of items in arr1 are the same... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


Classic Unlimited Knapsack Problem Variant: Coin Change via Dynamic Programming and Depth First Search Algorithm

You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


How to Remove Vowels from a String in C++?

Given a string S, remove the vowels ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’ from it, and return the new string. Example 1: Input:... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


How to Compute the Min Cost of Climbing Stairs via Dynamic Programming Algorithm?

On a staircase, the i-th step has some non-negative cost cost assigned (0 indexed). Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]


The Beginners’ Guide to Trie: How to Use the Trie in C++?

The Trie is common used data structure to speed up the word searching. The Trie is a structure to index the words character by character. Trie Definition in C++ Take a lowercase 26 letters for... [[ This is a content summary only. Visit my website for full links, other content, and more! ]]