Skip to content

Commit 5de0d48

Browse files
committed
feat: add 121. Best time to buy and sell stock
1 parent 875eb7b commit 5de0d48

File tree

8 files changed

+106
-20
lines changed

8 files changed

+106
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@
2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
2424
src/.DS_Store
25+
src/com/nphausg/.DS_Store

README.md

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,31 @@
1616
LeetCode is the best platform to help you enhance your skills, expand your knowledge and prepare for technical
1717
interviews.
1818

19-
https://leetcode.com/nphausg
20-
21-
🔥 Easy 🔥🔥 Medium 🔥🔥🔥 Hard
22-
23-
## 💎 Coding Challenge
24-
25-
| Challenge | Level | Java | Kotlin |
26-
|------------------------------------------------------------------------------------------------------------------|--------|---------------------------------------------------------------------------|-------------------------------------------------------------------|
27-
| [20. ValidParentheses](https://leetcode.com/problems/valid-parentheses) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/ValidParentheses.java) | - |
28-
| [26. Remove Duplicates](https://leetcode.com/problems/remove-duplicates-from-sorted-array) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/RemoveDuplicates.java) | - |
29-
| [27. Remove Element](https://leetcode.com/problems/remove-element) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/RemoveElement.java) | - |
30-
| [1929. Concatenation Of Array](https://leetcode.com/problems/concatenation-of-array) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/ConcatenationArray.java) | - |
31-
| [17. Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/LetterCombinations.java) | - |
32-
| [91. Decode Ways ](https://leetcode.com/problems/decode-ways) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/DecodeWays.java) | - |
33-
| [1143. Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/LongestCommonSubsequence.java) |
34-
| [658. Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/FindClosestElements.java) | - |
35-
| [41. First Missing Positive](https://leetcode.com/problems/first-missing-positive) | 🔥🔥🔥 | [Solution](src/com/nphausg/leetcode/hard/FirstMissingPositive.java) | [Solution](src/com/nphausg/leetcode/hard/FirstMissingPositive.kt) |
19+
Leetcode: https://leetcode.com/nphausg
20+
21+
Medium: https://medium.com/@nphausg
22+
23+
🔥 Easy
24+
25+
🔥🔥 Medium
26+
27+
🔥🔥🔥 Hard
28+
29+
## 💎 Coding Challenges
30+
31+
| Challenge | Level | Java | Kotlin |
32+
|--------------------------------------------------------------------------------------------------------------------|--------|-----------------------------------------------------------------------------|-------------------------------------------------------------------|
33+
| [0001. Two sum](https://leetcode.com/problems/two-sum) | 🔥 | - | [Solution](src/com/nphausg/leetcode/easy/TwoSum.kt) |
34+
| [0020. ValidParentheses](https://leetcode.com/problems/valid-parentheses) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/ValidParentheses.java) | - |
35+
| [0026. Remove Duplicates](https://leetcode.com/problems/remove-duplicates-from-sorted-array) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/RemoveDuplicates.java) | - |
36+
| [0027. Remove Element](https://leetcode.com/problems/remove-element) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/RemoveElement.java) | - |
37+
| [0121. Best time to buy and sell stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock) | 🔥 | - | [Solution](src/com/nphausg/leetcode/easy/BuyAndSellStock.kt) |
38+
| [1929. Concatenation Of Array](https://leetcode.com/problems/concatenation-of-array) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/ConcatenationArray.java) | - |
39+
| [0017. Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/LetterCombinations.java) | - |
40+
| [0091. Decode Ways ](https://leetcode.com/problems/decode-ways) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/DecodeWays.java) | - |
41+
| [1143. Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/LongestCommonSubsequence.java) |
42+
| [0658. Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/FindClosestElements.java) | - |
43+
| [0041. First Missing Positive](https://leetcode.com/problems/first-missing-positive) | 🔥🔥🔥 | [Solution](src/com/nphausg/leetcode/hard/FirstMissingPositive.java) | [Solution](src/com/nphausg/leetcode/hard/FirstMissingPositive.kt) |
3644

3745
![-----------------------------------------------------](https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/colored.png)
3846

src/com/nphausg/leetcode/.DS_Store

6 KB
Binary file not shown.

src/com/nphausg/leetcode/Main.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
package com.nphausg.leetcode;
77

8-
import com.nphausg.leetcode.medium.LongestCommonSubsequence;
8+
import com.nphausg.leetcode.practice.Practice;
99

1010
public class Main {
1111

@@ -23,6 +23,7 @@ public static void main(String[] args) {
2323
// String text1 = "abc";
2424
// String text2 = "def";
2525

26-
System.out.println(LongestCommonSubsequence.longestCommonSubsequence(text1, text2));
26+
// System.out.println(LongestCommonSubsequence.longestCommonSubsequence(text1, text2));
27+
System.out.println(Practice.powerOf2(50));
2728
}
2829
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.nphausg.leetcode.easy
2+
3+
/**
4+
* <a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock">Buy and Sell stock </a>
5+
* */
6+
object BuyAndSellStock {
7+
8+
fun maxProfit(prices: IntArray): Int {
9+
var maxProfit = 0
10+
var minPrice = Int.MAX_VALUE
11+
12+
for (price in prices) {
13+
if (price < minPrice) {
14+
minPrice = price // Update the minimum price
15+
} else {
16+
val profit = price - minPrice // Calculate potential profit
17+
if (profit > maxProfit) {
18+
maxProfit = profit // Update max profit if needed
19+
}
20+
}
21+
}
22+
return maxProfit
23+
}
24+
}

src/com/nphausg/leetcode/easy/RemoveElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
package com.nphausg.leetcode.easy;
77

88
/**
9-
* <a href="https://leetcode.com/problems/remove-element/">...</a>
9+
* <a href="https://leetcode.com/problems/remove-element/">Remove Element</a>
1010
*/
1111
public class RemoveElement {
1212

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.nphausg.leetcode.easy
2+
3+
/**
4+
* <a href="https://leetcode.com/problems/two-sum">Two Sum</a>
5+
* */
6+
object TwoSum {
7+
8+
fun solution_brute_force(nums: IntArray, target: Int): IntArray {
9+
for (i in nums.indices) {
10+
for (j in i + 1 until nums.size) {
11+
if (nums[i] + nums[j] == target) {
12+
return intArrayOf(i, j)
13+
}
14+
}
15+
}
16+
throw IllegalArgumentException("No two sum solution")
17+
}
18+
19+
fun solution_hash_map(nums: IntArray, target: Int): IntArray {
20+
val map = mutableMapOf<Int, Int>()
21+
for (i in nums.indices) {
22+
val complement = target - nums[i]
23+
if (map.containsKey(complement)) {
24+
return intArrayOf(map[complement]!!, i)
25+
}
26+
map[nums[i]] = i
27+
}
28+
throw IllegalArgumentException("No two sum solution")
29+
}
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.nphausg.leetcode.practice;
2+
3+
/**
4+
* The following function prints the powers of 2 from 1 through n (inclusive).
5+
* For example, if n is 4, it would be print 1, 2, and 4.
6+
*/
7+
public class Practice {
8+
9+
public static int powerOf2(int n) {
10+
if (n < 1) {
11+
return 0;
12+
}
13+
if (n == 1) {
14+
System.out.println(1);
15+
return 1;
16+
}
17+
int prev = powerOf2(n / 2);
18+
int curr = prev * 2;
19+
System.out.println(curr);
20+
return curr;
21+
}
22+
}

0 commit comments

Comments
 (0)