Logistic Regression vs. SVM

Logistic Regression and SVM often give the similar results. SVM costs longer to train than logistic regression, so it seems that there is no obvious reason to use SVM. Actually, in industry logistic regression is the most frequently used algorithm.

The reason that logistic regression and SVM have similar performance is that the training data is linearly separable, which happens very often. Therefore, there is no need to project the value to a higher dimension to separate them.

Get Target Number Using Number List and Arithmetic Operations

Given a list of numbers and a target number, write a program to determine whether the target number can be calculated by applying “+-*/” operations to the number list? You can assume () is automatically added when necessary. An operator should be put between each two consecutive numbers. So each number has to be used.

For example, given {1,2,3,4} and 21, return true. Because (1+2)*(3+4)=21

LeetCode – Max Chunks To Make Sorted (Java)

Given an array arr that is a permutation of [0, 1, …, arr.length – 1], we split the array into some number of “chunks” (partitions), and individually sort each chunk. After concatenating them, the result equals the sorted array.

LeetCode -Toeplitz Matrix

A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Given a matrix, check if it is toeplitz. (Assume the matrix is not empty) Java Solution public boolean isToeplitzMatrix(int[][] matrix) { int m=matrix.length; int n=matrix[0].length; for(int i=0; i<m; i++){ for(int j=0; j<n; j++){ if(i+1<m && j+1<n && matrix[i][j]!=matrix[i+1][j+1]){ return false; … Read more

LeetCode – Number of Subarrays with Bounded Maximum (Java)

We are given an array A of positive integers, and two positive integers L and R (L <= R).

LeetCode – Rotated Digits (Java)

X is a good number if after rotating EACH digit individually by 180 degrees, we get a valid number that is different from X. A number is valid if each digit remains a digit after rotation. 0, 1, and 8 rotate to themselves; 2 and 5 rotate to each other; 6 and 9 rotate to each other, and the rest of the numbers do not rotate to any other number.

Top 16 Java Utility Classes

In Java, a utility class is a class that defines a set of methods that perform common functions. This post shows the most frequently used Java utility classes and their most commonly used methods. Both the class list and their method list are ordered by popularity. The data is based on randomly selected 50,000 open source Java projects from GitHub.

LeetCode – Count of Smaller Numbers After Self (Java)

You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i]. Example: Input: [5,2,6,1] Output: [2,1,1,0] Java Solution 1 public List<Integer> countSmaller(int[] nums) { List<Integer> result = new ArrayList<Integer>(); ArrayList<Integer> sorted … Read more

Top 10 API Related Questions from Stack Overflow

Stack Overflow is a large repository of valuable programming knowledge. Millions of questions have been answered on Stack Overflow and many answers have very high quality. This is the reason that Stack Overflow answers are often located on the top of the search result from Google.

Java Dependency Injection Example

Dependency injection (DI) is a technique in which a class receives its dependency from outside. If class A uses class B, class A is dependent on class B, and B is a dependency of A.

LeetCode – Minimum Increment to Make Array Unique (Java)

Java Solution 1 public int minIncrementForUnique(int[] A) { Arrays.sort(A);   int count = 0; for(int i=1; i<A.length; i++){ if(A[i]<=A[i-1]){ count+=A[i-1]+1; A[i]=A[i]+1; } }   return count; }public int minIncrementForUnique(int[] A) { Arrays.sort(A); int count = 0; for(int i=1; i<A.length; i++){ if(A[i]<=A[i-1]){ count+=A[i-1]+1; A[i]=A[i]+1; } } return count; } Actually we do not need to increment … Read more

LeetCode – Most Stones Removed with Same Row or Column (Java)

The easiest solution for this problem is the union-find. The number of island problem can help understand how union-find works. The basic idea is that we use a disjoint set to track each component. Whenever two stones can be connected, the number of islands decrease one. The final result, the number of movement, is the … Read more

LeetCode – Is Subsequence (Java)

Given a string s and a string t, check if s is subsequence of t.

You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100).