Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions LongestIncreasingSubsequence/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
```java
// Create a memory to store the maxLength for each element
// In each step, check if there is a smaller number in the past
// If yes, then count up, then compare with the max number we already have
// Time complexity: O(N**2)
// Space complexity: O(N) memory of the dp.
// Time spend: 08:30
class Solution {
public int lengthOfLIS(int[] nums) {
int[] dp = new int[nums.length];
dp[0] = 1;
int maxLength = 1;

for (int i = 1; i < nums.length; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
maxLength = Math.max(maxLength, dp[i]);
}

return maxLength;
}
}
```
30 changes: 30 additions & 0 deletions LongestIncreasingSubsequence/step2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
```java
// After checking the solutions tab...What's an elegant solution
// Time complexity: O(NlogN)
// Time spend: 07:30
class Solution {
public int lengthOfLIS(int[] nums) {
int[] tails = new int[nums.length];
int maxLength = 0;

for (int num : nums) {
int l = 0;
int r = maxLength;
while (l != r) {
int mid = l + (r - l) / 2;
if (tails[mid] < num) {
l = mid + 1;
continue;
}
r = mid;
}
tails[l] = num;
if (l == maxLength) {
maxLength++;
}
}

return maxLength;
}
}
```
70 changes: 70 additions & 0 deletions LongestIncreasingSubsequence/step3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
```java
// Time spend: 04:20
class Solution {
public int lengthOfLIS(int[] nums) {
List<Integer> increasingNums = new ArrayList<>();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
List<Integer> increasingNums = new ArrayList<>();
var increasingNums = new ArrayList<Integer>();

長らく触ってないのでJavaの常識があまりわからないのですが、varを使った方がわかりやすい気がしました。しかしArrayListまで絞らずListにしておいた方が良いという考え方もありそうですね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

実際に業務で使ってる人に質問になるのですが、業務で型推論は推奨になっていますか?
(個人的には型推論利用したい派はあるが、諸事情で使っていません)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、右側だけで型が明確である場合、varの方が好ましいのよくわかります。
今回の場合、Listインタフェースを利用したいというのがあって、明示的に書くしかなさそうです。

increasingNums.add(nums[0]);

for (int i = 1; i < nums.length; i++) {
if (nums[i] > increasingNums.get(increasingNums.size() - 1)) {
increasingNums.add(nums[i]);
continue;
}
int low = Collections.binarySearch(increasingNums, nums[i]);
if (low < 0) {
// bit否定?
low = -(low + 1);
}
increasingNums.set(low, nums[i]);
}

return increasingNums.size();
}
}
```

```java
// Time spend: 03:30
class Solution {
public int lengthOfLIS(int[] nums) {
List<Integer> increasingNums = new ArrayList<>();
increasingNums.add(nums[0]);

for (int i = 1; i < nums.length; i++) {
if (nums[i] > increasingNums.get(increasingNums.size() - 1)) {
increasingNums.add(nums[i]);
continue;
}
int index = Collections.binarySearch(increasingNums, nums[i]);
if (index < 0) {
increasingNums.set(-(index + 1), nums[i]);
}
}

return increasingNums.size();
}
}
```

```java
// Time spend: 02:20
class Solution {
public int lengthOfLIS(int[] nums) {
List<Integer> increasingNums = new ArrayList<>();
increasingNums.add(nums[0]);

for (int i = 0; i < nums.length; i++) {
if (nums[i] > increasingNums.get(increasingNums.size() - 1)) {
increasingNums.add(nums[i]);
continue;
}
int index = Collections.binarySearch(increasingNums, nums[i]);
if (index < 0) {
increasingNums.set(-index-1, nums[i]);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-index-1 は、~index でしたっけ?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

はい。bit演算普段あまりみないので、このままにしました。

}
}

return increasingNums.size();
}
}
```