Skip to content

Commit 68c006a

Browse files
Adding job-sequencing algo
1 parent 0ba0f5d commit 68c006a

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

Go/greedy/greedyAlgos.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package greedy
2+
3+
import "fmt"
4+
5+
func Greedy() {
6+
var choice int
7+
fmt.Println("1. Activity-Selection Algo 2. Fractional Knapsack 3. Job Sequencing")
8+
fmt.Scan(&choice)
9+
switch choice {
10+
case 1:
11+
ActivitSelection()
12+
break
13+
case 2:
14+
Fractional_Knapsack()
15+
break
16+
case 3:
17+
Job_Sequencing()
18+
break
19+
}
20+
}

Go/greedy/job_sequencing.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Given an array of jobs where every job has a deadline and associated profit if the job is finished before the
3+
deadline. It is also given that every job takes the single unit of time, so the minimum possible deadline for
4+
any job is 1.How to maximize total profit if only one job can be scheduled at a time.
5+
*/
6+
7+
package greedy
8+
9+
import (
10+
"fmt"
11+
"sort"
12+
)
13+
14+
type job struct {
15+
id int
16+
deadline int
17+
profit int
18+
}
19+
20+
func Job_Sequencing() {
21+
j := []job{{1, 2, 100}, {2, 1, 19}, {3, 2, 27}, {4, 1, 25}, {5, 3, 15}}
22+
n := len(j)
23+
sequence, profit := jobSequencing(j, n)
24+
fmt.Println("Max Profit is ", profit, " Sequence : ")
25+
for i := 0; i < len(sequence); i++ {
26+
if sequence[i] != 0 {
27+
fmt.Print(sequence[i], "->")
28+
}
29+
}
30+
}
31+
32+
func jobSequencing(jb []job, n int) ([]int, int) {
33+
maxProfit := 0
34+
deadlineMap := make(map[int]int)
35+
maxDeadline := 0
36+
37+
sort.SliceStable(jb, func(secondJob, firstJob int) bool {
38+
return jb[secondJob].profit > jb[firstJob].profit
39+
})
40+
41+
for i := 0; i < n; i++ {
42+
if _, ok := deadlineMap[jb[i].deadline]; !ok {
43+
deadlineMap[jb[i].deadline] = jb[i].id
44+
maxProfit += jb[i].profit
45+
if jb[i].deadline > maxDeadline {
46+
maxDeadline = jb[i].deadline
47+
}
48+
}
49+
}
50+
result := make([]int, maxDeadline+1)
51+
for key, value := range deadlineMap {
52+
result[key] = value
53+
}
54+
return result, maxProfit
55+
}

Go/strings/kmp_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package strings
2+
3+
import "testing"
4+
5+
func TestKMP(t *testing.T) {
6+
type args struct {
7+
haystack string
8+
needle string
9+
}
10+
tests := []struct {
11+
name string
12+
args args
13+
want int
14+
}{
15+
{
16+
name: "test-1",
17+
args: args{
18+
haystack: "aabaaabaaac",
19+
needle: "aabaaac",
20+
},
21+
want: 4,
22+
},
23+
{
24+
name: "test-2",
25+
args: args{
26+
haystack: "onionionions",
27+
needle: "onions",
28+
},
29+
want: 6,
30+
},
31+
}
32+
for _, tt := range tests {
33+
t.Run(tt.name, func(t *testing.T) {
34+
if got := KMP(tt.args.haystack, tt.args.needle); got != tt.want {
35+
t.Errorf("KMP() = %v, want %v", got, tt.want)
36+
}
37+
})
38+
}
39+
}

0 commit comments

Comments
 (0)