7/25/2017

521, 522. Longest Uncommon Subsequence I & II Solution with explanation

Longest Uncommon Subsequence I
Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.
A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.
The input will be two strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.
Example 1:
Input: "aba", "cdc"
Output: 3
Explanation: The longest uncommon subsequence is "aba" (or "cdc"), 
because "aba" is a subsequence of "aba", 
but not a subsequence of any other strings in the group of two strings. 
Note:
  1. Both strings' lengths will not exceed 100.
  2. Only letters from a ~ z will appear in input strings.
Think:
This problem is simple since there are only two strings in the array. What I gonna do is to compare these two strings by using equals() method in java.
Solution:


Longest Uncommon Subsequence II
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.
A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.
The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.
Example 1:
Input: "aba", "cdc", "eae"
Output: 3
Note:
  1. All the given strings' lengths will not exceed 10.
  2. The length of the given list will be in the range of [2, 50].
Think:
In the very beginning,  I tried to solve this problem in the same way as previous one. Sort the array and then compare string [ i ] to string [ i+1 ], to check whether they are the same or [ i ] contains [ i+1 ]. However, it didn't work like a charm and it has the fatal logic flaw.

The right way to do is:
1. sort the array by string.length()
2. count the frequency of the strings in the array first, take out all strings which only show up once.
3. check subsequence of those strings character by character 
4. loop until the unique string comes out, otherwise, return -1.

Solution:

7/15/2017

Leetcode 463. Island Perimeter solution with SUPER EASY explanation 超简单思路

You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn't have "lakes" (water inside that isn't connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
Example:
[[0,1,0,0],
 [1,1,1,0],
 [0,1,0,0],
 [1,1,0,0]]

Answer: 16
Explanation: The perimeter is the 16 yellow stripes in the image below:

Think:
Suppose I have only 1 land in the beginning, and the perimeter is 4.
1,0,0,0
0,0,0,0
0,0,0,0
if I gonna add 1 land, the graph would be like:
1,1,0,0
0,0,0,0
0,0,0,0
The perimeter is 6.
Continue doing this, obviously, I can figure out the perimeter match this pattern:
perimeter = 4+(n-1)*2
But there is an exception if the island looks like
1,1,0,0
1,0,0,0
0,0,0,0
when I add a new land in [2,2] the perimeter won't change
1,1,0,0
1,1,0,0
0,0,0,0
Which means, in the formula: perimeter = 4+(n-1)*2
the n needs to minus 1.

Solution:

7/05/2017

Leetcode 496. Next Greater Element I Solution with explanation Java解答

You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.
The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.
Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
    For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
    For number 1 in the first array, the next greater number for it in the second array is 3.
    For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
    For number 2 in the first array, the next greater number for it in the second array is 3.
    For number 4 in the first array, there is no next greater number for it in the second array, so output -1.
Note:
  1. All elements in nums1 and nums2 are unique.
  2. The length of both nums1 and nums2 would not exceed 1000.
My consideration:
In the beginning, I was confused with that: in Example 1, why the [2] in nums1 without a greater number? it Looked like 4 should be the answer. After I got some tips, the right way to go should be like:
Step 1: looking for all the numbers of nums1 in nums2.
for example, nums1=[4,1,2] in nums2 would be nums2 [1,3,4,2].
Step 2: get rid of nums1, find the NEXT GREATER NUMBER ONLY in nums2 [1,3,4,2].
Step 3: And you can see in [1,3,4,2]
The next great number of [1] is 3
The next great number of [4] is null, so -1
The next great number of [2] is null since [2] is the last element, no numbers follow it! So the result is -1
Step 4: Back to nums1's sequence, [ 4,1,2] would return [-1, 3, -1]

This explanation is not a coding mindset, just for helping to understand. 

The java solution:



ref: https://discuss.leetcode.com/topic/80119/java-o-n-hashmap-method-beats-95-currently

6/19/2017

Leetcode 3: Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

Solutions:




Leetcode 15. 3Sum solution with detail explanation

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.


Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

Keywords
Before 3sum, there is a two sum problem. 
Literally, we can make the 3sum as another 2 sum problem. 
For example, I need to have:

a + b + c = 0;
transfer it into:
a + b = -c;

There are some ways to solve this problem. 

#1: Easy Understanding But Dumb:
Thinking in an easy way to solve this problem, I gonna quick create 3 pointers in the array:
[ -1, 0, 1, 2, -1, 4]
   ^   ^  ^
   A  B C
First round, fixed A and B, let C go through all rest of the numbers and check if( A + B + C == 0) every time.
Second round, keep A, move B to the next number, here is 1, iterate C and check A + B + C =0.
[ -1, 0, 1, 2, -1, 4]
   ^       ^  ^
   A       B C
After B and C iterate all items, move A to 0, and restart it. Until pointer A moves to 2, the algorithm checks all possibilities in the array.
However, it would cost cubic time complexity which is not smart enough.


#2: The Better Way costs O(n^2) like following:

Solution:



Reference: 
Interview Question: Three Sum https://youtu.be/-AMHUdZc9ss

2/03/2017

Codeigniter: Unable to connect to your database server using the provided settings Solution Summary

Environment: Win 10, XAMPP
After I git clone a Codeigniter project, set up the environment, import the .sql file.
When I go to the localhost/index.php it shows:

Unable to connect to your database server using the provided settings

I have ..\application\config\database.php like this:
$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => 'root',
    'database' => 'comptracker_db',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,//(ENVIRONMENT != 'production'),
    'cache_on' => FALSE,
I googled this error and got some solutions:
1. in your database.php add:
$db['default']['port'] = '3306';
Some developers say it works, but not for me.

2. Go to your my.cnf and comment the line
bind 127.0.0.1
It doesn't work either. This line has already been comment in my.cnf

3. I find some code below and added to my database.php
echo '<pre>';
 print_r($db['default']);
echo '</pre>';

echo 'Trying to connect to database: ' .$db['default']['database'];
$dbh=mysql_connect
(
$db['default']['hostname'],
$db['default']['username'],
$db['default']['password'])
or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ($db['default']['database']);

echo '<br />   Connected OK:'  ;
die( 'file: ' .__FILE__ . '--> Line: ' .__LINE__);
Fresh localhost, I got:
Access denied for user 'root'@'localhost' (using password: YES)

Go to your C:\xampp\phpmyadmin\config.inc.php
Check this:
/* Authentication type and info */
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'] = true;
$cfg['Lang'] = '';
Make sure your 'password' here is the same as database.php
$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'comptracker_db',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,//(ENVIRONMENT != 'production'),
    'cache_on' => FALSE,

And this problem gets solved.