File tree 3 files changed +75
-0
lines changed
3 files changed +75
-0
lines changed Original file line number Diff line number Diff line change
1
+ # AlgoProblems_java
2
+
3
+
4
+ This Repository contains java codes for the common algo problems from major coding practice sites.Such as:
5
+
6
+ - LeetCode
7
+
8
+ ### LeetCode [ /src/com/leetcode]
9
+
10
+ * [ Two-Sum] - [ /src/com/leetCode/TwoSum.java]
11
+ * [ Best-time-to-buy-and-sell-stock] - [ /src/com/leetCode/BuySellStocks.java]
12
+
13
+
14
+ ### Todos
15
+
16
+ - Improve Time Complexity/ Space complexity or overall complexity
17
+ - Raise a PR
18
+
19
+
20
+
21
+
22
+ ** Let's code it out!!**
23
+
24
+ [ // ] : # ( These are reference links used in the body of this note and get stripped out when the markdown processor does its job. There is no need to format nicely because it shouldn't be seen. Thanks SO - http://stackoverflow.com/questions/4823468/store-comments-in-markdown-syntax )
25
+
26
+
27
+ [ Two-Sum ] : < https://leetcode.com/problems/two-sum/r >
28
+ [ /src/com/leetCode/TwoSum.java ] : < https://github.com/LeetCode-ScalerTeam/AlgoProblems_java/blob/master/src/com/leetCode/TwoSum.java >
29
+ [ Best-time-to-buy-and-sell-stock ] : < https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ >
30
+ [ /src/com/leetCode/BuySellStocks.java ] : < https://github.com/LeetCode-ScalerTeam/AlgoProblems_java/blob/master/src/com/leetCode/BuySellStocks.java >
31
+
32
+
Original file line number Diff line number Diff line change
1
+ package com .leetCode ;
2
+ class BuySellStocksSolution {
3
+ public int maxProfit (int [] prices ) {
4
+ if (prices .length <= 0 )
5
+ return 0 ;
6
+ int max = prices [prices .length -1 ] , diffMax =0 ;
7
+ //System.out.println("max "+max +" diffMax "+diffMax);
8
+ for (int i = prices .length -2 ; i >= 0 ; i --){
9
+ if (prices [i ] <= max && diffMax < (max - prices [i ]))
10
+ diffMax = max - prices [i ];
11
+ else if (prices [i ] > max )
12
+ max = prices [i ];
13
+ //System.out.println("max "+max +" prices[i]=> "+prices[i]+" diffMax "+diffMax);
14
+ }
15
+ return diffMax ;
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ package com .leetCode ;
2
+
3
+ import java .util .*;
4
+
5
+ class TwoSumsSolution {
6
+ public int [] twoSum (int [] nums , int target ) {
7
+ Map <Integer , Integer > seenMap = new HashMap <>();
8
+ int res [] = new int [2 ];
9
+ for (int i = 0 ; i < nums .length ; i ++){
10
+ int temp = target - nums [i ];
11
+ //System.out.println("temp -> "+temp+" seenMap.containsKey(temp) -> "+seenMap.containsKey(temp));
12
+ if (seenMap .containsKey (temp )){
13
+ //System.out.println("In If");
14
+ res [0 ] = seenMap .get (temp );
15
+ res [1 ] = i ;
16
+ return res ;
17
+ }
18
+ else {
19
+ //System.out.println("else ");
20
+ seenMap .put (nums [i ],i );
21
+ }
22
+ //System.out.println(seenMap);
23
+ }
24
+ return res ;
25
+ }
26
+ }
You can’t perform that action at this time.
0 commit comments