Skip to main content

Posts

[Interview] Check if String has Unique Characters

[Interview] Check if String has Unique Characters Implement an algorithm to determine if a string has all unique characters What if you can not use additional data structure? package com.Array_Strings; /** * @Question: 1-1. Implement an algorithm to determine if a string has all * unique characters What if you can not use additional data * structure? */ public class Ans1_1 { public boolean helper(String str) { boolean[] map = new boolean[256]; for (int i = 0; i < str.length(); i++) { int val = str.charAt(i); if (map[val]) return false; map[val] = true; } return true; } } Complexity Analysis: Time Complexity: \(O(n)\) Space Complexity: \(O(n)\) Here is my Video Explanation

[LeetCode Solution 131]: Palindrome Partitioning

[LeetCode Solution 131]: Palindrome Partitioning ********************************************************************************* Question: Given a string   s , partition   s   such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of   s . For example, given   s   =   "aab" , Return [ ["aa","b"], ["a","a","b"] ] -------------------------------------------------------------------------------------------------- Recursive Method Strategy We are going to use backtracking to solve this problem, the idea is if we find a palindrome  substring, we add it in a temp and go further searching, the idea is as shows in the following picture: Java public class Solution { public List<List<String>> partition(String s) { if (s.length() == 0 || s == null) return null; List<List<String>> res = new ArrayList&

[LeetCode Solution 40]: Combination Sum II

[LeetCode Solution 40]: Combination Sum II ********************************************************************************* Question: Given a collection of candidate numbers ( C ) and a target number ( T ), find all unique combinations in  C  where the candidate numbers sums to  T . Each number in  C  may only be used  once  in the combination. Note: All numbers (including the target) will be positive integers. The solution set must not contain duplicate combinations. For example, given candidate set  [10, 1, 2, 7, 6, 1, 5]  and target, 8 A solution set is:  [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] -------------------------------------------------------------------------------------------------- Approach Recursive Method Strategy The idea is same with the  [LeetCode Solution 39] Combination Sum , using backtracking method, but the only difference is how to get the value that we have to skip that can help us to avoid the duplication. H

[Machine Learning] #10 Solving Problem of Overfitting

[Machine Learning] #10 Solving Problem of Overfitting  [Write Infront]: In the previous Machine Learning Topic, we talk about the Linear Regression and Logistic Regression, but we don't focus on the very important topic, Overfitting.  ************************************************************************ ************************************************************************ [The Problem of Overfitting]: What is Overfitting, in a very easy word, overfitting is the function we got is soo.. accurate for the training set but not generalize for the new. Typical overfitting means that error on the training data is very low but error on new data is high. Do not do that soo accurate, because our life is not that accurate. right? So, the problem is how we can avoid such problem? 1) Reduce the number of features: Manually select which feature to keep. Use model selection Algo (We will discuss later) 2) Regularization: Keep all the features,

[Machine Learning] #9 Multi-class Classification

[Machine Learning] #9 Multi-class Classification [Write Infront]: Binary classification posted in the previous blog  [Machine Learning]: #8 Logistic Model . The picture (Allen Turing) is different from the previous one no tense expression, so topic today seems like much easier to understand...😃 ********************************************************************************* ********************************************************************************* [Muti-Classification] The different is we want to seperate the output in diferent class, that's it! Instead of \(y = \{ 0, 1\}\), we will expand our defination so that  \(y = \{ 0, 1, 2, 3 ..., n\}\). So, for each \(y\), we have to divide our problem into \(n+1\)binary classification problems. In each one, we predict the probability that 'y' is a member of one of our classes. $$\eqalign{   & y \in \{ 0,1,......n\}   \cr   & H_\theta ^0(x) = P(y = 0|x,\theta )  \cr   & H_\theta ^1(

[Machine Learning]: #8 Logistic Model

[Machine Learning]: #8 Logistic Model *************************************** [Write Infront]: let's review the model we have already defined in  [Machine Learning]: #7 Classification Introduction   Training Sets $$\{ ({x^{(1)}},{y^{(1)}}),({x^{(2)}},{y^{(2)}}),.....({x^{(m)}},{y^{(m)}})\} $$ where  $$x \in \left[ {\matrix{    1  \cr    {{x_1}}  \cr    {...}  \cr    {{x_n}}  \cr  } } \right]$$ and \(x_0 = 1\ , y \in \{ 0,1\}\) $${H_\theta }(x) = {1 \over {1 + {e^{ - {\theta ^T}x}}}}$$. Our problem become to that how to define a Cost Function for \({H_\theta }(x)\) to determine a Best \(\theta \) ********************************************************************************* ********************************************************************************* [Cost Function for Logistic Function]: Some of you may ask why we don't use the  Cost Function for Linear Regression? It is because we want to define the Cost Function is Convex !!! Whi

[Machine Learning]: #7 Classification Introduction

[Machine Learning] #7 Classification Introduction **************************************** [What is Classification Problem]: if or not problem/ yes or no problem. Is it a spam email? Are you a good boy? Do you like this girl?......  [Logistic Regression Model]: Linear Regression performs badly in the classification problems. because the classification problem is not 'Continuous'. Intuitively, it also doesn't make sense for \({H_\theta }(x)\) which is larger than 1 or smaller than 0 we when knowing the \(y = \{ 0,1\}\), \(y\) is just 0 or 1. So we try to make the value of  \({H_\theta }(x)\) between 0 and 1, how can we do that? Yes, Logistic Function. $${H_\theta }(x) = g({\theta ^T}x)$$ $$z = {\theta ^T}x$$ $$g(z) = {1 \over {1 + {e^{ - z}}}}$$ And the following image shows that what the function looks like: [What does the funtion means]: \({H_\theta }(x)\) gives us the probability that the output is 1. For example.  \({H_\theta }(x)=0.8\