Skip to content

Commit 713c752

Browse files
author
Prasannjit Kumar
committed
upload rest of the codes
1 parent faf85ad commit 713c752

File tree

51 files changed

+1759
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1759
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func getArea(h, max int, values []int) int {
2+
for i, v := range values{
3+
height := h
4+
if v < h{
5+
height = v
6+
}
7+
area := i*height
8+
if area > max{
9+
max = area
10+
}
11+
}
12+
return max
13+
}
14+
func maxArea(height []int) int {
15+
max := 0
16+
for i := 0; i < len(height); i++{
17+
max = getArea(height[i], max, height[i:])
18+
}
19+
return max
20+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
func firstMissingPositive(nums []int) int {
2+
if len(nums) == 0{
3+
return 1
4+
}
5+
m := make(map[int]bool)
6+
max := nums[0]
7+
for _, v := range nums{
8+
m[v] = true
9+
if max < v && v > 0 {
10+
max = v
11+
}
12+
}
13+
if max <= 0{
14+
return 1
15+
}
16+
if max == 1{
17+
return 2
18+
}
19+
i := 1
20+
for ; i < max; i++ {
21+
if !m[i] {
22+
return i
23+
}
24+
}
25+
if i == 1 {
26+
return 1
27+
} else {
28+
return i + 1
29+
}
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func countSegments(s string) int {
2+
strs := strings.Split(s, " ")
3+
count := 0
4+
for _, v := range strs{
5+
if v != ""{
6+
count++
7+
}
8+
}
9+
return count
10+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import "fmt"
2+
type MyPoint struct {
3+
x, y int
4+
}
5+
6+
func createPoints(l, n int) ([]MyPoint, int) {
7+
count := 0
8+
var points []MyPoint
9+
j := 0
10+
i := 0
11+
for count < l {
12+
for i = 0; i < n; i++ {
13+
var p MyPoint
14+
p.x = i
15+
p.y = j
16+
points = append(points, p)
17+
count++
18+
if count == l {
19+
return points, j
20+
}
21+
}
22+
j++
23+
for i = n - 2; i > 0; i-- {
24+
var p MyPoint
25+
p.x = i
26+
p.y = j
27+
j++
28+
points = append(points, p)
29+
count++
30+
if count == l {
31+
return points, j
32+
}
33+
}
34+
}
35+
return points, j
36+
}
37+
38+
func convert(s string, numRows int) string {
39+
points, col := createPoints(len(s), numRows)
40+
mat := make([][]rune, numRows)
41+
for i := range mat{
42+
mat[i] = make([]rune, col+1)
43+
}
44+
runes := []rune(s)
45+
for i := 0; i < len(mat); i++{
46+
for j := 0; j < len(mat[i]); j++{
47+
mat[i][j] = '0'
48+
}
49+
}
50+
for i := 0; i < len(points); i++{
51+
mat[points[i].x][points[i].y] = runes[i]
52+
}
53+
str := ""
54+
for i := 0; i < len(mat); i++{
55+
for j := 0; j < len(mat[i]); j++{
56+
if mat[i][j] != '0' {
57+
str += string(mat[i][j])
58+
}
59+
}
60+
}
61+
return str
62+
}

go/700.search-in-a-binary-search-tree.167914973.ac.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,49 @@
1+
/*
2+
* @lc app=leetcode id=700 lang=golang
3+
*
4+
* [700] Search in a Binary Search Tree
5+
*
6+
* https://leetcode.com/problems/search-in-a-binary-search-tree/description/
7+
*
8+
* algorithms
9+
* Easy (63.18%)
10+
* Total Accepted: 24.2K
11+
* Total Submissions: 38.3K
12+
* Testcase Example: '[4,2,7,1,3]\n2'
13+
*
14+
* Given the root node of a binary search tree (BST) and a value. You need to
15+
* find the node in the BST that the node's value equals the given value.
16+
* Return the subtree rooted with that node. If such node doesn't exist, you
17+
* should return NULL.
18+
*
19+
* For example, 
20+
*
21+
*
22+
* Given the tree:
23+
* ⁠ 4
24+
* ⁠ / \
25+
* ⁠ 2 7
26+
* ⁠ / \
27+
* ⁠ 1 3
28+
*
29+
* And the value to search: 2
30+
*
31+
*
32+
* You should return this subtree:
33+
*
34+
*
35+
* ⁠ 2
36+
* ⁠ / \
37+
* ⁠ 1 3
38+
*
39+
*
40+
* In the example above, if we want to search the value 5, since there is no
41+
* node with value 5, we should return NULL.
42+
*
43+
* Note that an empty tree is represented by NULL, therefore you would see the
44+
* expected output (serialized tree format) as [], not null.
45+
*
46+
*/
147
/**
248
* Definition for a binary tree node.
349
* type TreeNode struct {

go/705.design-hashset.184751839.ac.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
type MyHashSet struct {
2+
b []bool
3+
}
4+
5+
6+
/** Initialize your data structure here. */
7+
func Constructor() MyHashSet {
8+
b := make([]bool, 1000001)
9+
return MyHashSet{b}
10+
}
11+
12+
13+
func (this *MyHashSet) Add(key int) {
14+
this.b[key] = true
15+
}
16+
17+
18+
func (this *MyHashSet) Remove(key int) {
19+
this.b[key] = false
20+
}
21+
22+
23+
/** Returns true if this set contains the specified element */
24+
func (this *MyHashSet) Contains(key int) bool {
25+
return this.b[key]
26+
}
27+
28+
29+
/**
30+
* Your MyHashSet object will be instantiated and called as such:
31+
* obj := Constructor();
32+
* obj.Add(key);
33+
* obj.Remove(key);
34+
* param_3 := obj.Contains(key);
35+
*/
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* @lc app=leetcode id=896 lang=golang
3+
*
4+
* [896] Monotonic Array
5+
*
6+
* https://leetcode.com/problems/monotonic-array/description/
7+
*
8+
* algorithms
9+
* Easy (54.11%)
10+
* Total Accepted: 20.2K
11+
* Total Submissions: 37.4K
12+
* Testcase Example: '[1,2,2,3]'
13+
*
14+
* An array is monotonic if it is either monotone increasing or monotone
15+
* decreasing.
16+
*
17+
* An array A is monotone increasing if for all i <= j, A[i] <= A[j].  An array
18+
* A is monotone decreasing if for all i <= j, A[i] >= A[j].
19+
*
20+
* Return true if and only if the given array A is monotonic.
21+
*
22+
*
23+
*
24+
*
25+
*
26+
*
27+
*
28+
* Example 1:
29+
*
30+
*
31+
* Input: [1,2,2,3]
32+
* Output: true
33+
*
34+
*
35+
*
36+
* Example 2:
37+
*
38+
*
39+
* Input: [6,5,4,4]
40+
* Output: true
41+
*
42+
*
43+
*
44+
* Example 3:
45+
*
46+
*
47+
* Input: [1,3,2]
48+
* Output: false
49+
*
50+
*
51+
*
52+
* Example 4:
53+
*
54+
*
55+
* Input: [1,2,4,5]
56+
* Output: true
57+
*
58+
*
59+
*
60+
* Example 5:
61+
*
62+
*
63+
* Input: [1,1,1]
64+
* Output: true
65+
*
66+
*
67+
*
68+
*
69+
* Note:
70+
*
71+
*
72+
* 1 <= A.length <= 50000
73+
* -100000 <= A[i] <= 100000
74+
*
75+
*
76+
*
77+
*
78+
*
79+
*
80+
*
81+
*/
82+
func isMonotonic(A []int) bool {
83+
if len(A) < 2 {
84+
return true
85+
}
86+
count1 := 0
87+
for i := 1; i < len(A); i++{
88+
if A[i-1] <= A[i] {
89+
count1++
90+
}
91+
}
92+
count2 := 0
93+
for i := 1; i < len(A); i++{
94+
if A[i-1] >= A[i] {
95+
count2++
96+
}
97+
}
98+
if count1 == len(A)-1 || count2 == len(A)-1 {
99+
return true
100+
}
101+
return false
102+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
func getCounts(s string) (string, []int) {
2+
runes := []rune(s)
3+
str := ""
4+
var values []int
5+
for i := 0; i < len(runes); {
6+
c := runes[i]
7+
str += string(c)
8+
count := 1
9+
j := i+1
10+
for ; j < len(runes); j++{
11+
if c != runes[j] {
12+
break
13+
}
14+
count++
15+
}
16+
i = j
17+
values = append(values, count)
18+
}
19+
return str, values
20+
}
21+
func isLongPressedName(name string, typed string) bool {
22+
v1, v2 := getCounts(name)
23+
v3, v4 := getCounts(typed)
24+
if v1 != v3 {
25+
return false
26+
}
27+
for i := 0; i < len(v2); i++{
28+
if v2[i] > v4[i]{
29+
return false
30+
}
31+
}
32+
return true
33+
}

0 commit comments

Comments
 (0)