int main()
{
	Solution s;;
	vector<int> nums{ 1, 3 , 3 , 5 , 5 ,8 , 8 ,9};
	cout<<s.removeDuplicates(nums)<<endl;
}
Beispiel #2
0
int main(int argc, const char * argv[]) {
    Solution test;
    cout << test.countAndSay(5) << endl;
    return 0;
}
Beispiel #3
0
    /**
     * Merging two solutions. The result is stored in the first solution.
     *
     * @param first first solution
     * @param second second solution
     */
    void merge(Solution& first, Solution& second)
    {
      for(std::vector<Point>::iterator i = second.mPoints.begin(); i != second.mPoints.end(); i ++) 
	first.pushPoint(*i); 
    }
Beispiel #4
0
#define CATCH_CONFIG_MAIN
#define CATCH_CONFIG_COLOUR_NONE
#include <catch.hpp>
#include "solution.cpp"

Solution sln;

TEST_CASE("normal values", "[Solution]")
{
	REQUIRE(sln.isAnagram("abc", "cba") == true);
	REQUIRE(sln.isAnagram("abc", "cab") == true);
	REQUIRE(sln.isAnagram("abc", "ca") == false);
}

TEST_CASE("empty values", "[Solution]")
{
	REQUIRE(sln.isAnagram("abc", "") == false);
	REQUIRE(sln.isAnagram("", "abc") == false);
	REQUIRE(sln.isAnagram("", "") == true);
}

TEST_CASE("same characters", "[Solution]")
{
	REQUIRE(sln.isAnagram("aaa", "aab") == false);
	REQUIRE(sln.isAnagram("aaa", "aaa") == true);
	REQUIRE(sln.isAnagram("aba", "baa") == true);
}

TEST_CASE("long strings", "[Solution]")
{
	REQUIRE(sln.isAnagram("qwertyuiopasdfghjklzxcvbnm", "zxcvbnmasdfghjklqwertyuiop") == true);
Beispiel #5
0
int main() {
    Solution s;
    cout << s.combine(4, 2) << endl;
    return 0;
}
Beispiel #6
0
TEST(removeElement, test3) {
    Solution s;
    vector<int> nums = {3,3,3,3,3};
    EXPECT_EQ(s.removeElement(nums, 3), 0);
}
int main() {
    Solution s;
    vector<int> nums = {};
    cout << s.findPeakElement(nums) << endl;
    return 0;
}
Beispiel #8
0
int main(int argc, char *argv[]){
    Solution sol;
    sol.solveNQueens(stoi(argv[1]));
    return 0;
}
int main() {
    vector<int> num = {100, 4, 200, 1, 3, 2};
    Solution sol;
    cout << sol.longestConsecutive(num) << endl;
    return 0;
}
Beispiel #10
0
int main() {
    Solution solution;
    solution.isAdditiveNumber("111122335588143");
    return 0;
}
Beispiel #11
0
int main() {
    Solution solution;
    solution.integerBreak(20);
    return 0;
}
#include <vector>
#include <algorithm>
using namespace std;

#define CATCH_CONFIG_MAIN
#include "catch.hpp"

#include "315-count-of-smaller-numbers-after-self.cpp"

TEST_CASE("1 element", "")
{
  Solution s;
  vector<int> vec = {0}, tar = {0};
  CHECK(s.countSmaller(vec) == tar);

  vec = {1};
  CHECK(s.countSmaller(vec) == tar);
}

TEST_CASE("2 elements", "")
{
  Solution s;
  vector<int> vec = {0, 0}, tar = {0, 0};
  CHECK(s.countSmaller(vec) == tar);

  vec = {1, 0}, tar = {1, 0};
  CHECK(s.countSmaller(vec) == tar);
}

TEST_CASE("Random", "")
{
int main()
{
	/*special*/
	{
		//tr1::unordered_set<string> dict = {"lint", "code"};
		tr1::unordered_set<string> dict;
		string s = "lintcodeisawesome";

		Solution sol;
		cout << "no1 ";
		if (!sol.wordSegmentation(s, dict))
		{
			cout << "correct!" << endl;
		}
		else
		{
			cout << "wrong!" << endl;
		}
	}
	{
		//tr1::unordered_set<string> dict = {"lint", "code"};
		tr1::unordered_set<string> dict;
		dict.insert("awesome");
		dict.insert("is");
		dict.insert("lint");
		dict.insert("code");
		string s = "";

		Solution sol;
		cout << "no2 ";
		if (sol.wordSegmentation(s, dict))
		{
			cout << "correct!" << endl;
		}
		else
		{
			cout << "wrong!" << endl;
		}
	}
	{
		//tr1::unordered_set<string> dict = {"lint", "code"};
		tr1::unordered_set<string> dict;
		string s = "";

		Solution sol;
		cout << "no21 ";
		if (sol.wordSegmentation(s, dict))
		{
			cout << "correct!" << endl;
		}
		else
		{
			cout << "wrong!" << endl;
		}
	}
	/*positive*/
	{
		//tr1::unordered_set<string> dict = {"lint", "code"};
		tr1::unordered_set<string> dict;
		dict.insert("a");
		string s = "a";

		Solution sol;
		cout << "no3 ";
		if (sol.wordSegmentation(s, dict))
		{
			cout << "correct!" << endl;
		}
		else
		{
			cout << "wrong!" << endl;
		}
	}
	{
		//tr1::unordered_set<string> dict = {"lint", "code"};
		tr1::unordered_set<string> dict;
		dict.insert("awesome");
		dict.insert("is");
		dict.insert("lint");
		dict.insert("code");
		string s = "lintcodeisawesome";

		Solution sol;
		cout << "no4 ";
		if (sol.wordSegmentation(s, dict))
		{
			cout << "correct!" << endl;
		}
		else
		{
			cout << "wrong!" << endl;
		}
	}
	{
		//tr1::unordered_set<string> dict = {"lint", "code"};
		tr1::unordered_set<string> dict;
		dict.insert("lint");
		dict.insert("lint");
		dict.insert("code");
		string s = "lintcode";

		Solution sol;
		cout << "no5 ";
		if (sol.wordSegmentation(s, dict))
		{
			cout << "correct!" << endl;
		}
		else
		{
			cout << "wrong!" << endl;
		}
	}
	{
		//tr1::unordered_set<string> dict = {"lint", "code"};
		tr1::unordered_set<string> dict;
		dict.insert("aaaa");
		dict.insert("aaa");
		string s = "aaaaaaa";

		Solution sol;
		cout << "*no51 ";
		if (sol.wordSegmentation(s, dict))
		{
			cout << "correct!" << endl;
		}
		else
		{
			cout << "wrong!" << endl;
		}
	}
	/*negtive*/
	{
		tr1::unordered_set<string> dict;
		dict.insert("lint");
		dict.insert("ode");
		string s = "lintcode";

		Solution sol;
		cout << "no6 ";
		if (sol.wordSegmentation(s, dict))
		{
			cout << "wrong!" << endl;
		}
		else
		{
			cout << "correct!" << endl;
		}
	}

	{
		tr1::unordered_set<string> dict;
		dict.insert("lint");
		dict.insert("tcode");
		string s = "lintcode";

		Solution sol;
		cout << "no7 ";
		if (sol.wordSegmentation(s, dict))
		{
			cout << "wrong!" << endl;
		}
		else
		{
			cout << "correct!" << endl;
		}
	}
	return 0;
}
int main(){
	Solution s;
	cout << s.removeDuplicateLetters("")<< endl;
	return 0;
}
int main() {
    Solution sol;
    vector<int> res = sol.getRow(3);
    for (int i : res) cout << i << " ";
    cout << endl;
}
Beispiel #16
0
int main(int argc, char* argv[])
{
  // Time measurement.
  TimePeriod cpu_time;
  cpu_time.tick();

  // Load the mesh.
  Mesh mesh;
  H2DReader mloader;
  mloader.load("square.mesh", &mesh);

  // Perform initial mesh refinements.
  for (int i=0; i<INIT_REF; i++) mesh.refine_all_elements();
  
  // Create an L2 space with default shapeset.
  L2Space space(&mesh, bc_types, NULL, Ord2(P_H, P_V));
  int ndof = Space::get_num_dofs(&space);
  info("ndof = %d", ndof);

  // Initialize the weak formulation.
  WeakForm wf;
  wf.add_matrix_form(callback(bilinear_form));
  wf.add_vector_form(callback(linear_form));
  wf.add_matrix_form_surf(callback(bilinear_form_boundary), H2D_DG_BOUNDARY_EDGE);
  wf.add_vector_form_surf(callback(linear_form_boundary), H2D_DG_BOUNDARY_EDGE);
  wf.add_matrix_form_surf(callback(bilinear_form_interface), H2D_DG_INNER_EDGE);

  // Initialize the FE problem.
  bool is_linear = true;
  DiscreteProblem dp(&wf, &space, is_linear);
  
  // Set up the solver, matrix, and rhs according to the solver selection.
  SparseMatrix* matrix = create_matrix(matrix_solver);
  Vector* rhs = create_vector(matrix_solver);
  Solver* solver = create_linear_solver(matrix_solver, matrix, rhs);
  
  // Initialize the preconditioner in the case of SOLVER_AZTECOO.
  if (matrix_solver == SOLVER_AZTECOO) 
  {
    ((AztecOOSolver*) solver)->set_solver(iterative_method);
    ((AztecOOSolver*) solver)->set_precond(preconditioner);
    // Using default iteration parameters (see solver/aztecoo.h).
  }
    
  // Initialize the solution.
  Solution sln;
  
  // Assemble the stiffness matrix and right-hand side vector.
  info("Assembling the stiffness matrix and right-hand side vector.");
  dp.assemble(matrix, rhs);
  
  // Solve the linear system and if successful, obtain the solution.
  info("Solving the matrix problem.");
  if(solver->solve())
    Solution::vector_to_solution(solver->get_solution(), &space, &sln);
  else
    error ("Matrix solver failed.\n");
  
  // Time measurement.
  cpu_time.tick();
  
  // Clean up.
  delete solver;
  delete matrix;
  delete rhs;
  
  info("ndof = %d", ndof);
  info("Coordinate ( 0.1, 0.1) value = %lf", sln.get_pt_value(0.1, 0.1));
  info("Coordinate ( 0.3, 0.3) value = %lf", sln.get_pt_value(0.3, 0.3));
  info("Coordinate ( 0.5, 0.5) value = %lf", sln.get_pt_value(0.5, 0.5));
  info("Coordinate ( 0.7, 0.7) value = %lf", sln.get_pt_value(0.7, 0.7));

  double coor_xy[4] = {0.1, 0.3, 0.5, 0.7};
  double value[4] = {0.999885, 0.844340, 0.000000, 0.000000};
  for (int i = 0; i < 4; i++)
  {
    if ((value[i] - sln.get_pt_value(coor_xy[i], coor_xy[i])) < 1E-6)
    {
      printf("Success!\n");
    }
    else
    {
      printf("Failure!\n");
      return ERR_FAILURE;
    }
  }
  return ERR_SUCCESS;
}
Beispiel #17
0
TEST(removeElement, test2) {
    Solution s;
    vector<int> nums = {2};
    EXPECT_EQ(s.removeElement(nums, 3), 1);
    EXPECT_EQ(nums[0], 2);
}
int main(){
	Solution A;
	A.test();
	system("pause");
}
Beispiel #19
0
 void main(){
	 vector<int> peak;
	 peak.push_back(1);
	 peak.push_back(2);
	 peak.push_back(3);
	 peak.push_back(4);
	 peak.push_back(5);
	 peak.push_back(6);
	 peak.push_back(8);
	 int f_peak = find_peak(peak);

	 vector<int> first_oc;
	 first_oc.push_back(1);
	 first_oc.push_back(2);
	 first_oc.push_back(2);
	 first_oc.push_back(2);
	 first_oc.push_back(2);
	 first_oc.push_back(2);
	 first_oc.push_back(3);
	 int wrong = first_occr(first_oc, 1);
	 wrong = last_occr(first_oc, 2);


	 Solution_search_range search_range;
	 int A[1] = { 1 };
	 search_range.searchRange(A, 1, 1);

	 string conver_test = "";
	 conver_test = conver_test + 'a';

	 Solution_wordladder wordladder;
	 unordered_set<string> word_set = { "hot", "cog", "dog", "tot", "hog", "hop", "pot", "dot" };
	 int word_ladder_ans = wordladder.ladderLength("hot", "dog", word_set);

	 Solution_perumu perumu;
	 vector<int> test_perumu;
	 test_perumu.push_back(5);
	 test_perumu.push_back(4);
	 test_perumu.push_back(2);
	 test_perumu.push_back(6);
	 test_perumu.push_back(8);
	 vector<vector<int>> res_perumu = perumu.permute(test_perumu);

	 Solution_subset subset;
	 vector<int> test_sub;
	 test_sub.push_back(0);
	 test_sub.erase(test_sub.begin());
	 test_sub.insert(test_sub.begin(), 0);
	 subset.subsetsWithDup(test_sub);

	 char* ha = "mississippi";
	 char* nb = "mississippi";
	 char* sb = strStr(ha, nb);

	 Solution_wordbreak wordbreak;
	 unordered_set<string> sset = { "a", "aa" };
	 string sss = "aaa";
	 vector<string> wordbreak_ret = wordbreak.wordBreak(sss, sset);


	 Solution_solve solve;
	 vector<vector<char>> input(6,vector<char>(6,'O'));
	 input[0][4] = 'X';
	 input[0][5] = 'X';
	 input[2][1] = 'X';
	 input[2][3] = 'X';
	 input[3][1] = 'X';
	 input[3][4] = 'X';
	 input[4][1] = 'X';
	 input[4][3] = 'X';
	 input[5][1] = 'X';
	 solve.solve(input);

	 char* a = "";
	 char* b = "";
	 strStr(a,b);

	 Solution_wordsearch wordsearch;
	 vector<vector<char> > board;
	 board.resize(2);
	 board[0].push_back('a');
	 board[0].push_back('b');
	 board[1].push_back('c');
	 board[1].push_back('d');
	 wordsearch.exist(board, "cdba");

	 Solution_min minpart;
	 int min_part = minpart.minCut("cdd");

	 Solution_longest longest;
	 string end = longest.longestPalindrome("abb");

	 Solution_part part;
	 part.partition("abc");

	 ListNode* leet = new ListNode(1);
	 partition(leet, 2);

	 char* st = "-2147483647";
	 float sst = _atoi(st);

	 set<int> ss;
	 ss.insert(4);
	 ss.insert(8);
	 ss.insert(1);
	 ss.insert(2);
	 set<int>::iterator it = ss.begin();
	 while (it != ss.end())
		 it++;

	 ListNode* s1 = new ListNode(1);
	 s1->next = new ListNode(2);
	 s1->next->next = new ListNode(3);
	 Solution t;
	 t.reverseKGroup(s1, 2);


 }
Beispiel #20
0
int main() {
    Solution solve;
    cout<<solve.numSquares(13);
    return 0;
}
int main(){
    Solution so;
    vector<int> res = so.constructArray(3, 2);
    for (vector<int>::iterator it=res.begin(); it!=res.end(); ++it )
        cout << *it << " ";
}
int main(int argc, char **argv)
{

	  //*****************************SET PARAMETERS*************************************************
	  Parameters params(argc, argv, 0);
	  Parameters params2(argc, argv, 1);
	  //********************************************************************************************

	  //**********************READ DATA AND MAKE SOLUTION*******************************************
	  Solution* sol = constructSolution(params);
	  preprocesData(sol);
	  Solution* sol2 = constructSolution(params2);
	  preprocesData(sol2);
	 //*********************************************************************************************


	  //*****************************SET PARAMETERS*************************************************
	  if(sol->getNumberOfProcesses() * sol->getNumberOfMachines() <= 100 * 1000) {
		  params2.delta = params.delta = 40;
		  params2.nmb_iters_bpr = params.nmb_iters_bpr = 300;
		  params2.nmbRanges = params.nmbRanges = 7;
		  params2.rangeLength = params.rangeLength = sol->getNumberOfProcesses() / params.nmbRanges;
	  }
	  else {
		  params2.nmb_iters_bpr = params.nmb_iters_bpr = 100;
		  params2.nmbRanges = params.nmbRanges = 5;
		  params2.rangeLength = params.rangeLength = sol->getNumberOfProcesses() / params.nmbRanges;
		  params2.numberOfBestProcessesToConsiderShiftSwap =
				  params.numberOfBestProcessesToConsiderShiftSwap = 3;
		  params2.nmbLoops = params.nmbLoops = 2;

		  if(sol->getNumberOfProcesses() * sol->getNumberOfMachines() >= 20000 * 2000)
			  params2.nmbLoops = params.nmbLoops = 1;
	  }

	  params2.seedValue = params.seedValue + 1000;
	  //********************************************************************************************


	  if(sol->getNumberOfProcesses() * sol->getNumberOfMachines() <= 100 * 1000)  // A instances
		  solveA(params, sol, params2, sol2);
	  else  																	  // B instances
		  solveB(params, sol, params2, sol2);

	  long timeToSleep = params.time_limit - (time(0) - params.programStartTime) - 3;
	  if (timeToSleep < 0)
		  timeToSleep = 0;

	  sleep(timeToSleep);

	  // Read Solutions From Files And Write a better one

	  sol->setOriginalLoadCostWeights();
	  sol2->setOriginalLoadCostWeights();
	  //cout << " sol->solutionFilename   " << sol->solutionFilename << endl;
	  //cout << " sol2->solutionFilename  " << sol2->solutionFilename << endl;

	  //cout << " sol->getCost()   " << sol->getCost() << endl;
	  //cout << " sol2->getCost()  " << sol2->getCost() << endl;

	  readSolutionFromFile( sol,  sol->solutionFilename);
	  readSolutionFromFile(sol2, sol2->solutionFilename);


	  //cout << " after sol->getCost()   " << sol->getCost() << endl;
	  //cout << " after sol2->getCost()  " << sol2->getCost() << endl;
	  if ((sol->getCost() < sol2->getCost()) && (sol->checkConflict(false)))
	    sol->writeToFile(params.solution_filename);
	  else
		sol2->writeToFile(params.solution_filename);


	  //remove solution files
	  ostringstream oss1(""); oss1 << "rm " << sol->solutionFilename;
	  system(oss1.str().c_str());
	  ostringstream oss2(""); oss2 << "rm " << sol2->solutionFilename;
	  system(oss2.str().c_str());


	  //*****************************************FILES (REMOVE THIS PART)********************************************************

	  /*fstream fileResults("results", ios::out | ios::app);

	  fileResults << setw(5)  << params.seedValue << setw(30) << params.data_filename
			      << setw(20) << sol->getCost()
		          << setw(20) << sol2->getCost()
		          << setw(20) << *(params.best_objective)
			      << setw(15)  << time(0) - params.programStartTime << endl;

	  // running times
	  fstream fileRunnungTimes("runnung_times", ios::out | ios::app);
	  fileRunnungTimes << setw(5)  << setw(30) << params.data_filename << setw(20) << *(params.best_objective)
	  			       << setw(20)  << time(0) - params.programStartTime << endl;

	  // check solution with official checker
	  ostringstream oss("");
	  oss << "./checker " << params.data_filename << " " << params.initial_assignment_filename
		  << " " << params.solution_filename;
	  oss << " >> checkerFile ";
	  system(oss.str().c_str());
	   */
	  //*************************************************************************************************************************

	  return 0;

}
Beispiel #23
0
int main(){
	Solution test;
	vector<string> res = test.generateParenthesis(6);
	cout << res.size() << endl;
	system("pause");
}
Beispiel #24
0
int main()
{
    Solution HanKin;
    HanKin.replaceSpace("We Are Happy", 12);
    return 0;
}
Beispiel #25
0
int main(int argc,char *argv[]){
	Solution s;
	char *test = "-2147483648";
	int result = s.atoi(test);
	cout<<result;
}
Beispiel #26
0
int main() {
    Solution solution;
    std::cout << solution.uniquePaths(3, 7) << std::endl;
    return 0;
}
Beispiel #27
0
int main()
{
    Solution s;
    cout<<s.isValid("]");
    return 0;
}
Beispiel #28
0
int main() {
    Solution s;
    vector<string> a{"with", "example", "science"};
    cout<<s.minStickers(a, "thehat")<<endl;
}
Beispiel #29
0
SolutionSet *MOCHC::execute() {
	
  int populationSize;
  int iterations;
  int maxEvaluations;
  int convergenceValue;
  int minimumDistance;
  int evaluations;

  double preservedPopulation;
  double initialConvergenceCount;
  bool condition = false;
  SolutionSet *solutionSet, *offSpringPopulation, *newPopulation; 

  Comparator * crowdingComparator = new CrowdingComparator();

  SolutionSet * population;
  SolutionSet * offspringPopulation;
  SolutionSet * unionSolution;

  Operator * cataclysmicMutation;
  Operator * crossover;
  Operator * parentSelection;


  //Read the parameters
  populationSize = *(int *) getInputParameter("populationSize");
  maxEvaluations = *(int *) getInputParameter("maxEvaluations");
  convergenceValue = *(int *) getInputParameter("convergenceValue");
  initialConvergenceCount = *(double *)getInputParameter("initialConvergenceCount");
  preservedPopulation = *(double *)getInputParameter("preservedPopulation");
  

  //Read the operators
  cataclysmicMutation = operators_["mutation"];
  crossover	      = operators_["crossover"];
  parentSelection     = operators_["parentSelection"];
  
  iterations  = 0;
  evaluations = 0;

  // calculating the maximum problem sizes .... 
  Solution * sol = new Solution(problem_);
  int size = 0;
  for (int var = 0; var < problem_->getNumberOfVariables(); var++) {
	Binary *binaryVar;
        binaryVar  = (Binary *)sol->getDecisionVariables()[var];
	size += binaryVar->getNumberOfBits();
  } 
  minimumDistance = (int) std::floor(initialConvergenceCount*size);

  // Create the initial solutionSet
  Solution * newSolution;
  population = new SolutionSet(populationSize);
  for (int i = 0; i < populationSize; i++) {
    newSolution = new Solution(problem_);
    problem_->evaluate(newSolution);
    problem_->evaluateConstraints(newSolution);
    evaluations++;
    population->add(newSolution);
  } //for


  while (!condition) {
	offSpringPopulation = new SolutionSet(populationSize);
 	Solution **parents = new Solution*[2];
	
	for (int i = 0; i < population->size()/2; i++) {
  		parents[0] = (Solution *) (parentSelection->execute(population));
		parents[1] = (Solution *) (parentSelection->execute(population));

		if (hammingDistance(*parents[0],*parents[1])>= minimumDistance) {
		   Solution ** offSpring = (Solution **) (crossover->execute(parents));
		   problem_->evaluate(offSpring[0]);
		   problem_->evaluateConstraints(offSpring[0]);
	           problem_->evaluate(offSpring[1]);
		   problem_->evaluateConstraints(offSpring[1]);
		   evaluations+=2;
		   offSpringPopulation->add(offSpring[0]);
		   offSpringPopulation->add(offSpring[1]);
		}		
	}  
	SolutionSet *join = population->join(offSpringPopulation);
 	delete offSpringPopulation;

	newPopulation = rankingAndCrowdingSelection(join,populationSize);
	delete join;
        if (equals(*population,*newPopulation)) {
		minimumDistance--;
	}   

	if (minimumDistance <= -convergenceValue) {
		minimumDistance = (int) (1.0/size * (1-1.0/size) * size);
		int preserve = (int) std::floor(preservedPopulation*populationSize);
		newPopulation->clear(); //do the new in c++ really hurts me(juanjo)
		population->sort(crowdingComparator);
		for (int i = 0; i < preserve; i++) {
			newPopulation->add(new Solution(population->get(i)));
		}
		for (int i = preserve; i < populationSize; i++) {
			Solution * solution = new Solution(population->get(i));
			cataclysmicMutation->execute(solution);
			problem_->evaluate(solution);
			problem_->evaluateConstraints(solution);	
			newPopulation->add(solution);
		}
		
	}

	iterations++;
	delete population;
	population = newPopulation;
	if (evaluations >= maxEvaluations) {
		condition = true;		
	}
  }

  return population;

}
Beispiel #30
0
int main() {
    Solution s;
    vector<int> nums({1, 2, 3, 4, 5});
    cout << s.rob(nums) << endl;
    return 0;
}