Skip to content

"Added new Problem" #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>java-algo-n-ds</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
4 changes: 4 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
2 changes: 2 additions & 0 deletions .settings/org.eclipse.jdt.apt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=false
9 changes: 9 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.7
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Binary file not shown.
204 changes: 204 additions & 0 deletions src/main/java/com/my/ic/ds/MergeMeetings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package com.my.ic.ds;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class MergeMeetings {
public static class Meeting {

private int startTime;
private int endTime;

public Meeting(int startTime, int endTime) {
// number of 30 min blocks past 9:00 am
this.startTime = startTime;
this.endTime = endTime;
}

public int getStartTime() {
return startTime;
}

public void setStartTime(int startTime) {
this.startTime = startTime;
}

public int getEndTime() {
return endTime;
}

public void setEndTime(int endTime) {
this.endTime = endTime;
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof Meeting)) {
return false;
}
final Meeting meeting = (Meeting) o;
return startTime == meeting.startTime && endTime == meeting.endTime;
}

@Override
public int hashCode() {
int result = 17;
result = result * 31 + startTime;
result = result * 31 + endTime;
return result;
}

@Override
public String toString() {
return String.format("(%d, %d)", startTime, endTime);
}
}

public static List<Meeting> mergeRanges(List<Meeting> meetings) {

// merge meetings ranges
List<Meeting> sortedMeetings = new ArrayList<>();
for(Meeting meeting:meetings){
Meeting meetingCopy=new Meeting(meeting.getStartTime(),meeting.getEndTime());
sortedMeetings.add(meetingCopy);
}

Collections.sort(sortedMeetings,new Comparator<Meeting>(){
@Override
public int compare(Meeting m1 , Meeting m2){
return m1.getStartTime()- m2.getStartTime();
}
});
List<Meeting> mergedMeetings = new ArrayList<>();
mergedMeetings.add(sortedMeetings.get(0));

for(Meeting currentMeeting : sortedMeetings){
Meeting lastMergedMeeting = mergedMeetings.get(mergedMeetings.size()-1);
if(currentMeeting.getStartTime() <= lastMergedMeeting.getEndTime()){
lastMergedMeeting.setEndTime(Math.max(lastMergedMeeting.getEndTime(),currentMeeting.getEndTime()));
}else{
mergedMeetings.add(currentMeeting);
}
}
return mergedMeetings;
}


















// tests

public void meetingsOverlapTest() {
final List<Meeting> meetings = Arrays.asList(new Meeting(1, 3), new Meeting(2, 4));
final List<Meeting> actual = mergeRanges(meetings);
final List<Meeting> expected = Arrays.asList(new Meeting(1, 4));
System.out.println("meetingsOverlapTest::expected:"+expected);
System.out.println("meetingsOverlapTest::actual:"+actual);
}

public void meetingsTouchTest() {
final List<Meeting> meetings = Arrays.asList(new Meeting(5, 6), new Meeting(6, 8));
final List<Meeting> actual = mergeRanges(meetings);
final List<Meeting> expected = Arrays.asList(new Meeting(5, 8));
System.out.println("meetingsTouchTest::expected:"+expected);
System.out.println("meetingsTouchTest::actual:"+actual);
}

public void meetingContainsOtherMeetingTest() {
final List<Meeting> meetings = Arrays.asList(new Meeting(1, 8), new Meeting(2, 5));
final List<Meeting> actual = mergeRanges(meetings);
final List<Meeting> expected = Arrays.asList(new Meeting(1, 8));
System.out.println("meetingContainsOtherMeetingTest::expected:"+expected);
System.out.println("meetingContainsOtherMeetingTest::actual:"+actual);
}

public void meetingsStaySeparateTest() {
final List<Meeting> meetings = Arrays.asList(new Meeting(1, 3), new Meeting(4, 8));
final List<Meeting> actual = mergeRanges(meetings);
final List<Meeting> expected = Arrays.asList(
new Meeting(1, 3), new Meeting(4, 8)
);
System.out.println("meetingsStaySeparateTest::expected:"+expected);
System.out.println("meetingsStaySeparateTest::actual:"+actual);
}

public void multipleMergedMeetingsTest() {
final List<Meeting> meetings = Arrays.asList(
new Meeting(1, 4), new Meeting(2, 5), new Meeting (5, 8));
final List<Meeting> actual = mergeRanges(meetings);
final List<Meeting> expected = Arrays.asList(new Meeting(1, 8));
System.out.println("multipleMergedMeetingsTest::expected:"+expected);
System.out.println("multipleMergedMeetingsTest::actual:"+actual);
}

public void meetingsNotSortedTest() {
final List<Meeting> meetings = Arrays.asList(
new Meeting(5, 8), new Meeting(1, 4), new Meeting(6, 8));
final List<Meeting> actual = mergeRanges(meetings);
final List<Meeting> expected = Arrays.asList(
new Meeting(1, 4), new Meeting(5, 8)
);
System.out.println("meetingsNotSortedTest::expected:"+expected);
System.out.println("meetingsNotSortedTest::actual:"+actual);
}

public void oneLongMeetingContainsSmallerMeetingsTest() {
final List<Meeting> meetings = Arrays.asList(
new Meeting(1, 10), new Meeting(2, 5), new Meeting(6, 8),
new Meeting(9, 10), new Meeting(10, 12)
);
final List<Meeting> actual = mergeRanges(meetings);
final List<Meeting> expected = Arrays.asList(
new Meeting(1, 12)
);
System.out.println("oneLongMeetingContainsSmallerMeetingsTest::expected:"+expected);
System.out.println("oneLongMeetingContainsSmallerMeetingsTest::actual:"+actual);
}

public void sampleInputTest() {
final List<Meeting> meetings = Arrays.asList(
new Meeting(0, 1), new Meeting(3, 5), new Meeting(4, 8),
new Meeting(10, 12), new Meeting(9, 10)
);
final List<Meeting> actual = mergeRanges(meetings);
final List<Meeting> expected = Arrays.asList(
new Meeting(0, 1), new Meeting(3, 8), new Meeting(9, 12)
);
System.out.println("sampleInputTest::expected:"+expected);
System.out.println("sampleInputTest::actual:"+actual);
}

public static void main(String[] args) {
MergeMeetings mm=new MergeMeetings();
mm.meetingsOverlapTest();
mm.meetingsTouchTest();
mm.meetingContainsOtherMeetingTest();
mm.meetingsStaySeparateTest();
mm.multipleMergedMeetingsTest();
mm.meetingsNotSortedTest();
mm.oneLongMeetingContainsSmallerMeetingsTest();
mm.sampleInputTest();
}
}
66 changes: 66 additions & 0 deletions src/main/java/com/my/leetcode/AddTwoNumbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.my.leetcode;
/*
You are given two non-empty linked lists representing two non-negative integers.
The digits are stored in reverse order, and each of their nodes contains a single digit.
Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
*/
public class AddTwoNumbers {

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode p = l1, q = l2, curr = dummyHead;
int carry = 0;
while (p != null || q != null) {
int x = (p != null) ? p.val : 0;
int y = (q != null) ? q.val : 0;
int sum = carry + x + y;
carry = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if (p != null) p = p.next;
if (q != null) q = q.next;
}
if (carry > 0) {
curr.next = new ListNode(carry);
}
return dummyHead.next;
}

public static void main(String args[]) {
AddTwoNumbers atn=new AddTwoNumbers();
int[] list1={9,9,9,9,9,9,9};
int[] list2= {9,9,9,9};
ListNode l1=new ListNode(list1[0]);
ListNode l2=new ListNode(list2[0]);
ListNode p = l1, q = l2;
for(int i=1;i<list1.length;i++) {
ListNode nextNode=new ListNode(list1[i]);
l1.next=nextNode;
l1=l1.next;
}
for(int i=1;i<list2.length;i++) {
ListNode nextNode=new ListNode(list2[i]);
l2.next=nextNode;
l2=l2.next;
}
ListNode ln=atn.addTwoNumbers(p, q);
ListNode head=ln;
while(ln!=null){
System.out.print((ln==head)?"["+ln.val:ln.val);
System.out.print((ln.next!=null)?",":"]");
ln=ln.next;
}
}



}

class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
8 changes: 8 additions & 0 deletions target/classes/com/my/common/notes/bigo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# What is Big-O notation
### Big-O is a way to describe how long an algorithm or a program take to execute. In a nutshell It describe the performance or complexity of an algorithm/program.

#### * It's like math calculation but instead of focusing on exact result it gives an estimate and focus on what's basically happening.

#### * In computer Science big O notation is used to describe the execution time or space required for an algorithm/program with diffrent input data set. It gives estimate about performance when input gets arbitrarily large.

Either the time of Study or after starting enginnering job Big-O was _a tough row to hoe_ for me.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Arrays and Strings
Binary file not shown.
Binary file not shown.
Loading