Saturday, April 4, 2020

LeetCode : Single Number (C++)

Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4

Solution :

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
class Solution
{
public:
static int singleNumber( std::vector<int> nums )
{
unsigned int result = 0;
// Get all unique number and add them
std::set<int> mSet( nums.begin(), nums.end() );
for( std::set<int>::iterator it = mSet.begin(); it != mSet.end(); it++ )
{
result += *it;
}
// we must have pairs , so double it
result = 2 * result;
// Reduce all the number to find what's left that must be the
// un-matched number
for( unsigned int i = 0; i < nums.size(); i++ )
{
result -= nums[i];
}
return result;
}
};
int main()
{
std::vector<int> nums = { 4, 1, 2, 1, 2 };
// should return 4 for this case
std::cout << Solution::singleNumber( nums ) << std::endl;
return 0;
}

LeetCode: Contains Duplicate (c++)

Given an array of integers, find if the array contains any duplicates.
Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
Example 1:
Input: [1,2,3,1]
Output: true
Example 2:
Input: [1,2,3,4]
Output: false
Example 3:
Input: [1,1,1,3,3,4,3,2,4,2]
Output: true

// Solution1: brute force that works but exceeds time limits due to complexity of 0(n^2)
bool containsDuplicate(vector & amp; nums) {
for (unsigned int i = 0; i & lt; nums.size(); i++)
{
for (unsigned int j = i + 1; j & lt; nums.size(); j++)
{
if (nums[i] == nums[j])
{
return true;
}
}
}
return false;
}
// Solution 2: Complexity is reduced (Accepted Answer)
bool containsDuplicate(vector & amp; nums) {
// Prevent against bad input
if (nums.size() == 0)
{
return false;
}
// Put the data in set
std::set mSet(nums.begin(), nums.end());
// Sets are unique, and thus if the size is
// same as vector then there are no duplicates
if (mSet.size() == nums.size())
{
return false;
} else
{
return true;
}
}

LeetCode : Solution Rotate Array (C++)

Given an array, rotate the array to the right by k steps, where k is non-negative.
Example 1:
Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2:
Input: [-1,-100,3,99] and k = 2
Output: [3,99,-1,-100]
Explanation: 
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]
Note:
  • Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
  • Could you do it in-place with O(1) extra space?

class Solution {
public:
void rotate(vector&amp; nums, int k) {
for(unsigned int i=0; i {
nums.emplace(nums.begin(), nums[nums.size()-1]);
nums.pop_back();
}
}
};

LeetCode : Solution Best Time to Buy and Sell Stock II Solution (C++)

Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
#include <iostream>
#include <vector>
int maxProfit( const std::vector<unsigned int> &prices )
{
// Prevent against extreme case
if(prices.size() <= 1)
{
return 0;
}
int profit = 0;
// Calculate the profit, loop through prices and
// keep on adding to profit when we are from
// velly to peak, as we need to buy and sell
for(int i =0 ; i< prices.size()-1 ; i++ )
{
if(prices[i] < prices[i+1])
{
profit += prices[i+1] - prices[i];
}
}
return profit;
}
int main()
{
std::vector<unsigned int> nums ={7,1,5,3,6,4};
// should return 7 for this case
std::cout<< maxProfit(nums) << std::endl;
return 0;
}
view raw MaxProfit.cpp hosted with ❤ by GitHub

Friday, April 3, 2020

debian how to open a folder in same gui


if you have the above issue.

1. go to System>Preference>File Management
2. select Behavior tab
3. tick the "Always open in browser windows


now the browsing would be via browser