Kate was given a birthday gift of three theater tickets. Now she is browsing the theater program for the next N days. On the program, performances are named by integers. Every day, one performance is staged. Kate wants to choose three days (not necessarily consecutive) to go to the theater.
In how many ways can she use her tickets? Two ways are different if the sequences of watched performances are different. Kate likes the theater, so she may watch one performance more than once. For example, if N = 4 and theater program looks as following: [1, 2, 1, 1], Kate has four possibilities to choose the dates: [1, 2, 1, 1], [1, 2, 1, 1], [1, 2, 1, 1], and [1, 2, 1, 1], but they create only three different sequences: (1, 2, 1), (1, 1, 1) and (2, 1, 1). The correct answer for this example is 3. Notice that the order of performances matters, so the first and the last sequences are considered different.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A consisting of N integers, denoting names of performances for the next N days, returns the number of possible ways to spend the tickets. Since the answer can be very large, provide it modulo 109 + 7 (1,000,000,007).
For example, given A = [1, 2, 1, 1], the function should return 3 as exmplained above.
Given A = [1, 2, 3, 4], the function should return 4. There are four ways to spend tickets: (1, 2, 3), (1, 2, 4), (1, 3, 4) and (2, 3, 4).
Given A = [2, 2, 2, 2], the function should return 1. There is only one way to spend tickets: (2, 2, 2).
Given A = [2, 2, 1, 2, 2], the function should return 4. There are four ways to spend tickets: (1, 2, 2), (2, 1, 2), (2, 2, 1) and (2, 2, 2).
Given A = [1, 2], the function should return 0. Kate cannot use all three tickets in only two days.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..100,000];
- each element of array A is an integer within the range [1..N].
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
private const ulong ModuloValue = 1000000007;
private ulong GetModuloValue(ulong u)
{
return u % ModuloValue;
}
public class NumberCombo
{
public ulong ComboOfTwo { get; set; }
public ulong ComboOfThree { get; set; }
}
public int solution(int[] A) {
// total of combinations of two
ulong totalTwo = 0;
// total of combinations of three
ulong totalThree = 0;
// total of different numbers in the array
ulong totalDifferentNumbers = 0;
var numberCombos = new NumberCombo[100001];
for(var i = 0; i<A.Length; i++)
{
var currentNumber = A[i];
if(i == 0)
{
numberCombos[currentNumber] = new NumberCombo() { ComboOfTwo = 0, ComboOfThree = 0 };
totalDifferentNumbers++;
}
else if(i == 1)
{
if(numberCombos[currentNumber] == null)
{
totalDifferentNumbers++;
numberCombos[currentNumber] = new NumberCombo() { ComboOfTwo = 0, ComboOfThree = 0 };
}
numberCombos[currentNumber].ComboOfTwo = 1;
totalTwo = 1;
}
else // make calculations for combos of three
{
var isNewNumber = numberCombos[currentNumber] == null;
if(isNewNumber)
{
numberCombos[currentNumber] = new NumberCombo() { ComboOfTwo = 0, ComboOfThree = 0 };
}
// make calculations about combo of three
var currentNumberThreeCombo = GetModuloValue(totalTwo);
var deltaThree = GetModuloValue(totalTwo - numberCombos[currentNumber].ComboOfThree);
numberCombos[currentNumber].ComboOfThree = currentNumberThreeCombo;
totalThree += deltaThree;
// make calculations about combo of two
var currentNumberTwoCombo = GetModuloValue(totalDifferentNumbers);
var deltaTwo = GetModuloValue(totalDifferentNumbers - numberCombos[currentNumber].ComboOfTwo);
numberCombos[currentNumber].ComboOfTwo = currentNumberTwoCombo;
totalTwo += deltaTwo;
// update total different numbers
if(isNewNumber)
{
GetModuloValue(totalDifferentNumbers++);
}
}
}
return Convert.ToInt32(totalThree);
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
private const ulong ModuloValue = 1000000007;
private ulong GetModuloValue(ulong u)
{
return u % ModuloValue;
}
public class NumberCombo
{
public ulong ComboOfTwo { get; set; }
public ulong ComboOfThree { get; set; }
}
public int solution(int[] A) {
// total of combinations of two
ulong totalTwo = 0;
// total of combinations of three
ulong totalThree = 0;
// total of different numbers in the array
ulong totalDifferentNumbers = 0;
var numberCombos = new NumberCombo[100001];
for(var i = 0; i<A.Length; i++)
{
var currentNumber = A[i];
if(i == 0)
{
numberCombos[currentNumber] = new NumberCombo() { ComboOfTwo = 0, ComboOfThree = 0 };
totalDifferentNumbers++;
}
else if(i == 1)
{
if(numberCombos[currentNumber] == null)
{
totalDifferentNumbers++;
numberCombos[currentNumber] = new NumberCombo() { ComboOfTwo = 0, ComboOfThree = 0 };
}
numberCombos[currentNumber].ComboOfTwo = 1;
totalTwo = 1;
}
else // make calculations for combos of three
{
var isNewNumber = numberCombos[currentNumber] == null;
if(isNewNumber)
{
numberCombos[currentNumber] = new NumberCombo() { ComboOfTwo = 0, ComboOfThree = 0 };
}
// make calculations about combo of three
var currentNumberThreeCombo = GetModuloValue(totalTwo);
var deltaThree = GetModuloValue(totalTwo - numberCombos[currentNumber].ComboOfThree);
numberCombos[currentNumber].ComboOfThree = currentNumberThreeCombo;
totalThree += deltaThree;
// make calculations about combo of two
var currentNumberTwoCombo = GetModuloValue(totalDifferentNumbers);
var deltaTwo = GetModuloValue(totalDifferentNumbers - numberCombos[currentNumber].ComboOfTwo);
numberCombos[currentNumber].ComboOfTwo = currentNumberTwoCombo;
totalTwo += deltaTwo;
// update total different numbers
if(isNewNumber)
{
GetModuloValue(totalDifferentNumbers++);
}
}
}
return Convert.ToInt32(GetModuloValue(totalThree));
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
private const ulong ModuloValue = 1000000007;
private ulong GetModuloValue(ulong u)
{
return u % ModuloValue;
}
public class NumberCombo
{
public ulong ComboOfTwo { get; set; }
public ulong ComboOfThree { get; set; }
}
public int solution(int[] A) {
// total of combinations of two
ulong totalTwo = 0;
// total of combinations of three
ulong totalThree = 0;
// total of different numbers in the array
ulong totalDifferentNumbers = 0;
var numberCombos = new NumberCombo[100001];
for(var i = 0; i<A.Length; i++)
{
var currentNumber = A[i];
if(i == 0)
{
numberCombos[currentNumber] = new NumberCombo() { ComboOfTwo = 0, ComboOfThree = 0 };
totalDifferentNumbers++;
}
else if(i == 1)
{
if(numberCombos[currentNumber] == null)
{
totalDifferentNumbers++;
numberCombos[currentNumber] = new NumberCombo() { ComboOfTwo = 0, ComboOfThree = 0 };
}
numberCombos[currentNumber].ComboOfTwo = 1;
totalTwo = 1;
}
else // make calculations for combos of three
{
var isNewNumber = numberCombos[currentNumber] == null;
if(isNewNumber)
{
numberCombos[currentNumber] = new NumberCombo() { ComboOfTwo = 0, ComboOfThree = 0 };
}
// make calculations about combo of three
var currentNumberThreeCombo = GetModuloValue(totalTwo);
var deltaThree = GetModuloValue(totalTwo - numberCombos[currentNumber].ComboOfThree);
numberCombos[currentNumber].ComboOfThree = currentNumberThreeCombo;
totalThree += deltaThree;
// make calculations about combo of two
var currentNumberTwoCombo = GetModuloValue(totalDifferentNumbers);
var deltaTwo = GetModuloValue(totalDifferentNumbers - numberCombos[currentNumber].ComboOfTwo);
numberCombos[currentNumber].ComboOfTwo = currentNumberTwoCombo;
totalTwo += deltaTwo;
// update total different numbers
if(isNewNumber)
{
GetModuloValue(totalDifferentNumbers++);
}
}
}
return Convert.ToInt32(GetModuloValue(totalThree));
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
private const ulong ModuloValue = 1000000007;
private ulong GetModuloValue(ulong u)
{
return u % ModuloValue;
}
public class NumberCombo
{
public ulong ComboOfTwo { get; set; }
public ulong ComboOfThree { get; set; }
}
public int solution(int[] A) {
// total of combinations of two
ulong totalTwo = 0;
// total of combinations of three
ulong totalThree = 0;
// total of different numbers in the array
ulong totalDifferentNumbers = 0;
var numberCombos = new NumberCombo[100001];
for(var i = 0; i<A.Length; i++)
{
var currentNumber = A[i];
if(i == 0)
{
numberCombos[currentNumber] = new NumberCombo() { ComboOfTwo = 0, ComboOfThree = 0 };
totalDifferentNumbers++;
}
else if(i == 1)
{
if(numberCombos[currentNumber] == null)
{
totalDifferentNumbers++;
numberCombos[currentNumber] = new NumberCombo() { ComboOfTwo = 0, ComboOfThree = 0 };
}
numberCombos[currentNumber].ComboOfTwo = 1;
totalTwo = 1;
}
else // make calculations for combos of three
{
var isNewNumber = numberCombos[currentNumber] == null;
if(isNewNumber)
{
numberCombos[currentNumber] = new NumberCombo() { ComboOfTwo = 0, ComboOfThree = 0 };
}
// make calculations about combo of three
var currentNumberThreeCombo = GetModuloValue(totalTwo);
var deltaThree = GetModuloValue(totalTwo - numberCombos[currentNumber].ComboOfThree);
numberCombos[currentNumber].ComboOfThree = currentNumberThreeCombo;
totalThree += deltaThree;
// make calculations about combo of two
var currentNumberTwoCombo = GetModuloValue(totalDifferentNumbers);
var deltaTwo = GetModuloValue(totalDifferentNumbers - numberCombos[currentNumber].ComboOfTwo);
numberCombos[currentNumber].ComboOfTwo = currentNumberTwoCombo;
totalTwo += deltaTwo;
// update total different numbers
if(isNewNumber)
{
GetModuloValue(totalDifferentNumbers++);
}
}
}
return Convert.ToInt32(GetModuloValue(totalThree));
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
private const ulong ModuloValue = 1000000007;
private ulong GetModuloValue(ulong u)
{
return u % ModuloValue;
}
public class NumberCombo
{
public ulong ComboOfTwo { get; set; }
public ulong ComboOfThree { get; set; }
}
public int solution(int[] A) {
// total of combinations of two
ulong totalTwo = 0;
// total of combinations of three
ulong totalThree = 0;
// total of different numbers in the array
ulong totalDifferentNumbers = 0;
var numberCombos = new NumberCombo[100001];
for(var i = 0; i<A.Length; i++)
{
var currentNumber = A[i];
if(i == 0)
{
numberCombos[currentNumber] = new NumberCombo() { ComboOfTwo = 0, ComboOfThree = 0 };
totalDifferentNumbers++;
}
else if(i == 1)
{
if(numberCombos[currentNumber] == null)
{
totalDifferentNumbers++;
numberCombos[currentNumber] = new NumberCombo() { ComboOfTwo = 0, ComboOfThree = 0 };
}
numberCombos[currentNumber].ComboOfTwo = 1;
totalTwo = 1;
}
else // make calculations for combos of three
{
var isNewNumber = numberCombos[currentNumber] == null;
if(isNewNumber)
{
numberCombos[currentNumber] = new NumberCombo() { ComboOfTwo = 0, ComboOfThree = 0 };
}
// make calculations about combo of three
var currentNumberThreeCombo = GetModuloValue(totalTwo);
var deltaThree = GetModuloValue(totalTwo - numberCombos[currentNumber].ComboOfThree);
numberCombos[currentNumber].ComboOfThree = currentNumberThreeCombo;
totalThree += deltaThree;
// make calculations about combo of two
var currentNumberTwoCombo = GetModuloValue(totalDifferentNumbers);
var deltaTwo = GetModuloValue(totalDifferentNumbers - numberCombos[currentNumber].ComboOfTwo);
numberCombos[currentNumber].ComboOfTwo = currentNumberTwoCombo;
totalTwo += deltaTwo;
// update total different numbers
if(isNewNumber)
{
GetModuloValue(totalDifferentNumbers++);
}
}
}
return Convert.ToInt32(GetModuloValue(totalThree));
}
}
The solution obtained perfect score.