Skip to content

Commit 17c7ded

Browse files
[LEET-0000] clean up
1 parent 64c7d1c commit 17c7ded

File tree

5 files changed

+147
-1
lines changed

5 files changed

+147
-1
lines changed

leetcode-algorithms/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@
6363
|339|[Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/NestedListWeightSum.java)| O(n)|O(h)) | Easy| DFS
6464
|338|[Counting Bits](https://leetcode.com/problems/counting-bits/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/CountingBits.java)| O(nlogn)|O(h) | Medium|
6565
|336|[Palindrome Pairs](https://leetcode.com/problems/palindrome-pairs/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/PalindromePairs.java)| O(n^2)|O(n) | Hard|
66+
|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/IncreasingTripletSubsequence.java)| O(n^2)|O(1) | Medium|
6667
|333|[Largest BST Subtree](https://leetcode.com/problems/largest-bst-subtree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/LargestBSTSubtree.java)| O(n)|O(n) | Medium|
68+
|329|[Longest Increasing Path in a Matrix](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/LongestIncreasingPathInAMatrix.java)| O(?)|O(?) | Hard|
6769
|325|[Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/)|[Solution] | O(n)|O(n) | Medium| HashMap
6870
|323|[Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/NumberOfConnectedComponentsInAnUndirectedGraph.java)| O(?)|O(?)| Medium|
6971
|317|[Shortest Distance from All Buildings](https://leetcode.com/problems/shortest-distance-from-all-buildings/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ShortestDistanceFromAllBuildings.java)| O(?)|O(?) | Hard|
@@ -88,12 +90,14 @@
8890
|286|[Walls and Gates](https://leetcode.com/problems/walls-and-gates/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/WallsAndGates.java)| O(m*n)|O(g) | Medium| BFS
8991
|285|[Inorder Successor In BST](https://leetcode.com/problems/inorder-successor-in-bst/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/InorderSuccessorInBST.java)| O(h)|O(1) | Medium|
9092
|283|[Move Zeroes](https://leetcode.com/problems/move-zeroes/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/MoveZeroes.java)| O(n)|O(1) | Easy|
93+
|282|[Expression Add Operators](https://leetcode.com/problems/expression-add-operators/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ExpressionAddOperators.java)| O(?)|O(?) | Hard|
9194
|281|[Zigzag Iterator](https://leetcode.com/problems/zigzag-iterator/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ZigzagIterator.java)| O(n)|O(n) | Medium|
9295
|280|[Wiggle Sort](https://leetcode.com/problems/wiggle-sort/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/WiggleSort.java)| O(n)|O(1) | Medium|
9396
|279|[Perfect Squares](https://leetcode.com/problems/perfect-squares/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/PerfectSquares.java)| O(n)|O(1) | Medium|
9497
|278|[First Bad Version](https://leetcode.com/problems/first-bad-version/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FirstBadVersion.java)| O(logn)|O(1) | Easy| Binary Search
9598
|277|[Find the Celebrity](https://leetcode.com/problems/find-the-celebrity/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindtheCelebrity.java)| O(n)|O(1) | Medium|
9699
|276|[Paint Fence](https://leetcode.com/problems/paint-fence/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/PaintFence.java)| O(n)|O(1) | Easy| DP
100+
|274|[H-Index](https://leetcode.com/problems/h-index/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/HIndex.java)| O(nlogn)|O(1) | Medium|
97101
|273|[Integer to English Words](https://leetcode.com/problems/integer-to-english-words/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/IntegertoEnglishWords.java)| O(n)|O(1) | Hard|
98102
|272|[Closest Binary Search Tree Value II](https://leetcode.com/problems/closest-binary-search-tree-value-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ClosestBinarySearchTreeValueII.java)| O(h+k)|O(h) | Hard| Stack
99103
|271|[Encode and Decode Strings](https://leetcode.com/problems/encode-and-decode-strings/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/EncodeandDecodeStrings.java)| O(n)|O(1) | Medium|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.stevesun.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +, -, or * between the digits so they evaluate to the target value.
8+
9+
Examples:
10+
"123", 6 -> ["1+2+3", "1*2*3"]
11+
"232", 8 -> ["2*3+2", "2+3*2"]
12+
"105", 5 -> ["1*0+5","10-5"]
13+
"00", 0 -> ["0+0", "0-0", "0*0"]
14+
"3456237490", 9191 -> []
15+
*/
16+
public class ExpressionAddOperators {
17+
18+
public List<String> addOperators(String num, int target) {
19+
List<String> res = new ArrayList<String>();
20+
StringBuilder sb = new StringBuilder();
21+
dfs(res, sb, num, 0, target, 0, 0);
22+
return res;
23+
24+
}
25+
26+
private void dfs(List<String> res, StringBuilder sb, String num, int pos, int target, long prev, long multi) {
27+
if(pos == num.length()){
28+
if(target == prev) res.add(sb.toString());
29+
return;
30+
}
31+
for(int i = pos; i < num.length(); i++){
32+
if(num.charAt(pos) == '0' && i != pos) break;
33+
long curr = Long.parseLong(num.substring(pos, i+1));
34+
int len = sb.length();
35+
if(pos == 0){
36+
dfs(res, sb.append(curr), num, i+1, target, curr, curr);
37+
sb.setLength(len);
38+
} else {
39+
dfs(res, sb.append("+").append(curr), num, i+1, target, prev+curr, curr);
40+
sb.setLength(len);
41+
42+
dfs(res, sb.append("-").append(curr), num, i+1, target, prev-curr, -curr);
43+
sb.setLength(len);
44+
45+
dfs(res, sb.append("*").append(curr), num, i+1, target, prev - multi + multi*curr, multi*curr);
46+
sb.setLength(len);
47+
}
48+
}
49+
}
50+
51+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.stevesun.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
7+
8+
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
9+
10+
For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively. Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, his h-index is 3.
11+
12+
Note: If there are several possible values for h, the maximum one is taken as the h-index.
13+
14+
Hint:
15+
16+
An easy approach is to sort the array first.
17+
What are the possible values of h-index?
18+
A faster approach is to use extra space.
19+
*/
20+
public class HIndex {
21+
22+
public int hIndex(int[] citations) {
23+
if(citations == null || citations.length == 0)
24+
return 0;
25+
26+
Arrays.sort(citations);
27+
for(int i = 0; i < citations.length; i++){
28+
if(citations[i] >= citations.length - i)
29+
return citations.length - i;
30+
}
31+
return 0;
32+
}
33+
34+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.stevesun.solutions;
2+
3+
/**
4+
* Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.
5+
6+
Formally the function should:
7+
Return true if there exists i, j, k
8+
such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false.
9+
Your algorithm should run in O(n) time complexity and O(1) space complexity.
10+
11+
Examples:
12+
Given [1, 2, 3, 4, 5],
13+
return true.
14+
15+
Given [5, 4, 3, 2, 1],
16+
return false.
17+
*/
18+
public class IncreasingTripletSubsequence {
19+
20+
public boolean increasingTriplet(int[] nums) {
21+
if(nums == null || nums.length == 0) return false;
22+
int small = nums[0], medium = Integer.MAX_VALUE;
23+
for(int i = 1; i < nums.length; i++){
24+
small = Math.min(small, nums[i-1]);
25+
if(nums[i] > small){
26+
medium = Math.min(medium, nums[i]);
27+
for(int j = i+1; j < nums.length; j++){
28+
if(nums[j] > nums[i] || nums[j] > medium) return true;
29+
}
30+
}
31+
}
32+
return false;
33+
}
34+
35+
}

leetcode-algorithms/src/main/java/com/stevesun/solutions/LongestIncreasingPathInAMatrix.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
11
package com.stevesun.solutions;
22

3+
/**Given an integer matrix, find the length of the longest increasing path.
4+
5+
From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).
6+
7+
Example 1:
8+
9+
nums = [
10+
[9,9,4],
11+
[6,6,8],
12+
[2,1,1]
13+
]
14+
Return 4
15+
The longest increasing path is [1, 2, 6, 9].
16+
17+
Example 2:
18+
19+
nums = [
20+
[3,4,5],
21+
[3,2,6],
22+
[2,2,1]
23+
]
24+
Return 4
25+
The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.*/
326
public class LongestIncreasingPathInAMatrix {
427
//inspired by this solution: https://discuss.leetcode.com/topic/34835/15ms-concise-java-solution, wrote it myself:
528

6-
729
final int dirs[] = new int[]{0, 1, 0, -1, 0};
830

931
public int longestIncreasingPath(int[][] matrix) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy