This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public int canCompleteCircuit(int[] gas, int[] cost) { | |
| int sumRemaining=0; | |
| int total=0; | |
| int start=0; | |
| for(int i=0;i<gas.length;i++){ | |
| int remaining=gas[i]-cost[i]; | |
| if(sumRemaining>=0){ | |
| sumRemaining+=remaining; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public int minCost(String s, int[] cost) { | |
| int size=s.length(); | |
| int CurrentCost=cost[0]; | |
| int prevCost=cost[0]; | |
| int ans=0; | |
| char ch=s.charAt(0); | |
| if(size<=1){ | |
| return 0; | |
| } | |
| for(int i=1;i<size;i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Definition for singly-linked list. | |
| * public class ListNode { | |
| * int val; | |
| * ListNode next; | |
| * ListNode() {} | |
| * ListNode(int val) { this.val = val; } | |
| * ListNode(int val, ListNode next) { this.val = val; this.next = next; } | |
| * } | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Definition for singly-linked list. | |
| * public class ListNode { | |
| * int val; | |
| * ListNode next; | |
| * ListNode() {} | |
| * ListNode(int val) { this.val = val; } | |
| * ListNode(int val, ListNode next) { this.val = val; this.next = next; } | |
| * } | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public int[][] merge(int[][] intervals) { | |
| List<int[]> result = new ArrayList<>(); | |
| if(intervals==null||intervals.length==0){ | |
| return result.toArray(new int[0][]); | |
| } | |
| Arrays.sort(intervals,Comparator.comparingInt(a->a[0])); | |
| int start=intervals[0][0]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public int firstMissingPositive(int[] nums) { | |
| int n=nums.length; | |
| for (int i=0;i<n;i++){ | |
| if(nums[i]<=0||nums[i]>n){ | |
| nums[i]=n+1; | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public String decodeString(String s) { | |
| Stack<String> result= new Stack(); | |
| Stack<Integer> counts= new Stack(); | |
| String res=""; | |
| int index=0; | |
| while(index <s.length()){ | |
| if(Character.isDigit(s.charAt(index))){ | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public int countCharacters(String[] words, String chars) { | |
| int good_word_sum=0; | |
| int[] char_counts= new int[26]; | |
| for(char c : chars.toCharArray()){ | |
| char_counts[c-'a']++; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public String longestDiverseString(int a, int b, int c) { | |
| int total= a+b+c; | |
| int A=0,B=0,C=0; | |
| StringBuilder sb= new StringBuilder(); | |
| while(total-- >0){ | |
| if((a>=b&&a>=c && A !=2)||(a>0 && (B==2||C==2))){ | |
| sb.append("a"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public boolean isValid(String s) { | |
| if(s.length()%2!=0) return false; | |
| Stack<Character> stack = new Stack(); | |
| for(char c:s.toCharArray()){ | |
| if(c=='(' || c=='{'|| c=='['){ | |
| stack.push(c); | |
| }else if(c==')' && !stack.isEmpty() && stack.peek()=='('){ |
NewerOlder