algorithm

A collection of 116 post

Factorial

Problem Enter a natural number N to get the value of N!. N! = n(n-1)(n-2)…2*1. If N=5 then 5!=54321=120. input In the first line, the natural number N (3<=n<=10) is entered. output Print the N factorial value on the first line. Solution plus subtract

Number of Combinations (Memoization)

Problem The equation above is solution for number of combinations. But instead of using this formula, write a program that uses the following equation to find the combinatorial number using recursion. input The first line contains the natural numbers n (3<=n<=33) and r (0<=r<=n). output Prints the…

Finding Permutations

Problem If N natural numbers less than or equal to 10 are given, M out of them are selected and all methods are printed out. input The first line is given the natural numbers N(3<=N<=10) and M(2<=M<=N) In the second line, N natural numbers are given in ascending order output Prints the result on the…

Coin Exchange

Problem How can I exchange change for the smallest number of coins when several units of coins are given as follows? Each unit of coins can be used indefinitely. input In the first line, the number of types of coins N (1<=N<=12) is given. The second line is given N types of coins, and the next line…

Find Duplicate Permutations

Problem There are marbles numbered from 1 to N. Of these, duplicates are allowed, and all methods of selecting M times and arranging them are printed out. input The first line is given the natural numbers N(3<=N<=10) and M(2<=M<=N). output Prints the result on the first line. Prints the last total…

Finding the Maximum Score (DFS)

Problem In order to get good grades in this information olympiad, Hyun-soo is going to solve the N problems given by the teacher. Each question is given a score for solving it and the time it takes to solve it. We need to get the maximum score out of N problems within the time limit M. (The problem…

Dog Riding

Problem Cheol-su wants to go to the market with his dog. But his truck cannot carry more than C kilograms. Cheol-su wants to burn his dog the most without going over C. Given N dogs and the weight W of each dog, write a program to find the heaviest weight that Cheolsu can carry in a truck. input The…

Subsets with Equal Sum (DFS)

Problem Given a set of natural numbers with N elements, When this set is divided into two subsets, if there is a case where the sum of the elements of the two subsets is equal, “YES” is output, Otherwise, write a program that prints “NO”. The two subsets divisible by two are disjoint sets, and the…

Subset (DFS)

Problem Given a natural number N, write a program that prints all subsets of a set with elements 1 through N. input The first line is given a natural number N (1<=N<=10). output From the first line, the subsets are printed in the same order as in the output example below, one for each line However…

Binary Tree Traversal (Depth First Search)

Problem Practice forward and backward traversal of the binary tree as shown in the figure below. output Pre-traversal output: Inter-traversal output: Post-traversal output: Solution Pre traversal Inter traversal Post traversal

Recursive Binary

Problem Write a program that converts decimal number N into binary number and outputs it. However, it must be output using a recursive function. input The first line is given a decimal number N (1<=N<=1,000). output Print the binary number on the first line. Solution

Recursive Function

Problem Write a program that outputs 1 to N using a recursive function when a natural number N is input. input In the first line, an integer N (3<=N<=10) is entered. output print on the first line Solution

Stable Setup (Decision Algorithm)

Problem N stables are on a vertical line. Each stable has the coordinates of x1, x2, x3, …, xN, and there is no overlap of coordinates between the stables. Hyeonsu has C horses, who don’t like being close to each other. Each stable can only hold one horse, and we want the horses to be placed in the…

Music Video (Decision Tree Algorithm)

Problem Genie Records is trying to sell the live video of the undisputed singer Youngpil Cho on DVD. A total of N songs are included in a DVD, and when recording on a DVD, the order in live should be maintained. Our singer Youngpil Cho hates the change of order. That is, in order to record songs…

Binary Search

Problem Any N numbers are given as input. Write a program that sorts N numbers in ascending order and, given M, which is one of the N numbers, uses binary search to find the number of M in the sorted state. However, there are no duplicate values. input The first line gives the natural numbers N (…

Meeting Room Assignment

Problem Conference Room There is one conference room, and we want to create a conference room usage table for n conferences that we want to use. Find the maximum number of meetings that are given the time and can use the conference room without overlapping each meeting. However, once a meeting…

Wedding Ceremony

Problem Hyun-soo is getting married next month. Hyun-soo rents a place for the wedding reception and plans to hold it for three days without a break. Hyeon-soo requested information about the time of N friends attending the reception in advance. Each friend told Hyeon-soo what time he would arrive…

Coordinate Alignment

Problem Given N coordinates on a plane (x, y), write a program that sorts all coordinates in ascending order. The sort criterion is first sorted by the x value, and if the x value is the same, it is sorted by the y value. input The first line is given the number of coordinates N (3<=N<=100,00…

Naughty Hyeonsu

Problem A new semester has begun. Hyunsoo was so excited to meet his new mate. There are N students in Hyunsoo’s class. In order to assign class numbers to the class, the teacher placed the class in a row on the playground, starting with the tallest student. Class numbers are numbered from 1 to N…

Least Recently Used

Problem Cache memory is a high-speed temporary memory between the CPU and main memory (DRAM). It is very expensive and has a small capacity, so it must be used efficiently. The cache memory usage rules of the computer of the withdrawal follow the LRU algorithm. LRU algorithm is an abbreviation of…

Insertion Sort

Problem Write a program that sorts in ascending order when N numbers are input and outputs them. The sorting method is insertion sort. input The first line is given a natural number N (1<=N<=100). In the second line, N natural numbers are entered with a space between them. Each natural number is in…

Special Sort

Problem If N integers are entered, you must sort the entered values. Negative integers must be on the front and positive integers on the back. Also, the order of positive and negative integers should not change. input Integer N (5<=N<=100) is given to the first line, and integers including negative…

Bubble Sort

Problem Write a program that sorts in ascending order when N numbers are input and outputs them. The sorting method is bubble sort. input The first line is given a natural number N (1<=N<=100) In the second line, N natural numbers are entered with a space between them. Each natural number is in the…

Selection Sort

Problem Write a program that sorts in ascending order when N numbers are input and outputs them. The sorting method is selection sort. input The first line is given a natural number N (1<=N<=100) In the second line, N natural numbers are entered with a space between them. Each natural number is in…

Curriculum Design

Problem Hyeonsu must plan a one-year course. There are compulsory subjects in class. These compulsory courses must be completed, and the order is set. If there are total subjects A, B, C, D, E, F, G, and the compulsory subjects are given as CBA, the compulsory subjects are C, B, and A subjects, and…

Save the Princess

Problem The only daughter Princess of the neighboring country of the information kingdom has been captured by a monster in the forest. There are N princes in the information kingdom, and they both agree to go rescue the princess. The king of the information kingdom decides which prince to go to…

Iron Stick

Problem I am trying to cut several iron rods with a laser. For efficient operation, the iron rods are stacked from the bottom up, and the laser is fired vertically from above to cut the iron rods. The arrangement of the iron rod and laser satisfies the following conditions. An iron rod can only be…

Postfix Operation

Problem Write a program that outputs the result of the operation given a postfix expression. If 3(5+2)-9 is expressed as a postfix expression, it is expressed as 352+9- and the result is 12. input Postfix expression is given in the first line. The length of the expression cannot exceed 5…

Crane Game

Problem Jordi, a game developer, wants to make a crane puppet machine into a mobile game. In order to increase the fun of the game, Jordi tries to reflect the screen composition and rules in the game logic as follows. The game screen is an N x N square grid of 1 x 1 squares with a crane on the top…

Remove Parentheses Letter (Stack)

Problem Write a program that removes all characters between parentheses ( ) in the input string and prints only the remaining characters. input A string of parentheses is entered on the first line The length of the string does not exceed 100. output Only the remaining characters are printed…

Correct Parenthesis (Stack)

Problem If parentheses are entered, it outputs “YES” if it is a valid parenthesis, and “NO” if it is not correct. (())() This is the correct placement of the pair of parentheses, but (()())) is not the correct parentheses. input A string of parentheses is entered on the first line The maximum length…

Find All Anagrams (Hash, Two-Pointer, Sliding Window)

Problem Write a program that counts the number of substrings of S that are anagrams and T strings in S string. When identifying anagrams, case is sensitive. Substrings must be contiguous strings. input The first S string is entered on the first line, and the T string is entered on the second line…

Anagram (Hash)

Problem Anagrams Two words are said to be anagrams when two strings differ in alphabetical order, but their composition matches. For example, AbaAeCe and baeeACA have different alphabets, but if you look at their composition, they are A(2), a(1), b(1), C(1), e(2), the alphabets and their numbers…

Class President (Hash)

Problem A, B, C, D, and E candidates registered as candidates to select the class president. The ballot is written with the symbol (alphabetically) of the candidate chosen by the students in the class, and the teacher announces the symbol. After the teacher’s presentation, write a program that…

Maximum Sales

Problem Hyun-soo’s father runs a bakery. Hyeon-soo’s father gave him the sales record for N days and told him to find the maximum sales for K consecutive days. If N=10, the sales record for 10 days is as follows. In this case, if K = 3 The maximum sales for 3 consecutive days is 11+20+25=560,000 won…

Consecutive Sub Sequence 2

Problem You are given a sequence of N numbers. Write a program to find how many times in this sequence the sum of consecutive subsequences is less than or equal to a specific number M. If N=5, M=5 and the sequence is A continuous subsequence whose sum is 5 or less is {1}, {3}, {1}, {2}, {3}, {1,…

Consecutive Sub Sequence

Problem You are given a sequence of N numbers. Write a program to count how many times in this sequence the sum of consecutive subsequences is a specific number M. If N=8, M=6 and the sequence is There are three consecutive subsequences whose sum is 6: {2, 1, 3}, {1, 3, 1, 1}, and {3, 1, 1,…

Finding Commons

Problem Given two sets A and B, write a program that extracts the common elements of the two sets and outputs them in ascending order. input The first line gives the size N of set A (1<=N<=30,000). The second line gives N elements. Duplicate elements are not given. The third line gives the size M of…

Concatenate Two Arrays

Problem Given two arrays sorted in ascending order, write a program that merges the two arrays in ascending order and prints them. input The first line gives the size N (1<=N<=100) of the first array. The second line gives the N array elements in ascending order. The third line gives the size M (…

Kth Largest Number

Problem Hyeonsu has N cards with natural numbers between 1 and 100. There can be multiple cards of the same number. Hyeon-soo draws 3 of them and tries to record the sum of the numbers on each card. Record all three possible draws. Write a program that prints the Kth largest number among recorded…

Graduation Gift

Problem The teacher is going to give a graduation gift to the classmates who are graduating this year. Students were asked to choose a product they wanted from an online shopping mall and submit the price and shipping cost of that product. Teachers have a limited budget. Write a program to find out…

Mentoring

Problem Hyunsoo’s class teacher wants to create a mentoring system to improve the students’ math scores. Mentoring is a pair of mentors (students helping) and mentees (students receiving help), and the mentor helps the mentee study math. The teacher selects a mentor and a mentee based on the M…

Reverse Prime Number

Problem Write a program that flips each natural number when N natural numbers are input and outputs the prime number if the reversed number is prime. For example, flipping 32 is 23, and 23 is a prime number. Then it outputs 23. If you flip 910, you have to number it as 19. Consecutive zeros from the…

Sum of Digits

Problem Write a program that, when N natural numbers are input, finds the sum of the digits of each natural number, and outputs the natural number with the largest sum. If the sum of digits is the same, the answer is the number with the larger original number. If 235 and 1234 can be the answer at…

String Compression

Problem Write a program that compresses a character string by inputting a character string consisting of uppercase letters and writing the number of repetitions to the right of the repeated character when the same character is repeated continuously.
However, if the number of repetitions is…

Shortest Distance Between Strings

Problem Given a string s and a letter t, write a program that prints the minimum distance each letter of string is from the letter . input The first line is given the string and the letter . Strings and characters are given only in lower case. The length of the string does not exceed 100. output…

Extract Numbers

Problem Given a string with a mixture of letters and numbers, only the numbers are extracted and a natural number is created in that order. If you extract only numbers from “tge0a1h205er”, it is 0, 1, 2, 0, 5, and if you make a natural number from this, it becomes 1205. The natural number produced…

Valid Palindrome

Problem A string that is the same whether read from the front or from the back is called a palindrome. Write a program that outputs “YES” if a string is input and “NO” if it is a palindrome. However, when examining palindromes, only alphabets are used to check palindromes, and case is not sensitive…

Palindrome String

Problem A string that is the same when read from the front or read from the back is called a palindrome. When a string is input, write a program that outputs “YES” if the string is a palindrome string, and “NO” if it is not a palindrome string. However, it is not case sensitive when checking…

Count Peaks

Problem Map information is given on an N*N grid. Each grid is marked with the height of that area. The number on each grid that is greater than its top, bottom, left, and right numbers is the peak area. Write a program to find out how many peak regions there are. It is assumed that the edges of the…

Max Grid Sum

Problem Numbers are written on the 5*5 grid as shown below. Given a grid of N*N, output the largest of the sum of each row, the sum of each column, and the sum of the two diagonals. input A natural number N is given in the first line (1<=N<=50). From the second line to the N lines, each line is…

Ranking

Problem Write a program that outputs each student’s rank in the order in which they are entered when the Korean language scores of N (1<=N<=100) students are entered. input N (3<=N<=1000) is input in the first line, and N integers representing the Korean score are input in the second line. If the…

Counting Score

Problem An OX problem is a problem that has two possible answers: right or wrong. In the case of consecutive correct answers in a test made of multiple OX questions, it was decided to calculate the score as follows to give additional points. If the first question is correct, it is counted as 1 point…

Rock Scissor Paper

Problem Two people, A and B, play a rock-paper-scissors game. A total of N games are played, and if A wins, output A, and if B wins, output B. In case of a tie, D is output. Scissors, rock, and paper information will be set to 1: Scissors, 2: Rock, and 3: Paper. For example, if N=5 Round 1 2 3 4…

Visible Students

Problem The teacher lined up N (1<=N<=1000) students. Write a program to find the number of students that the teacher standing in front can see when the heights of the students standing in a line are given in order from the front. (If it is larger than the people standing in front, it is visible, if…

Larger Number

Problem Write a program that takes N (1<=N<=100) integers as input and outputs only the number that is greater than the number immediately preceding itself. (The first number is unconditionally printed.) input A natural number N is given in the first line, and N integers are entered in the next line…

Remove Duplicates Words

Problem Write a program that removes duplicated strings when N strings are input and outputs them. The output string maintains the original input order. input A natural number N is given in the first line (3<=N<=30). Starting from the second line, N strings are given. The length of the string does…

The Middle Letter

Problem Write a program that prints the middle letter of a word (string) in lowercase letters when it is entered. However, if the length of a word is even, the middle two characters are printed. input A string is entered on the first line. The length of the string does not exceed 100. output Prints…

The Longest Word

Problem Write a program that prints the longest of N strings when inputted. input A natural number N is given in the first line (3<=N<=30). Starting from the second line, N strings are given. The length of the string does not exceed 100. Each string has a different length. output Print the longest…

Convert String Case

Problem Write a program that receives a string with both uppercase and lowercase letters as input and converts uppercase letters to lowercase letters and lowercase letters to uppercase letters. input A string is entered on the first line. The length of the string does not exceed 100. output On the…

To Uppercase

Problem Write a program that receives a string with both uppercase and lowercase letters as input and outputs the string by unifying all uppercase letters. input A string is entered on the first line. The length of the string does not exceed 100. output A unified character string with uppercase…

Find-Uppercase

Problem Write a program that takes a string as input and finds how many uppercase letters in that string. input A string is entered on the first line. The length of the string does not exceed 100. output Prints the number of uppercase letters on the first line. Solution using using code

Find String

Problem Write a program that receives a single string, receives a specific character, and finds out how many of that specific character exists in the inputted string. The length of the string does not exceed 100. input A string is given on the first line, and a character is given on the second line…

A to '#'

Problem Write a program that outputs an English word consisting of uppercase letters by replacing all ‘A’ in the word with ’#‘. input A string is entered in the first line. output Prints the changed word on the first line. Solution using using

7 Dwarfs

Problem A crisis came to Snow White, who was living peacefully with the seven dwarfs to escape the queen. There were nine dwarfs who returned from work instead of seven. All nine dwarfs claimed to be the protagonists of “Snow White and the Seven Dwarfs.” Snow White, who had great mathematical…

10-Day-Rotation

Problem From June 1st, the Seoul Metropolitan Government will implement the 10 car system to prevent traffic congestion. The 10 car system prohibits the operation of the car if the first digit of the car number matches the first digit of the date For example, if the day digit of the car number is…

Odd Number

Problem Given 7 natural numbers, write a program to select all odd natural numbers, find the sum, and find the smallest of the odd numbers. For example, given 7 natural numbers 12, 77, 38, 41, 53, 92, 85, the odd numbers among them are 77, 41, 53, 85, so the sum is Therefore, the minimum value…

Finding-the-Minimum

Problem Given 7 numbers, write a program that prints the smallest of those numbers. input Seven numbers are given in the first line. output Print the smallest value on the first line. Solution using using first value using & Dot spread using

Print the sum from 1 to N

Problem Write a program that outputs the sum from 1 to N when a natural number N is input. input A natural number N less than or equal to 20 is entered in the first line. output Prints the sum from 1 to N on the first line. Solution using

Number of Pencils

Problem One dozen pencils is 12. Write a program that calculates the number of pencils needed when N students enter the number of students, assuming that one pencil is distributed to each student. input A natural number N less than or equal to 1000 is entered in the first line. output Print the…

Identify Triangle

Problem Given three bars of different lengths (A, B, C), “YES” is output if these three bars can form a triangle, and “NO” if they cannot be made. input The first line gives you 100 different lengths of bars A, B, and C. output Print “YES” and “NO” on the first line. Solution using using using

Min of Three

Problem Write a program that accepts natural numbers A, B, and C less than or equal to 100 and prints the smallest of the three numbers. (don’t use sort) input Three natural numbers less than or equal to 100 are entered in the first line. output Print the smallest number on the first line. Solution…

Remove Duplicates

Problem Write a program that removes duplicate characters and outputs when a single lowercase character string is input. Each character in the stripped string retains the order of the original string. input A string is entered on the first line. output Prints a string with duplicate characters…

Activate Fountain

Problem My Solution success

Error Digit Range

Problem My Solution success

Max Double Slice Sum

Problem A non-empty array A consisting of N integers is given. A triplet (X, Y, Z), such that 0 ≤ X < Y < Z < N, is called a double slice. The sum of double slice (X, Y, Z) is the total of AX + 1 + AX + 2 + … + AY − 1 + AY + 1 + AY + 2 + … + AZ − 1. For example, array A such that: contains the…

EquiLeader

Problem A non-empty array A consisting of N integers is given. The leader of this array is the value that occurs in more than half of the elements of A. An equi leader is an index S such that 0 ≤ S < N − 1 and two sequences A0, A1, …, AS and AS + 1, AS + 2, …, AN − 1 have leaders of the same value…

Dominator

Problem An array A consisting of N integers is given. The dominator of array A is the value that occurs in more than half of the elements of A. For example, consider array A such that The dominator of A is 3 because it occurs in 5 out of 8 elements of A (namely in those with indices 0, 2, 4, 6 and…

Stone Wall

Problem You are going to build a stone wall. The wall should be straight and N meters long, and its thickness should be constant; however, it should have different heights in different places. The height of the wall is specified by an array H of N positive integers. HI is the height of the wall from…

Nesting

Problem A string S consisting of N characters is called properly nested if: S is empty; S has the form “(U)” where U is a properly nested string; S has the form “VW” where V and W are properly nested strings. For example, string ”(()(())())” is properly nested but string ”())” isn’t. Write a…

Fish

Problem You are given two non-empty arrays A and B consisting of N integers. Arrays A and B represent N voracious fish in a river, ordered downstream along the flow of the river. The fish are numbered from 0 to N − 1. If P and Q are two fish and P < Q, then fish P is initially upstream of fish Q…

Brackets

Problem A string S consisting of N characters is considered to be properly nested if any of the following conditions is true: S is empty; S has the form “(U)” or ”U” or “{U}” where U is a properly nested string; S has the form “VW” where V and W are properly nested strings. For example, the string…

Triangle

Problem An array A consisting of N integers is given. A triplet (P, Q, R) is triangular if 0 ≤ P < Q < R < N and: For example, consider array A such that: Triplet (0, 2, 4) is triangular. Write a function: that, given an array A consisting of N integers, returns 1 if there exists a triangular…

Number of Disc Intersection

Problem We draw N discs on a plane. The discs are numbered from 0 to N − 1. An array A of N non-negative integers, specifying the radiuses of the discs, is given. The J-th disc is drawn with its center at (J, 0) and radius AJ. We say that the J-th disc and K-th disc intersect if J ≠ K and the J-th…

Max Product Of Three

Problem A non-empty array A consisting of N integers is given. The product of triplet (P, Q, R) equates to AP _ AQ _ AR (0 ≤ P < Q < R < N). For example, array A such that: contains the following example triplets: (0, 1, 2), product is −3 _ 1 _ 2 = −6 (1, 2, 4), product is 1 _ 2 _ 5 = 10 (2, 4,…

Distinct

Problem Write a function that, given an array A consisting of N integers, returns the number of distinct values in array A. For example, given array A consisting of six elements such that: the function should return 3, because there are 3 distinct values appearing in array A, namely 1, 2 and…

Passing Cars

Problem A non-empty array A consisting of N integers is given. The consecutive elements of array A represent consecutive cars on a road. Array A contains only 0s and/or 1s: 0 represents a car traveling east, 1 represents a car traveling west. The goal is to count passing cars. We say that a pair of…

Min Avg Two Slice

Problem A non-empty array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P < Q < N, is called a slice of array A (notice that the slice contains at least two elements). The average of a slice (P, Q) is the sum of AP + AP + 1 + … + AQ divided by the length of the slice…

Genomic Range Query

Problem A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A, C, G and T have impact factors of 1, 2, 3 and…

Count Div

Problem Write a function: that, given three integers A, B and K, returns the number of integers within the range A..B that are divisible by K, i.e.: { i : A ≤ i ≤ B, i mod K = 0 } For example, for A = 6, B = 11 and K = 2, your function should return 3, because there are three numbers divisible by…

Target Number

Problem There are n non-negative integers. We want to add or subtract these numbers appropriately to make our target number. For example, to make the number 3 from 1, 1, 1, 1, 1, you can use the following five methods: Write the solution function to return the number of ways to make the target…

Tape Equilibrium

Problem A non-empty array A consisting of N integers is given. Array A represents numbers on a tape. Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: The difference between the two parts is the value of: In other words, it is the absolute difference between the sum of…

Perm Missing Elem

Problem An array A consisting of N different integers is given. The array contains integers in the range 1..(N + 1), which means that exactly one element is missing. Your goal is to find that missing element. Write a function: that, given an array A, returns the value of the missing element. For…

Perm Check

Problem A non-empty array A consisting of N integers is given. A permutation is a sequence containing each element from 1 to N once, and only once. For example, array A such that: is a permutation, but array A such that: is not a permutation, because value 2 is missing. The goal is to check whether…

Cyclic Rotation

Problem An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = 3, 8, 9, 7, 6 is 6, 3, 8, 9, 7 (elements are shifted right by one…

Missing Integer

Problem This is a demo task. Write a function: that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A. For example, given A = 1, 3, 6, 4, 1, 2, the function should return 5. Given A = 1, 2, 3, the function should return 4. Given A = −…

Max Counters

Problem You are given N counters, initially set to 0, and you have two possible operations on them: increase(X) − counter X is increased by 1, max counter − all counters are set to the maximum value of any counter. A non-empty array A of M integers is given. This array represents consecutive…

Math Haters

Problem Math-hater is short for a person who has given up on mathematics. Three math-haters are trying to take all math problems for the practice test. Math-haters are taken from question 1 to the last question as follows. Haters Answers Math-hater 1  1, 2, 3, 4, 5, 1, 2, 3, 4, 5, … Math-hater 2…

Frog River One

Problem A small frog wants to get to the other side of a river. The frog is initially located on one bank of the river (position 0) and wants to get to the opposite bank (position X+1). Leaves fall from a tree onto the surface of the river. You are given an array A consisting of N integers…

Frog Jump

Problem A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D. Count the minimal number of jumps that the small frog must perform to reach its…

Demo Test

Problem function solution(A); that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A. For example, given A = 1, 3, 6, 4, 1, 2, the function should return 5. Given A = 1, 2, 3, the function should return 4. Given A = −1, −3, the function…

Odd Occurrences In Array

Problem A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired. For example, in array A such that: the elements at…

Binary Gap

Problem A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N. For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary…

Preprocess Dates

Problem My Solution success

JavaScript Tips for Algorithm

String Tips Light cloning Use regexp for .replace(a,b) is exchange first letter of first argument to second argument. For convert every matching letter, put the letter inside regexp, . example Convert Number There are a lot of ways to convert strings to numbers, but is recommended. Plus, It…

Two Sum

Problem Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Example 1: Example…

Reverse Integer

Problem Given a 32-bit signed integer, reverse digits of an integer. Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: . For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows…

Palindrome Number

Problem Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Follow up: Could you solve it without converting the integer to a string? Example 1: Example 2: Example 3: Example 4: Constraints: -231 <= x <= 231 - 1 My Solution success…

Roman to Integer

Problem Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, is written as in Roman numeral, just two one’s added together. is written as , which is simply . The number is written as , which is . Roman…

Longest Common Prefix

Problem Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Example 2: Constraints: 0 <= strs.length <= 200 0 <= strsi.length <= 200 strsi consists of only lower-case English letters. My Solution…

Valid Parentheses

Problem Given a string s containing just the characters , , , , and , determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Example 1: Example 2: Example 3: Example…

Merge Two Sorted Lists

Problem Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists. Example 1: Example 2: Example 3: Constraints: The number of nodes in both lists is in the range 0, 50. -100 <= Node.val <= 100 Both l1 and l2 are…

Remove Duplicates from Sorted Array

Problem Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Clarification: Confused why the…

Remove Element

Problem Given an array nums and a value , remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with extra memory. The order of elements can be changed. It doesn’t matter what you…

Implement strStr()

Problem Implement . Return the index of the first occurrence of needle in haystack, or -1 if is not part of . Clarification: What should we return when is an empty string? This is a great question to ask during an interview. For the purpose of this problem, we will return 0 when is an empty…

Search Insert Position

Problem Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Example 1: Example 2: Example 3: Example 4: Example 5: Constraints: 1 <= nums.length <= 104 -104 <= numsi <= 10…