Skip to content

Commit 4b7dbfc

Browse files
committed
init comit
0 parents  commit 4b7dbfc

16 files changed

+719
-0
lines changed

.github/workflows/java.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Java
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v3
17+
- name: Setup JDK 11
18+
uses: actions/setup-java@v3
19+
with:
20+
distribution: 'adopt'
21+
java-version: '11'
22+
- name: Grant execution permission for gradlew
23+
run: chmod +x gradlew
24+
- name: Cache Gradle packages
25+
uses: actions/cache@v3
26+
with:
27+
path: ~/.gradle/caches
28+
key: ${{ runner.os }}-gradle-${{ github.event.repository.name }}-${{ hashFiles('**/*.gradle')}}
29+
restore-keys: ${{ runner.os }}-gradle-${{ github.event.repository.name }}
30+
- name: Build with Gradlew
31+
run: ./gradlew build
32+
- name: Test with Gradlew
33+
run: ./gradlew :test --tests "SolutionTest"

.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
.gradle
2+
build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
!**/src/main/**/build/
5+
!**/src/test/**/build/
6+
7+
### IntelliJ IDEA ###
8+
.idea/modules.xml
9+
.idea/jarRepositories.xml
10+
.idea/compiler.xml
11+
.idea/libraries/
12+
*.iws
13+
*.iml
14+
*.ipr
15+
out/
16+
!**/src/main/**/out/
17+
!**/src/test/**/out/
18+
19+
### Eclipse ###
20+
.apt_generated
21+
.classpath
22+
.factorypath
23+
.project
24+
.settings
25+
.springBeans
26+
.sts4-cache
27+
bin/
28+
!**/src/main/**/bin/
29+
!**/src/test/**/bin/
30+
31+
### NetBeans ###
32+
/nbproject/private/
33+
/nbbuild/
34+
/dist/
35+
/nbdist/
36+
/.nb-gradle/
37+
38+
### VS Code ###
39+
.vscode/
40+
41+
### Mac OS ###
42+
.DS_Store

.husky/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
./gradlew :test --tests "SolutionTest"

.idea/gradle.xml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 127 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# java_container_with_most_water
2+
3+
You are given an integer array `height` of length `n`. There are `n` vertical lines drawn such that the two endpoints of the `ith` line are `(i, 0)` and `(i, height[i])`.
4+
5+
Find two lines that together with the x-axis form a container, such that the container contains the most water.
6+
7+
Return *the maximum amount of water a container can store*.
8+
9+
**Notice** that you may not slant the container.
10+
11+
## Examples
12+
13+
**Example 1:**
14+
15+
![https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg)
16+
17+
```
18+
Input: height = [1,8,6,2,5,4,8,3,7]
19+
Output: 49
20+
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
21+
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: height = [1,1]
28+
Output: 1
29+
30+
```
31+
32+
**Constraints:**
33+
34+
- `n == height.length`
35+
- `2 <= n <= 105`
36+
- `0 <= height[i] <= 104`
37+
38+
## 解析
39+
40+
給定一個非負整數陣列 height
41+
42+
其中每個 height[i] 代表 index i 的格版高度
43+
44+
假設 每兩個 height 之間相隔為單位 1
45+
46+
往中間注水 假設格版本身很薄 所以不計
47+
48+
則兩個 height 所能裝的水的面積 剛好是由比較小的 height * 兩個 height 的 X軸相查距離 = min(height[i], height[j]) *(j -i) if i < j
49+
50+
51+
![](https://i.imgur.com/Uqn879I.png)
52+
53+
54+
要求寫一個演算法來計算再給定的 height 中所能注入最多面積是多少
55+
56+
由於每個面積都是由兩個 height 來圍出
57+
58+
所以可以透過 two pointer 然後逐步更新最短的 height 的 point 來找尋下一個更多面積的可能值
59+
60+
直到兩個 height 只差 1
61+
62+
而再這之間不斷把當下最大的做紀錄
63+
64+
初始化 lp =0, rp = len(height), maxArea = 0
65+
66+
當 lp < rp 做以下運算
67+
68+
area = min(height[lp], height[rp]) *(rp-lp)
69+
70+
如果 area > maxArea, 則更新 更新 maxArea = area
71+
72+
如果 height[lp] > height[rp], 更新 rp -= 1 否則更新 lp += 1
73+
74+
![](https://i.imgur.com/0ovM7Ly.png)
75+
76+
## 程式碼
77+
```java
78+
public class Solution {
79+
public int maxArea(int[] height) {
80+
int mArea = 0;
81+
int lp = 0, rp = height.length -1;
82+
while (lp < rp) {
83+
mArea = Math.max(mArea, Math.min(height[lp], height[rp]) * (rp - lp));
84+
if (height[lp] > height[rp]) {
85+
rp--;
86+
} else {
87+
lp++;
88+
}
89+
}
90+
return mArea;
91+
}
92+
}
93+
```
94+
## 困難點
95+
96+
1. 要看出與面積相關的計算方式
97+
2. 要看出如何找下一個可能的更大的面積
98+
99+
## Solve Point
100+
101+
- [x] 初始化 rp =0, lp = len(height) - 1, maxArea = 0
102+
- [x] 當 rp < lp 做以下運算
103+
- [x] area = min(height[lp], height[rp]) *(rp-lp)
104+
- [x] if area > maxArea 更新 maxArea = area
105+
- [x] 當 height[lp] > height[rp] 更新 rp -= 1 否則更新 lp += 1
106+
- [x] 回傳 maxArea

build.gradle

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
group 'org.example'
6+
version '1.0-SNAPSHOT'
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
14+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
15+
}
16+
17+
test {
18+
useJUnitPlatform()
19+
}

gradle/wrapper/gradle-wrapper.jar

58.4 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)