Roman to Integer
/*
#--------------------------------------------------#
# #
# version 0.0.1 #
# https://leetcode.com/problems/roman-to-integer #
# #
# Aleksiej Ostrowski, 2022 #
# #
# https://aleksiej.com #
# #
#--------------------------------------------------#
*/
function romanToInt(s: string): number {
if (s.length == 0) {
return 0;
}
let res = 0;
let old_level = 1;
let max_level = -1;
//for (const el of s.split("").reverse()) {
for (let i = s.length - 1; i >= 0; i--) {
// console.log(el)
switch (s[i]) {
case "I": {
res = max_level > 1 ? res - 1 : res + 1;
old_level = 1;
break;
}
case "V": {
res = max_level > 2 ? res - 5 : res + 5;
old_level = 2;
break;
}
case "X": {
res = max_level > 3 ? res - 10 : res + 10;
old_level = 3;
break;
}
case "L": {
res = max_level > 4 ? res - 50 : res + 50;
old_level = 4;
break;
}
case "C": {
res = max_level > 5 ? res - 100 : res + 100;
old_level = 5;
break;
}
case "D": {
res = max_level > 6 ? res - 500 : res + 500;
old_level = 6;
break;
}
case "M": {
res += 1000;
old_level = 7;
break;
}
}
max_level = Math.max(max_level, old_level);
}
return res;
}
console.log(romanToInt("MCMXCIV"));
Fancy Sequence
/*
#-------------------------------------------------------------#
# #
# version 0.0.2 #
# https://leetcode.com/problems/fancy-sequence/ #
# #
# Aleksiej Ostrowski, 2021 #
# #
# https://aleksiej.com #
# #
#-------------------------------------------------------------#
*/
package main
import (
"fmt"
"os"
)
const crt = 84467440737095516 // max(uint64) = 8446744073709551615
const md = 1000000007
type Oper struct {
o int8
v int8
h int32
}
type Fancy struct {
arr []int8
opr []Oper
cas map[int]int
}
func Constructor() Fancy {
return Fancy{}
}
func (this *Fancy) Append(val int) {
this.arr = append(this.arr, int8(val))
}
func printList(l Fancy) {
for _, v := range l.arr {
fmt.Print(v, " ")
}
fmt.Println()
}
func (this *Fancy) AddAll(inc int) {
this.opr = append(this.opr, Oper{-2, int8(inc), int32(len(this.arr))})
this.cas = make(map[int]int)
/*
for k := range this.cas {
delete(this.cas, k)
}
*/
}
func (this *Fancy) MultAll(m int) {
this.opr = append(this.opr, Oper{-1, int8(m), int32(len(this.arr))})
this.cas = make(map[int]int)
/*
for k := range this.cas {
delete(this.cas, k)
}
*/
}
func (this *Fancy) GetIndex(idx int) int {
if idx < 0 || idx >= len(this.arr) {
return -1
}
if val, ok := this.cas[idx]; ok {
return val
}
var vv uint64
vv = uint64(this.arr[idx])
for _, v := range this.opr {
if idx >= int(v.h) {
continue
}
switch os := v.o; os {
case -2:
vv += uint64(v.v)
case -1:
vv *= uint64(v.v)
}
if vv > crt {
vv = vv % md
}
}
if vv > md {
vv = vv % md
}
if this.cas == nil {
this.cas = make(map[int]int)
}
this.cas[idx] = int(vv)
return int(vv)
}
func main () {
obj := Constructor()
obj.Append(2)
obj.Append(4)
obj.Append(1)
obj.Append(6)
printList(obj)
obj.AddAll(9)
obj.AddAll(1)
obj.MultAll(2)
fmt.Println(obj.GetIndex(0))
os.Exit(1)
obj.MultAll(9)
obj.AddAll(2)
fmt.Println(obj.GetIndex(3))
obj.Append(7)
obj.Append(5)
obj.Append(3)
obj.AddAll(4)
obj.MultAll(5)
obj.AddAll(4)
fmt.Println(obj.GetIndex(1))
}
Median of Two Sorted Arrays
/*
#-------------------------------------------------------------#
# #
# version 0.0.1 #
# https://leetcode.com/problems/median-of-two-sorted-arrays/ #
# #
# Aleksiej Ostrowski, 2020 #
# #
# https://aleksiej.com #
# #
#-------------------------------------------------------------#
*/
package main
import (
"fmt"
)
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
i1 := 0
i2 := 0
var x1 []int
var x2 []int
x1 = nums1
x2 = nums2
switch {
case (len(nums1) == 0) && (len(nums2) > 0):
x1 = nums2
x2 = nums1
case (len(nums1) > 0) && (len(nums2) > 0):
if nums1[i1] > nums2[i2] {
x1 = nums2
x2 = nums1
}
}
r := make([]int, len(x1) + len(x2))
ir := 0
for {
if i1 >= len(x1) {
break
}
e1 := x1[i1]
r[ir] = e1
ir += 1
e3 := e1
flag := true
if i1 + 1 < len(x1) {
e3 = x1[i1 + 1]
flag = false
}
for {
if i2 >= len(x2) {
break
}
e2 := x2[i2]
if (e2 >= e1) && ( (e2 <= e3) || flag) {
r[ir] = e2
ir += 1
i2 += 1
} else {
break;
}
}
i1 += 1
}
ifz := len(r) % 2 == 0
fmt.Println(r)
ind := len(r) / 2
fmt.Println("ind = ", ind, "el = ", r[ind])
if ifz {
return (float64(r[ind - 1]) + float64(r[ind])) * 0.5
} else {
return float64(r[ind])
}
}
func main() {
a := []int{1,34,56,100,200, 201, 500}
b := []int{250}
// a := []int{1,2,3,4,5}
// b := []int{}
fmt.Println("a = ", a)
fmt.Println("b = ", b)
fmt.Println(findMedianSortedArrays(a, b))
}
Two Sum
#-----------------------------------------#
# #
# version 0.0.1 #
# https://leetcode.com/problems/two-sum/ #
# #
# Aleksiej Ostrowski, 2020 #
# #
# https://aleksiej.com #
# #
#-----------------------------------------#
from itertools import combinations
class Solution(object):
def __init__(self, nums, target):
self.nums = nums
self.target = target
def twoSum(self):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
new_nums = sorted(list(enumerate(self.nums)), key = lambda x: x[1])
for x in combinations(range(len(new_nums)), 2):
if new_nums[x[0]][1] + new_nums[x[1]][1] == self.target:
return list((new_nums[x[0]][0], new_nums[x[1]][0]))
'''
nums = [0,4,3,0]
target = 0
nums = [2, 20, 100, 7, 11, 15]
target = 9
'''
nums = [-3,4,3,90]
target = 0
print(nums)
print(target)
sol = Solution(nums, target)
print(sol.twoSum())
Add Two Numbers
/*
#---------------------------------------------------#
# #
# version 0.0.1 #
# https://leetcode.com/problems/add-two-numbers/ #
# #
# Aleksiej Ostrowski, 2020 #
# #
# https://aleksiej.com #
# #
#---------------------------------------------------#
*/
package main
import (
"fmt"
)
type ListNode struct {
Val int
Next *ListNode
}
/*
func reverseList(l *ListNode) *ListNode {
temp := []int{}
x := l
for {
if x == nil {
break
}
temp = append(temp, x.Val)
x = x.Next
}
// https://stackoverflow.com/questions/19239449/how-do-i-reverse-an-array-in-go
for i, j := 0, len(temp)-1; i < j; i, j = i+1, j-1 {
temp[i], temp[j] = temp[j], temp[i]
}
var newl = &ListNode{0, nil}
var start = newl
for i, e := range temp {
newl.Val = e
if i != len(temp)-1 {
newl.Next = &ListNode{0, nil}
newl = newl.Next
}
}
return start
}
*/
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
if (l1 == nil) || (l2 == nil) {
return nil
}
x1 := l1
x2 := l2
var newL = &ListNode{0, nil}
var start = newL
flag1 := true
flag2 := true
extra := 0
for {
newVal := extra
if flag1 {
newVal += x1.Val
}
if flag2 {
newVal += x2.Val
}
extra = 0
if newVal > 9 {
extra = 1
newVal -= 10
}
newL.Val = newVal
if flag1 {
x1 = x1.Next
flag1 = x1 != nil
}
if flag2 {
x2 = x2.Next
flag2 = x2 != nil
}
if flag1 || flag2 || (extra > 0) {
newL.Next = &ListNode{0, nil}
newL = newL.Next
} else {
break
}
}
return start
}
func ListCreate(a []int) *ListNode {
var l = &ListNode{0, nil}
var start = l
for i, s := range a {
l.Val = s
if i != len(a)-1 {
l.Next = &ListNode{0, nil}
l = l.Next
}
}
return start
}
func printList(l *ListNode) {
x := l
for {
if x == nil {
break
}
fmt.Print(x.Val, " ")
x = x.Next
}
fmt.Println()
}
func main() {
a := []int{2, 4, 3}
b := []int{5, 6, 4}
l1 := ListCreate(a)
l2 := ListCreate(b)
printList(l1)
printList(l2)
printList(addTwoNumbers(l1, l2))
// 7 0 8
}