diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 842e520..4880f42 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -31,10 +31,16 @@ "containerUser": "vscode", "workspaceMount": "source=${localWorkspaceFolder},target=/workspaces/${localWorkspaceFolderBasename},type=bind,Z", "runArgs": [ - "--userns=keep-id" + "--userns=keep-id", + "--cap-add=SYS_PTRACE", + "--security-opt", + "seccomp=unconfined" ], "features": { - "rust": "latest" + "rust": "latest", + "golang": "latest", + "python": "os-provided", + "node": "lts" } } diff --git a/candy/sol.go b/candy/sol.go new file mode 100644 index 0000000..e74d544 --- /dev/null +++ b/candy/sol.go @@ -0,0 +1,27 @@ +package main + +func candy(ratings []int) int { + count := 1 + for idx := 0; idx < len(ratings)-1; { + for idx < len(ratings)-1 && ratings[idx+1] == ratings[idx] { + count++ + idx++ + } + next_inc := 1 + for idx < len(ratings)-1 && ratings[idx+1] > ratings[idx] { + next_inc++ + idx++ + count += next_inc + } + next_dec := 1 + for idx < len(ratings)-1 && ratings[idx+1] < ratings[idx] { + count += next_dec + next_dec++ + idx++ + } + if next_dec > next_inc { + count += next_dec - next_inc + } + } + return count +} diff --git a/longest-consecutive-sequence/sol.go b/longest-consecutive-sequence/sol.go new file mode 100644 index 0000000..be2f735 --- /dev/null +++ b/longest-consecutive-sequence/sol.go @@ -0,0 +1,30 @@ +package main + +func longestConsecutive(nums []int) (max int) { + exists := make(map[int]bool, len(nums)) + for _, n := range nums { + exists[n] = true + } + done := make(map[int]bool, len(nums)) + for _, n := range nums { + if _, ok := done[n]; !ok { + done[n] = true + l := 1 + for exists[n+1] { + l++ + n++ + done[n] = true + } + n -= l - 1 + for exists[n-1] { + l++ + n-- + done[n] = true + } + if l > max { + max = l + } + } + } + return max +} diff --git a/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/sol.go b/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/sol.go new file mode 100644 index 0000000..556bfee --- /dev/null +++ b/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/sol.go @@ -0,0 +1,27 @@ +package main + +import "sort" + +const M = 1000000007 + +func maxArea(h int, w int, horizontalCuts []int, verticalCuts []int) int { + sort.Ints(horizontalCuts) + sort.Ints(verticalCuts) + + hs, vs := len(horizontalCuts), len(verticalCuts) + maxH, maxW := horizontalCuts[0], verticalCuts[0] + horizontalCuts, verticalCuts = append(horizontalCuts, h), append(verticalCuts, w) + for i := 0; i < hs; i++ { + d := horizontalCuts[i+1] - horizontalCuts[i] + if d > maxH { + maxH = d + } + } + for i := 0; i < vs; i++ { + d := verticalCuts[i+1] - verticalCuts[i] + if d > maxW { + maxW = d + } + } + return (maxW * maxH) % M +} diff --git a/maximum-units-on-a-truck/sol.go b/maximum-units-on-a-truck/sol.go new file mode 100644 index 0000000..7e7ca96 --- /dev/null +++ b/maximum-units-on-a-truck/sol.go @@ -0,0 +1,20 @@ +package main + +import "sort" + +func maximumUnits(boxTypes [][]int, truckSize int) int { + sort.Slice(boxTypes, func(i, j int) bool { + return boxTypes[i][1] > boxTypes[j][1] + }) + units := 0 + count := 0 + for idx := 0; idx < len(boxTypes); idx++ { + units += boxTypes[idx][0] * boxTypes[idx][1] + count += boxTypes[idx][0] + if count >= truckSize { + units -= (count - truckSize) * boxTypes[idx][1] + return units + } + } + return units +} diff --git a/minimum-deletions-to-make-character-frequencies-unique/sol.go b/minimum-deletions-to-make-character-frequencies-unique/sol.go new file mode 100644 index 0000000..3be874c --- /dev/null +++ b/minimum-deletions-to-make-character-frequencies-unique/sol.go @@ -0,0 +1,30 @@ +package main + +import "sort" + +func minDeletions(s string) int { + freq := make([]int, 26) // only lower-case english letters + for _, chr := range s { + freq[chr-'a']++ + } + sort.Ints(freq) + + count, max := 0, len(s) + for idx := len(freq) - 1; idx >= 0 && freq[idx] != 0; idx-- { + f := freq[idx] + if f > max { + count += f - max + f = max + } + if f > 0 { + max = f - 1 + } else { + max = 0 + } + } + return count +} + +func main() { + println(minDeletions("bbcebab")) +} diff --git a/number-of-steps-to-reduce-a-number-to-zero/sol.go b/number-of-steps-to-reduce-a-number-to-zero/sol.go new file mode 100644 index 0000000..4eb72cc --- /dev/null +++ b/number-of-steps-to-reduce-a-number-to-zero/sol.go @@ -0,0 +1,13 @@ +package main + +func numberOfSteps(num int) (steps int) { + for num != 1 { + if num%2 == 0 { + num /= 2 + } else { + num-- + } + steps++ + } + return steps +} diff --git a/partitioning-into-minimum-number-of-deci-binary-numbers/sol.rs b/partitioning-into-minimum-number-of-deci-binary-numbers/sol.rs new file mode 100644 index 0000000..5f4e6f4 --- /dev/null +++ b/partitioning-into-minimum-number-of-deci-binary-numbers/sol.rs @@ -0,0 +1,7 @@ +impl Solution { + pub fn min_partitions(n: String) -> i32 { + (n.bytes() + .max() + .unwrap() - b'0') as i32 + } +} \ No newline at end of file diff --git a/queue-reconstruction-by-height/sol.go b/queue-reconstruction-by-height/sol.go new file mode 100644 index 0000000..48c42c3 --- /dev/null +++ b/queue-reconstruction-by-height/sol.go @@ -0,0 +1,15 @@ +package main + +import "sort" + +func reconstructQueue(people [][]int) [][]int { + sort.Slice(people, func(i, j int) bool { + return people[i][0] > people[j][0] || (people[i][0] == people[j][0] && + people[i][1] < people[j][1]) + }) + for idx, p := range people { + copy(people[p[1]+1:idx+1], people[p[1]:idx+1]) + people[p[1]] = p + } + return people +} diff --git a/wiggle-subsequence/sol.go b/wiggle-subsequence/sol.go new file mode 100644 index 0000000..47ed465 --- /dev/null +++ b/wiggle-subsequence/sol.go @@ -0,0 +1,17 @@ +package main + +func wiggleMaxLength(nums []int) int { + // Current's value only matters in terms of negative / 0 / positive. + // The numerical value doesn't get used and therefore can be messed with. + current, count := 0, len(nums) + for i := 0; i < len(nums)-1; i++ { + new := nums[i+1] - nums[i] + if (current > 0 && new > 0) || (current < 0 && new < 0) || new == 0 { + count-- + } + if new != 0 { + current = new + } + } + return count +}