Skip to content

Commit be57b6c

Browse files
committed
Solved 4 problems
1 parent ef6bcea commit be57b6c

4 files changed

+134
-0
lines changed

Medium/My Calendar II.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class MyCalendarTwo {
2+
private List<int[]> books = new ArrayList<>();
3+
4+
public boolean book(int s, int e) {
5+
6+
MyCalendar overlaps = new MyCalendar();
7+
for (int[] b : books)
8+
if (Math.max(b[0], s) < Math.min(b[1], e))
9+
if (!overlaps.book(Math.max(b[0], s), Math.min(b[1], e))) return false;
10+
books.add(new int[]{ s, e });
11+
return true;
12+
}
13+
14+
private static class MyCalendar {
15+
List<int[]> books = new ArrayList<>();
16+
public boolean book(int start, int end) {
17+
for (int[] b : books)
18+
if (Math.max(b[0], start) < Math.min(b[1], end)) return false;
19+
books.add(new int[]{ start, end });
20+
return true;
21+
}
22+
}
23+
}
24+
25+
/**
26+
* Your MyCalendarTwo object will be instantiated and called as such:
27+
* MyCalendarTwo obj = new MyCalendarTwo();
28+
* boolean param_1 = obj.book(start,end);
29+
*/

Medium/Self Dividing Numbers.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public List<Integer> selfDividingNumbers(int left, int right) {
3+
ArrayList<Integer> arr = new ArrayList<>();
4+
5+
while(left <= right) {
6+
if(selfDivide(left)) {
7+
arr.add(left);
8+
}
9+
left++;
10+
}
11+
12+
return arr;
13+
}
14+
15+
private boolean selfDivide(int n) {
16+
int copy = n;
17+
while(n>0) {
18+
int rem = n%10;
19+
if (rem == 0) return false;
20+
if(copy%rem != 0) {
21+
return false;
22+
}
23+
n /= 10;
24+
}
25+
26+
return true;
27+
}
28+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
10+
class Solution {
11+
public ListNode[] splitListToParts(ListNode root, int k) {
12+
int m = findLength(root);
13+
ListNode temp = root;
14+
15+
ListNode[] res = new ListNode[k];
16+
int size=m/k;
17+
int l= m%k;
18+
19+
for(int i=0;i<k;i++){
20+
ListNode ans= temp;
21+
int j=size;
22+
while(temp!=null && j>1){
23+
temp=temp.next;j--;
24+
}
25+
if(size!=0 && l>0 && temp!=null){ temp=temp.next; l--;}
26+
ListNode temp1=temp;
27+
if(temp!=null) temp=temp.next;
28+
if(temp1!=null) temp1.next=null;
29+
res[i]=ans;
30+
}
31+
return res;
32+
}
33+
34+
private int findLength(ListNode root) {
35+
if (root == null) return 0;
36+
int l = 0;
37+
while(root != null) {
38+
l++;
39+
root = root.next;
40+
}
41+
42+
return l;
43+
}
44+
}

Medium/Swap Nodes in Pair.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode swapPairs(ListNode head) {
11+
if(head == null || head.next == null) return head;
12+
13+
ListNode temp = head.next;
14+
ListNode prev = null;
15+
16+
while(head != null && head.next != null) {
17+
18+
if(prev != null) {
19+
prev.next = head.next;
20+
}
21+
22+
ListNode temp1 = head.next;
23+
ListNode temp2 = head;
24+
head.next = head.next.next;
25+
head = temp1;
26+
head.next = temp2;
27+
prev = head.next;
28+
head = head.next.next;
29+
}
30+
31+
return temp;
32+
}
33+
}

0 commit comments

Comments
 (0)