Time out on Leetcode 160 Intersection of Two Linked Lists - c++

I am trying to solve Leetcode 160. Intersection of Two Linked Lists:
Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.
For example, the following two linked lists begin to intersect at node c1:
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
Note that the linked lists must retain their original structure after the function returns.
I wanted to use the two pointers to solve it, but the Leet Code site gives me a Time Limit Exceeded error.
My code is shown below:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *tempA = headA, *tempB = headB;
while (tempA != tempB) {
if (tempA->next != NULL) { tempA = tempA->next;}
else { tempA->next = headB;}
if (tempB->next != NULL) { tempB = tempB->next;}
else { tempB->next = headA; }
}
return tempA;
}
I then tried this existing solution and it works, but I cannot tell what's different:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *tempA = headA, *tempB = headB;
while (tempA != tempB) {
if (tempA != NULL) { tempA = tempA->next;}
else { tempA = headB;}
if (tempB != NULL) { tempB = tempB->next;}
else { tempB = headA;}
}
return tempA;
}

The algorithm should be a "read-only" algorithm, i.e. there should be no reason to modify the linked lists to determine the solution.
Yet, that is what the first version of the code does: it assigns a new reference to a node's next property. This should never happen: there is no good reason why a node's next property would need to get a new value when the only thing you want to do is "calculate" where a certain node is located and return it.
In this particular case tempA->next = headB; is linking the end of linked list A with the start of linked list B. To picture this, let's imagine the two lists are initially like this:
tempA
↓
┌────────────┐
headA:─► │ data: 2 │
│ next: ───────┐
└────────────┘ │ ┌────────────┐
└──► │ data: 3 │
┌──► │ next: NULL │
┌────────────┐ ┌────────────┐ │ └────────────┘
headB:─► │ data: 4 │ │ data: 5 │ │
│ next: ──────────► │ next: ───────┘
└────────────┘ └────────────┘
↑
tempB
The loop will in the first iterations always execute the if cases, and so after one iteration tempA and tempB will reference the next nodes:
┌────────────┐
headA:─► │ data: 2 │ tempA
│ next: ───────┐ ↓
└────────────┘ │ ┌────────────┐
└──► │ data: 3 │
┌──► │ next: NULL │
┌────────────┐ ┌────────────┐ │ └────────────┘
headB:─► │ data: 4 │ │ data: 5 │ │
│ next: ──────────► │ next: ───────┘
└────────────┘ └────────────┘
↑
tempB
And now, in the second iteration, the first else block will execute tempA->next = headB; which alters that tail node's next property, bringing about the following situation:
┌────────────┐
headA:─► │ data: 2 │ tempA
│ next: ───────┐ ↓
└────────────┘ │ ┌────────────┐
└──► │ data: 3 │
┌──► │ next: ────────┐
┌────────────┐ ┌────────────┐ │ └────────────┘ │
headB:─► │ data: 4 │ │ data: 5 │ │ │
┌─► │ next: ──────────► │ next: ───────┘ │
│ └────────────┘ └────────────┘ │
└───────────────────────────────────────────────────────────┘
↑
tempB
This is introducing a cycle in the linked list: there is now no more tail node -- there is no node anymore whose next property is NULL. As you can see that means the while condition will from now on always be true,... this has become an infinite loop. This explains why the platform that runs this code is signaling a "time limit exceeded" error.

Related

Is it possible to subtract a list column?

I want to operate this expression with data.
But it gives the error that List(float64) type doesn't have the operation.
I guess the list type isn't implemented element-wise operations.
(col("vec").last() - col("vec")).abs().sum()
vec
---------
list[f64]
============================
0: [-0.000000, -1.11111, ..., ]
1: [-2.222222, 3.33333, ..., ]
...
n: [ 8.888888, -9.99999, ..., ]
Then if I want to subtract each row with the last row, what is the best way to do it?
The below is what i want to do:
0: sum(abs([ 8.888888, -9.99999, ..., ] - [-0.000000, -1.11111, ..., ]))
1: sum(abs([ 8.888888, -9.99999, ..., ] - [-2.222222, 3.33333, ..., ]))
...
n: sum(abs([ 8.888888, -9.99999, ..., ] - [ 8.888888, -9.99999, ..., ]))
I was able to finagle an answer by converting the lists to structs, unnesting said structs to get the equivalent data in a (vec, len(vec)) shaped DataFrame of floats, then doing standard operations from there and converting back to list:
>>> x = pl.DataFrame(data={'vec' : [[0., -1-1/9], [-2-2/9, 3+3/9], [8+8/9, -10.]]})
>>> (x
.select(dummy=pl.col('vec').arr.to_struct())
.unnest('dummy')
.select(vec=pl.concat_list(abs(pl.all().last() - pl.all()))
)
shape: (3, 1)
┌────────────────────────┐
│ vec │
│ --- │
│ list[f64] │
╞════════════════════════╡
│ [8.888889, 8.888889] │
│ [11.111111, 13.333333] │
│ [0.0, 0.0] │
└────────────────────────┘
This code was tested on Rust v1.67 for polars in v0.27.2.
Add these features in Cargo.toml:
[dependencies]
polars = { version = "*", features = [ "lazy", "lazy_regex", "list_eval" ] }
color-eyre = "*"
The main function:
use color_eyre::Result;
use polars::prelude::*;
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
let row1 = vec![0.0, -1.0 - 1.0 / 9.0, 1.77];
let row2 = vec![-2.0 - 2.0 / 9.0, 3.0 + 3.0 / 9.0, 2.93];
let row3 = vec![3.0 + 3.0 / 9.0, -4.0 - 1.0 / 9.0, 3.56];
let row4 = vec![8.0 + 8.0 / 9.0, -10.0, 7.26];
let series1: Series = Series::new("a", &row1);
let series2: Series = Series::new("b", &row2);
let series3: Series = Series::new("c", &row3);
let series4: Series = Series::new("d", &row4);
let list = Series::new("vec", &[series1, series2, series3, series4]);
let df: DataFrame = DataFrame::new(vec![list])?;
println!("df:\n{df}\n");
let mut lazyframe = df.lazy();
let mut new_columns: Vec<String> = Vec::new();
for i in 0..row1.len() {
let column_name: String = format!("vec_{i}");
let subtraction: String = format!("sub_{i}");
new_columns.extend([column_name.clone(), subtraction.clone()]);
lazyframe = lazyframe
.with_columns([
// split list into new intermediate columns
col("vec").arr().get(lit(i as i64)).alias(&column_name),
//col("vec").arr().eval(lit(2.0) * col(""), true)
//.alias("test multiplication by 2"),
])
.with_columns([
(col(&column_name).last() - col(&column_name))
.apply(absolute_value, GetOutput::from_type(DataType::Float64))
.alias(&subtraction)
]);
}
lazyframe = lazyframe
.select([
all(),
concat_lst([col("^sub_.*$")]).alias("Concat lists")
]);
lazyframe = lazyframe
.with_columns([
col("Concat lists").arr().sum().alias("Sum")
]);
// uncomment to discard intermediate columns
// lazyframe = lazyframe.drop_columns(new_columns);
println!("dataframe:\n{}\n", lazyframe.collect()?);
Ok(())
}
The absolute_value function is given below:
fn absolute_value(str_val: Series) -> Result<Option<Series>, PolarsError> {
let series: Series = str_val
.f64()
.expect("fn absolute_value: series was not an f64 dtype")
.into_iter()
.map(|opt_value: Option<f64>| opt_value.map(|value: f64| value.abs()))
.collect::<Float64Chunked>()
.into_series();
Ok(Some(series))
}
The initial DataFrame:
df:
shape: (4, 1)
┌─────────────────────────────┐
│ vec │
│ --- │
│ list[f64] │
╞═════════════════════════════╡
│ [0.0, -1.111111, 1.77] │
│ [-2.222222, 3.333333, 2.93] │
│ [3.333333, -4.111111, 3.56] │
│ [8.888889, -10.0, 7.26] │
└─────────────────────────────┘
The final result is:
dataframe:
shape: (4, 9)
┌─────────────────────────────┬───────────┬───────────┬───────────┬─────┬───────┬───────┬──────────────────────────────┬───────────┐
│ vec ┆ vec_0 ┆ sub_0 ┆ vec_1 ┆ ... ┆ vec_2 ┆ sub_2 ┆ Concat lists ┆ Sum │
│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │
│ list[f64] ┆ f64 ┆ f64 ┆ f64 ┆ ┆ f64 ┆ f64 ┆ list[f64] ┆ f64 │
╞═════════════════════════════╪═══════════╪═══════════╪═══════════╪═════╪═══════╪═══════╪══════════════════════════════╪═══════════╡
│ [0.0, -1.111111, 1.77] ┆ 0.0 ┆ 8.888889 ┆ -1.111111 ┆ ... ┆ 1.77 ┆ 5.49 ┆ [8.888889, 8.888889, 5.49] ┆ 23.267778 │
│ [-2.222222, 3.333333, 2.93] ┆ -2.222222 ┆ 11.111111 ┆ 3.333333 ┆ ... ┆ 2.93 ┆ 4.33 ┆ [11.111111, 13.333333, 4.33] ┆ 28.774444 │
│ [3.333333, -4.111111, 3.56] ┆ 3.333333 ┆ 5.555556 ┆ -4.111111 ┆ ... ┆ 3.56 ┆ 3.7 ┆ [5.555556, 5.888889, 3.7] ┆ 15.144444 │
│ [8.888889, -10.0, 7.26] ┆ 8.888889 ┆ 0.0 ┆ -10.0 ┆ ... ┆ 7.26 ┆ 0.0 ┆ [0.0, 0.0, 0.0] ┆ 0.0 │
└─────────────────────────────┴───────────┴───────────┴───────────┴─────┴───────┴───────┴──────────────────────────────┴───────────┘

Terraform The given expressions are tuple and object, respectively

I am new to terraform can some one point me what is the issue here ??
Error: Inconsistent conditional result types
│
│ on .terraform\modules\frontend_website.cloudfront\main.tf line 17, in resource "aws_cloudfront_distribution" "www_distribution":
│ 17: for_each = length(var.custom_error_response) == 0 ? [] : var.custom_error_response
│ ├────────────────
│ │ var.custom_error_response is object with 4 attributes
│
│ The true and false result expressions must have consistent types. The given
│ expressions are tuple and object, respectively.
dynamic "custom_error_response" {
for_each = length(var.custom_error_response) == 0 ? [] : var.custom_error_response
content {
error_caching_min_ttl = lookup(custom_error_response.value, "error_caching_min_ttl", "3000")
error_code = lookup(custom_error_response.value, "error_code", "404")
response_code = lookup(custom_error_response.value, "response_code", "200")
response_page_path = lookup(custom_error_response.value, "response_page_path", "/index.html")
}
}
Thanks Mark for the reply , i fixed it instead of object i put a square bracket and made it is array of object , that fixed it , thanks

onnxruntime C++ how to get outputTensor dynamic shape?

Try using something like this:
std::vector<Ort::Value> ort_inputs;
for (int i = 0; i < inputNames.size(); ++i) {
ort_inputs.emplace_back(Ort::Value::CreateTensor<float>(
memoryInfo, static_cast<float *>(inputs[i].data), inputs[i].get_size(),
inputShapes[i].data(), inputShapes[i].size()));
}
std::vector<Ort::Value> outputTensors =
session.Run(Ort::RunOptions{nullptr}, inputNames.data(),
ort_inputs.data(), 1, outputNames.data(), outputNames.size());
Now, my model is like this:
yolox_tiny_cpunms.onnx Detail
╭──────────────┬────────────────────────────────┬────────────────────────┬───────────────╮
│ Name │ Shape │ Input/Output │ Dtype │
├──────────────┼────────────────────────────────┼────────────────────────┼───────────────┤
│ input │ [1, 3, 416, 416] │ input │ float32 │
│ boxes │ [1, -1, -1] │ output │ float32 │
│ scores │ [1, -1] │ output │ float32 │
│ labels │ [1, -1] │ output │ int64 │
╰──────────────┴────────────────────────────────┴────────────────────────┴───────────────╯
As you can see, the output is dynamic, but C++ code output Tensor give me shape [1, 0, 4]
, [1, 0], [1,0]
How can I get the output shape in. C++?
I get the output shape in C++ like this
auto outputTensor = session.Run(runOptions, inputNames.data(), &inputTensor, 1, outputNames.data(), 1);
assert(outputTensor.size() == 1 && outpuTensor.front().IsTensor());
if (outputTensor[0].IsTensor())
{
auto outputInfo = outputTensor[0].GetTensorTypeAndShapeInfo();
std::cout << "GetElementType: " << outputInfo.GetElementType() << "\n";
std::cout << "Dimensions of the output: " << outputInfo.GetShape().size() << "\n";
std::cout << "Shape of the output: ";
for (unsigned int shapeI = 0; shapeI < outputInfo.GetShape().size(); shapeI++)
std::cout << outputInfo.GetShape()[shapeI] << ", ";
}

How to create many secrets in AWS secrets manager using terraform

What I want to do is feed a list of key names to a module that will be used to generate many secrets with different random passwords in secrets manager.
I have tried many different things but have failed so far.
This is what I have currently:
module "secrets-manager-1" {
source = "lgallard/secrets-manager/aws"
for_each = var.list
secrets = {
"${each.value}" = {
description = each.value
recovery_window_in_days = 7
secret_string = random_password.special_password.result
}
}
tags = var.standard_tags
}
resource "random_password" "special_password" {
count = 2
length = 16
special = true
}
variable "list" {
type = list(string)
default = [
"secret_key_1",
"secret_key_2"
]
}
The Error:
│ Error: Invalid for_each argument
│
│ on ..\..\modules\jitsi\jitsi_secrets.tf line 54, in module "secrets-manager-1":
│ 54: for_each = var.list
│ ├────────────────
│ │ var.list is list of string with 2 elements
│
│ The given "for_each" argument value is unsuitable: the "for_each" argument must be a map, or set of strings, and you have provided a value of type list of string.
╵
Releasing state lock. This may take a few moments...
Unfortunately what you are providing is not even valid Terraform code. What I believe you would want to achieve the following:
// Create N random password. In this case N = 2
resource "random_password" "special_password" {
count = 2
length = 16
special = true
}
// Import a third party module
module "secrets-manager-1" {
source = "lgallard/secrets-manager/aws"
// Loop through the random_passowrd resouces and create the secrets
secrets = {
for index, pwd in random_password.special_password.*.result : "${element(var.list, index)}" => {
secret_string: "${pwd}",
recovery_window_in_days = 7
}
}
}
You may want to check out the splat expressions for being able to iterate over multiple resources. This is used for the for expression in the secrets-manager-1 module.

stat.h expected unqualified-id before string constant __BEGIN_DECLS in in standard file

I try to complier a file on ubuntu64-16.04. In CMakeLists.txt, I set
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
But there is an error in the standard file both in "sys/stat.h" and "sys/fcntl.h". I do not know what happened. The error message is listed below.
When I try to not include "stat.h" the same error occurred in "fcntl.h". So I think as the compilation order, it should compile "stat.h" firstly and then "fcntl.h", so I think the error occurred in "Serial.h" for I only include both the file in "Serial.h".
I find a similar question here. But I really have the file "sys/cedfs.h".
-rw-r--r-- 1 root root 15412 Feb 6 04:04 sys/cdefs.h
#ifndef SERIAL_H
#define SERIAL_H
#include "Header.h"
#include <sys/cdefs.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
#include <sys/ioctl.h>
class Serialport
{
public:
Serialport(string port);//定义Serialport类的成员函数,
Serialport();
~ Serialport();
int open_port(string port);
int set_opt(int nSpeed = 115200 , int nBits = 8, char nEvent ='N', int nStop = 1);
bool send(char *str);
bool sendAngle(float angleYaw,float anglePitch,float Dis, bool big, bool insight ,bool get);
void readMode(int &carMode);
uint8_t readAngle(CarData &cardata);
private:
int fd ;
char tmpchar[19];
const char *buffer;
unsigned char rData[255];
char guapichar[4];
};
#endif
Scanning dependencies of target SentryVision
[ 16%] Building CXX object bin/CMakeFiles/SentryVision.dir/main.cpp.o
In file included from /usr/include/features.h:367:0,
from /usr/include/x86_64-linux-gnu/c++/5/bits/os_defines.h:39,
from /usr/include/x86_64-linux-gnu/c++/5/bits/c++config.h:482,
from /usr/include/c++/5/string:38,
from /home/wang/Documents/copy_from_zhou/src/../include/Header.h:5,
from /home/wang/Documents/copy_from_zhou/src/main.cpp:1:
/usr/include/x86_64-linux-gnu/sys/stat.h:102:1: error: expected unqualified-id before string constant
__BEGIN_DECLS
^
bin/CMakeFiles/SentryVision.dir/build.make:62: recipe for target 'bin/CMakeFiles/SentryVision.dir/main.cpp.o' failed
make[2]: *** [bin/CMakeFiles/SentryVision.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:85: recipe for target 'bin/CMakeFiles/SentryVision.dir/all' failed
make[1]: *** [bin/CMakeFiles/SentryVision.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
For the main is so long, I show the declaration:
#include "../include/Header.h"
#include "../include/ArmorDetector.h"
#include "../include/AngleCalculate.h"
#include "../include/Serial.hpp"
#include "../include/RMVideoCapture.hpp"
#include "../include/WatchDog.hpp"
#include "../include/DxImageProc.h"
#include "../include/GxIAPI.h"
#include <thread>
#define MEMORY_ALLOT_ERROR -1
Mat m_image;
bool is_implemented = false;
int64_t m_pixel_color = 0;
char *m_rgb_image = NULL;
GX_DEV_HANDLE g_device = NULL; //设备句柄
GX_FRAME_DATA g_frame_data = {0}; //采集图像参数
pthread_t g_acquire_thread = 0; //采集线程ID
thread t;
Mat ImgDown;
Serialport stm32("/dev/ttyUSB0");
ArmorDetector armorDown;
ArmorData armorDataDown;
CarData carData;
bool g_get_image = false; //采集线程是否结束的标志
//获取图像大小并申请图像数据空间
int PreForImage();
//释放资源
int UnPreForImage();
//采集线程函数
void *ProcGetImage(void *param);
//获取错误信息描述
void GetErrorString(GX_STATUS error_status);
//开起相机设备
bool OpenDevice();
//在得到图片之后寻找目标
void findTarget(unsigned int &lost);
And the Header.h:
#ifndef HEADER_H
#define HEADER_H
#include "config.h"
#include <string>
#include <iostream>
#include <vector>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
typedef struct __ArmorData__
{
int Cam;
bool isGet;
bool shoot;
float angleYaw;
float anglePitch;
float x;
float y;
float z;
float atocDistance;
bool isBig;
int picWidth;
__ArmorData__()
{
Cam = picWidth = 0;
isBig = isGet = shoot = false;
atocDistance = anglePitch = angleYaw = x = y = z = 0;
}
}ArmorData;
typedef struct __CarData__
{
float absAngleYaw;
float absAnglePitch;
__CarData__()
{
absAnglePitch = absAngleYaw = 0;
}
}CarData;
#endif
config.h:
#ifndef CONFIG_H
#define CONFIG_H
//#define watchdog
//#define RED
#define BLUE
#define DEBUG0
#define DEBUG1
// #define DEBUG2
// #define DEBUG3
#endif
My directory tree is:
.
├── CMakeLists.txt
├── autoDog.sh
├── autoSentry.sh
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ │ ├── 3.5.1
│ │ │ ├── CMakeCCompiler.cmake
│ │ │ ├── CMakeCXXCompiler.cmake
│ │ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ │ ├── CMakeSystem.cmake
│ │ │ ├── CompilerIdC
│ │ │ │ ├── CMakeCCompilerId.c
│ │ │ │ └── a.out
│ │ │ └── CompilerIdCXX
│ │ │ ├── CMakeCXXCompilerId.cpp
│ │ │ └── a.out
│ │ ├── CMakeDirectoryInformation.cmake
│ │ ├── CMakeError.log
│ │ ├── CMakeOutput.log
│ │ ├── CMakeTmp
│ │ ├── Makefile.cmake
│ │ ├── Makefile2
│ │ ├── Progress
│ │ │ ├── 1
│ │ │ └── count.txt
│ │ ├── TargetDirectories.txt
│ │ ├── cmake.check_cache
│ │ ├── feature_tests.bin
│ │ ├── feature_tests.c
│ │ ├── feature_tests.cxx
│ │ └── progress.marks
│ ├── Makefile
│ ├── bin
│ │ ├── CMakeFiles
│ │ │ ├── CMakeDirectoryInformation.cmake
│ │ │ ├── SentryVision.dir
│ │ │ │ ├── CXX.includecache
│ │ │ │ ├── DependInfo.cmake
│ │ │ │ ├── build.make
│ │ │ │ ├── cmake_clean.cmake
│ │ │ │ ├── depend.internal
│ │ │ │ ├── depend.make
│ │ │ │ ├── flags.make
│ │ │ │ ├── link.txt
│ │ │ │ └── progress.make
│ │ │ └── progress.marks
│ │ ├── Makefile
│ │ └── cmake_install.cmake
│ └── cmake_install.cmake
├── include
│ ├── AngleCalculate.h
│ ├── ArmorDetector.h
│ ├── DxImageProc.h
│ ├── GxIAPI.h
│ ├── Header.h
│ ├── RMVideoCapture.hpp
│ ├── Serial.hpp
│ ├── WatchDog.hpp
│ └── config.h
├── src
│ ├── AngleCalculate.cpp
│ ├── ArmorDetector.cpp
│ ├── CMakeLists.txt
│ ├── RMVideoCapture.cpp
│ ├── Serial.cpp
│ └── main.cpp
└── watchdog
├── CMakeLists.txt
└── src
├── CMakeLists.txt
└── main.cpp
14 directories, 59 files